-
Notifications
You must be signed in to change notification settings - Fork 2
/
Standalone.swift
96 lines (72 loc) · 3.67 KB
/
Standalone.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//=----------------------------------------------------------------------------=
// This source file is part of the DiffableTextViews open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
//*============================================================================*
// MARK: * Standalone
//*============================================================================*
/// Grants ownership of the style's cache.
///
/// Use this modifier to control when the cache is created and destroyed.
///
public struct StandaloneTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
public typealias Value = Base.Value
//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
public var base: Base
@usableFromInline let shared: Storage
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
@inlinable public init(_ base: Base) {
self.base = base; self.shared = Storage(base.cache())
}
//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
@inlinable public func format(_ value: Value, with cache: inout Void) -> String {
base.update(&shared.cache); return base.format(value, with: &shared.cache)
}
@inlinable public func interpret(_ value: Value, with cache: inout Void) -> Commit<Value> {
base.update(&shared.cache); return base.interpret(value, with: &shared.cache)
}
@inlinable public func resolve(_ proposal: Proposal, with cache: inout Void) throws -> Commit<Value> {
base.update(&shared.cache); return try base.resolve(proposal, with: &shared.cache)
}
//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
@inlinable public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.base == rhs.base
}
//*========================================================================*
// MARK: * Storage [...]
//*========================================================================*
@usableFromInline final class Storage {
//=--------------------------------------------------------------------=
@usableFromInline var cache: Base.Cache
//=--------------------------------------------------------------------=
@inlinable init(_ cache: Base.Cache) { self.cache = cache }
}
}
//*============================================================================*
// MARK: * Standalone x Style
//*============================================================================*
public extension DiffableTextStyle {
typealias Standalone = StandaloneTextStyle<Self>
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
/// Grants ownership of the style's cache.
///
/// Use this modifier to control when the cache is created and destroyed.
///
@inlinable func standalone() -> Standalone {
Standalone(self)
}
}