Skip to content

Commit 3ffb734

Browse files
committed
#40: Added .initializeNow() to LazyContainer
Implemented it everywhere in this package, added tests. Also moved tests to the proper folder.
1 parent 60e6ed8 commit 3ffb734

14 files changed

+235
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ Initial movement from 4.x.
4242

4343
- Transitioned to Swift Testing
4444
- Removed `LinuxMain.swift` & `XCTestManifests.swift` from tests since they're no longer needed
45+
46+
- Introduced `.initializeNow()`
47+
- https://github.com/RougeWare/Swift-Lazy-Containers/issues/40

Sources/LazyContainers/FunctionalLazy.swift renamed to Sources/Lazy/FunctionalLazy.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public struct FunctionalLazy<Value>: LazyContainer {
7373
public var isInitialized: Bool { _guts.isInitialized }
7474

7575

76+
public mutating func initializeNow() {
77+
_guts.initializeNow()
78+
}
79+
80+
7681

7782
/// The actual functionality of `FunctionalLazy`, separated so that the semantics work out better
7883
@propertyWrapper
@@ -114,6 +119,11 @@ public struct FunctionalLazy<Value>: LazyContainer {
114119
set { initializer = { newValue } }
115120
}
116121

122+
123+
func initializeNow() {
124+
_ = initializer()
125+
}
126+
117127

118128
/// Indicates whether the value has indeed been initialized
119129
public var isInitialized: Bool { nil == semaphore }

Sources/LazyContainers/Lazy.swift renamed to Sources/Lazy/Lazy.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public struct Lazy<Value>: LazyContainer {
1919

2020
/// Privatizes the inner-workings of this functional lazy container
2121
@ValueReference
22-
private var guts: ValueHolder<Value>
22+
private var guts: ValueHolder
2323

2424

2525
/// Allows other initializers to have a shared point of initialization
26-
private init(_guts: ValueReference<ValueHolder<Value>>) {
26+
private init(_guts: ValueReference<ValueHolder>) {
2727
self._guts = _guts
2828
}
2929

@@ -73,6 +73,11 @@ public struct Lazy<Value>: LazyContainer {
7373

7474
/// Indicates whether the value has indeed been initialized
7575
public var isInitialized: Bool { _guts.wrappedValue.hasValue }
76+
77+
78+
public mutating func initializeNow() {
79+
guts.initializeNow()
80+
}
7681
}
7782

7883

@@ -120,6 +125,18 @@ public enum LazyContainerValueHolder<Value> {
120125
case .unset(initializer: _): return false
121126
}
122127
}
128+
129+
130+
/// Immediately initializes the value held inside this value holder
131+
///
132+
/// If this holder already contains a value, this does nothing
133+
mutating func initializeNow() {
134+
switch self {
135+
case .hasValue(_): return
136+
case .unset(let initializer):
137+
self = .hasValue(value: initializer())
138+
}
139+
}
123140
}
124141

125142

@@ -128,7 +145,5 @@ public enum LazyContainerValueHolder<Value> {
128145
public extension LazyContainer {
129146

130147
/// Takes care of keeping track of the state, value, and initializer as needed
131-
///
132-
/// - Attention: This will change in version 5, to be an alias to `LazyContainerValueHolder<Value>`
133-
typealias ValueHolder = LazyContainerValueHolder
148+
typealias ValueHolder = LazyContainerValueHolder<Value>
134149
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

Sources/LazyContainers/LazyContainer.swift renamed to Sources/Lazy/LazyContainer.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public protocol LazyContainer {
3434
var isInitialized: Bool { get }
3535

3636

37+
/// Immediately initializes the value held inside this lazy container
38+
///
39+
/// If this holder already contains a value, this does nothing
40+
// https://github.com/RougeWare/Swift-Lazy-Containers/issues/40
41+
mutating func initializeNow()
42+
43+
3744
/// Creates a lazy container that already contains an initialized value.
3845
///
3946
/// This is useful when you need a uniform API (for instance, when implementing a protocol that requires a `Lazy`),
@@ -68,7 +75,5 @@ public final class LazyContainerValueReference<Value> {
6875
public extension LazyContainer {
6976

7077
/// Allows you to use reference semantics to hold a value inside a lazy container.
71-
///
72-
/// - Attention: This will change in version 5, to be an alias to `LazyContainerValueReference<Value>`
7378
typealias ValueReference = LazyContainerValueReference
7479
}

Sources/LazyContainers/ResettableLazy.swift renamed to Sources/Lazy/ResettableLazy.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public struct ResettableLazy<Value>: LazyContainer {
2020

2121
/// Privatizes the inner-workings of this functional lazy container
2222
@ValueReference
23-
private var guts: ResettableValueHolder<Value>
23+
private var guts: ResettableValueHolder
2424

2525

2626
/// Allows other initializers to have a shared point of initialization
27-
private init(_guts: ValueReference<ResettableValueHolder<Value>>) {
27+
private init(_guts: ValueReference<ResettableValueHolder>) {
2828
self._guts = _guts
2929
}
3030

@@ -80,6 +80,11 @@ public struct ResettableLazy<Value>: LazyContainer {
8080
public var isInitialized: Bool { _guts.wrappedValue.hasValue }
8181

8282

83+
public mutating func initializeNow() {
84+
guts.initializeNow()
85+
}
86+
87+
8388
/// Resets this lazy structure back to its unset state. Next time a value is needed, it will be regenerated using
8489
/// the initializer given by the constructor
8590
public func clear() {
@@ -147,6 +152,18 @@ public enum LazyContainerResettableValueHolder<Value> {
147152
case .unset(initializer: _): return false
148153
}
149154
}
155+
156+
157+
/// Immediately initializes the value in this holder.
158+
///
159+
/// If this holder already contains a value, this does nothing
160+
public mutating func initializeNow() {
161+
switch self {
162+
case .hasValue(value: _, initializer: _): return
163+
case .unset(let initializer):
164+
self = .hasValue(value: initializer(), initializer: initializer)
165+
}
166+
}
150167
}
151168

152169

@@ -156,7 +173,5 @@ public enum LazyContainerResettableValueHolder<Value> {
156173
public extension LazyContainer {
157174

158175
/// Takes care of keeping track of the state, value, and initializer as needed
159-
///
160-
/// - Attention: This will change in version 5, to be an alias to `LazyContainerResettableValueHolder<Value>`
161-
typealias ResettableValueHolder = LazyContainerResettableValueHolder
176+
typealias ResettableValueHolder = LazyContainerResettableValueHolder<Value>
162177
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)