|
| 1 | +// |
| 2 | +// Snowflake+IntegerConformances.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Jaehong Kang on 8/17/24. |
| 6 | +// |
| 7 | + |
| 8 | +extension Snowflake { |
| 9 | + private init?(rawValue: RawValue?) { |
| 10 | + guard let rawValue = rawValue else { |
| 11 | + return nil |
| 12 | + } |
| 13 | + self.init(rawValue: rawValue) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +extension Snowflake: AdditiveArithmetic { |
| 18 | + public static func + (lhs: Snowflake, rhs: Snowflake) -> Snowflake { |
| 19 | + .init(rawValue: lhs.rawValue + rhs.rawValue) |
| 20 | + } |
| 21 | + |
| 22 | + public static func - (lhs: Snowflake, rhs: Snowflake) -> Snowflake { |
| 23 | + .init(rawValue: lhs.rawValue - rhs.rawValue) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +extension Snowflake: ExpressibleByIntegerLiteral { |
| 28 | + public init(integerLiteral value: RawValue) { |
| 29 | + self.init(value) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +extension Snowflake: Numeric { |
| 34 | + public typealias Magnitude = RawValue.Magnitude |
| 35 | + |
| 36 | + public static func * (lhs: Snowflake, rhs: Snowflake) -> Snowflake { |
| 37 | + self.init(rawValue: lhs.rawValue * rhs.rawValue) |
| 38 | + } |
| 39 | + |
| 40 | + public static func *= (lhs: inout Snowflake, rhs: Snowflake) { |
| 41 | + lhs.rawValue *= rhs.rawValue |
| 42 | + } |
| 43 | + |
| 44 | + public var magnitude: RawValue.Magnitude { |
| 45 | + rawValue.magnitude |
| 46 | + } |
| 47 | + |
| 48 | + public init?<T>(exactly source: T) where T : BinaryInteger { |
| 49 | + self.init(rawValue: RawValue(exactly: source)) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +extension Snowflake: Strideable { |
| 54 | + public typealias Stride = RawValue.Stride |
| 55 | + |
| 56 | + public func distance(to other: Snowflake) -> RawValue.Stride { |
| 57 | + rawValue.distance(to: other.rawValue) |
| 58 | + } |
| 59 | + |
| 60 | + public func advanced(by n: RawValue.Stride) -> Snowflake { |
| 61 | + Snowflake(rawValue: rawValue.advanced(by: n)) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +extension Snowflake: BinaryInteger { |
| 66 | + public typealias Words = RawValue.Words |
| 67 | + |
| 68 | + public static var isSigned: Bool { |
| 69 | + RawValue.isSigned |
| 70 | + } |
| 71 | + |
| 72 | + public static prefix func ~ (x: Snowflake) -> Snowflake { |
| 73 | + .init(rawValue: ~x.rawValue) |
| 74 | + } |
| 75 | + |
| 76 | + public static func <<= <RHS>(lhs: inout Snowflake, rhs: RHS) where RHS : BinaryInteger { |
| 77 | + lhs.rawValue <<= rhs |
| 78 | + } |
| 79 | + |
| 80 | + public static func >>= <RHS>(lhs: inout Snowflake, rhs: RHS) where RHS : BinaryInteger { |
| 81 | + lhs.rawValue >>= rhs |
| 82 | + } |
| 83 | + |
| 84 | + public static func / (lhs: Snowflake, rhs: Snowflake) -> Snowflake { |
| 85 | + .init(rawValue: lhs.rawValue / rhs.rawValue) |
| 86 | + } |
| 87 | + |
| 88 | + public static func % (lhs: Snowflake, rhs: Snowflake) -> Snowflake { |
| 89 | + .init(rawValue: lhs.rawValue % rhs.rawValue) |
| 90 | + } |
| 91 | + |
| 92 | + public static func %= (lhs: inout Snowflake, rhs: Snowflake) { |
| 93 | + lhs.rawValue %= rhs.rawValue |
| 94 | + } |
| 95 | + |
| 96 | + public static func &= (lhs: inout Snowflake, rhs: Snowflake) { |
| 97 | + lhs.rawValue &= rhs.rawValue |
| 98 | + } |
| 99 | + |
| 100 | + public static func |= (lhs: inout Snowflake, rhs: Snowflake) { |
| 101 | + lhs.rawValue |= rhs.rawValue |
| 102 | + } |
| 103 | + |
| 104 | + public static func ^= (lhs: inout Snowflake, rhs: Snowflake) { |
| 105 | + lhs.rawValue ^= rhs.rawValue |
| 106 | + } |
| 107 | + |
| 108 | + public static func /= (lhs: inout Snowflake, rhs: Snowflake) { |
| 109 | + lhs.rawValue /= rhs.rawValue |
| 110 | + } |
| 111 | + |
| 112 | + public var words: RawValue.Words { |
| 113 | + rawValue.words |
| 114 | + } |
| 115 | + |
| 116 | + public var bitWidth: Int { |
| 117 | + rawValue.bitWidth |
| 118 | + } |
| 119 | + |
| 120 | + public var trailingZeroBitCount: Int { |
| 121 | + rawValue.trailingZeroBitCount |
| 122 | + } |
| 123 | + |
| 124 | + public init?<T>(exactly source: T) where T : BinaryFloatingPoint { |
| 125 | + self.init(rawValue: RawValue(exactly: source)) |
| 126 | + } |
| 127 | + |
| 128 | + public init<T>(_ source: T) where T : BinaryFloatingPoint { |
| 129 | + self.init(rawValue: RawValue(source)) |
| 130 | + } |
| 131 | + |
| 132 | + public init<T>(_ source: T) where T : BinaryInteger { |
| 133 | + self.init(rawValue: RawValue(source)) |
| 134 | + } |
| 135 | + |
| 136 | + public init<T>(clamping source: T) where T : BinaryInteger { |
| 137 | + self.init(rawValue: RawValue(clamping: source)) |
| 138 | + } |
| 139 | + |
| 140 | + public init<T>(truncatingIfNeeded source: T) where T : BinaryInteger { |
| 141 | + self.init(rawValue: RawValue(truncatingIfNeeded: source)) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +extension Snowflake: FixedWidthInteger { |
| 146 | + public static var bitWidth: Int { |
| 147 | + RawValue.bitWidth |
| 148 | + } |
| 149 | + |
| 150 | + public static var max: Snowflake { |
| 151 | + Snowflake(rawValue: RawValue.max) |
| 152 | + } |
| 153 | + |
| 154 | + public static var min: Snowflake { |
| 155 | + Snowflake(rawValue: RawValue.min) |
| 156 | + } |
| 157 | + |
| 158 | + public var nonzeroBitCount: Int { |
| 159 | + rawValue.nonzeroBitCount |
| 160 | + } |
| 161 | + |
| 162 | + public var leadingZeroBitCount: Int { |
| 163 | + rawValue.leadingZeroBitCount |
| 164 | + } |
| 165 | + |
| 166 | + public var byteSwapped: Snowflake { |
| 167 | + .init(rawValue: rawValue.byteSwapped) |
| 168 | + } |
| 169 | + |
| 170 | + public init(_truncatingBits source: UInt) { |
| 171 | + self.init(rawValue: RawValue(_truncatingBits: source)) |
| 172 | + } |
| 173 | + |
| 174 | + public func addingReportingOverflow(_ rhs: Snowflake) -> (partialValue: Snowflake, overflow: Bool) { |
| 175 | + let result = rawValue.addingReportingOverflow(rhs.rawValue) |
| 176 | + |
| 177 | + return ( |
| 178 | + partialValue: .init(rawValue: result.partialValue), |
| 179 | + overflow: result.overflow |
| 180 | + ) |
| 181 | + } |
| 182 | + |
| 183 | + public func subtractingReportingOverflow(_ rhs: Snowflake) -> (partialValue: Snowflake, overflow: Bool) { |
| 184 | + let result = rawValue.subtractingReportingOverflow(rhs.rawValue) |
| 185 | + |
| 186 | + return ( |
| 187 | + partialValue: .init(rawValue: result.partialValue), |
| 188 | + overflow: result.overflow |
| 189 | + ) |
| 190 | + } |
| 191 | + |
| 192 | + public func multipliedReportingOverflow(by rhs: Snowflake) -> (partialValue: Snowflake, overflow: Bool) { |
| 193 | + let result = rawValue.multipliedReportingOverflow(by: rhs.rawValue) |
| 194 | + |
| 195 | + return ( |
| 196 | + partialValue: .init(rawValue: result.partialValue), |
| 197 | + overflow: result.overflow |
| 198 | + ) |
| 199 | + } |
| 200 | + |
| 201 | + public func dividedReportingOverflow(by rhs: Snowflake) -> (partialValue: Snowflake, overflow: Bool) { |
| 202 | + let result = rawValue.dividedReportingOverflow(by: rhs.rawValue) |
| 203 | + |
| 204 | + return ( |
| 205 | + partialValue: .init(rawValue: result.partialValue), |
| 206 | + overflow: result.overflow |
| 207 | + ) |
| 208 | + } |
| 209 | + |
| 210 | + public func remainderReportingOverflow(dividingBy rhs: Snowflake) -> (partialValue: Snowflake, overflow: Bool) { |
| 211 | + let result = rawValue.remainderReportingOverflow(dividingBy: rhs.rawValue) |
| 212 | + |
| 213 | + return ( |
| 214 | + partialValue: .init(rawValue: result.partialValue), |
| 215 | + overflow: result.overflow |
| 216 | + ) |
| 217 | + } |
| 218 | + |
| 219 | + public func dividingFullWidth(_ dividend: (high: Snowflake, low: RawValue.Magnitude)) -> (quotient: Snowflake, remainder: Snowflake) { |
| 220 | + let result = rawValue.dividingFullWidth((high: dividend.high.rawValue, low: dividend.low)) |
| 221 | + |
| 222 | + return ( |
| 223 | + quotient: .init(rawValue: result.quotient), |
| 224 | + remainder: .init(rawValue: result.remainder) |
| 225 | + ) |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +extension Snowflake: UnsignedInteger { } |
0 commit comments