Skip to content

Commit fee63b8

Browse files
author
Alexis Beingessner
committed
Make bridging eager (minimal impl)
1 parent aea0897 commit fee63b8

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,10 @@ public struct Set<Element : Hashable> :
398398
_sanityCheck(_isBridgedVerbatimToObjectiveC(Element.self),
399399
"Set can be backed by NSSet _variantBuffer only when the member type can be bridged verbatim to Objective-C")
400400

401-
// TODO: rewrite this
402-
self.init()
401+
_buffer = _NativeBuffer(minimumCapacity: _immutableCocoaSet.count)
402+
_immutableCocoaSet.enumerateObjectsUsingBlock { (key, stop) in
403+
insert(_forceBridgeFromObjectiveC(key, Element.self))
404+
}
403405
}
404406
#endif
405407

@@ -1204,19 +1206,12 @@ public func _setDownCastIndirect<SourceValue, TargetValue>(
12041206
/// are reference types.
12051207
public func _setDownCast<BaseValue, DerivedValue>(_ source: Set<BaseValue>)
12061208
-> Set<DerivedValue> {
1207-
/* TODO: check out what this did
12081209
#if _runtime(_ObjC)
12091210
if _isClassOrObjCExistential(BaseValue.self)
12101211
&& _isClassOrObjCExistential(DerivedValue.self) {
1211-
switch source._variantBuffer {
1212-
case _VariantSetBuffer.native(let buffer):
1213-
return Set(_immutableCocoaSet: buffer.bridged())
1214-
case _VariantSetBuffer.cocoa(let cocoaBuffer):
1215-
return Set(_immutableCocoaSet: cocoaBuffer.cocoaSet)
1216-
}
1212+
return Set(_immutableCocoaSet: source._bridgeToObjectiveCImpl())
12171213
}
12181214
#endif
1219-
*/
12201215
return _setDownCastConditional(source)!
12211216
}
12221217

@@ -1537,15 +1532,18 @@ public struct Dictionary<Key : Hashable, Value> :
15371532
/// * `Key` and `Value` are bridged verbatim to Objective-C (i.e.,
15381533
/// are reference types).
15391534
public init(_immutableCocoaDictionary: _NSDictionary) {
1540-
/* TODO: replace this
15411535
_sanityCheck(
15421536
_isBridgedVerbatimToObjectiveC(Key.self) &&
15431537
_isBridgedVerbatimToObjectiveC(Value.self),
15441538
"Dictionary can be backed by NSDictionary buffer only when both key and value are bridged verbatim to Objective-C")
1545-
_buffer = .cocoa(
1546-
_CocoaDictionaryBuffer(cocoaDictionary: _immutableCocoaDictionary))
1547-
*/
1548-
self.init()
1539+
1540+
_buffer = _NativeBuffer(minimumCapacity: _immutableCocoaDictionary.count)
1541+
_immutableCocoaDictionary.enumerateKeysAndObjectsUsingBlock {
1542+
(key, value, stop) in
1543+
let _ = _buffer.insert(
1544+
_forceBridgeFromObjectiveC(value, Value.self),
1545+
forKey: _forceBridgeFromObjectiveC(key, Key.self))
1546+
}
15491547
}
15501548
#endif
15511549

@@ -2106,24 +2104,17 @@ public func _dictionaryDownCast<BaseKey, BaseValue, DerivedKey, DerivedValue>(
21062104
_ source: Dictionary<BaseKey, BaseValue>
21072105
) -> Dictionary<DerivedKey, DerivedValue> {
21082106

2109-
/* TODO: figure out what this did
21102107
#if _runtime(_ObjC)
21112108
if _isClassOrObjCExistential(BaseKey.self)
21122109
&& _isClassOrObjCExistential(BaseValue.self)
21132110
&& _isClassOrObjCExistential(DerivedKey.self)
21142111
&& _isClassOrObjCExistential(DerivedValue.self) {
2115-
2116-
switch source._variantBuffer {
2117-
case .native(let buffer):
2118-
// Note: it is safe to treat the buffer as immutable here because
2119-
// Dictionary will not mutate buffer with reference count greater than 1.
2120-
return Dictionary(_immutableCocoaDictionary: buffer.bridged())
2121-
case .cocoa(let cocoaBuffer):
2122-
return Dictionary(_immutableCocoaDictionary: cocoaBuffer.cocoaDictionary)
2123-
}
2112+
// Note: it is safe to treat the buffer as immutable here because
2113+
// Dictionary will not mutate buffer with reference count greater than 1.
2114+
return Dictionary(
2115+
_immutableCocoaDictionary: source._bridgeToObjectiveCImpl())
21242116
}
21252117
#endif
2126-
*/
21272118
return _dictionaryDownCastConditional(source)!
21282119
}
21292120

@@ -4031,7 +4022,7 @@ extension ${Self} {
40314022

40324023
#if _runtime(_ObjC)
40334024
extension ${Self} {
4034-
public func _bridgeToObjectiveCImpl() -> _NS${Self}Core {
4025+
public func _bridgeToObjectiveCImpl() -> _NS${Self} {
40354026
return _buffer.bridged()
40364027
}
40374028

stdlib/public/core/ShadowProtocols.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ public protocol _NSDictionary : _NSDictionaryCore {
127127
// importer.
128128
func getObjects(_ objects: UnsafeMutablePointer<AnyObject>?,
129129
andKeys keys: UnsafeMutablePointer<AnyObject>?)
130-
}
130+
131+
func enumerateKeysAndObjectsUsingBlock(
132+
_ block: @convention(block) (AnyObject, AnyObject, UnsafeMutablePointer<UInt8>) -> ())
133+
}
131134

132135
/// A shadow for the "core operations" of NSSet.
133136
///
@@ -169,6 +172,8 @@ public protocol _NSSetCore :
169172
/// supplies.
170173
@unsafe_no_objc_tagged_pointer @objc
171174
public protocol _NSSet : _NSSetCore {
175+
func enumerateObjectsUsingBlock(
176+
_ block: @convention(block) (AnyObject, UnsafeMutablePointer<UInt8>) -> ())
172177
}
173178

174179
/// A shadow for the API of NSNumber we will use in the core

0 commit comments

Comments
 (0)