Skip to content

Commit 5eba8e1

Browse files
Moved primitive unwrapping to its own function
1 parent 8749cc6 commit 5eba8e1

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Sources/DynamicCodable/DynamicCodableDecoder.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,11 @@ extension DynamicCodableDecoder.Decoder: Swift.Decoder {
145145

146146
@inline(__always)
147147
func unwrap<T: Decodable>() throws -> T {
148-
let value = representation
149-
150148
typealias Primitive = DynamicCodable
151149

152150
switch T.self {
153151
// Return DynamicCodable as is if it is being decoded
154-
case is DynamicCodable.Type: return unsafeBitCast(value, to: T.self)
152+
case is DynamicCodable.Type: return unsafeBitCast(representation, to: T.self)
155153
// Primitive Types fast-path
156154
case is Primitive.Float32.Type: return unsafeBitCast(try unwrapFloatingPoint() as Primitive.Float32, to: T.self)
157155
case is Primitive.Float64.Type: return unsafeBitCast(try unwrapFloatingPoint() as Primitive.Float64, to: T.self)
@@ -170,12 +168,17 @@ extension DynamicCodableDecoder.Decoder: Swift.Decoder {
170168
is Primitive.Nil.Type,
171169
is Primitive.Bool.Type,
172170
is Primitive.String.Type,
173-
is Primitive.Empty.Type: return try value.unwrap { throw createTypeMismatchError(type: T.self) }
171+
is Primitive.Empty.Type: return try unwrapPrimitive()
174172
// Decodable Types
175173
default: return try T(from: self)
176174
}
177175
}
178176

177+
@inline(__always)
178+
private func unwrapPrimitive<T>() throws -> T {
179+
try representation.unwrap { throw createTypeMismatchError(type: T.self) }
180+
}
181+
179182
@inline(__always)
180183
private func unwrapFloatingPoint<T: BinaryFloatingPoint>() throws -> T {
181184
@inline(__always)

0 commit comments

Comments
 (0)