Allow to create ByteBufferView of all bytes in a ByteBuffer (#485)

Motivation:

We used an incorrect precondition and so it would be hit if someone tried to create a ByteBufferView add all bytes in a ByteBuffer.

Modifications:

Allow upperBound up to capacity

Result:

Fixes https://github.com/apple/swift-nio/issues/482.
This commit is contained in:
Norman Maurer authored and Cory Benfield committed 2018-06-12 08:12:27 -07:00
1 parent 8fd397b3d4
commit a67e85677a
3 files changed
+10 -1

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@ public struct ByteBufferView: ContiguousCollection, RandomAccessCollection {
private let range: Range<Index>
internal init(buffer: ByteBuffer, range: Range<Index>) {
precondition(range.lowerBound >= 0 && range.upperBound < buffer.capacity)
precondition(range.lowerBound >= 0 && range.upperBound <= buffer.capacity)
self.buffer = buffer
self.range = range
}
@@ -123,6 +123,7 @@ extension ByteBufferTest {
("testBytesView", testBytesView),
("testViewsStartIndexIsStable", testViewsStartIndexIsStable),
("testSlicesOfByteBufferViewsAreByteBufferViews", testSlicesOfByteBufferViewsAreByteBufferViews),
("testReadableBufferViewRangeEqualCapacity", testReadableBufferViewRangeEqualCapacity),
]
}
}
+8
View File
@@ -1437,6 +1437,14 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual("l", String(decoding: viewSlice.dropFirst(), as: UTF8.self))
XCTAssertEqual("", String(decoding: viewSlice.dropFirst().dropLast(), as: UTF8.self))
}
func testReadableBufferViewRangeEqualCapacity() throws {
self.buf.clear()
self.buf.moveWriterIndex(forwardBy: buf.capacity)
let view = self.buf.readableBytesView
let viewSlice: ByteBufferView = view[view.startIndex ..< view.endIndex]
XCTAssertEqual(buf.readableBytes, viewSlice.count)
}
}
private enum AllocationExpectationState: Int {