Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Int(buffer:) initializer to FixedWidthInteger types #3047

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/NIOCore/ByteBuffer-int.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ extension FixedWidthInteger {

return 1 << ((Self.bitWidth - 1) - self.leadingZeroBitCount)
}

/// Initialize an integer from a ByteBuffer. The buffer must contain enough bytes to represent the integer.
/// The bytes will be read using the host system's endianness.
/// If the buffer doesn't contain enough bytes to represent the integer, the program will crash.
///
/// - Parameters:
/// - buffer: The ByteBuffer to read from
///
/// - Returns: The integer value read from the buffer
@inlinable
public init(buffer: ByteBuffer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should definitely be failable, so that we can avoid the force-unwrap. We also want to add tests.

There's a separate question about whether this should be exactly the right size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weissi strong opinions on checking that the buffer size matches the int size exactly?

If we should verify the size match, the most straightforward way would be to check buffer.readableBytes == MemoryLayout<Self.self>.size, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm definitely in favour of checking that this is exactly the right size. It's weird to left garbage around, if that's expected readInteger is the right tool. Endianness should also be configurable (but defaulted to .host) Something like

var buffer = buffer
guard let value = buffer.readInteger(endianness: endianness, as: Self.self), buffer.readableBytes == 0 else {
    return nil
}
return value

var buffer = buffer
self = buffer.readInteger(endianness: .host, as: Self.self)!
}
}

extension UInt32 {
Expand Down
30 changes: 30 additions & 0 deletions Tests/NIOCoreTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3499,6 +3499,36 @@ extension ByteBufferTest {

}

// MARK: - Int / FixedWidthInteger init
extension ByteBufferTest {
func testCreateIntegersFromByteBuffer() {
// 8-bit
let uint8Buffer = ByteBuffer(bytes: [42])
XCTAssertEqual(UInt8(buffer: uint8Buffer), 42)
XCTAssertEqual(Int8(buffer: uint8Buffer), 42)

// 16-bit
let uint16Bytes: [UInt8] = Endianness.host == .little ? [0x02, 0x01] : [0x01, 0x02]
let uint16Buffer = ByteBuffer(bytes: uint16Bytes)
XCTAssertEqual(UInt16(buffer: uint16Buffer), 0x0102)
XCTAssertEqual(Int16(buffer: uint16Buffer), 0x0102)

// 32-bit
let uint32Bytes: [UInt8] = Endianness.host == .little ? [0x04, 0x03, 0x02, 0x01] : [0x01, 0x02, 0x03, 0x04]
let uint32Buffer = ByteBuffer(bytes: uint32Bytes)
XCTAssertEqual(UInt32(buffer: uint32Buffer), 0x01020304)
XCTAssertEqual(Int32(buffer: uint32Buffer), 0x01020304)

// 64-bit
let uint64Bytes: [UInt8] = Endianness.host == .little ?
[0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01] :
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
let uint64Buffer = ByteBuffer(bytes: uint64Bytes)
XCTAssertEqual(UInt64(buffer: uint64Buffer), 0x0102030405060708)
XCTAssertEqual(Int64(buffer: uint64Buffer), 0x0102030405060708)
}
}

// MARK: - DispatchData init
extension ByteBufferTest {

Expand Down