-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow in-place mutation of
NIOLoopBoundBox.value
(#2771)
* Allow in-place mutation of `NIOLoopBoundBox.value` * Fix typos in docs
- Loading branch information
Showing
3 changed files
with
43 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters