Skip to content

0.2.0 - Array Extensions

Latest

Choose a tag to compare

@coenttb coenttb released this 21 Nov 06:19
· 22 commits to main since this release

New Features

Array Serialization/Deserialization

Added support for deserializing multiple Float and Double values from byte arrays:

API Additions

[Double] Extensions

extension [Double] {
    public init?<C: Collection>(bytes: C, endianness: [UInt8].Endianness = .little)
    where C.Element == UInt8
}

[Float] Extensions

extension [Float] {
    public init?<C: Collection>(bytes: C, endianness: [UInt8].Endianness = .little)
    where C.Element == UInt8
}

Usage Examples

Deserialize multiple Doubles:

let bytes: [UInt8] = [
    0x6E, 0x86, 0x1B, 0xF0, 0xF9, 0x21, 0x09, 0x40,  // 3.14159
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F,  // 1.0
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40   // 2.0
]
let doubles = [Double](bytes: bytes)
// Optional([3.14159, 1.0, 2.0])

Deserialize multiple Floats:

let bytes: [UInt8] = [
    0xD0, 0x0F, 0x49, 0x40,  // 3.14159
    0x00, 0x00, 0x80, 0x3F,  // 1.0
    0x00, 0x00, 0x00, 0x40   // 2.0
]
let floats = [Float](bytes: bytes)
// Optional([3.14159, 1.0, 2.0])

Big-endian support:

let doubles = [Double](bytes: bytes, endianness: .big)

Features

  • Works with any Collection of UInt8
  • Returns nil if byte count is not a multiple of element size (4 for Float, 8 for Double)
  • Preserves all special values (NaN, infinity, signed zero, subnormals)
  • Supports both little-endian (default) and big-endian byte order
  • Efficient implementation with capacity pre-reservation

Testing

Comprehensive test coverage added:

  • 224 tests in 60 suites (increased from 174 in 55)
  • Parameterized tests for invalid byte counts
  • Subnormal and extreme value preservation tests
  • Mixed endianness validation tests
  • Collection types compatibility tests
  • 12 new performance benchmarks for array operations

Performance benchmarks demonstrate excellent efficiency:

  • Deserialize 1000 elements: ~700µs
  • Round-trip 10,000 random values: ~10ms

Documentation

  • Updated README with array extension examples
  • Enhanced API documentation
  • Added usage examples for common patterns

Full Changelog: 0.1.0...0.2.0