Skip to content

Commit

Permalink
Deprecate and replace ByteBuffer.set(buffer:at:) (#1078)
Browse files Browse the repository at this point in the history
Motivation:

In NIO2 we tried to adopt the Swift convention of avoiding functions
that differ only by the word of their first label where that label is
describing their type. Sadly, we missed one.

Modifications:

- Wrote setBuffer(_:at:)
- Deprecated set(buffer:at:) in favour of the new function.

Result:

Users will get some deprecation warnings I guess.
  • Loading branch information
Lukasa authored and weissi committed Jul 19, 2019
1 parent 22822a9 commit da23f75
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Sources/NIO/ByteBuffer-aux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,19 @@ extension ByteBuffer {
/// - index: The index for the first byte.
/// - returns: The number of bytes written.
@discardableResult
@available(*, deprecated, renamed: "setBuffer(_:at:)")
public mutating func set(buffer: ByteBuffer, at index: Int) -> Int {
return self.setBuffer(buffer, at: index)
}

/// Copy `buffer`'s readable bytes into this `ByteBuffer` starting at `index`. Does not move any of the reader or writer indices.
///
/// - parameters:
/// - buffer: The `ByteBuffer` to copy.
/// - index: The index for the first byte.
/// - returns: The number of bytes written.
@discardableResult
public mutating func setBuffer(_ buffer: ByteBuffer, at index: Int) -> Int {
return buffer.withUnsafeReadableBytes{ p in
self.setBytes(p, at: index)
}
Expand All @@ -298,7 +310,7 @@ extension ByteBuffer {
/// - returns: The number of bytes written to this `ByteBuffer` which is equal to the number of bytes read from `buffer`.
@discardableResult
public mutating func writeBuffer(_ buffer: inout ByteBuffer) -> Int {
let written = set(buffer: buffer, at: writerIndex)
let written = self.setBuffer(buffer, at: writerIndex)
self._moveWriterIndex(forwardBy: written)
buffer._moveReaderIndex(forwardBy: written)
return written
Expand Down
2 changes: 1 addition & 1 deletion Tests/NIOTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ class ByteBufferTest: XCTestCase {
var src = self.allocator.buffer(capacity: 4)
src.writeBytes(Data([0, 1, 2, 3]))

self.buf.set(buffer: src, at: 1)
self.buf.setBuffer(src, at: 1)

/* Should bit increase the writerIndex of the src buffer */
XCTAssertEqual(4, src.readableBytes)
Expand Down

0 comments on commit da23f75

Please sign in to comment.