Skip to content

Commit 1ea5458

Browse files
authored
Merge pull request #78841 from CrazyFanFan/feature/replace_self_type_with_generics
[cxx-interop]: Refactor `CxxDictionary` protocols to use generics instead of `Self`.
2 parents f21d029 + b4531a6 commit 1ea5458

File tree

2 files changed

+117
-20
lines changed

2 files changed

+117
-20
lines changed

stdlib/public/Cxx/CxxDictionary.swift

+20-20
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ extension CxxDictionary {
174174
}
175175

176176
@inlinable
177-
public mutating func merge<S: Sequence>(
177+
public mutating func merge<S: Sequence, E>(
178178
_ other: __owned S,
179-
uniquingKeysWith combine: (Value, Value) throws -> Value
180-
) rethrows where S.Element == (Key, Value) {
179+
uniquingKeysWith combine: (Value, Value) throws(E) -> Value
180+
) throws(E) where S.Element == (Key, Value) {
181181
for (key, value) in other {
182182
var iter = self.__findMutatingUnsafe(key)
183183
if iter != self.__endMutatingUnsafe() {
@@ -190,10 +190,10 @@ extension CxxDictionary {
190190
}
191191

192192
@inlinable
193-
public mutating func merge(
193+
public mutating func merge<E>(
194194
_ other: __owned Dictionary<Key, Value>,
195-
uniquingKeysWith combine: (Value, Value) throws -> Value
196-
) rethrows where Key: Hashable {
195+
uniquingKeysWith combine: (Value, Value) throws(E) -> Value
196+
) throws(E) where Key: Hashable {
197197
for (key, value) in other {
198198
var iter = self.__findMutatingUnsafe(key)
199199
if iter != self.__endMutatingUnsafe() {
@@ -206,10 +206,10 @@ extension CxxDictionary {
206206
}
207207

208208
@inlinable
209-
public mutating func merge(
210-
_ other: __owned Self,
211-
uniquingKeysWith combine: (Value, Value) throws -> Value
212-
) rethrows {
209+
public mutating func merge<T: CxxDictionary, E>(
210+
_ other: __owned T,
211+
uniquingKeysWith combine: (Value, Value) throws(E) -> Value
212+
) throws(E) where T.Key == Key, T.Value == Value {
213213
var iterator = other.__beginUnsafe()
214214
while iterator != other.__endUnsafe() {
215215
var iter = self.__findMutatingUnsafe(iterator.pointee.first)
@@ -224,30 +224,30 @@ extension CxxDictionary {
224224
}
225225

226226
@inlinable
227-
public __consuming func merging<S: Sequence>(
227+
public __consuming func merging<S: Sequence, E>(
228228
_ other: __owned S,
229-
uniquingKeysWith combine: (Value, Value) throws -> Value
230-
) rethrows -> Self where S.Element == (Key, Value) {
229+
uniquingKeysWith combine: (Value, Value) throws(E) -> Value
230+
) throws(E) -> Self where S.Element == (Key, Value) {
231231
var result = self
232232
try result.merge(other, uniquingKeysWith: combine)
233233
return result
234234
}
235235

236236
@inlinable
237-
public __consuming func merging(
237+
public __consuming func merging<E>(
238238
_ other: __owned Dictionary<Key, Value>,
239-
uniquingKeysWith combine: (Value, Value) throws -> Value
240-
) rethrows -> Self where Key: Hashable {
239+
uniquingKeysWith combine: (Value, Value) throws(E) -> Value
240+
) throws(E) -> Self where Key: Hashable {
241241
var result = self
242242
try result.merge(other, uniquingKeysWith: combine)
243243
return result
244244
}
245245

246246
@inlinable
247-
public __consuming func merging(
248-
_ other: __owned Self,
249-
uniquingKeysWith combine: (Value, Value) throws -> Value
250-
) rethrows -> Self {
247+
public __consuming func merging<T: CxxDictionary, E>(
248+
_ other: __owned T,
249+
uniquingKeysWith combine: (Value, Value) throws(E) -> Value
250+
) throws(E) -> Self where T.Key == Key, T.Value == Value {
251251
var result = self
252252
try result.merge(other, uniquingKeysWith: combine)
253253
return result

test/Interop/Cxx/stdlib/use-std-map.swift

+97
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,103 @@ StdMapTestSuite.test("UnorderedMap.merge(map)") {
427427
expectEqual(m[3], 3)
428428
}
429429

430+
StdMapTestSuite.test("Map.merge(CxxDictionary)") {
431+
// Empty map merging with empty map.
432+
var emptyMap = initEmptyMap()
433+
emptyMap.merge(initEmptyUnorderedMap()) { v0, _ in v0 }
434+
expectTrue(emptyMap.empty())
435+
436+
emptyMap.merge(initEmptyUnorderedMap()) { _, v1 in v1 }
437+
expectTrue(emptyMap.empty())
438+
439+
// Empty map merging with non-empty map.
440+
emptyMap.merge(initUnorderedMap()) { v0, _ in v0 }
441+
expectEqual(emptyMap[1], 3)
442+
expectEqual(emptyMap[2], 2)
443+
expectEqual(emptyMap[3], 3)
444+
445+
emptyMap = initEmptyMap()
446+
emptyMap.merge(initUnorderedMap()) { _, v1 in v1 }
447+
expectEqual(emptyMap[1], 3)
448+
expectEqual(emptyMap[2], 2)
449+
expectEqual(emptyMap[3], 3)
450+
451+
// Non-empty map merging with empty map.
452+
var map = initMap()
453+
map.merge(initUnorderedMap()) { v0, _ in v0 }
454+
expectEqual(emptyMap[1], 3)
455+
expectEqual(emptyMap[2], 2)
456+
expectEqual(emptyMap[3], 3)
457+
458+
map.merge(initEmptyUnorderedMap()) { _, v1 in v1 }
459+
expectEqual(map[1], 3)
460+
expectEqual(map[2], 2)
461+
expectEqual(map[3], 3)
462+
463+
// Non-empty map merging with non-empty map.
464+
let noneEmptydMap = UnorderedMap([1: 4, 2: 5, 3: 6, 4: 7])
465+
map.merge(noneEmptydMap) { v0, _ in v0 }
466+
expectEqual(map[1], 3)
467+
expectEqual(map[2], 2)
468+
expectEqual(map[3], 3)
469+
expectEqual(map[4], 7)
470+
471+
map.merge(noneEmptydMap) { _, v1 in v1 }
472+
expectEqual(map[1], 4)
473+
expectEqual(map[2], 5)
474+
expectEqual(map[3], 6)
475+
expectEqual(map[4], 7)
476+
}
477+
478+
StdMapTestSuite.test("UnorderedMap.merge(CxxDictionary)") {
479+
// Empty map merging with empty map.
480+
var emptyMap = initEmptyUnorderedMap()
481+
emptyMap.merge(initEmptyMap()) { v0, _ in v0 }
482+
expectTrue(emptyMap.empty())
483+
484+
emptyMap.merge(initEmptyMap()) { _, v1 in v1 }
485+
expectTrue(emptyMap.empty())
486+
487+
// Empty map merging with non-empty map.
488+
emptyMap.merge(initMap()) { v0, _ in v0 }
489+
expectEqual(emptyMap[1], 3)
490+
expectEqual(emptyMap[2], 2)
491+
expectEqual(emptyMap[3], 3)
492+
493+
emptyMap = initEmptyUnorderedMap()
494+
emptyMap.merge(initMap()) { _, v1 in v1 }
495+
expectEqual(emptyMap[1], 3)
496+
expectEqual(emptyMap[2], 2)
497+
expectEqual(emptyMap[3], 3)
498+
499+
// Non-empty map merging with empty map.
500+
var map = initUnorderedMap()
501+
map.merge(initEmptyMap()) { _, v1 in v1 }
502+
expectEqual(map[1], 3)
503+
expectEqual(map[2], 2)
504+
expectEqual(map[3], 3)
505+
506+
map.merge(initEmptyMap()) { v0, _ in v0 }
507+
expectEqual(map[1], 3)
508+
expectEqual(map[2], 2)
509+
expectEqual(map[3], 3)
510+
511+
// Non-empty map merging with non-empty map.
512+
let noneEmptyMap = Map([1: 4, 2: 5, 3: 6, 4: 7])
513+
514+
map.merge(noneEmptyMap) { v0, _ in v0 }
515+
expectEqual(map[1], 3)
516+
expectEqual(map[2], 2)
517+
expectEqual(map[3], 3)
518+
expectEqual(map[4], 7)
519+
520+
map.merge(noneEmptyMap) { _, v1 in v1 }
521+
expectEqual(map[1], 4)
522+
expectEqual(map[2], 5)
523+
expectEqual(map[3], 6)
524+
expectEqual(map[4], 7)
525+
}
526+
430527
// `merging` is implemented by calling `merge`, so we can skip this test
431528

432529
runAllTests()

0 commit comments

Comments
 (0)