Skip to content

Commit fb3455e

Browse files
committed
[stdlib] Generalize withExtendedLifetime; add extendLifetime
1 parent 2014305 commit fb3455e

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

stdlib/public/core/LifetimeManager.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
/// Extends the lifetime of the given instance.
14+
///
15+
/// - Parameters:
16+
/// - x: An instance to preserve until this function returns.
17+
@_alwaysEmitIntoClient
18+
public func extendLifetime<T: ~Copyable & ~Escapable>(
19+
_ x: borrowing T
20+
) {
21+
Builtin.fixLifetime(x)
22+
}
23+
1324
/// Evaluates a closure while ensuring that the given instance is not destroyed
1425
/// before the closure returns.
1526
///
@@ -20,7 +31,11 @@
2031
/// return value for the `withExtendedLifetime(_:_:)` method.
2132
/// - Returns: The return value, if any, of the `body` closure parameter.
2233
@_alwaysEmitIntoClient
23-
public func withExtendedLifetime<T: ~Copyable, E: Error, Result: ~Copyable>(
34+
public func withExtendedLifetime<
35+
T: ~Copyable & ~Escapable,
36+
E: Error,
37+
Result: ~Copyable
38+
>(
2439
_ x: borrowing T,
2540
_ body: () throws(E) -> Result
2641
) throws(E) -> Result {
@@ -32,7 +47,8 @@ public func withExtendedLifetime<T: ~Copyable, E: Error, Result: ~Copyable>(
3247
@_silgen_name("$ss20withExtendedLifetimeyq_x_q_yKXEtKr0_lF")
3348
@usableFromInline
3449
internal func __abi_withExtendedLifetime<T, Result>(
35-
_ x: T, _ body: () throws -> Result
50+
_ x: T,
51+
_ body: () throws -> Result
3652
) rethrows -> Result {
3753
defer { _fixLifetime(x) }
3854
return try body()
@@ -48,7 +64,11 @@ internal func __abi_withExtendedLifetime<T, Result>(
4864
/// return value for the `withExtendedLifetime(_:_:)` method.
4965
/// - Returns: The return value, if any, of the `body` closure parameter.
5066
@_alwaysEmitIntoClient
51-
public func withExtendedLifetime<T: ~Copyable, E: Error, Result: ~Copyable>(
67+
public func withExtendedLifetime<
68+
T: ~Copyable & ~Escapable,
69+
E: Error,
70+
Result: ~Copyable
71+
>(
5272
_ x: borrowing T,
5373
_ body: (borrowing T) throws(E) -> Result
5474
) throws(E) -> Result {
@@ -70,7 +90,7 @@ internal func __abi_withExtendedLifetime<T, Result>(
7090
// shorten the lifetime of x to be before this point.
7191
@_transparent
7292
@_preInverseGenerics
73-
public func _fixLifetime<T: ~Copyable>(_ x: borrowing T) {
93+
public func _fixLifetime<T: ~Copyable & ~Escapable>(_ x: borrowing T) {
7494
Builtin.fixLifetime(x)
7595
}
7696

0 commit comments

Comments
 (0)