-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(collection): Fixed
assign
not working with custom collection (#41)
- Loading branch information
Showing
9 changed files
with
111 additions
and
51 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
14 changes: 14 additions & 0 deletions
14
Sources/CohesionKit/UnsafePointer/BufferedCollection.swift
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,14 @@ | ||
import Foundation | ||
|
||
/// A collection providing an access to its buffer. | ||
/// | ||
/// Idea was to directly use `BufferedCollection.withContiguousStorageIfAvailable`` **but default implementation returns nil** | ||
/// (see https://github.com/apple/swift-evolution/blob/main/proposals/0237-contiguous-collection.md) | ||
public protocol BufferedCollection: Collection { | ||
/// Provides an access to collection inner buffer. | ||
/// | ||
/// Don't forward to `withContiguousStorageIfAvailable` which default implementation returns nil 🤦♂️ | ||
mutating func withUnsafeMutableBufferPointer<R>(_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R) rethrows -> R | ||
} | ||
|
||
extension Array: BufferedCollection { } |
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
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
41 changes: 0 additions & 41 deletions
41
Tests/CohesionKitTests/KeyPath/UnsafeMutablePointerTests.swift
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
Tests/CohesionKitTests/UnsafePointer/UnsafeMutablePointerTests.swift
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,87 @@ | ||
import XCTest | ||
@testable import CohesionKit | ||
|
||
class UnsafeMutablePointerTests: XCTestCase { | ||
func test_assign_propertyIsImmutable_propertyIsChanged() { | ||
var hello = Hello(collection: [], singleValue: "hello") | ||
|
||
withUnsafeMutablePointer(to: &hello) { | ||
$0.assign("world", to: \Hello.singleValue) | ||
} | ||
|
||
XCTAssertEqual(hello.singleValue, "world") | ||
} | ||
|
||
func test_assign_keyPathIsArray_propertyIsImmutable_arrayIsChanged() { | ||
var hello = Hello(collection: [1, 2, 3, 4], singleValue: "") | ||
|
||
withUnsafeMutablePointer(to: &hello) { | ||
$0.assign(5, to: \Hello.collection, index: 3) | ||
} | ||
|
||
XCTAssertEqual(hello.collection, [1, 2, 3, 5]) | ||
} | ||
|
||
func test_assign_keyPathIsArray_mutipleAssignments_arrayIsChanged() { | ||
var hello = Hello(collection: [1, 2, 3, 4], singleValue: "") | ||
|
||
withUnsafeMutablePointer(to: &hello) { | ||
$0.assign(4, to: \Hello.collection, index: 0) | ||
$0.assign(3, to: \Hello.collection, index: 0) | ||
$0.assign(2, to: \Hello.collection, index: 0) | ||
} | ||
|
||
XCTAssertEqual(hello.collection, [2, 2, 3, 4]) | ||
} | ||
|
||
func test_assign_keyPathIsCustomCollection_colectionIsChanged() { | ||
var hello = Hello(collection: [], singleValue: "") | ||
|
||
hello.myCollection = ["1", "2"] | ||
|
||
withUnsafeMutablePointer(to: &hello) { | ||
$0.assign("3", to: \.myCollection, index: 0) | ||
$0.assign("4", to: \.myCollection, index: 1) | ||
} | ||
|
||
XCTAssertEqual(hello.myCollection, ["3", "4"]) | ||
} | ||
} | ||
|
||
private struct Hello { | ||
let collection: [Int] | ||
let singleValue: String | ||
var myCollection: MyCollection = [] | ||
} | ||
|
||
struct MyCollection: BufferedCollection, Equatable, ExpressibleByArrayLiteral { | ||
typealias Element = Array<String>.Element | ||
typealias Index = Array<String>.Index | ||
|
||
var elements: [String] = [] | ||
|
||
var startIndex: Index { elements.startIndex } | ||
|
||
var endIndex: Index { elements.endIndex } | ||
|
||
init(arrayLiteral elements: String...) { | ||
self.elements = elements | ||
} | ||
|
||
subscript(position: Index) -> Element { | ||
get { | ||
elements[position] | ||
} | ||
set(newValue) { | ||
elements[position] = newValue | ||
} | ||
} | ||
|
||
func index(after i: Index) -> Index { | ||
elements.index(after: i) | ||
} | ||
|
||
mutating func withUnsafeMutableBufferPointer<R>(_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R) rethrows -> R { | ||
try elements.withUnsafeMutableBufferPointer(body) | ||
} | ||
} |
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