|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
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 domain 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 | +/// |
| 23 | +/// - Reference types with no mutable storage |
| 24 | +/// |
| 25 | +/// - Reference types that internally manage access to their state |
| 26 | +/// |
| 27 | +/// - Functions and closures (by marking them with `@Sendable`) |
| 28 | +/// |
| 29 | +/// Although this protocol doesn't have any required methods or properties, |
| 30 | +/// it does have semantic requirements that are enforced at compile time. |
| 31 | +/// These requirements are listed in the sections below. |
| 32 | +/// Conformance to `Sendable` must be declared |
| 33 | +/// in the same file as the type's declaration. |
| 34 | +/// |
| 35 | +/// To declare conformance to `Sendable` without any compiler enforcement, |
| 36 | +/// write `@unchecked Sendable`. |
| 37 | +/// You are responsible for the correctness of unchecked sendable types, |
| 38 | +/// for example, by protecting all access to its state with a lock or a queue. |
| 39 | +/// Unchecked conformance to `Sendable` also disables enforcement |
| 40 | +/// of the rule that conformance must be in the same file. |
| 41 | +/// |
| 42 | +/// For information about the language-level concurrency model that `Task` is part of, |
| 43 | +/// see [Concurrency][concurrency] in [The Swift Programming Language][tspl]. |
| 44 | +/// |
| 45 | +/// [concurrency]: https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html |
| 46 | +/// [tspl]: https://docs.swift.org/swift-book/ |
| 47 | +/// |
| 48 | +/// ### Sendable Structures and Enumerations |
| 49 | +/// |
| 50 | +/// To satisfy the requirements of the `Sendable` protocol, |
| 51 | +/// an enumeration or structure must have only sendable |
| 52 | +/// members and associated values. |
| 53 | +/// In some cases, structures and enumerations |
| 54 | +/// that satisfy the requirements implicitly conform to `Sendable`: |
| 55 | +/// |
| 56 | +/// - Frozen structures and enumerations |
| 57 | +/// |
| 58 | +/// - Structures and enumerations |
| 59 | +/// that aren't public and aren't marked `@usableFromInline`. |
| 60 | +/// |
| 61 | +/// Otherwise, you need to declare conformance to `Sendable` explicitly. |
| 62 | +/// |
| 63 | +/// Structures that have nonsendable stored properties |
| 64 | +/// and enumerations that have nonsendable associated values |
| 65 | +/// can be marked as `@unchecked Sendable`, |
| 66 | +/// disabling compile-time correctness checks, |
| 67 | +/// after you manually verify that |
| 68 | +/// they satisfy the `Sendable` protocol's semantic requirements. |
| 69 | +/// |
| 70 | +/// ### Sendable Actors |
| 71 | +/// |
| 72 | +/// All actor types implicitly conform to `Sendable` |
| 73 | +/// because actors ensure that all access to their mutable state |
| 74 | +/// is performed sequentially. |
| 75 | +/// |
| 76 | +/// ### Sendable Classes |
| 77 | +/// |
| 78 | +/// To satisfy the requirements of the `Sendable` protocol, |
| 79 | +/// a class must: |
| 80 | +/// |
| 81 | +/// - Be marked `final` |
| 82 | +/// |
| 83 | +/// - Contain only stored properties that are immutable and sendable |
| 84 | +/// |
| 85 | +/// - Have no superclass or have `NSObject` as the superclass |
| 86 | +/// |
| 87 | +/// Classes marked with `@MainActor` are implicitly sendable, |
| 88 | +/// because the main actor coordinates all access to its state. |
| 89 | +/// These classes can have stored properties that are mutable and nonsendable. |
| 90 | +/// |
| 91 | +/// Classes that don't meet the requirements above |
| 92 | +/// can be marked as `@unchecked Sendable`, |
| 93 | +/// disabling compile-time correctness checks, |
| 94 | +/// after you manually verify that |
| 95 | +/// they satisfy the `Sendable` protocol's semantic requirements. |
| 96 | +/// |
| 97 | +/// ### Sendable Functions and Closures |
| 98 | +/// |
| 99 | +/// Instead of conforming to the `Sendable` protocol, |
| 100 | +/// you mark sendable functions and closures with the `@Sendable` attribute. |
| 101 | +/// Any values that the function or closure captures must be sendable. |
| 102 | +/// In addition, sendable closures must use only by-value captures, |
| 103 | +/// and the captured values must be of a sendable type. |
| 104 | +/// |
| 105 | +/// In a context that expects a sendable closure, |
| 106 | +/// a closure that satisfies the requirements |
| 107 | +/// implicitly conforms to `Sendable` --- |
| 108 | +/// for example, in a call to `Task.detached(priority:operation:)`. |
| 109 | +/// |
| 110 | +/// You can explicitly mark a closure as sendable |
| 111 | +/// by writing `@Sendable` as part of a type annotation, |
| 112 | +/// or by writing `@Sendable` before the closure's parameters --- |
| 113 | +/// for example: |
| 114 | +/// |
| 115 | +/// let sendableClosure = { @Sendable (number: Int) -> String in |
| 116 | +/// if number > 12 { |
| 117 | +/// return "More than a dozen." |
| 118 | +/// } else { |
| 119 | +/// return "Less than a dozen" |
| 120 | +/// } |
| 121 | +/// } |
| 122 | +/// |
| 123 | +/// ### Sendable Tuples |
| 124 | +/// |
| 125 | +/// To satisfy the requirements of the `Sendable` protocol, |
| 126 | +/// all of the elements of the tuple must be sendable. |
| 127 | +/// Tuples that satisfy the requirements implicitly conform to `Sendable`. |
| 128 | +/// |
| 129 | +/// ### Sendable Metatypes |
| 130 | +/// |
| 131 | +/// Metatypes such as `Int.Type` implicitly conform to the `Sendable` protocol. |
15 | 132 | @_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. |
| 133 | +/// |
| 134 | +/// A type whose values can safely be passed across concurrency domains by copying, |
| 135 | +/// but which disables some safety checking at the conformance site. |
| 136 | +/// |
| 137 | +/// Use an unchecked conformance to `Sendable` instead --- for example: |
| 138 | +/// |
| 139 | +/// struct MyStructure: @unchecked Sendable { ... } |
20 | 140 | @available(*, deprecated, message: "Use @unchecked Sendable instead")
|
21 | 141 | @_marker public protocol UnsafeSendable: Sendable { }
|
22 | 142 |
|
|
0 commit comments