Skip to content

Commit 872fcdc

Browse files
committed
add default value static var to Property protocol
1 parent db9807a commit 872fcdc

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Sources/FoundationEssentials/ProgressReporter/ProgressReporter+Properties.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,43 @@ extension ProgressReporter {
1515
public struct Properties: Sendable {
1616
public var totalFileCount: TotalFileCount.Type { TotalFileCount.self }
1717
public struct TotalFileCount: Sendable, Property {
18+
public static var defaultValue: Int { return 0 }
19+
1820
public typealias T = Int
1921
}
2022

2123
public var completedFileCount: CompletedFileCount.Type { CompletedFileCount.self }
2224
public struct CompletedFileCount: Sendable, Property {
25+
public static var defaultValue: Int { return 0 }
26+
2327
public typealias T = Int
2428
}
2529

2630
public var totalByteCount: TotalByteCount.Type { TotalByteCount.self }
2731
public struct TotalByteCount: Sendable, Property {
32+
public static var defaultValue: Int64 { return 0 }
33+
2834
public typealias T = Int64
2935
}
3036

3137
public var completedByteCount: CompletedByteCount.Type { CompletedByteCount.self }
3238
public struct CompletedByteCount: Sendable, Property {
39+
public static var defaultValue: Int64 { return 0 }
40+
3341
public typealias T = Int64
3442
}
3543

3644
public var throughput: Throughput.Type { Throughput.self }
3745
public struct Throughput: Sendable, Property {
46+
public static var defaultValue: Int64 { return 0 }
47+
3848
public typealias T = Int64
3949
}
4050

4151
public var estimatedTimeRemaining: EstimatedTimeRemaining.Type { EstimatedTimeRemaining.self }
4252
public struct EstimatedTimeRemaining: Sendable, Property {
53+
public static var defaultValue: Duration { return Duration.seconds(0) }
54+
4355
public typealias T = Duration
4456
}
4557
}

Sources/FoundationEssentials/ProgressReporter/ProgressReporter.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
110110

111111
associatedtype T: Sendable
112112

113+
static var defaultValue: T { get }
114+
113115
/// Aggregates an array of `T` into a single value `T`.
114116
/// - Parameter all: Array of `T` to be aggregated.
115117
/// - Returns: A new instance of `T`.
@@ -166,9 +168,13 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
166168
}
167169

168170
/// Returns a property value that a key path indicates.
169-
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressReporter.Properties, P.Type>) -> P.T? {
171+
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressReporter.Properties, P.Type>) -> P.T {
170172
get {
171-
state.otherProperties[AnyMetatypeWrapper(metatype: P.self)] as? P.T
173+
if let val = state.otherProperties[AnyMetatypeWrapper(metatype: P.self)] as? P.T {
174+
return val
175+
} else {
176+
return P.Type.defaultValue
177+
}
172178
}
173179

174180
set {

0 commit comments

Comments
 (0)