1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
use crate::buf::{IoBuf, IoBufMut};
use std::cmp;
use std::ops;
/// An owned view into a contiguous sequence of bytes.
///
/// This is similar to Rust slices (`&buf[..]`) but owns the underlying buffer.
/// This type is useful for performing io-uring read and write operations using
/// a subset of a buffer.
///
/// Slices are created using [`IoBuf::slice`].
///
/// # Examples
///
/// Creating a slice
///
/// ```
/// use tokio_uring::buf::IoBuf;
///
/// let buf = b"hello world".to_vec();
/// let slice = buf.slice(..5);
///
/// assert_eq!(&slice[..], b"hello");
/// ```
pub struct Slice<T> {
buf: T,
begin: usize,
end: usize,
}
impl<T> Slice<T> {
pub(crate) fn new(buf: T, begin: usize, end: usize) -> Slice<T> {
Slice { buf, begin, end }
}
/// Offset in the underlying buffer at which this slice starts.
///
/// # Examples
///
/// ```
/// use tokio_uring::buf::IoBuf;
///
/// let buf = b"hello world".to_vec();
/// let slice = buf.slice(1..5);
///
/// assert_eq!(1, slice.begin());
/// ```
pub fn begin(&self) -> usize {
self.begin
}
/// Ofset in the underlying buffer at which this slice ends.
///
/// # Examples
///
/// ```
/// use tokio_uring::buf::IoBuf;
///
/// let buf = b"hello world".to_vec();
/// let slice = buf.slice(1..5);
///
/// assert_eq!(5, slice.end());
/// ```
pub fn end(&self) -> usize {
self.end
}
/// Gets a reference to the underlying buffer.
///
/// This method escapes the slice's view.
///
/// # Examples
///
/// ```
/// use tokio_uring::buf::IoBuf;
///
/// let buf = b"hello world".to_vec();
/// let slice = buf.slice(..5);
///
/// assert_eq!(slice.get_ref(), b"hello world");
/// assert_eq!(&slice[..], b"hello");
/// ```
pub fn get_ref(&self) -> &T {
&self.buf
}
/// Gets a mutable reference to the underlying buffer.
///
/// This method escapes the slice's view.
///
/// # Examples
///
/// ```
/// use tokio_uring::buf::IoBuf;
///
/// let buf = b"hello world".to_vec();
/// let mut slice = buf.slice(..5);
///
/// slice.get_mut()[0] = b'b';
///
/// assert_eq!(slice.get_mut(), b"bello world");
/// assert_eq!(&slice[..], b"bello");
/// ```
pub fn get_mut(&mut self) -> &mut T {
&mut self.buf
}
/// Unwraps this `Slice`, returning the underlying buffer.
///
/// # Examples
///
/// ```
/// use tokio_uring::buf::IoBuf;
///
/// let buf = b"hello world".to_vec();
/// let slice = buf.slice(..5);
///
/// let buf = slice.into_inner();
/// assert_eq!(buf, b"hello world");
/// ```
pub fn into_inner(self) -> T {
self.buf
}
}
impl<T: IoBuf> ops::Deref for Slice<T> {
type Target = [u8];
fn deref(&self) -> &[u8] {
let buf_bytes = super::deref(&self.buf);
let end = cmp::min(self.end, buf_bytes.len());
&buf_bytes[self.begin..end]
}
}
impl<T: IoBufMut> ops::DerefMut for Slice<T> {
fn deref_mut(&mut self) -> &mut [u8] {
let buf_bytes = super::deref_mut(&mut self.buf);
let end = cmp::min(self.end, buf_bytes.len());
&mut buf_bytes[self.begin..end]
}
}
unsafe impl<T: IoBuf> IoBuf for Slice<T> {
fn stable_ptr(&self) -> *const u8 {
super::deref(&self.buf)[self.begin..].as_ptr()
}
fn bytes_init(&self) -> usize {
ops::Deref::deref(self).len()
}
fn bytes_total(&self) -> usize {
self.end - self.begin
}
}
unsafe impl<T: IoBufMut> IoBufMut for Slice<T> {
fn stable_mut_ptr(&mut self) -> *mut u8 {
super::deref_mut(&mut self.buf)[self.begin..].as_mut_ptr()
}
unsafe fn set_init(&mut self, pos: usize) {
self.buf.set_init(self.begin + pos);
}
}