-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] API additions for basic noncopyable primitives #73807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ab51f16
8dddef6
f2dc894
9d947e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -529,24 +529,24 @@ extension MutableCollection { | |
@inlinable | ||
@_preInverseGenerics | ||
public func swap<T: ~Copyable>(_ a: inout T, _ b: inout T) { | ||
// Semantically equivalent to (a, b) = (b, a). | ||
// Microoptimized to avoid retain/release traffic. | ||
#if $BuiltinUnprotectedAddressOf | ||
let p1 = Builtin.unprotectedAddressOf(&a) | ||
let p2 = Builtin.unprotectedAddressOf(&b) | ||
#else | ||
let p1 = Builtin.addressof(&a) | ||
let p2 = Builtin.addressof(&b) | ||
#endif | ||
_debugPrecondition( | ||
p1 != p2, | ||
"swapping a location with itself is not supported") | ||
|
||
// Take from P1. | ||
let tmp: T = Builtin.take(p1) | ||
// Transfer P2 into P1. | ||
Builtin.initialize(Builtin.take(p2) as T, p1) | ||
// Initialize P2. | ||
Builtin.initialize(tmp, p2) | ||
let temp = consume a | ||
a = consume b | ||
b = consume temp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
} | ||
|
||
/// Replaces the value of a mutable value with the supplied new value, | ||
/// returning the original. | ||
/// | ||
/// - Parameters: | ||
/// - item: A mutable binding. | ||
/// - newValue: The new value of `item`. | ||
/// - Returns: The original value of `item`. | ||
@_alwaysEmitIntoClient | ||
public func exchange<T: ~Copyable>( | ||
_ item: inout T, | ||
with newValue: consuming T | ||
) -> T { | ||
let oldValue = consume item | ||
item = consume newValue | ||
return oldValue | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,7 +232,7 @@ extension Optional where Wrapped: ~Copyable { | |
) throws(E) -> U? { | ||
#if compiler(>=6.0) && $NoncopyableGenerics | ||
switch self { | ||
case .some(_borrowing y): | ||
case .some(let y): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming these changes are in reaction to warnings about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Local testing on release/6.0 indicates we're good -- those old compilers appear to accept this PR (or rather, the sibling PR #73810) as is, with no changes. Switching back from |
||
return .some(try transform(y)) | ||
case .none: | ||
return .none | ||
|
@@ -310,7 +310,7 @@ extension Optional where Wrapped: ~Copyable { | |
) throws(E) -> U? { | ||
#if compiler(>=6.0) && $NoncopyableGenerics | ||
switch self { | ||
case .some(_borrowing y): | ||
case .some(let y): | ||
return try transform(y) | ||
case .none: | ||
return .none | ||
|
@@ -418,8 +418,8 @@ extension Optional where Wrapped: ~Copyable { | |
/// | ||
/// - Returns: The wrapped value being stored in this instance. If this | ||
/// instance is `nil`, returns `nil`. | ||
@_alwaysEmitIntoClient // FIXME(NCG): Make this public. | ||
public mutating func _take() -> Self { | ||
@_alwaysEmitIntoClient | ||
public mutating func take() -> Self { | ||
let result = consume self | ||
self = nil | ||
return result | ||
|
Uh oh!
There was an error while loading. Please reload this page.