Skip to content

Commit

Permalink
Clean up README.
Browse files Browse the repository at this point in the history
Not sure why Package.resolved wasn't commited previously.
  • Loading branch information
Justin Kolb committed Jun 23, 2024
1 parent a74346f commit aac7384
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system",
"state" : {
"revision" : "f9266c85189c2751589a50ea5aec72799797e471",
"version" : "1.3.0"
}
}
],
"version" : 2
}
49 changes: 32 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extension MyType.Custom: ByteDecoder {
// Values are loaded/stored in reverse order from the type definition and using big endian
let value2 = try reader.read(Float64.BigEndian.self)
let value1 = try reader.read(Int8.self)
let _ = try reader.read(reader.readCount.offset(alignment: 4)) // Make sure next read occurs after three padding bytes
try reader.alignTo(4) // Make sure next read occurs after three padding bytes
let value0 = Int(try reader.read(UInt32.BigEndian.self))

return MyType(
Expand Down Expand Up @@ -102,9 +102,11 @@ let myType = try buffer.slice(...) { bytes in
var reader = ByteSpanReader(bytes)
return try reader.read(MyType.self)
}
```

// If the data was stored on disk using the Custom encoding instead
let myTypeCustom = try reader.read(MyType.Custom.self)
If the data was stored on disk using the Custom encoding instead
```swift
return try reader.read(MyType.Custom.self)
```

## Writing types to disk
Expand All @@ -114,16 +116,19 @@ import SystemPackage

let myType = MyType(value0: 100, value1: 8, value2: 300.56)
let buffer = ByteBuffer(count: 13)
var writer = SpanByteWriter(buffer)
try writer.write(myType)

// If we wanted to store the data using the Custom encoding instead
try writer.write(myType, as: MyType.Custom.self)

try buffer.slice(...) { bytes in
var writer = SpanByteWriter(buffer)
try writer.write(myType)
}
let file = try FileDescriptor.open(path, .writeOnly)
file.writeAll(buffer)
```

If we wanted to store the data using the Custom encoding instead
```swift
try writer.write(myType, as: MyType.Custom.self)
```

## Reading arrays of decodable types

```swift
Expand All @@ -140,8 +145,8 @@ try writer.write(arrayOfMyType, as: Element<MyType>.self)

## Custom array decoding/encoding

Note: This is not built in to the library due to decisions on how to encode the count value could differ wildly across use cases.
```swift
// Note: This is not built in to the library due to decisions on how to encode the count value could differ wildly across use cases.
@frozen struct MyArray<E> {}

extension MyArray: ByteDecoder where E: ByteDecoder {
Expand All @@ -160,8 +165,10 @@ extension MyArray: ByteEncoder where E: ByteEncoder {
}
}
}
```

// Usage (note that the count value is handled without extra work now)
Usage (note that the count value is handled without extra work now)
```swift
let arrayOfMyType = try reader.read(MyArray<MyType>.self)

try writer.write(arrayOfMyType, as: MyArray<MyType>.self)
Expand All @@ -170,27 +177,35 @@ try writer.write(arrayOfMyType, as: MyArray<MyType>.self)
## Foundation Integration
```swift
let data = Data(...) // Get data somehow
var reader = DataReader(data: data, maxReadCount: 8) // Choose a value that represents the maximum amount of bytes read in one call to `read`
var reader = DataReader(data: data, maxReadCount: 8)
let myType = try reader.read(MyType.self)
```

Choose a value for `maxReadCount` that represents the maximum amount of bytes read in one call to `read`. In the example above the longest read is a `Double` which is 8 bytes. In the worst case you can omit the `maxReadCount` parameter and then the entire length of the Data would be used as the default.

## Small Gotcha In Version 12.0.0+ API

All of the `read` methods on `ByteReader` and `write` methods on `ByteWriter` that do not involve a `ByteDecoder`/`ByteEncoder` no longer check to see if there are enough bytes to complete the operation. This is now handled separately be calling `ensure`. For example `try reader.ensure(5)` will throw if there are not enough bytes left to read.

A side effect of this change is that when you write a single `UInt8` it no longer triggers the `UInt8` implementation of `ByteEncoder`.

Previously you would do this and it would throw if there was not enough space to write:
```swift
// Previously you would do this and it would throw if there was not enough space to write
try writer.write(UInt8(7))
```

Now the new `write` method on `ByteWriter` that takes a single `UInt8` superceeds this.
To get the new behavior do this:

// Now the new `write` method on `ByteWriter` that takes a single `UInt8` superceeds this
// To get the new behavior do this:
```swift
try ensure(1)
writer.write(UInt8(7))
```

// OR
or do this which triggers the `ByteEncoder` implementation manually:

try writer.write(UInt8(7), as: UInt8.self) // This triggers the `ByteEncoder` implementation manually
```swift
try writer.write(UInt8(7), as: UInt8.self)
```

## Adding `Lilliput` as a Dependency
Expand Down

0 comments on commit aac7384

Please sign in to comment.