-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDictionaryCasting.swift
155 lines (144 loc) · 5.54 KB
/
DictionaryCasting.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//===--- Compiler conversion/casting entry points for Dictionary<K, V> ----===//
extension Dictionary {
@_alwaysEmitIntoClient @inlinable // Introduced in 5.1
@inline(__always)
internal init?<C: Collection>(
_mapping source: C,
allowingDuplicates: Bool,
transform: (C.Element) -> (key: Key, value: Value)?
) {
var target = _NativeDictionary<Key, Value>(capacity: source.count)
if allowingDuplicates {
for member in source {
guard let (key, value) = transform(member) else { return nil }
target._unsafeUpdate(key: key, value: value)
}
} else {
for member in source {
guard let (key, value) = transform(member) else { return nil }
target._unsafeInsertNew(key: key, value: value)
}
}
self.init(_native: target)
}
}
/// Perform a non-bridged upcast that always succeeds.
///
/// - Precondition: `BaseKey` and `BaseValue` are base classes or base `@objc`
/// protocols (such as `AnyObject`) of `DerivedKey` and `DerivedValue`,
/// respectively.
@inlinable
public func _dictionaryUpCast<DerivedKey, DerivedValue, BaseKey, BaseValue>(
_ source: Dictionary<DerivedKey, DerivedValue>
) -> Dictionary<BaseKey, BaseValue> {
return Dictionary(
_mapping: source,
// String and NSString have different concepts of equality, so
// NSString-keyed Dictionaries may generate key collisions when "upcasted"
// to String. See rdar://problem/35995647
allowingDuplicates: (BaseKey.self == String.self)
) { k, v in
(k as! BaseKey, v as! BaseValue)
}!
}
/// Called by the casting machinery.
@_silgen_name("_swift_dictionaryDownCastIndirect")
internal func _dictionaryDownCastIndirect<SourceKey, SourceValue,
TargetKey, TargetValue>(
_ source: UnsafePointer<Dictionary<SourceKey, SourceValue>>,
_ target: UnsafeMutablePointer<Dictionary<TargetKey, TargetValue>>) {
target.initialize(to: _dictionaryDownCast(source.pointee))
}
/// Implements a forced downcast. This operation should have O(1) complexity.
///
/// The cast can fail if bridging fails. The actual checks and bridging can be
/// deferred.
///
/// - Precondition: `DerivedKey` is a subtype of `BaseKey`, `DerivedValue` is
/// a subtype of `BaseValue`, and all of these types are reference types.
@inlinable
public func _dictionaryDownCast<BaseKey, BaseValue, DerivedKey, DerivedValue>(
_ source: Dictionary<BaseKey, BaseValue>
) -> Dictionary<DerivedKey, DerivedValue> {
#if _runtime(_ObjC)
if _isClassOrObjCExistential(BaseKey.self)
&& _isClassOrObjCExistential(BaseValue.self)
&& _isClassOrObjCExistential(DerivedKey.self)
&& _isClassOrObjCExistential(DerivedValue.self) {
guard source._variant.isNative else {
return Dictionary(
_immutableCocoaDictionary: source._variant.asCocoa.object)
}
// Note: it is safe to treat the buffer as immutable here because
// Dictionary will not mutate buffer with reference count greater than 1.
return Dictionary(
_immutableCocoaDictionary: source._variant.asNative.bridged())
}
#endif
// Note: We can't delegate this call to _dictionaryDownCastConditional,
// because we rely on as! to generate nice runtime errors when the downcast
// fails.
return Dictionary(
_mapping: source,
// String and NSString have different concepts of equality, so
// NSString-keyed Dictionaries may generate key collisions when downcasted
// to String. See rdar://problem/35995647
allowingDuplicates: (DerivedKey.self == String.self)
) { k, v in
(k as! DerivedKey, v as! DerivedValue)
}!
}
/// Called by the casting machinery.
@_silgen_name("_swift_dictionaryDownCastConditionalIndirect")
internal func _dictionaryDownCastConditionalIndirect<SourceKey, SourceValue,
TargetKey, TargetValue>(
_ source: UnsafePointer<Dictionary<SourceKey, SourceValue>>,
_ target: UnsafeMutablePointer<Dictionary<TargetKey, TargetValue>>
) -> Bool {
if let result: Dictionary<TargetKey, TargetValue>
= _dictionaryDownCastConditional(source.pointee) {
target.initialize(to: result)
return true
}
return false
}
/// Implements a conditional downcast.
///
/// If the cast fails, the function returns `nil`. All checks should be
/// performed eagerly.
///
/// - Precondition: `DerivedKey` is a subtype of `BaseKey`, `DerivedValue` is
/// a subtype of `BaseValue`, and all of these types are reference types.
@inlinable
public func _dictionaryDownCastConditional<
BaseKey, BaseValue, DerivedKey, DerivedValue
>(
_ source: Dictionary<BaseKey, BaseValue>
) -> Dictionary<DerivedKey, DerivedValue>? {
return Dictionary(
_mapping: source,
// String and NSString have different concepts of equality, so
// NSString-keyed Dictionaries may generate key collisions when downcasted
// to String. See rdar://problem/35995647
allowingDuplicates: (DerivedKey.self == String.self)
) { k, v in
guard
let key = k as? DerivedKey,
let value = v as? DerivedValue
else {
return nil
}
return (key, value)
}
}