Skip to content

Commit 4429496

Browse files
author
Justin Kolb
committed
Fix #6
1 parent 9019449 commit 4429496

File tree

5 files changed

+40
-30
lines changed

5 files changed

+40
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ try writer.write(arrayOfMyType, as: MyArray<MyType>.self)
170170
To use the `Lilliput` library in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file:
171171

172172
```swift
173-
.package(url: "https://github.com/jkolb/Lilliput.git", from: "10.0.0"),
173+
.package(url: "https://github.com/jkolb/Lilliput.git", from: "11.0.0"),
174174
```
175175

176176
Finally, include `"Lilliput"` as a dependency for your target:
@@ -179,7 +179,7 @@ Finally, include `"Lilliput"` as a dependency for your target:
179179
let package = Package(
180180
// name, platforms, products, etc.
181181
dependencies: [
182-
.package(url: "https://github.com/jkolb/Lilliput.git", from: "10.0.0"),
182+
.package(url: "https://github.com/jkolb/Lilliput.git", from: "11.0.0"),
183183
// other dependencies
184184
],
185185
targets: [

Sources/Lilliput/ByteBuffer.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,36 @@ public final class ByteBuffer {
1313
bytes.deallocate()
1414
}
1515

16-
@inlinable public subscript(bounds: Range<Int>) -> UnsafeMutableRawBufferPointer {
17-
return UnsafeMutableRawBufferPointer(rebasing: bytes[bounds])
16+
@inlinable public func slice<T>(_ bounds: Range<Int>, body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
17+
return try body(UnsafeMutableRawBufferPointer(rebasing: bytes[bounds]))
1818
}
1919

20-
@inlinable public subscript<R>(r: R) -> UnsafeMutableRawBufferPointer where R : RangeExpression, Int == R.Bound {
21-
return self[r.relative(to: bytes.indices)]
20+
@inlinable public func slice<R: RangeExpression, T>(_ r: R, body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T where Int == R.Bound {
21+
return try slice(r.relative(to: bytes.indices), body: body)
2222
}
2323

24-
@inlinable public subscript(x: (UnboundedRange_) -> ()) -> UnsafeMutableRawBufferPointer {
25-
return bytes
24+
@inlinable public func slice<T>(_ x: (UnboundedRange_) -> (), body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
25+
return try body(bytes)
2626
}
2727

28-
@inlinable public subscript(start: Int , count countValue: Int) -> UnsafeMutableRawBufferPointer {
29-
return UnsafeMutableRawBufferPointer(rebasing: UnsafeMutableRawBufferPointer(rebasing: bytes[start...])[..<countValue])
28+
@inlinable public func slice<T>(start: Int , count: Int, body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
29+
return try body(UnsafeMutableRawBufferPointer(rebasing: UnsafeMutableRawBufferPointer(rebasing: bytes[start...])[..<count]))
3030
}
3131
}
3232

3333
extension ByteBuffer : CollectionByteDecoder {
3434
@inlinable public static func decode<R: ByteReader>(from reader: inout R, count: Int) throws -> ByteBuffer {
3535
let buffer = ByteBuffer(count: count)
36-
buffer[...].copyMemory(from: try reader.read(count).buffer)
36+
try buffer.slice(...) { $0.copyMemory(from: try reader.read(count).buffer) }
3737
return buffer
3838
}
3939
}
4040

4141
extension ByteBuffer : ByteEncoder {
42-
@inlinable public static func encode<W>(_ value: ByteBuffer, to writer: inout W) throws where W : ByteWriter {
43-
let span = ByteSpan(value[...])
44-
try writer.write(span)
42+
@inlinable public static func encode<W>(_ buffer: ByteBuffer, to writer: inout W) throws where W : ByteWriter {
43+
try buffer.slice(...) {
44+
let span = ByteSpan($0)
45+
try writer.write(span)
46+
}
4547
}
4648
}

Sources/Lilliput/ByteReader.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public extension ByteReader {
2222
return span.count
2323
}
2424

25-
public init(_ byteBuffer: ByteBuffer) {
26-
self.init(byteBuffer[...])
27-
}
28-
2925
public init(_ buffer: UnsafeMutableRawBufferPointer) {
3026
self.init(span: ByteSpan(buffer))
3127
}

Sources/Lilliput/ByteWriter.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public extension ByteWriter {
4444
return span.count
4545
}
4646

47-
public init(_ byteBuffer: ByteBuffer) {
48-
self.init(byteBuffer[...])
49-
}
50-
5147
public init(_ buffer: UnsafeMutableRawBufferPointer) {
5248
self.init(span: MutableByteSpan(buffer))
5349
}

Sources/Lilliput/FileDescriptor+ByteBuffer.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,50 @@ import SystemPackage
22

33
public extension FileDescriptor {
44
@inlinable func read(into buffer: ByteBuffer, retryOnInterrupt: Bool = true) throws -> Int {
5-
return try read(into: buffer[...], retryOnInterrupt: retryOnInterrupt)
5+
return try buffer.slice(...) { bytes in
6+
return try read(into: bytes, retryOnInterrupt: retryOnInterrupt)
7+
}
68
}
79

810
@inlinable func readAll(into buffer: ByteBuffer, retryOnInterrupt: Bool = true) throws {
9-
return try readAll(into: buffer[...], retryOnInterrupt: retryOnInterrupt)
11+
return try buffer.slice(...) { bytes in
12+
return try readAll(into: bytes, retryOnInterrupt: retryOnInterrupt)
13+
}
1014
}
1115

1216
@inlinable func read(fromAbsoluteOffset offset: Int64, into buffer: ByteBuffer, retryOnInterrupt: Bool = true) throws -> Int {
13-
return try read(fromAbsoluteOffset: offset, into: buffer[...], retryOnInterrupt: retryOnInterrupt)
17+
return try buffer.slice(...) { bytes in
18+
return try read(fromAbsoluteOffset: offset, into: bytes, retryOnInterrupt: retryOnInterrupt)
19+
}
1420
}
1521

1622
@inlinable func readAll(fromAbsoluteOffset offset: Int64, into buffer: ByteBuffer, retryOnInterrupt: Bool = true) throws {
17-
return try readAll(fromAbsoluteOffset: offset, into: buffer[...], retryOnInterrupt: retryOnInterrupt)
23+
return try buffer.slice(...) { bytes in
24+
return try readAll(fromAbsoluteOffset: offset, into: bytes, retryOnInterrupt: retryOnInterrupt)
25+
}
1826
}
1927

2028
@inlinable func write(_ buffer: ByteBuffer, retryOnInterrupt: Bool = true) throws -> Int {
21-
return try write(UnsafeRawBufferPointer(buffer[...]), retryOnInterrupt: retryOnInterrupt)
29+
return try buffer.slice(...) { bytes in
30+
return try write(UnsafeRawBufferPointer(bytes), retryOnInterrupt: retryOnInterrupt)
31+
}
2232
}
2333

2434
@inlinable func write(toAbsoluteOffset offset: Int64, _ buffer: ByteBuffer, retryOnInterrupt: Bool = true) throws -> Int {
25-
return try write(toAbsoluteOffset: offset, UnsafeRawBufferPointer(buffer[...]), retryOnInterrupt: retryOnInterrupt)
35+
return try buffer.slice(...) { bytes in
36+
return try write(toAbsoluteOffset: offset, UnsafeRawBufferPointer(bytes), retryOnInterrupt: retryOnInterrupt)
37+
}
2638
}
2739

2840
@inlinable func writeAll(_ buffer: ByteBuffer) throws {
29-
try writeAll(buffer[...])
41+
return try buffer.slice(...) { bytes in
42+
try writeAll(bytes)
43+
}
3044
}
3145

3246
@inlinable func writeAll(toAbsoluteOffset offset: Int64, _ buffer: ByteBuffer) throws {
33-
try writeAll(toAbsoluteOffset: offset, buffer[...])
47+
return try buffer.slice(...) { bytes in
48+
try writeAll(toAbsoluteOffset: offset, bytes)
49+
}
3450
}
3551
}

0 commit comments

Comments
 (0)