Skip to content

A Swift library that uses result builders to allow you to construct Data objects from various Swift data types.

License

Notifications You must be signed in to change notification settings

mierau/swift-databuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

swift-databuilder

A simple Swift library that uses Result Builders to allow you to construct Data objects from various Swift data types cleanly.

Here is an example of using DataBuilder to build Data from a 4 byte (UInt32) value defined elsewhere, a UTF-8 string, a macOS Roman encoded string, a conditional based on an outside value that inserts a single byte (UInt8), an array of bytes (UInt8), and an existing Data object (16 zero filled bytes).

let value: UInt32 = 21
let emptyData: Data = Data(repeating: 0, count: 16)
let byteArray: [UInt8] = [0x48, 0x65, 0x6C, 0x6C, 0x6F]

let data = Data {
  value
  "hello world"
  ("hello", .macOSRoman)
  if value == 21 {
    UInt8(255)
  }
  byteArray
  emptyData
}

You can also change the endianness of the written bytes in two ways.

Set a default for all data:

let data = Data(endian: .big) { ... }

Or individually for each value:

let Data = Data {
   (UInt32(100), .big)
}

About

A Swift library that uses result builders to allow you to construct Data objects from various Swift data types.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages