File tree Expand file tree Collapse file tree 5 files changed +79
-4
lines changed
test/Interop/Cxx/stdlib/overlay Expand file tree Collapse file tree 5 files changed +79
-4
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ foreach(sdk ${SWIFT_SDKS})
15
15
endif ()
16
16
17
17
set (outputs )
18
- foreach (source libcxxshim.modulemap libcxxshim.h )
18
+ foreach (source libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h )
19
19
add_custom_command (OUTPUT ${module_dir} /${source}
20
20
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR} /${source}
21
21
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR} /${source} " "${module_dir} /${source} "
@@ -41,11 +41,11 @@ foreach(sdk ${SWIFT_SDKS})
41
41
list (APPEND libcxxshim_modulemap_target_list cxxshim-${sdk}-${arch} )
42
42
43
43
44
- swift_install_in_component (FILES libcxxshim.modulemap libcxxshim.h
44
+ swift_install_in_component (FILES libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h
45
45
DESTINATION "lib/swift/${arch_subdir} "
46
46
COMPONENT compiler )
47
47
if (SWIFT_BUILD_STATIC_STDLIB )
48
- swift_install_in_component (FILES libcxxshim.modulemap libcxxshim.h
48
+ swift_install_in_component (FILES libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h
49
49
DESTINATION "lib/swift_static/${arch_subdir} "
50
50
COMPONENT compiler )
51
51
endif ()
Original file line number Diff line number Diff line change 1
1
module CxxShim {
2
2
header "libcxxshim.h"
3
3
requires cplusplus
4
- }
4
+ }
5
+
6
+ module CxxStdlibShim {
7
+ header "libcxxstdlibshim.h"
8
+ requires cplusplus
9
+ }
Original file line number Diff line number Diff line change
1
+ #include < functional>
2
+ #include < string>
3
+
4
+ // / Used for std::string conformance to Swift.Hashable
5
+ typedef std::hash<std::string> __swift_interopHashOfString;
6
+
7
+ // / Used for std::u16string conformance to Swift.Hashable
8
+ typedef std::hash<std::u16string> __swift_interopHashOfU16String;
Original file line number Diff line number Diff line change 10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
+ import CxxStdlibShim
14
+
13
15
// MARK: Initializing C++ string from a Swift String
14
16
15
17
extension std . string {
@@ -87,6 +89,24 @@ extension std.u16string: Equatable {
87
89
}
88
90
}
89
91
92
+ // MARK: Hashing C++ strings
93
+
94
+ extension std . string : Hashable {
95
+ public func hash( into hasher: inout Hasher ) {
96
+ // Call std::hash<std::string>::operator()
97
+ let cxxHash = __swift_interopHashOfString ( ) . callAsFunction ( self )
98
+ hasher. combine ( cxxHash)
99
+ }
100
+ }
101
+
102
+ extension std . u16string : Hashable {
103
+ public func hash( into hasher: inout Hasher ) {
104
+ // Call std::hash<std::u16string>::operator()
105
+ let cxxHash = __swift_interopHashOfU16String ( ) . callAsFunction ( self )
106
+ hasher. combine ( cxxHash)
107
+ }
108
+ }
109
+
90
110
// MARK: Getting a Swift description of a C++ string
91
111
92
112
extension std . string : CustomDebugStringConvertible {
Original file line number Diff line number Diff line change @@ -85,6 +85,48 @@ StdStringOverlayTestSuite.test("std::u16string operators") {
85
85
expectTrue ( s1 == " something123literal " )
86
86
}
87
87
88
+ StdStringOverlayTestSuite . test ( " std::string as Hashable " ) {
89
+ let s0 = std. string ( )
90
+ let h0 = s0. hashValue
91
+
92
+ let s1 = std. string ( " something " )
93
+ let h1 = s1. hashValue
94
+
95
+ let s2 = std. string ( " something123 " )
96
+ let h2 = s2. hashValue
97
+
98
+ let s3 = std. string ( " something " )
99
+ let h3 = s3. hashValue
100
+
101
+ expectEqual ( h1, h3)
102
+ expectNotEqual ( h0, h1)
103
+ expectNotEqual ( h0, h2)
104
+ expectNotEqual ( h0, h3)
105
+ expectNotEqual ( h1, h2)
106
+ expectNotEqual ( h2, h3)
107
+ }
108
+
109
+ StdStringOverlayTestSuite . test ( " std::u16string as Hashable " ) {
110
+ let s0 = std. u16string ( )
111
+ let h0 = s0. hashValue
112
+
113
+ let s1 = std. u16string ( " something " )
114
+ let h1 = s1. hashValue
115
+
116
+ let s2 = std. u16string ( " something123 " )
117
+ let h2 = s2. hashValue
118
+
119
+ let s3 = std. u16string ( " something " )
120
+ let h3 = s3. hashValue
121
+
122
+ expectEqual ( h1, h3)
123
+ expectNotEqual ( h0, h1)
124
+ expectNotEqual ( h0, h2)
125
+ expectNotEqual ( h0, h3)
126
+ expectNotEqual ( h1, h2)
127
+ expectNotEqual ( h2, h3)
128
+ }
129
+
88
130
StdStringOverlayTestSuite . test ( " std::u16string <=> Swift.String " ) {
89
131
let cxx1 = std. u16string ( )
90
132
let swift1 = String ( cxx1)
You can’t perform that action at this time.
0 commit comments