mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Add __consuming and __owned to Set and Dictionary members where applicable. Ignore compiler intrinsics for casting for now — their ARC behavior is covered by unit tests that need to be updated.
43 lines
1.3 KiB
Swift
43 lines
1.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Initializes a `Dictionary` from unique members.
|
|
///
|
|
/// Using a builder can be faster than inserting members into an empty
|
|
/// `Dictionary`.
|
|
@_fixed_layout
|
|
public // SPI(Foundation)
|
|
struct _DictionaryBuilder<Key: Hashable, Value> {
|
|
@usableFromInline
|
|
internal var _target: _NativeDictionary<Key, Value>
|
|
@usableFromInline
|
|
internal let _requestedCount: Int
|
|
|
|
@inlinable
|
|
public init(count: Int) {
|
|
_target = _NativeDictionary(capacity: count)
|
|
_requestedCount = count
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func add(key newKey: Key, value: Value) {
|
|
_target.insertNew(key: newKey, value: value)
|
|
}
|
|
|
|
@inlinable
|
|
public __consuming func take() -> Dictionary<Key, Value> {
|
|
_precondition(_target.count == _requestedCount,
|
|
"The number of members added does not match the promised count")
|
|
return Dictionary(_native: _target)
|
|
}
|
|
}
|