Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Apr 10, 2023
1 parent 38569d2 commit a6ca0c8
Show file tree
Hide file tree
Showing 24 changed files with 2,691 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

struct Wrapper<Value> {
var value: Value {
get { self.storage.value }
set {
if Swift.isKnownUniquelyReferenced(&self.storage) {
self.storage.value = newValue
} else {
self.storage = Storage(value: newValue)
}
}
}

private var storage: Storage

init(value: Value) {
self.storage = Storage(value: value)
}

mutating func isKnownUniquelyReferenced() -> Bool {
Swift.isKnownUniquelyReferenced(&self.storage)
}

private class Storage {
var value: Value
init(value: Value) {
self.value = value
}
}
}

import Foundation

extension Wrapper: Equatable where Value: Equatable {
static func == (lhs: Self, rhs: Self) -> Bool {
if lhs.storage === rhs.storage {
return true
}
Thread.sleep(forTimeInterval: 3)
return lhs.value == rhs.value
}
}

var x = Wrapper(value: 1)
x.isKnownUniquelyReferenced()
var y = x
x.isKnownUniquelyReferenced()
y.isKnownUniquelyReferenced()
x == y
x.value = 2
y.value = 2
x == y
y.value
x.value
x.isKnownUniquelyReferenced()
y.isKnownUniquelyReferenced()

var z = 1
var w = z
z = 2
w
z
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Loading

0 comments on commit a6ca0c8

Please sign in to comment.