Skip to content

Commit

Permalink
Allow in-place mutation of NIOLoopBoundBox.value (#2771)
Browse files Browse the repository at this point in the history
* Allow in-place mutation of `NIOLoopBoundBox.value`

* Fix typos in docs
  • Loading branch information
dnadoba authored Jul 8, 2024
1 parent 9b86559 commit e947421
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/NIOCore/NIOLoopBound.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public struct NIOLoopBound<Value>: @unchecked Sendable {
self._eventLoop.preconditionInEventLoop()
return self._value
}
set {
_modify {
self._eventLoop.preconditionInEventLoop()
self._value = newValue
yield &self._value
}
}
}
Expand Down Expand Up @@ -136,9 +136,9 @@ public final class NIOLoopBoundBox<Value>: @unchecked Sendable {
self._eventLoop.preconditionInEventLoop()
return self._value
}
set {
_modify {
self._eventLoop.preconditionInEventLoop()
self._value = newValue
yield &self._value
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/NIOPosixTests/CoWValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

/// A Copy on Write (CoW) type that can be used in tests to assert in-place mutation
struct CoWValue: @unchecked Sendable {
private final class UniquenessIndicator {}

/// This reference is "copied" if not uniquely referenced
private var uniquenessIndicator = UniquenessIndicator()

/// mutates `self` and returns a boolean whether it was mutated in place or not
/// - Returns: true if mutation happened in-place, false if Copy on Write (CoW) was triggered
mutating func mutateInPlace() -> Bool {
guard isKnownUniquelyReferenced(&self.uniquenessIndicator) else {
self.uniquenessIndicator = UniquenessIndicator()
return false
}
return true
}
}
8 changes: 8 additions & 0 deletions Tests/NIOPosixTests/NIOLoopBoundTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ final class NIOLoopBoundTests: XCTestCase {
}.wait())
}

func testInPlaceMutation() {
var loopBound = NIOLoopBound(CoWValue(), eventLoop: loop)
XCTAssertTrue(loopBound.value.mutateInPlace())

let loopBoundBox = NIOLoopBoundBox(CoWValue(), eventLoop: loop)
XCTAssertTrue(loopBoundBox.value.mutateInPlace())
}

// MARK: - Helpers
func sendableBlackhole<S: Sendable>(_ sendableThing: S) {}

Expand Down

0 comments on commit e947421

Please sign in to comment.