Skip to content

Commit ce9e30a

Browse files
committed
Replace transformation methods with init pattern
Use idiomatic Swift inits for type transformations instead of methods. Transformations should use `init`, not methods like `.asDouble()`. Changes: - Remove [UInt8].IEEE754.asDouble(endianness:) method - Remove [UInt8].IEEE754.asFloat(endianness:) method - Add Double.init(_ ieee754: [UInt8].IEEE754, endianness:) - Add Float.init(_ ieee754: [UInt8].IEEE754, endianness:) - Make IEEE754 namespace struct inits internal (not public) - Update all tests to use init pattern API now uses idiomatic Swift transformation pattern: let value = Double(bytes.ieee754) // Namespace wrapper let value = Double(bytes: data) // Direct canonical let value = Float(bytes.ieee754, endianness: .big) Tests: All 12 tests passing
1 parent 0d11386 commit ce9e30a

File tree

4 files changed

+35
-49
lines changed

4 files changed

+35
-49
lines changed

Sources/IEEE_754/Double+IEEE_754.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension Double {
1919
public struct IEEE754 {
2020
public let double: Double
2121

22-
public init(double: Double) {
22+
init(double: Double) {
2323
self.double = double
2424
}
2525
}

Sources/IEEE_754/Float+IEEE_754.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension Float {
1919
public struct IEEE754 {
2020
public let float: Float
2121

22-
public init(float: Float) {
22+
init(float: Float) {
2323
self.float = float
2424
}
2525
}
@@ -52,6 +52,28 @@ extension Float {
5252
}
5353
self = value
5454
}
55+
56+
/// Creates Float from namespace-wrapped IEEE 754 binary32 bytes
57+
///
58+
/// Convenience init for transforming namespace-wrapped bytes to Float.
59+
/// Delegates to `IEEE_754.Binary32.value(from:endianness:)`.
60+
///
61+
/// - Parameters:
62+
/// - ieee754: Namespace-wrapped byte array
63+
/// - endianness: Byte order of input bytes
64+
/// - Returns: nil if bytes.count ≠ 4
65+
///
66+
/// Example:
67+
/// ```swift
68+
/// let value = Float(bytes.ieee754)
69+
/// let value = Float(bytes.ieee754, endianness: .big)
70+
/// ```
71+
public init?(_ ieee754: [UInt8].IEEE754, endianness: [UInt8].Endianness = .little) {
72+
guard let value = IEEE_754.Binary32.value(from: ieee754.bytes, endianness: endianness) else {
73+
return nil
74+
}
75+
self = value
76+
}
5577
}
5678

5779
// MARK: - Type-level Methods

Sources/IEEE_754/[UInt8]+IEEE_754.swift

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension [UInt8] {
1919
public struct IEEE754 {
2020
public let bytes: [UInt8]
2121

22-
public init(bytes: [UInt8]) {
22+
init(bytes: [UInt8]) {
2323
self.bytes = bytes
2424
}
2525
}
@@ -71,42 +71,6 @@ extension [UInt8].IEEE754 {
7171
}
7272
}
7373

74-
// MARK: - Instance Methods (Deserialization)
75-
76-
extension [UInt8].IEEE754 {
77-
/// Interprets bytes as IEEE 754 binary64 and returns Double
78-
///
79-
/// Convenience that delegates to `IEEE_754.Binary64.value(from:endianness:)`.
80-
///
81-
/// - Parameter endianness: Byte order of input bytes
82-
/// - Returns: Double value, or nil if bytes.count ≠ 8
83-
///
84-
/// Example:
85-
/// ```swift
86-
/// let value = bytes.ieee754.asDouble()
87-
/// let value = bytes.ieee754.asDouble(endianness: .big)
88-
/// ```
89-
public func asDouble(endianness: [UInt8].Endianness = .little) -> Double? {
90-
IEEE_754.Binary64.value(from: bytes, endianness: endianness)
91-
}
92-
93-
/// Interprets bytes as IEEE 754 binary32 and returns Float
94-
///
95-
/// Convenience that delegates to `IEEE_754.Binary32.value(from:endianness:)`.
96-
///
97-
/// - Parameter endianness: Byte order of input bytes
98-
/// - Returns: Float value, or nil if bytes.count ≠ 4
99-
///
100-
/// Example:
101-
/// ```swift
102-
/// let value = bytes.ieee754.asFloat()
103-
/// let value = bytes.ieee754.asFloat(endianness: .big)
104-
/// ```
105-
public func asFloat(endianness: [UInt8].Endianness = .little) -> Float? {
106-
IEEE_754.Binary32.value(from: bytes, endianness: endianness)
107-
}
108-
}
109-
11074
// MARK: - Canonical [UInt8] Initializers
11175

11276
extension [UInt8] {

Tests/IEEE_754_Tests/IEEE_754_Tests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Binary64Tests {
1212
func doubleRoundTrip() {
1313
let original: Double = 3.14159265358979323846
1414
let bytes = original.ieee754.bytes()
15-
let restored = bytes.ieee754.asDouble()
15+
let restored = Double(bytes.ieee754)
1616

1717
#expect(restored == original)
1818
}
@@ -30,7 +30,7 @@ struct Binary64Tests {
3030
func doubleBigEndian() {
3131
let value: Double = 3.14159
3232
let bytes = value.ieee754.bytes(endianness: .big)
33-
let restored = bytes.ieee754.asDouble(endianness: .big)
33+
let restored = Double(bytes.ieee754, endianness: .big)
3434

3535
#expect(restored == value)
3636
}
@@ -48,15 +48,15 @@ struct Binary64Tests {
4848
func doubleSpecialValues() {
4949
// Zero
5050
let zero = (0.0 as Double).ieee754.bytes()
51-
#expect(zero.ieee754.asDouble() == 0.0)
51+
#expect(Double(bytes: zero) == 0.0)
5252

5353
// Infinity
5454
let inf = Double.infinity.ieee754.bytes()
55-
#expect(inf.ieee754.asDouble() == Double.infinity)
55+
#expect(Double(bytes: inf) == Double.infinity)
5656

5757
// NaN
5858
let nan = Double.nan.ieee754.bytes()
59-
#expect(nan.ieee754.asDouble()?.isNaN == true)
59+
#expect(Double(bytes: nan)?.isNaN == true)
6060
}
6161
}
6262

@@ -66,7 +66,7 @@ struct Binary32Tests {
6666
func floatRoundTrip() {
6767
let original: Float = 3.14159
6868
let bytes = original.ieee754.bytes()
69-
let restored = bytes.ieee754.asFloat()
69+
let restored = Float(bytes.ieee754)
7070

7171
#expect(restored == original)
7272
}
@@ -84,7 +84,7 @@ struct Binary32Tests {
8484
func floatBigEndian() {
8585
let value: Float = 3.14
8686
let bytes = value.ieee754.bytes(endianness: .big)
87-
let restored = bytes.ieee754.asFloat(endianness: .big)
87+
let restored = Float(bytes.ieee754, endianness: .big)
8888

8989
#expect(restored == value)
9090
}
@@ -102,15 +102,15 @@ struct Binary32Tests {
102102
func floatSpecialValues() {
103103
// Zero
104104
let zero = Float(0.0).ieee754.bytes()
105-
#expect(zero.ieee754.asFloat() == 0.0)
105+
#expect(Float(bytes: zero) == 0.0)
106106

107107
// Infinity
108108
let inf = Float.infinity.ieee754.bytes()
109-
#expect(inf.ieee754.asFloat() == Float.infinity)
109+
#expect(Float(bytes: inf) == Float.infinity)
110110

111111
// NaN
112112
let nan = Float.nan.ieee754.bytes()
113-
#expect(nan.ieee754.asFloat()?.isNaN == true)
113+
#expect(Float(bytes: nan)?.isNaN == true)
114114
}
115115
}
116116

0 commit comments

Comments
 (0)