3 ms·
> There are some APIs like this but last time I checked they didn’t cover all usecases. This refers to[1] <&mut [T]>::split_at_mut(usize) -> (&mut [T], &m
by estebank 4y ago
> There are some APIs like this but last time I checked they didn’t cover all usecases.
This refers to[1]
<&mut [T]>::split_at_mut(usize) -> (&mut [T], &mut [T])`
used as
let (first_42, rest) = mutable_slice.split_at_mut(42);
but you can see that the inner logic is just a bit of pointer twiddling[2]
let len = self.len();
let ptr = self.as_ptr();
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) }
[1]: https://doc.rust-lang.org/std/primitive.slice.html#method.split_at_mut https://doc.rust-lang.org/std/primitive.slice.html#method.sp...
[2]: https://doc.rust-lang.org/src/core/slice/mod.rs.html#1641-1645 https://doc.rust-lang.org/src/core/slice/mod.rs.html#1641-16...
- loeg 4y agoYes, exactly. I was using this for zero-copy serialization into a buffer. At the time, I don’t believe that API had been stabilized yet.