pub struct SharedVecMut<T> { /* private fields */ }Expand description
A container of a Vec allocation that can be split into multiple SharedVecPart values.
This type is useful when a single Vec needs to be partitioned into multiple independently
owned segments that still refer to the same underlying allocation.
let mut values = manual_share::SharedVecMut::from_vec(vec![1, 2, 3, 4]);
let mut part = values.split_off(2).unwrap();
let join_handle = std::thread::spawn(|| {
part.as_slice_mut().iter_mut().for_each(|v| *v += 1);
part
});
let part = join_handle.join().unwrap();
assert_eq!(part.as_slice(), &[4, 5]);
assert!(values.try_unsplit_off(part).is_ok());Dropping the SharedVecMut without returning all SharedVecPart values leaks the underlying allocation.
When the panic-on-drop feature is enabled, it will panic:
let r = {
let mut values = manual_share::SharedVecMut::from_vec(vec![0]);
values.split_off(1).unwrap()
// panic here because the part was not returned to the original SharedVecMut.
};
println!("{:?}", r.as_slice());Implementations§
Sourcepub fn split_off(&mut self, at: usize) -> Option<SharedVecPart<T>>
pub fn split_off(&mut self, at: usize) -> Option<SharedVecPart<T>>
Split off the suffix of the vector starting at at.
This method is similar to bytes::BytesMut::split_off.
Returns None when:
atis greater than the length of the vector.borrow_countoverflowsusize.
If successful, the returned part will contain [at, len) and self will contain [0, at).
Here is an example of splitting a SharedVecMut into 3 parts:
let mut values = manual_share::SharedVecMut::from_vec(vec![1, 2, 3]);
let part1 = values.split_off(2).unwrap();
let part2 = values.split_off(1).unwrap();
let part3 = values.split_off(0).unwrap();
assert_eq!(part1.as_slice(), &[3]);
assert_eq!(part2.as_slice(), &[2]);
assert_eq!(part3.as_slice(), &[1]);
values.try_unsplit_off(part3).unwrap();
values.try_unsplit_off(part2).unwrap();
values.try_unsplit_off(part1).unwrap();Sourcepub fn split_to(&mut self, at: usize) -> Option<SharedVecPart<T>>
pub fn split_to(&mut self, at: usize) -> Option<SharedVecPart<T>>
Split off the prefix of the vector ending at at.
This method is similar to bytes::BytesMut::split_to.
Returns None when:
atis greater than the length of the vector.borrow_countoverflowsusize.
If successful, the returned part will contain [0, at) and self will contain [at, len).
Here is an example of splitting a SharedVecMut into 3 parts:
let mut values = manual_share::SharedVecMut::from_vec(vec![1, 2, 3]);
let part1 = values.split_to(1).unwrap();
let part2 = values.split_to(1).unwrap();
let part3 = values.split_to(1).unwrap();
assert_eq!(part1.as_slice(), &[1]);
assert_eq!(part2.as_slice(), &[2]);
assert_eq!(part3.as_slice(), &[3]);
values.try_unsplit_to(part3).unwrap();
values.try_unsplit_to(part2).unwrap();
values.try_unsplit_to(part1).unwrap();Sourcepub fn try_unsplit_off(
&mut self,
part: SharedVecPart<T>,
) -> Result<(), SharedVecPart<T>>
pub fn try_unsplit_off( &mut self, part: SharedVecPart<T>, ) -> Result<(), SharedVecPart<T>>
Try to unsplit a part that was previously split off with split_off.
Sourcepub fn try_unsplit_to(
&mut self,
part: SharedVecPart<T>,
) -> Result<(), SharedVecPart<T>>
pub fn try_unsplit_to( &mut self, part: SharedVecPart<T>, ) -> Result<(), SharedVecPart<T>>
Try to unsplit a part that was previously split off with split_to.
Sourcepub fn try_into_vec(self) -> Result<Vec<T>, Self>
pub fn try_into_vec(self) -> Result<Vec<T>, Self>
Try to convert the mutable view back into a Vec when no parts remain outstanding.
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Directly get a slice of the remaining part of the SharedVecMut.
let mut values = manual_share::SharedVecMut::from_vec(vec![1, 2, 3]);
let part1 = values.split_to(1).unwrap();
let part2 = values.split_off(1).unwrap();
assert_eq!(values.as_slice(), &[2]);
assert_eq!(part1.as_slice(), &[1]);
assert_eq!(part2.as_slice(), &[3]);
values.try_unsplit_off(part2).unwrap();
values.try_unsplit_to(part1).unwrap();
assert_eq!(values.as_slice(), &[1, 2, 3]);Further splitting is no longer possible as long as the returned slice is held alive:
let mut values = manual_share::SharedVecMut::from_vec(vec![1, 2, 3]);
let slice = values.as_slice();
let part = values.split_off(1).unwrap();
println!("{:?}", slice);Sourcepub fn as_slice_mut(&mut self) -> &mut [T]
pub fn as_slice_mut(&mut self) -> &mut [T]
Directly get a mutable slice of the remaining part of the SharedVecMut.