From 538c294fae1a2e067f152e14263068c4a8e5638b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pacheco=20Neves?= Date: Mon, 17 Jan 2022 16:53:15 +0000 Subject: [PATCH] =?UTF-8?q?Revise=20`Box`es=20=F0=9F=93=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We have had two `Box` variants for a very long time: `Box` and `VarBox`, containing an immutable and mutable value respectively. However, from our experience the immutable variant is not used often if at all, which means its immutability guarantee is not something that people want or need. Furthermore, following the latest language developments a `Box` is a very good candidate for a property wrapper, and more so now that they can be defined as local properties. ## Changes - Deprecate `VarBox` in favor of `Box`, making it wrap a mutable value. - Make `Box` a `@propertyWrapper`, and `@dynamicMemberLookup`. The latter is useful if we use `Box` as a regular type (i.e. not a PW). --- .../Network+URLSessionNetworkStack.swift | 8 ++-- Sources/Utils/Box.swift | 42 +++++++++++-------- .../DiskMemoryPersistenceTestCase.swift | 12 +++--- Tests/AlicerceTests/Utils/BoxTestCase.swift | 31 +++++++++++--- .../Utils/PthreadLockTestCase.swift | 6 +-- .../Utils/UnfairLockTestCase.swift | 6 +-- 6 files changed, 65 insertions(+), 40 deletions(-) diff --git a/Sources/Network/Network+URLSessionNetworkStack.swift b/Sources/Network/Network+URLSessionNetworkStack.swift index 0de9abcf..6497fa8f 100644 --- a/Sources/Network/Network+URLSessionNetworkStack.swift +++ b/Sources/Network/Network+URLSessionNetworkStack.swift @@ -114,20 +114,20 @@ extension Network { fatalError("🔥 `session` is `nil`! Forgot to 💉?") } - let taskIdentifierBox = VarBox(.max) + @Box var taskIdentifierBox = Int.max let cancelableBag = CancelableBag() let completionHandler = makeTaskCompletionHandler( request: request, resource: resource, - taskIdentifierBox: taskIdentifierBox, + taskIdentifierBox: _taskIdentifierBox, cancelableBag: cancelableBag, completion: completion ) let task = session.dataTask(with: request, completionHandler: completionHandler) - taskIdentifierBox.value = task.taskIdentifier + taskIdentifierBox = task.taskIdentifier cancelableBag.add(cancelable: WeakCancelable(task)) resource.interceptScheduledTask(withIdentifier: task.taskIdentifier, request: request) @@ -141,7 +141,7 @@ extension Network { private func makeTaskCompletionHandler( request: URLRequest, resource: Resource, - taskIdentifierBox: VarBox, + taskIdentifierBox: Box, cancelableBag: CancelableBag, completion: @escaping FetchCompletionClosure ) -> URLSessionDataTaskClosure { diff --git a/Sources/Utils/Box.swift b/Sources/Utils/Box.swift index bbd88908..b6c55ba7 100644 --- a/Sources/Utils/Box.swift +++ b/Sources/Utils/Box.swift @@ -1,33 +1,39 @@ -/// An arbitrary container which stores a **constant** value of type `T`. +/// An arbitrary container (and property wrapper) which stores a **mutable** value of type `T`. /// -/// This main purpose of this object is to encapsulate value types so that they can be used like a reference type -/// (e.g. pass value types around without copying them, "share" value types between closures, etc) +/// The main purpose of this object is to encapsulate value types so that they can be used like a reference type +/// (e.g. pass value types around without copying them, memoize value types inside closures via capture list, etc) +@propertyWrapper +@dynamicMemberLookup public final class Box { - /// The encapsulated value. - public let value: T + /// The wrapped value. + public var wrappedValue: T - /// Instantiate a new constant value box with the given value. + /// Instantiate a new mutable value box with the given value. /// - /// - parameter value: the value to encapsulate. + /// - parameter wrappedValue: the value to encapsulate. /// /// - returns: a newly instantiated box with the encapsulated value. - public init(_ value: T) { self.value = value } + public init(wrappedValue: T) { self.wrappedValue = wrappedValue } + + public subscript(dynamicMember keyPath: KeyPath) -> U { wrappedValue[keyPath: keyPath] } } -/// An arbitrary container which stores a **variable** value of type `T`. -/// -/// This main purpose of this object is to encapsulate value types so that they can be used like a reference type -/// (e.g. pass value types around without copying them, "share" value types between closures, etc) -public final class VarBox { +extension Box { - /// The encapsulated value. - public var value: T + /// The wrapped value (compact). + public var value: T { + get { wrappedValue } + set { wrappedValue = newValue } + } - /// Instantiate a new variable value box with the given value. + /// Instantiate a new mutable value box with the given value (compact). /// - /// - parameter value: the value to encapsulate. + /// - parameter wrappedValue: the value to encapsulate. /// /// - returns: a newly instantiated box with the encapsulated value. - public init(_ value: T) { self.value = value } + public convenience init(_ value: T) { self.init(wrappedValue: value) } } + +@available(*, unavailable, renamed: "Box") +public typealias VarBox = Box diff --git a/Tests/AlicerceTests/Persistence/DiskMemoryPersistenceTestCase.swift b/Tests/AlicerceTests/Persistence/DiskMemoryPersistenceTestCase.swift index 6d55a643..36f6f7f3 100644 --- a/Tests/AlicerceTests/Persistence/DiskMemoryPersistenceTestCase.swift +++ b/Tests/AlicerceTests/Persistence/DiskMemoryPersistenceTestCase.swift @@ -266,7 +266,7 @@ final class DiskMemoryPersistenceTestCase: XCTestCase { let measureExpectation = expectation(description: "measure") measureExpectation.expectedFulfillmentCount = 2 - performanceMetrics.measureInvokedClosure = { [count = VarBox(0)] identifier, metadata in + performanceMetrics.measureInvokedClosure = { [count = Box(0)] identifier, metadata in if count.value == 0 { XCTAssertEqual(identifier, performanceMetrics.memoryWriteIdentifier) XCTAssertDumpsEqual(metadata, [performanceMetrics.blobSizeMetadataKey : mrMinderSize, @@ -313,7 +313,7 @@ final class DiskMemoryPersistenceTestCase: XCTestCase { let measure = self.expectation(description: "measure") measure.expectedFulfillmentCount = 2 - performanceMetrics.measureInvokedClosure = { [count = VarBox(0)] identifier, metadata in + performanceMetrics.measureInvokedClosure = { [count = Box(0)] identifier, metadata in if count.value == 0 { XCTAssertEqual(identifier, performanceMetrics.memoryWriteIdentifier) XCTAssertDumpsEqual(metadata, [performanceMetrics.blobSizeMetadataKey : mrMinderSize, @@ -371,7 +371,7 @@ final class DiskMemoryPersistenceTestCase: XCTestCase { let measureExpectation = expectation(description: "measure") measureExpectation.expectedFulfillmentCount = 2 - performanceMetrics.measureInvokedClosure = { [count = VarBox(0)] identifier, metadata in + performanceMetrics.measureInvokedClosure = { [count = Box(0)] identifier, metadata in if count.value == 0 { XCTAssertEqual(identifier, performanceMetrics.memoryWriteIdentifier) XCTAssertDumpsEqual(metadata, [performanceMetrics.blobSizeMetadataKey : mrMinderSize, @@ -437,7 +437,7 @@ final class DiskMemoryPersistenceTestCase: XCTestCase { let measureExpectation = expectation(description: "measure") measureExpectation.expectedFulfillmentCount = 2 - performanceMetrics.measureInvokedClosure = { [count = VarBox(0)] identifier, metadata in + performanceMetrics.measureInvokedClosure = { [count = Box(0)] identifier, metadata in if count.value == 0 { XCTAssertEqual(identifier, performanceMetrics.memoryWriteIdentifier) XCTAssertDumpsEqual(metadata, [performanceMetrics.blobSizeMetadataKey : mrMinderSize, @@ -477,7 +477,7 @@ final class DiskMemoryPersistenceTestCase: XCTestCase { let measureExpectation2 = expectation(description: "measure") measureExpectation2.expectedFulfillmentCount = 3 - performanceMetrics.measureInvokedClosure = { [count = VarBox(0)] identifier, metadata in + performanceMetrics.measureInvokedClosure = { [count = Box(0)] identifier, metadata in if count.value == 0 { XCTAssertEqual(identifier, performanceMetrics.memoryReadIdentifier) // cache miss @@ -522,7 +522,7 @@ final class DiskMemoryPersistenceTestCase: XCTestCase { let measureExpectation = expectation(description: "measure") measureExpectation.expectedFulfillmentCount = 2 - performanceMetrics.measureInvokedClosure = { [count = VarBox(0)] identifier, metadata in + performanceMetrics.measureInvokedClosure = { [count = Box(0)] identifier, metadata in if count.value == 0 { XCTAssertEqual(identifier, performanceMetrics.memoryReadIdentifier) XCTAssertDumpsEqual(metadata, [performanceMetrics.blobSizeMetadataKey : 0, diff --git a/Tests/AlicerceTests/Utils/BoxTestCase.swift b/Tests/AlicerceTests/Utils/BoxTestCase.swift index 2cfd4e16..e03f9b09 100644 --- a/Tests/AlicerceTests/Utils/BoxTestCase.swift +++ b/Tests/AlicerceTests/Utils/BoxTestCase.swift @@ -3,24 +3,43 @@ import XCTest class BoxTestCase: XCTestCase { - func testBox_ShouldWrapValue() { + func test_init_ShouldWrapValueAndAllowMutation() { let value = 1337 let box = Box(value) XCTAssertEqual(value, box.value) + + let newValue = 7331 + box.value = newValue + + XCTAssertEqual(newValue, box.value) } - func testVarBox_ShouldWrapValueAndAllowModifying() { + func test_propertyWrapper_ShouldWrapValueAndAllowMutation() { let value = 1337 - let varBox = VarBox(value) + @Box var box = value - XCTAssertEqual(value, varBox.value) + XCTAssertEqual(value, box) let newValue = 7331 - varBox.value = newValue + box = newValue + + XCTAssertEqual(newValue, box) + } + + func test_dynamicMember_ShouldExposePropertiesInWrappedValue() { + + struct Foo { + + var foo: Int = 1337 + } + + let box = Box(.init()) + XCTAssertEqual(box.foo, 1337) - XCTAssertEqual(newValue, varBox.value) + @Box var box2 = Foo() + XCTAssertEqual(_box2.foo, 1337) } } diff --git a/Tests/AlicerceTests/Utils/PthreadLockTestCase.swift b/Tests/AlicerceTests/Utils/PthreadLockTestCase.swift index c5b4db87..cd09c21d 100644 --- a/Tests/AlicerceTests/Utils/PthreadLockTestCase.swift +++ b/Tests/AlicerceTests/Utils/PthreadLockTestCase.swift @@ -112,13 +112,13 @@ class PthreadLockTestCase: XCTestCase { return q } - let box = VarBox(0) + @Box var box = 0 for _ in 1...numWrites { queues.forEach { $0.addOperation { self.lock.lock() - box.value += 1 + box += 1 self.lock.unlock() writeExpectation.fulfill() } @@ -130,6 +130,6 @@ class PthreadLockTestCase: XCTestCase { waitForExpectations(timeout: 1) - XCTAssertEqual(box.value, numWrites * numQueues) + XCTAssertEqual(box, numWrites * numQueues) } } diff --git a/Tests/AlicerceTests/Utils/UnfairLockTestCase.swift b/Tests/AlicerceTests/Utils/UnfairLockTestCase.swift index 8ccd2603..82a32d6d 100644 --- a/Tests/AlicerceTests/Utils/UnfairLockTestCase.swift +++ b/Tests/AlicerceTests/Utils/UnfairLockTestCase.swift @@ -112,13 +112,13 @@ class UnfairLockTestCase: XCTestCase { return q } - let box = VarBox(0) + @Box var box = 0 for _ in 1...numWrites { queues.forEach { $0.addOperation { self.lock.lock() - box.value += 1 + box += 1 self.lock.unlock() writeExpectation.fulfill() } @@ -130,7 +130,7 @@ class UnfairLockTestCase: XCTestCase { waitForExpectations(timeout: 1) - XCTAssertEqual(box.value, numWrites * numQueues) + XCTAssertEqual(box, numWrites * numQueues) } }