-
Notifications
You must be signed in to change notification settings - Fork 155
BoundedBuf trait to take .slice API out of IoBuf
#172
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f71326d
chore: remove slice_mut.rs
mzabaluev 5dd615d
BoundedBuf trait to take .slice API out of IoBuf
mzabaluev 4474860
Add tests for .slice on Slice
mzabaluev a149855
Fix the doc on IoBufMut::set_init
mzabaluev b1fd9c9
buf: safety doc for BoundedBufMut::set_init
mzabaluev 8c94e71
buf: add method BoundedBuf::get_buf
mzabaluev eaac5e6
Merge branch 'master' into buf-slice-reform
mzabaluev 3e7c4a0
buf: remove Sized supertrait for BoundedBuf
mzabaluev 955a7fd
Add method BoundedBuf::slice_full
mzabaluev 53b81e7
Merge branch 'master' into buf-slice-reform
Noah-Kennedy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| use super::{IoBuf, IoBufMut, Slice}; | ||
|
|
||
| use std::ops; | ||
|
|
||
| /// A possibly bounded view into an owned [`IoBuf`] buffer. | ||
| /// | ||
| /// Because buffers are passed by ownership to the runtime, Rust's slice API | ||
| /// (`&buf[..]`) cannot be used. Instead, `tokio-uring` provides an owned slice | ||
| /// API: [`.slice()`]. The method takes ownership of the buffer and returns a | ||
| /// [`Slice`] value that tracks the requested range. | ||
| /// | ||
| /// This trait provides a generic way to use buffers and `Slice` views | ||
| /// into such buffers with `io-uring` operations. | ||
| /// | ||
| /// [`.slice()`]: BoundedBuf::slice | ||
| pub trait BoundedBuf: Sized + Unpin + 'static { | ||
| /// The type of the underlying buffer. | ||
| type Buf: IoBuf; | ||
|
|
||
| /// The type representing the range bounds of the view. | ||
| type Bounds: ops::RangeBounds<usize>; | ||
FrankReh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Returns a view of the buffer with the specified range. | ||
| /// | ||
| /// This method is similar to Rust's slicing (`&buf[..]`), but takes | ||
| /// ownership of the buffer. The range bounds are specified against | ||
| /// the possibly offset beginning of the `self` view into the buffer | ||
| /// and the end bound, if specified, must not exceed the view's total size. | ||
| /// Note that the range may extend into the uninitialized part of the | ||
| /// buffer, but it must start (if so bounded) in the initialized part | ||
| /// or immediately adjacent to it. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// If the range is invalid with regard to the recipient's total size or | ||
| /// the length of its initialized part, the implementation of this method | ||
| /// should panic. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use tokio_uring::buf::BoundedBuf; | ||
| /// | ||
| /// let buf = b"hello world".to_vec(); | ||
| /// let slice = buf.slice(5..10); | ||
| /// assert_eq!(&slice[..], b" worl"); | ||
| /// let slice = slice.slice(1..3); | ||
| /// assert_eq!(&slice[..], b"wo"); | ||
| /// ``` | ||
| fn slice(self, range: impl ops::RangeBounds<usize>) -> Slice<Self::Buf>; | ||
|
|
||
| /// Gets a reference to the underlying buffer. | ||
| fn get_buf(&self) -> &Self::Buf; | ||
|
|
||
| /// Returns the range bounds for this view. | ||
| fn bounds(&self) -> Self::Bounds; | ||
|
|
||
| /// Constructs a view from an underlying buffer and range bounds. | ||
| fn from_buf_bounds(buf: Self::Buf, bounds: Self::Bounds) -> Self; | ||
|
|
||
| /// Like [`IoBuf::stable_ptr`], | ||
| /// but possibly offset to the view's starting position. | ||
| fn stable_ptr(&self) -> *const u8; | ||
|
|
||
| /// Number of initialized bytes available via this view. | ||
| fn bytes_init(&self) -> usize; | ||
|
|
||
| /// Total size of the view, including uninitialized memory, if any. | ||
| fn bytes_total(&self) -> usize; | ||
| } | ||
|
|
||
| impl<T: IoBuf> BoundedBuf for T { | ||
FrankReh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| type Buf = Self; | ||
| type Bounds = ops::RangeFull; | ||
|
|
||
| fn slice(self, range: impl ops::RangeBounds<usize>) -> Slice<Self> { | ||
| use ops::Bound; | ||
|
|
||
| let begin = match range.start_bound() { | ||
| Bound::Included(&n) => n, | ||
| Bound::Excluded(&n) => n.checked_add(1).expect("out of range"), | ||
| Bound::Unbounded => 0, | ||
| }; | ||
|
|
||
| assert!(begin < self.bytes_total()); | ||
|
|
||
| let end = match range.end_bound() { | ||
| Bound::Included(&n) => n.checked_add(1).expect("out of range"), | ||
| Bound::Excluded(&n) => n, | ||
| Bound::Unbounded => self.bytes_total(), | ||
| }; | ||
|
|
||
| assert!(end <= self.bytes_total()); | ||
| assert!(begin <= self.bytes_init()); | ||
|
|
||
| Slice::new(self, begin, end) | ||
| } | ||
|
|
||
| fn get_buf(&self) -> &Self { | ||
| self | ||
| } | ||
|
|
||
| fn bounds(&self) -> Self::Bounds { | ||
| .. | ||
| } | ||
|
|
||
| fn from_buf_bounds(buf: Self, _: ops::RangeFull) -> Self { | ||
FrankReh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| buf | ||
| } | ||
|
|
||
| fn stable_ptr(&self) -> *const u8 { | ||
| IoBuf::stable_ptr(self) | ||
| } | ||
|
|
||
| fn bytes_init(&self) -> usize { | ||
| IoBuf::bytes_init(self) | ||
| } | ||
|
|
||
| fn bytes_total(&self) -> usize { | ||
| IoBuf::bytes_total(self) | ||
| } | ||
| } | ||
|
|
||
| /// A possibly bounded view into an owned [`IoBufMut`] buffer. | ||
| /// | ||
| /// This trait provides a generic way to use mutable buffers and `Slice` views | ||
| /// into such buffers with `io-uring` operations. | ||
| pub trait BoundedBufMut: BoundedBuf<Buf = Self::BufMut> { | ||
| /// The type of the underlying buffer. | ||
| type BufMut: IoBufMut; | ||
|
|
||
| /// Like [`IoBufMut::stable_mut_ptr`], | ||
| /// but possibly offset to the view's starting position. | ||
| fn stable_mut_ptr(&mut self) -> *mut u8; | ||
|
|
||
| /// Like [`IoBufMut::set_init`], | ||
| /// but the position is possibly offset to the view's starting position. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The caller must ensure that all bytes starting at `stable_mut_ptr()` up | ||
| /// to `pos` are initialized and owned by the buffer. | ||
| unsafe fn set_init(&mut self, pos: usize); | ||
| } | ||
|
|
||
| impl<T: IoBufMut> BoundedBufMut for T { | ||
| type BufMut = T; | ||
|
|
||
| fn stable_mut_ptr(&mut self) -> *mut u8 { | ||
| IoBufMut::stable_mut_ptr(self) | ||
| } | ||
|
|
||
| unsafe fn set_init(&mut self, pos: usize) { | ||
| IoBufMut::set_init(self, pos) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.