-
Notifications
You must be signed in to change notification settings - Fork 654
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
Conversation
Motivation: Makes it possible to make integers out of ByteBuffers directly with an initializer. I.e. `UInt32(buffer: ByteBuffer(bytes: [0, 1, 2, 3]))`. Modifications: - Adds the `Int(buffer:)` initializer in `ByteBuffer-int.swift` - Adds tests in `ByteBufferTest.swift`. _Holy hell this file is huge._ Result: Nicer direct initializer for integers from ByteBuffers.
Hmmmm. Unlike Perhaps it's worth adding a check and making this throwing, or keeping this optional? Or documenting that the user should ensure the length of the buffer is enough? |
I checked the docs for |
Sources/NIOCore/ByteBuffer-int.swift
Outdated
/// | ||
/// - Returns: The integer value read from the buffer | ||
@inlinable | ||
public init(buffer: ByteBuffer) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. You did all the work for me.
I thought making it optional is a better choice than making it throwing — I don't think anyone need to capture the specific error, so no need to make an extra error type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I'm happy with this but @Lukasa or someone from the NIO should approve too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a lovely patch @natikgadzhi, thank you so much! ✨
Ah dang, one quick note from the docs:
Looks like you should change this to just say "this initializer will fail if the buffer size does not match the size of the integer type". |
Ooops. That should do it. Deleted the whole returns line as the first line already matches "exactly the right size" |
Motivation:
Makes it possible to make integers out of ByteBuffers directly with an initializer. I.e.
UInt32(buffer: ByteBuffer(bytes: [0, 1, 2, 3]))
. Closes #3012.Modifications:
Int(buffer:)
initializer inByteBuffer-int.swift
ByteBufferTest.swift
. Holy hell this file is huge.Int(buffer:)
will crash if the buffer does not have enough bytes in it to represent the desired integer type.Endianness.host
, to keep the public API clean and simple. It's a convenience initializer, so I thought keeping it minimal is a good idea.Result:
Nicer direct initializer for integers from ByteBuffers.