Skip to content

Commit a0df68a

Browse files
Include an queue-backed actor example (#101)
* Include an queue-backed actor example * Comment out asUnownedSerialExecutor until compiler bug is addressed
1 parent ca995d7 commit a0df68a

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

Guide.docc/IncrementalAdoption.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,30 @@ class PersonalTransportation {
254254
}
255255
```
256256

257+
## Integrating DispatchSerialQueue with Actors
258+
259+
By default, the mechanism actors use to schedule and execute work
260+
is system-defined.
261+
However you can override this to provide a custom implementation.
262+
The `DispatchSerialQueue` type includes built-in support for this facility.
263+
264+
```swift
265+
actor LandingSite {
266+
private let queue = DispatchSerialQueue(label: "something")
267+
268+
nonisolated var unownedExecutor: UnownedSerialExecutor {
269+
queue.asUnownedSerialExecutor()
270+
}
271+
272+
func acceptTransport(_ transport: PersonalTransportation) {
273+
// this function will be running on queue
274+
}
275+
}
276+
```
277+
278+
This can be useful if you want to migrate a type towards the actor model
279+
while maintaining compatibility with code that depends on `DispatchQueue`.
280+
257281
## Backwards Compatibility
258282

259283
It's important to keep in mind that static isolation, being part of the type
@@ -332,6 +356,7 @@ NS_SWIFT_ASYNC_NAME
332356
NS_SWIFT_ASYNC_NOTHROW
333357
NS_SWIFT_UNAVAILABLE_FROM_ASYNC(msg)
334358
```
359+
335360
### Dealing with missing isolation annotations in Objective-C libraries
336361

337362
While the SDKs and other Objective-C libraries make progress in adopting Swift concurrency,

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ let package = Package(
3434
),
3535
.executableTarget(
3636
name: "Swift5Examples",
37-
dependencies: ["Library"],
37+
dependencies: ["Library", "ObjCLibrary"],
3838
swiftSettings: [
3939
.swiftLanguageVersion(.v5),
4040
.enableUpcomingFeature("StrictConcurrency"),
4141
]
4242
),
4343
.executableTarget(
4444
name: "Swift6Examples",
45-
dependencies: ["Library"]
45+
dependencies: ["Library", "ObjCLibrary"]
4646
)
4747
]
4848
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Dispatch
2+
import ObjCLibrary
3+
4+
/// Example that backs an actor with a queue.
5+
///
6+
/// > Note: `DispatchSerialQueue`'s initializer was only made available in more recent OS versions.
7+
@available(macOS 14.0, iOS 17.0, macCatalyst 17.0, tvOS 17.0, watchOS 10.0, *)
8+
actor LandingSite {
9+
private let queue = DispatchSerialQueue(label: "SerialQueue")
10+
11+
// this currently failed to build because of the @available usage, rdar://116684282
12+
// nonisolated var unownedExecutor: UnownedSerialExecutor {
13+
// queue.asUnownedSerialExecutor()
14+
// }
15+
16+
func acceptTransport(_ transport: JPKJetPack) {
17+
// this function will be running on queue
18+
}
19+
}
20+
21+
func exerciseIncrementalMigrationExamples() async {
22+
print("Incremental Migration Examples")
23+
24+
if #available(macOS 14.0, iOS 17.0, macCatalyst 17.0, tvOS 17.0, watchOS 10.0, *) {
25+
print(" - using an actor with a DispatchSerialQueue executor")
26+
let site = LandingSite()
27+
28+
let transport = JPKJetPack()
29+
30+
await site.acceptTransport(transport)
31+
await site.acceptTransport(transport)
32+
await site.acceptTransport(transport)
33+
}
34+
}

Sources/Examples/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ let manualSerialQueue = DispatchQueue(label: "com.apple.SwiftMigrationGuide")
77
await exerciseGlobalExamples()
88
await exerciseBoundaryCrossingExamples()
99
await exerciseConformanceMismatchExamples()
10+
await exerciseIncrementalMigrationExamples()

0 commit comments

Comments
 (0)