Skip to content

Commit 7db05ec

Browse files
committed
Stuff
1 parent a197a9f commit 7db05ec

File tree

4 files changed

+264
-136
lines changed

4 files changed

+264
-136
lines changed

Sources/SwiftPlot/Color.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ extension Color {
4545
(other.b - b) * offset + b,
4646
(other.a - a) * offset + a)
4747
}
48+
49+
public static func random() -> Color {
50+
return Color(.random(in: 0...1.0), .random(in: 0...1.0),
51+
.random(in: 0...1.0), .random(in: 0...1.0))
52+
}
4853
}
4954

5055
#if canImport(CoreGraphics)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
public protocol Copyable {
3+
func copy() -> Self
4+
}
5+
6+
/// This is an implementation detail. You shouldn't conform to this yourself.
7+
@dynamicMemberLookup
8+
public protocol _CopyOnWriteValue {
9+
associatedtype _COWStorage: AnyObject, Copyable
10+
var _storage: _COWStorage { get set }
11+
}
12+
13+
extension _CopyOnWriteValue {
14+
public subscript<T>(dynamicMember keypath: KeyPath<_COWStorage, T>) -> T {
15+
get { _storage[keyPath: keypath] }
16+
}
17+
public subscript<T>(dynamicMember keypath: ReferenceWritableKeyPath<_COWStorage, T>) -> T {
18+
get { _storage[keyPath: keypath] }
19+
_modify {
20+
if !isKnownUniquelyReferenced(&_storage) { _storage = _storage.copy() }
21+
yield &_storage[keyPath: keypath]
22+
}
23+
set {
24+
if !isKnownUniquelyReferenced(&_storage) { _storage = _storage.copy() }
25+
_storage[keyPath: keypath] = newValue
26+
}
27+
}
28+
}
29+
30+
public struct TestObj: _CopyOnWriteValue {
31+
32+
public final class _COWStorage: Copyable {
33+
public var propOne: Int
34+
public var propTwo: String
35+
public func copy() -> Self {
36+
print("copying")
37+
return Self(propOne, propTwo)
38+
}
39+
40+
init(_ p1: Int, _ p2: String) { self.propOne = p1; self.propTwo = p2 }
41+
}
42+
public var _storage = _COWStorage(0, "")
43+
44+
public init() {}
45+
}

0 commit comments

Comments
 (0)