Skip to content

Commit ec74b18

Browse files
committed
Add reference page for Sendable protocol.
1 parent 53bdf86 commit ec74b18

File tree

1 file changed

+100
-6
lines changed

1 file changed

+100
-6
lines changed

stdlib/public/core/Sendable.swift

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,107 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// The Sendable protocol indicates that value of the given type can
14-
/// be safely used in concurrent code.
13+
/// A type whose values can safely be passed across concurrency domains by copying.
14+
///
15+
/// You can safely pass values of a sendable type
16+
/// from one concurrency domains to another ---
17+
/// for example, you can pass a sendable value as the argument
18+
/// when calling an actor's methods.
19+
/// All of the following can be marked as sendable:
20+
///
21+
/// - Value types
22+
/// - References to immutable reference types
23+
/// - Reference types that internally manage access to their state
24+
/// - Functions and closures
25+
///
26+
/// Although this protocol doesn't have any required methods or properties,
27+
/// it does have semantic requirements that are enforced at compile time.
28+
/// These requirements are listed in the sections below.
29+
/// Conformance to `Sendable` must be declared
30+
/// in the same file as the type's declaration.
31+
///
32+
/// To declare conformance to `Sendable` without any compiler enforcement,
33+
/// write `@unchecked Sendable`.
34+
/// You are responsible for the correctness of unchecked sendable types.
35+
/// Unchecked conformance to `Sendable` also disables enforcement
36+
/// of the rule that conformance must be in the same file.
37+
///
38+
/// For information about the language-level concurrency model that `Task` is part of,
39+
/// see [Concurrency][concurrency] in [The Swift Programming Language][tspl].
40+
///
41+
/// [concurrency]: https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html
42+
/// [tspl]: https://docs.swift.org/swift-book/
43+
///
44+
/// ### Sendable Structures and Enumerations
45+
///
46+
/// To satisfy the requirements of the `Sendable` protocol,
47+
/// an enumeration or structure must have only sendable
48+
/// members and associated values.
49+
/// In some cases, structures and enumerations
50+
/// that satisfy the requirements implicitly conform to `Sendable`:
51+
///
52+
/// - Frozen structures and enumerations
53+
///
54+
/// - Structures and enumerations
55+
/// that aren't public and aren't marked `@usableFromInline`.
56+
///
57+
/// Otherwise, you need to declare conformance to `Sendable` explicitly.
58+
///
59+
/// ### Sendable Actors and Classes
60+
///
61+
/// All actor types implicitly conform to `Sendable`.
62+
///
63+
/// To satisfy the requirements of the `Sendable` protocol,
64+
/// final classes must contain only immutable stored properties,
65+
/// and must either be a subclass of `NSObject` or have no superclass.
66+
///
67+
/// Other classes can be marked as `@unchecked Sendable`,
68+
/// disabling compile-time correctness checks,
69+
/// after you manually verify that they satisfy the semantic requirements.
70+
///
71+
/// ### Sendable Functions and Closures
72+
///
73+
/// Instead of conforming to the `Sendable` protocol,
74+
/// you mark sendable functions and closures with the `@Sendable` attribute.
75+
/// Any values that the function or closure captures must be sendable.
76+
/// In addition, sendable closures must use only by-value captures,
77+
/// and the captured values must be of a sendable type.
78+
///
79+
/// In a context that expects a sendable closure,
80+
/// a closure closure that satisfies the requirements
81+
/// implicitly conforms to `Sendable` ---
82+
/// for example, in a call to `Task.detached(priority:operation:)`.
83+
///
84+
/// You can explicitly mark a closure as sendable
85+
/// by writing `@Sendable` as part of a type annotation,
86+
/// or by writing `@Sendable` before the closure's parameters ---
87+
/// for example:
88+
///
89+
/// ```
90+
/// let sendableClosure = { @Sendable (number: Int) -> String in
91+
/// if number > 12 {
92+
/// return "More than a dozen."
93+
/// } else {
94+
/// return "Less than a dozen"
95+
/// }
96+
/// }
97+
/// ```
98+
///
99+
/// ### Sendable Tuples
100+
///
101+
/// To satisfy the requirements of the `Sendable` protocol,
102+
/// all of the elements of the tuple must be sendable.
103+
/// Tuples that satisfy the requirements implicitly conform to `Sendable`.
15104
@_marker public protocol Sendable { }
16-
17-
/// The UnsafeSendable protocol indicates that value of the given type
18-
/// can be safely used in concurrent code, but disables some safety checking
19-
/// at the conformance site.
105+
///
106+
/// A type whose values can safely be passed across concurrency domains by copying,
107+
/// but which disables some safety checking at the conformance site.
108+
///
109+
/// Use an unchecked conformance to `Sendable` instead.
110+
///
111+
/// ```
112+
/// struct MyStructure: @unchecked Sendable { ... }
113+
/// ```
20114
@available(*, deprecated, message: "Use @unchecked Sendable instead")
21115
@_marker public protocol UnsafeSendable: Sendable { }
22116

0 commit comments

Comments
 (0)