Skip to content

Commit 4202f3b

Browse files
committed
Add UninitSlice::split_at_mut
1 parent 7b18c1c commit 4202f3b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/buf/uninit_slice.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@ impl UninitSlice {
136136
pub fn len(&self) -> usize {
137137
self.0.len()
138138
}
139+
140+
/// Divides one mutable slice into two at an index.
141+
///
142+
/// The first will contain all indices from `[0, mid)` (excluding the index
143+
/// `mid` itself) and the second will contain all indices from [mid, len)
144+
/// (excluding the index `len` itself).
145+
///
146+
/// # Panics
147+
///
148+
/// Panics if `mid > len`.
149+
///
150+
/// # Examples
151+
///
152+
/// ```
153+
/// use bytes::BufMut;
154+
///
155+
/// let mut data = [0, 1, 2];
156+
/// let mut slice = &mut data[..];
157+
/// let chunk = BufMut::chunk_mut(&mut slice);
158+
///
159+
/// let (left, right) = chunk.split_at_mut(2);
160+
/// left.copy_from_slice(&[3, 4]);
161+
///
162+
/// assert_eq!(data, [3, 4, 2]);
163+
/// ```
164+
pub fn split_at_mut(&mut self, mid: usize) -> (&mut UninitSlice, &mut UninitSlice) {
165+
let (left, right) = self.0.split_at_mut(mid);
166+
unsafe {
167+
let left = UninitSlice::from_raw_parts_mut(left.as_mut_ptr() as *mut u8, left.len());
168+
let right = UninitSlice::from_raw_parts_mut(right.as_mut_ptr() as *mut u8, right.len());
169+
(left, right)
170+
}
171+
}
139172
}
140173

141174
impl fmt::Debug for UninitSlice {

0 commit comments

Comments
 (0)