From 5eba8e1d562a5176f90e6197e52e5ef01e548b14 Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Thu, 3 Jun 2021 20:57:42 -0700 Subject: [PATCH] Moved primitive unwrapping to its own function --- Sources/DynamicCodable/DynamicCodableDecoder.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/DynamicCodable/DynamicCodableDecoder.swift b/Sources/DynamicCodable/DynamicCodableDecoder.swift index 3d7d393..714d50e 100644 --- a/Sources/DynamicCodable/DynamicCodableDecoder.swift +++ b/Sources/DynamicCodable/DynamicCodableDecoder.swift @@ -145,13 +145,11 @@ extension DynamicCodableDecoder.Decoder: Swift.Decoder { @inline(__always) func unwrap() 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) @@ -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() throws -> T { + try representation.unwrap { throw createTypeMismatchError(type: T.self) } + } + @inline(__always) private func unwrapFloatingPoint() throws -> T { @inline(__always)