Skip to main content

SharedVecMut

Struct SharedVecMut 

Source
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§

Source§

impl<T> SharedVecMut<T>

Source

pub fn from_vec(vec: Vec<T>) -> Self

Create a SharedVecMut by consuming a Vec.

Source

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:

  1. at is greater than the length of the vector.
  2. borrow_count overflows usize.

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();
Source

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:

  1. at is greater than the length of the vector.
  2. borrow_count overflows usize.

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();
Source

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.

Source

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.

Source

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.

Source

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);
Source

pub fn as_slice_mut(&mut self) -> &mut [T]

Directly get a mutable slice of the remaining part of the SharedVecMut.

Trait Implementations§

Source§

impl<T: Debug> Debug for SharedVecMut<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for SharedVecMut<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send> Send for SharedVecMut<T>

Source§

impl<T: Sync> Sync for SharedVecMut<T>

Auto Trait Implementations§

§

impl<T> Freeze for SharedVecMut<T>

§

impl<T> RefUnwindSafe for SharedVecMut<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for SharedVecMut<T>

§

impl<T> UnsafeUnpin for SharedVecMut<T>

§

impl<T> UnwindSafe for SharedVecMut<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.