-
Notifications
You must be signed in to change notification settings - Fork 65
/
Tagged.swift
241 lines (197 loc) · 7.12 KB
/
Tagged.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
public struct Tagged<Tag, RawValue> {
public var rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
public func map<B>(_ f: (RawValue) -> B) -> Tagged<Tag, B> {
return .init(rawValue: f(self.rawValue))
}
}
extension Tagged: CustomStringConvertible {
public var description: String {
return String(describing: self.rawValue)
}
}
extension Tagged: RawRepresentable {
}
extension Tagged: CustomPlaygroundDisplayConvertible {
public var playgroundDescription: Any {
return self.rawValue
}
}
// MARK: - Conditional Conformances
extension Tagged: Comparable where RawValue: Comparable {
public static func < (lhs: Tagged, rhs: Tagged) -> Bool {
return lhs.rawValue < rhs.rawValue
}
}
extension Tagged: Decodable where RawValue: Decodable {
public init(from decoder: Decoder) throws {
do {
self.init(rawValue: try decoder.singleValueContainer().decode(RawValue.self))
} catch {
self.init(rawValue: try .init(from: decoder))
}
}
}
extension Tagged: Encodable where RawValue: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.rawValue)
}
}
extension Tagged: Equatable where RawValue: Equatable {
public static func == (lhs: Tagged, rhs: Tagged) -> Bool {
return lhs.rawValue == rhs.rawValue
}
}
extension Tagged: Error where RawValue: Error {
}
extension Tagged: ExpressibleByBooleanLiteral where RawValue: ExpressibleByBooleanLiteral {
public typealias BooleanLiteralType = RawValue.BooleanLiteralType
public init(booleanLiteral value: RawValue.BooleanLiteralType) {
self.init(rawValue: RawValue(booleanLiteral: value))
}
}
extension Tagged: ExpressibleByExtendedGraphemeClusterLiteral where RawValue: ExpressibleByExtendedGraphemeClusterLiteral {
public typealias ExtendedGraphemeClusterLiteralType = RawValue.ExtendedGraphemeClusterLiteralType
public init(extendedGraphemeClusterLiteral: ExtendedGraphemeClusterLiteralType) {
self.init(rawValue: RawValue(extendedGraphemeClusterLiteral: extendedGraphemeClusterLiteral))
}
}
extension Tagged: ExpressibleByFloatLiteral where RawValue: ExpressibleByFloatLiteral {
public typealias FloatLiteralType = RawValue.FloatLiteralType
public init(floatLiteral: FloatLiteralType) {
self.init(rawValue: RawValue(floatLiteral: floatLiteral))
}
}
extension Tagged: ExpressibleByIntegerLiteral where RawValue: ExpressibleByIntegerLiteral {
public typealias IntegerLiteralType = RawValue.IntegerLiteralType
public init(integerLiteral: IntegerLiteralType) {
self.init(rawValue: RawValue(integerLiteral: integerLiteral))
}
}
extension Tagged: ExpressibleByStringLiteral where RawValue: ExpressibleByStringLiteral {
public typealias StringLiteralType = RawValue.StringLiteralType
public init(stringLiteral: StringLiteralType) {
self.init(rawValue: RawValue(stringLiteral: stringLiteral))
}
}
extension Tagged: ExpressibleByUnicodeScalarLiteral where RawValue: ExpressibleByUnicodeScalarLiteral {
public typealias UnicodeScalarLiteralType = RawValue.UnicodeScalarLiteralType
public init(unicodeScalarLiteral: UnicodeScalarLiteralType) {
self.init(rawValue: RawValue(unicodeScalarLiteral: unicodeScalarLiteral))
}
}
extension Tagged: LosslessStringConvertible where RawValue: LosslessStringConvertible {
public init?(_ description: String) {
guard let rawValue = RawValue(description) else { return nil }
self.init(rawValue: rawValue)
}
}
#if compiler(>=5)
extension Tagged: AdditiveArithmetic where RawValue: AdditiveArithmetic {
public static var zero: Tagged {
return self.init(rawValue: .zero)
}
public static func + (lhs: Tagged, rhs: Tagged) -> Tagged {
return self.init(rawValue: lhs.rawValue + rhs.rawValue)
}
public static func += (lhs: inout Tagged, rhs: Tagged) {
lhs.rawValue += rhs.rawValue
}
public static func - (lhs: Tagged, rhs: Tagged) -> Tagged {
return self.init(rawValue: lhs.rawValue - rhs.rawValue)
}
public static func -= (lhs: inout Tagged, rhs: Tagged) {
lhs.rawValue -= rhs.rawValue
}
}
extension Tagged: Numeric where RawValue: Numeric {
public init?<T>(exactly source: T) where T: BinaryInteger {
guard let rawValue = RawValue(exactly: source) else { return nil }
self.init(rawValue: rawValue)
}
public var magnitude: RawValue.Magnitude {
return self.rawValue.magnitude
}
public static func * (lhs: Tagged, rhs: Tagged) -> Tagged {
return self.init(rawValue: lhs.rawValue * rhs.rawValue)
}
public static func *= (lhs: inout Tagged, rhs: Tagged) {
lhs.rawValue *= rhs.rawValue
}
}
#else
extension Tagged: Numeric where RawValue: Numeric {
public typealias Magnitude = RawValue.Magnitude
public init?<T>(exactly source: T) where T: BinaryInteger {
guard let rawValue = RawValue(exactly: source) else { return nil }
self.init(rawValue: rawValue)
}
public var magnitude: RawValue.Magnitude {
return self.rawValue.magnitude
}
public static func + (lhs: Tagged<Tag, RawValue>, rhs: Tagged<Tag, RawValue>) -> Tagged<Tag, RawValue> {
return self.init(rawValue: lhs.rawValue + rhs.rawValue)
}
public static func += (lhs: inout Tagged<Tag, RawValue>, rhs: Tagged<Tag, RawValue>) {
lhs.rawValue += rhs.rawValue
}
public static func * (lhs: Tagged, rhs: Tagged) -> Tagged {
return self.init(rawValue: lhs.rawValue * rhs.rawValue)
}
public static func *= (lhs: inout Tagged, rhs: Tagged) {
lhs.rawValue *= rhs.rawValue
}
public static func - (lhs: Tagged, rhs: Tagged) -> Tagged<Tag, RawValue> {
return self.init(rawValue: lhs.rawValue - rhs.rawValue)
}
public static func -= (lhs: inout Tagged<Tag, RawValue>, rhs: Tagged<Tag, RawValue>) {
lhs.rawValue -= rhs.rawValue
}
}
#endif
extension Tagged: Hashable where RawValue: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(self.rawValue)
}
}
extension Tagged: SignedNumeric where RawValue: SignedNumeric {
}
// Commenting these out for Joe.
//
// https://twitter.com/jckarter/status/985375396601282560
//
//extension Tagged: ExpressibleByArrayLiteral where RawValue: ExpressibleByArrayLiteral {
// public typealias ArrayLiteralElement = RawValue.ArrayLiteralElement
//
// public init(arrayLiteral elements: ArrayLiteralElement...) {
// let f = unsafeBitCast(
// RawValue.init(arrayLiteral:) as (ArrayLiteralElement...) -> RawValue,
// to: (([ArrayLiteralElement]) -> RawValue).self
// )
//
// self.init(rawValue: f(elements))
// }
//}
//
//extension Tagged: ExpressibleByDictionaryLiteral where RawValue: ExpressibleByDictionaryLiteral {
// public typealias Key = RawValue.Key
// public typealias Value = RawValue.Value
//
// public init(dictionaryLiteral elements: (Key, Value)...) {
// let f = unsafeBitCast(
// RawValue.init(dictionaryLiteral:) as ((Key, Value)...) -> RawValue,
// to: (([(Key, Value)]) -> RawValue).self
// )
//
// self.init(rawValue: f(elements))
// }
//}
// MARK: - Coerce
extension Tagged {
public func coerced<Tag2>(to type: Tag2.Type) -> Tagged<Tag2, RawValue> {
return unsafeBitCast(self, to: Tagged<Tag2, RawValue>.self)
}
}