Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Use-case
I need to construct buffers which are aligned to 512-byte boundaries, in order to read data from disk using
O_DIRECT
on Linux (see issue #600 for a user who has the same use-case as me).Once the operating system has written into these buffers, I'd love to wrap the buffer in
Bytes
so I can:object_store
's API which returnsBytes
.Wrapping an aligned buffer is possible today using
Bytes
as it current stands (although I'm not entirely certain if this will be safelydrop
'd in all situations, becauseBytes::drop
doesn't know the alignment??):However, that requires two unnecessary steps:
slice::from_raw_parts_mut
andBox::from_raw
.This PR adds a very simple
Bytes::from_raw_parts
method (which is mostly copied fromimpl From<Box[u8]> for Bytes
).With this new PR, wrapping an aligned buffer in
Bytes
is a little more ergonomic for users, and a little more computationally efficient:(This is my first PR to a Rust open-source project, so please LMK if I've done anything wrong!)
TODO:
static Vtable
whereVtable.drop
knows the alignment of the data to drop. Is that correct? Is that even possible, given thatstruct Vtable.drop
can't accept a closure?! Maybe the solution is to create a macro to insert the alignment into the drop function (although that will only work when we know the alignment at compile time... although, for aligned buffers forO_DIRECT
, we could create a handful of alignments (e.g. 512-bytes and 4096-bytes) at compile-time, and select the correct one at runtime.)impl From<Box[u8]> for Bytes
by callingBytes::from_raw_parts
.Related