Skip to content

Commit

Permalink
Moved primitive unwrapping to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitribouniol committed Jun 4, 2021
1 parent 8749cc6 commit 5eba8e1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Sources/DynamicCodable/DynamicCodableDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,11 @@ extension DynamicCodableDecoder.Decoder: Swift.Decoder {

@inline(__always)
func unwrap<T: Decodable>() throws -> T {
let value = representation

typealias Primitive = DynamicCodable

switch T.self {
// Return DynamicCodable as is if it is being decoded
case is DynamicCodable.Type: return unsafeBitCast(value, to: T.self)
case is DynamicCodable.Type: return unsafeBitCast(representation, to: T.self)
// Primitive Types fast-path
case is Primitive.Float32.Type: return unsafeBitCast(try unwrapFloatingPoint() as Primitive.Float32, to: T.self)
case is Primitive.Float64.Type: return unsafeBitCast(try unwrapFloatingPoint() as Primitive.Float64, to: T.self)
Expand All @@ -170,12 +168,17 @@ extension DynamicCodableDecoder.Decoder: Swift.Decoder {
is Primitive.Nil.Type,
is Primitive.Bool.Type,
is Primitive.String.Type,
is Primitive.Empty.Type: return try value.unwrap { throw createTypeMismatchError(type: T.self) }
is Primitive.Empty.Type: return try unwrapPrimitive()
// Decodable Types
default: return try T(from: self)
}
}

@inline(__always)
private func unwrapPrimitive<T>() throws -> T {
try representation.unwrap { throw createTypeMismatchError(type: T.self) }
}

@inline(__always)
private func unwrapFloatingPoint<T: BinaryFloatingPoint>() throws -> T {
@inline(__always)
Expand Down

0 comments on commit 5eba8e1

Please sign in to comment.