|
| 1 | +use super::{IoBuf, IoBufMut, Slice}; |
| 2 | + |
| 3 | +use std::ops; |
| 4 | + |
| 5 | +/// A possibly bounded view into an owned [`IoBuf`] buffer. |
| 6 | +/// |
| 7 | +/// Because buffers are passed by ownership to the runtime, Rust's slice API |
| 8 | +/// (`&buf[..]`) cannot be used. Instead, `tokio-uring` provides an owned slice |
| 9 | +/// API: [`.slice()`]. The method takes ownership of the buffer and returns a |
| 10 | +/// [`Slice`] value that tracks the requested range. |
| 11 | +/// |
| 12 | +/// This trait provides a generic way to use buffers and `Slice` views |
| 13 | +/// into such buffers with `io-uring` operations. |
| 14 | +/// |
| 15 | +/// [`.slice()`]: BoundedBuf::slice |
| 16 | +pub trait BoundedBuf: Unpin + 'static { |
| 17 | + /// The type of the underlying buffer. |
| 18 | + type Buf: IoBuf; |
| 19 | + |
| 20 | + /// The type representing the range bounds of the view. |
| 21 | + type Bounds: ops::RangeBounds<usize>; |
| 22 | + |
| 23 | + /// Returns a view of the buffer with the specified range. |
| 24 | + /// |
| 25 | + /// This method is similar to Rust's slicing (`&buf[..]`), but takes |
| 26 | + /// ownership of the buffer. The range bounds are specified against |
| 27 | + /// the possibly offset beginning of the `self` view into the buffer |
| 28 | + /// and the end bound, if specified, must not exceed the view's total size. |
| 29 | + /// Note that the range may extend into the uninitialized part of the |
| 30 | + /// buffer, but it must start (if so bounded) in the initialized part |
| 31 | + /// or immediately adjacent to it. |
| 32 | + /// |
| 33 | + /// # Panics |
| 34 | + /// |
| 35 | + /// If the range is invalid with regard to the recipient's total size or |
| 36 | + /// the length of its initialized part, the implementation of this method |
| 37 | + /// should panic. |
| 38 | + /// |
| 39 | + /// # Examples |
| 40 | + /// |
| 41 | + /// ``` |
| 42 | + /// use tokio_uring::buf::BoundedBuf; |
| 43 | + /// |
| 44 | + /// let buf = b"hello world".to_vec(); |
| 45 | + /// let slice = buf.slice(5..10); |
| 46 | + /// assert_eq!(&slice[..], b" worl"); |
| 47 | + /// let slice = slice.slice(1..3); |
| 48 | + /// assert_eq!(&slice[..], b"wo"); |
| 49 | + /// ``` |
| 50 | + fn slice(self, range: impl ops::RangeBounds<usize>) -> Slice<Self::Buf>; |
| 51 | + |
| 52 | + /// Returns a `Slice` with the view's full range. |
| 53 | + /// |
| 54 | + /// This method is to be used by the `tokio-uring` runtime and it is not |
| 55 | + /// expected for users to call it directly. |
| 56 | + fn slice_full(self) -> Slice<Self::Buf>; |
| 57 | + |
| 58 | + /// Gets a reference to the underlying buffer. |
| 59 | + fn get_buf(&self) -> &Self::Buf; |
| 60 | + |
| 61 | + /// Returns the range bounds for this view. |
| 62 | + fn bounds(&self) -> Self::Bounds; |
| 63 | + |
| 64 | + /// Constructs a view from an underlying buffer and range bounds. |
| 65 | + fn from_buf_bounds(buf: Self::Buf, bounds: Self::Bounds) -> Self; |
| 66 | + |
| 67 | + /// Like [`IoBuf::stable_ptr`], |
| 68 | + /// but possibly offset to the view's starting position. |
| 69 | + fn stable_ptr(&self) -> *const u8; |
| 70 | + |
| 71 | + /// Number of initialized bytes available via this view. |
| 72 | + fn bytes_init(&self) -> usize; |
| 73 | + |
| 74 | + /// Total size of the view, including uninitialized memory, if any. |
| 75 | + fn bytes_total(&self) -> usize; |
| 76 | +} |
| 77 | + |
| 78 | +impl<T: IoBuf> BoundedBuf for T { |
| 79 | + type Buf = Self; |
| 80 | + type Bounds = ops::RangeFull; |
| 81 | + |
| 82 | + fn slice(self, range: impl ops::RangeBounds<usize>) -> Slice<Self> { |
| 83 | + use ops::Bound; |
| 84 | + |
| 85 | + let begin = match range.start_bound() { |
| 86 | + Bound::Included(&n) => n, |
| 87 | + Bound::Excluded(&n) => n.checked_add(1).expect("out of range"), |
| 88 | + Bound::Unbounded => 0, |
| 89 | + }; |
| 90 | + |
| 91 | + assert!(begin < self.bytes_total()); |
| 92 | + |
| 93 | + let end = match range.end_bound() { |
| 94 | + Bound::Included(&n) => n.checked_add(1).expect("out of range"), |
| 95 | + Bound::Excluded(&n) => n, |
| 96 | + Bound::Unbounded => self.bytes_total(), |
| 97 | + }; |
| 98 | + |
| 99 | + assert!(end <= self.bytes_total()); |
| 100 | + assert!(begin <= self.bytes_init()); |
| 101 | + |
| 102 | + Slice::new(self, begin, end) |
| 103 | + } |
| 104 | + |
| 105 | + fn slice_full(self) -> Slice<Self> { |
| 106 | + let end = self.bytes_total(); |
| 107 | + Slice::new(self, 0, end) |
| 108 | + } |
| 109 | + |
| 110 | + fn get_buf(&self) -> &Self { |
| 111 | + self |
| 112 | + } |
| 113 | + |
| 114 | + fn bounds(&self) -> Self::Bounds { |
| 115 | + .. |
| 116 | + } |
| 117 | + |
| 118 | + fn from_buf_bounds(buf: Self, _: ops::RangeFull) -> Self { |
| 119 | + buf |
| 120 | + } |
| 121 | + |
| 122 | + fn stable_ptr(&self) -> *const u8 { |
| 123 | + IoBuf::stable_ptr(self) |
| 124 | + } |
| 125 | + |
| 126 | + fn bytes_init(&self) -> usize { |
| 127 | + IoBuf::bytes_init(self) |
| 128 | + } |
| 129 | + |
| 130 | + fn bytes_total(&self) -> usize { |
| 131 | + IoBuf::bytes_total(self) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/// A possibly bounded view into an owned [`IoBufMut`] buffer. |
| 136 | +/// |
| 137 | +/// This trait provides a generic way to use mutable buffers and `Slice` views |
| 138 | +/// into such buffers with `io-uring` operations. |
| 139 | +pub trait BoundedBufMut: BoundedBuf<Buf = Self::BufMut> { |
| 140 | + /// The type of the underlying buffer. |
| 141 | + type BufMut: IoBufMut; |
| 142 | + |
| 143 | + /// Like [`IoBufMut::stable_mut_ptr`], |
| 144 | + /// but possibly offset to the view's starting position. |
| 145 | + fn stable_mut_ptr(&mut self) -> *mut u8; |
| 146 | + |
| 147 | + /// Like [`IoBufMut::set_init`], |
| 148 | + /// but the position is possibly offset to the view's starting position. |
| 149 | + /// |
| 150 | + /// # Safety |
| 151 | + /// |
| 152 | + /// The caller must ensure that all bytes starting at `stable_mut_ptr()` up |
| 153 | + /// to `pos` are initialized and owned by the buffer. |
| 154 | + unsafe fn set_init(&mut self, pos: usize); |
| 155 | +} |
| 156 | + |
| 157 | +impl<T: IoBufMut> BoundedBufMut for T { |
| 158 | + type BufMut = T; |
| 159 | + |
| 160 | + fn stable_mut_ptr(&mut self) -> *mut u8 { |
| 161 | + IoBufMut::stable_mut_ptr(self) |
| 162 | + } |
| 163 | + |
| 164 | + unsafe fn set_init(&mut self, pos: usize) { |
| 165 | + IoBufMut::set_init(self, pos) |
| 166 | + } |
| 167 | +} |
0 commit comments