File tree Expand file tree Collapse file tree 3 files changed +43
-4
lines changed Expand file tree Collapse file tree 3 files changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -47,9 +47,9 @@ public struct NIOLoopBound<Value>: @unchecked Sendable {
47
47
self . _eventLoop. preconditionInEventLoop ( )
48
48
return self . _value
49
49
}
50
- set {
50
+ _modify {
51
51
self . _eventLoop. preconditionInEventLoop ( )
52
- self . _value = newValue
52
+ yield & self . _value
53
53
}
54
54
}
55
55
}
@@ -136,9 +136,9 @@ public final class NIOLoopBoundBox<Value>: @unchecked Sendable {
136
136
self . _eventLoop. preconditionInEventLoop ( )
137
137
return self . _value
138
138
}
139
- set {
139
+ _modify {
140
140
self . _eventLoop. preconditionInEventLoop ( )
141
- self . _value = newValue
141
+ yield & self . _value
142
142
}
143
143
}
144
144
}
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the SwiftNIO open source project
4
+ //
5
+ // Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
6
+ // Licensed under Apache License v2.0
7
+ //
8
+ // See LICENSE.txt for license information
9
+ // See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10
+ //
11
+ // SPDX-License-Identifier: Apache-2.0
12
+ //
13
+ //===----------------------------------------------------------------------===//
14
+
15
+ /// A Copy on Write (CoW) type that can be used in tests to assert in-place mutation
16
+ struct CoWValue : @unchecked Sendable {
17
+ private final class UniquenessIndicator { }
18
+
19
+ /// This reference is "copied" if not uniquely referenced
20
+ private var uniquenessIndicator = UniquenessIndicator ( )
21
+
22
+ /// mutates `self` and returns a boolean whether it was mutated in place or not
23
+ /// - Returns: true if mutation happened in-place, false if Copy on Write (CoW) was triggered
24
+ mutating func mutateInPlace( ) -> Bool {
25
+ guard isKnownUniquelyReferenced ( & self . uniquenessIndicator) else {
26
+ self . uniquenessIndicator = UniquenessIndicator ( )
27
+ return false
28
+ }
29
+ return true
30
+ }
31
+ }
Original file line number Diff line number Diff line change @@ -68,6 +68,14 @@ final class NIOLoopBoundTests: XCTestCase {
68
68
} . wait ( ) )
69
69
}
70
70
71
+ func testInPlaceMutation( ) {
72
+ var loopBound = NIOLoopBound ( CoWValue ( ) , eventLoop: loop)
73
+ XCTAssertTrue ( loopBound. value. mutateInPlace ( ) )
74
+
75
+ let loopBoundBox = NIOLoopBoundBox ( CoWValue ( ) , eventLoop: loop)
76
+ XCTAssertTrue ( loopBoundBox. value. mutateInPlace ( ) )
77
+ }
78
+
71
79
// MARK: - Helpers
72
80
func sendableBlackhole< S: Sendable > ( _ sendableThing: S ) { }
73
81
You can’t perform that action at this time.
0 commit comments