Skip to content

Commit d54b880

Browse files
committed
fix struct copy on write issue
1 parent 4d27a65 commit d54b880

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CodableWrapper.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |spec|
1010
spec.name = "CodableWrapper"
11-
spec.version = "0.3.2"
11+
spec.version = "0.3.3"
1212
spec.requires_arc = true
1313
spec.summary = "Codable + PropertyWrapper"
1414
spec.description = "Codable + PropertyWrapper = ☕"

Sources/CodableWrapper/Codec.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import Foundation
99

1010
@propertyWrapper
1111
public struct Codec<Value>: Codable {
12-
let construct: CodecConstruct<Value>
12+
var construct: CodecConstruct<Value>
1313

1414
public var wrappedValue: Value {
1515
get {
16-
construct.storedValue!
16+
return construct.storedValue!
1717
}
1818
set {
19+
if !isKnownUniquelyReferenced(&construct) {
20+
let newConstruct = CodecConstruct<Value>(unsafed: ())
21+
newConstruct.transferFrom(construct)
22+
construct = newConstruct
23+
}
1924
construct.storedValue = newValue
2025
}
2126
}

Tests/CodableWrapperTests/ExampleTest.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ class ExampleTest: XCTestCase {
6464

6565
override class func setUp() {
6666
}
67+
68+
func testStructCopyOnWrite() {
69+
let a = ExampleModel()
70+
let valueInA = a.stringVal
71+
var b = a
72+
b.stringVal = "changed!"
73+
XCTAssertEqual(a.stringVal, valueInA)
74+
}
6775

6876
func testBasicUsage() throws {
6977
let json = #"{"stringVal": "pan", "intVal": "233", "bool": "1", "animal": "cat"}"#

0 commit comments

Comments
 (0)