-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagged.swift
More file actions
177 lines (137 loc) · 5.5 KB
/
Tagged.swift
File metadata and controls
177 lines (137 loc) · 5.5 KB
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
// SOURCE: https://github.com/pointfreeco/swift-tagged
public struct Tagged<Tag, RawValue> {
public var rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
}
extension Tagged: CustomStringConvertible {
public var description: String {
return String(describing: self.rawValue)
}
}
extension Tagged: RawRepresentable {
}
// 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 {
self.init(rawValue: try decoder.singleValueContainer().decode(RawValue.self))
}
}
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: ExpressibleByNilLiteral where RawValue: ExpressibleByNilLiteral {
public init(nilLiteral: ()) {
self.init(rawValue: RawValue(nilLiteral: nilLiteral))
}
}
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)
}
}
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 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
}
}
extension Tagged: Hashable where RawValue: Hashable {
public var hashValue: Int {
return self.rawValue.hashValue
}
}
extension Tagged: SignedNumeric where RawValue: SignedNumeric {
}