Struct orx_imp_vec::ImpVec

source ·
pub struct ImpVec<T, P = SplitVec<T>>
where P: PinnedVec<T>,
{ /* private fields */ }
Expand description

ImpVec, standing for immutable push vector 👿, is a data structure which allows appending elements with a shared reference.

Specifically, it extends vector capabilities with the following two methods:

  • fn imp_push(&self, value: T)
  • fn imp_extend_from_slice(&self, slice: &[T])

Note that both of these methods can be called with &self rather than &mut self.

§Motivation

Appending to a vector with a shared reference sounds unconventional, and it is. However, if we consider our vector as a bag of or a container of things rather than having a collective meaning; then, appending element or elements to the end of the vector:

  • does not mutate any of already added elements, and hence,
  • it is not different than creating a new element in the scope.

§Safety

It is natural to expect that appending elements to a vector does not affect already added elements. However, this is usually not the case due to underlying memory management. For instance, std::vec::Vec may move already added elements to different memory locations to maintain the contagious layout of the vector.

PinnedVec prevents such implicit changes in memory locations. It guarantees that push and extend methods keep memory locations of already added elements intact. Therefore, it is perfectly safe to hold on to references of the vector while appending elements.

Consider the classical example that does not compile, which is often presented to highlight the safety guarantees of rust:

let mut vec = vec![0, 1, 2, 3];

let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);

vec.push(4);

// does not compile due to the following reason:  cannot borrow `vec` as mutable because it is also borrowed as immutable
// assert_eq!(ref_to_first, &0);

This wonderful feature of the borrow checker of rust is not required and used for imp_push and imp_extend_from_slice methods of ImpVec since these methods do not require a &mut self reference. Therefore, the following code compiles and runs perfectly safely.

use orx_imp_vec::prelude::*;

let mut vec = ImpVec::new();
vec.extend_from_slice(&[0, 1, 2, 3]);

let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);

vec.imp_push(4);
assert_eq!(vec.len(), 5);

vec.imp_extend_from_slice(&[6, 7]);
assert_eq!(vec.len(), 7);

assert_eq!(ref_to_first, &0);

Implementations§

source§

impl<T, P: PinnedVec<T>> ImpVec<T, P>

source

pub fn into_inner(self) -> P

Consumes the imp-vec into the wrapped inner pinned vector.

§Example
use orx_split_vec::SplitVec;
use orx_imp_vec::ImpVec;

let pinned_vec = SplitVec::new();

let imp_vec = ImpVec::from(pinned_vec);
imp_vec.imp_push(42);

let pinned_vec = imp_vec.into_inner();
assert_eq!(&pinned_vec, &[42]);
source

pub fn imp_push(&self, value: T)

Pushes the value to the vector. This method differs from the push method with the required reference. Unlike push, imp_push allows to push the element with a shared reference.

§Example
use orx_imp_vec::prelude::*;

let mut vec = ImpVec::new();

// regular push with &mut self
vec.push(42);

// hold on to a reference to the first element
let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &42);

// imp_push with &self
vec.imp_push(7);

// due to `PinnedVec` guarantees, this push will never invalidate prior references
assert_eq!(ref_to_first, &42);
§Safety

Wrapping a PinnedVec with an ImpVec provides with two additional methods: imp_push and imp_extend_from_slice. Note that these push and extend methods grow the vector by appending elements to the end.

It is natural to expect that these operations do not change the memory locations of already added elements. However, this is usually not the case due to underlying allocations. For instance, std::vec::Vec may move already added elements in memory to maintain the contagious layout of the vector.

PinnedVec prevents such implicit changes in memory locations. It guarantees that push and extend methods keep memory locations of already added elements intact. Therefore, it is perfectly safe to hold on to references of the vector while appending elements.

Consider the classical example that does not compile, which is often presented to highlight the safety guarantees of rust:

let mut vec = vec![0, 1, 2, 3];

let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);

vec.push(4);

// does not compile due to the following reason:  cannot borrow `vec` as mutable because it is also borrowed as immutable
// assert_eq!(ref_to_first, &0);

This wonderful feature of the borrow checker of rust is not required and used for imp_push and imp_extend_from_slice methods of ImpVec since these methods do not require a &mut self reference. Therefore, the following code compiles and runs perfectly safely.

use orx_imp_vec::prelude::*;

let mut vec = ImpVec::new();
vec.extend_from_slice(&[0, 1, 2, 3]);

let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);

vec.imp_push(4);
assert_eq!(vec.len(), 5);

assert_eq!(ref_to_first, &0);

Although unconventional, this makes sense when we consider the ImpVec as a bag or container of things, rather than having a collective meaning. In other words, when we do not rely on reduction methods, such as count or sum, appending element or elements to the end of the vector:

  • does not mutate any of already added elements, and hence,
  • it is not different than creating a new element in the scope.
source

pub fn imp_extend_from_slice(&self, slice: &[T])
where T: Clone,

Extends the vector with the given slice. This method differs from the extend_from_slice method with the required reference. Unlike extend_from_slice, imp_extend_from_slice allows to push the element with a shared reference.

§Example
use orx_imp_vec::prelude::*;

let mut vec = ImpVec::new();

// regular extend_from_slice with &mut self
vec.extend_from_slice(&[42]);

// hold on to a reference to the first element
let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &42);

// imp_extend_from_slice with &self
vec.imp_extend_from_slice(&[0, 1, 2, 3]);
assert_eq!(vec.len(), 5);

// due to `PinnedVec` guarantees, this extend will never invalidate prior references
assert_eq!(ref_to_first, &42);
§Safety

Wrapping a PinnedVec with an ImpVec provides with two additional methods: imp_push and imp_extend_from_slice. Note that these push and extend methods grow the vector by appending elements to the end.

It is natural to expect that these operations do not change the memory locations of already added elements. However, this is usually not the case due to underlying allocations. For instance, std::vec::Vec may move already added elements in memory to maintain the contagious layout of the vector.

PinnedVec prevents such implicit changes in memory locations. It guarantees that push and extend methods keep memory locations of already added elements intact. Therefore, it is perfectly safe to hold on to references of the vector while appending elements.

Consider the classical example that does not compile, which is often presented to highlight the safety guarantees of rust:

let mut vec = vec![0];

let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);

vec.extend_from_slice(&[1, 2, 3, 4]);

// does not compile due to the following reason:  cannot borrow `vec` as mutable because it is also borrowed as immutable
// assert_eq!(ref_to_first, &0);

This wonderful feature of the borrow checker of rust is not required and used for imp_push and imp_extend_from_slice methods of ImpVec since these methods do not require a &mut self reference. Therefore, the following code compiles and runs perfectly safely.

use orx_imp_vec::prelude::*;

let mut vec = ImpVec::new();
vec.push(0);

let ref_to_first = &vec[0];
assert_eq!(ref_to_first, &0);

vec.imp_extend_from_slice(&[1, 2, 3, 4]);

assert_eq!(ref_to_first, &0);

Although unconventional, this makes sense when we consider the ImpVec as a bag or container of things, rather than having a collective meaning. In other words, when we do not rely on reduction methods, such as count or sum, appending element or elements to the end of the vector:

  • does not mutate any of already added elements, and hence,
  • it is not different than creating a new element in the scope.
source§

impl<T> ImpVec<T>

source

pub fn new() -> Self

Creates a new empty imp-vec.

§Example
use orx_imp_vec::prelude::*;

let imp_vec: ImpVec<char> = ImpVec::new();
assert!(imp_vec.is_empty());

Trait Implementations§

source§

impl<T: Clone, P> Clone for ImpVec<T, P>
where P: PinnedVec<T> + Clone,

source§

fn clone(&self) -> ImpVec<T, P>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T, P: PinnedVec<T> + Debug> Debug for ImpVec<T, P>

source§

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

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

impl<T> Default for ImpVec<T>

source§

fn default() -> Self

Creates a new empty imp-vec.

§Example
use orx_imp_vec::prelude::*;

let imp_vec: ImpVec<usize> = ImpVec::default();
assert!(imp_vec.is_empty());
source§

impl<T, P: PinnedVec<T>> Deref for ImpVec<T, P>

§

type Target = P

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T, P: PinnedVec<T>> DerefMut for ImpVec<T, P>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T, P: PinnedVec<T>> From<P> for ImpVec<T, P>

source§

fn from(pinned_vec: P) -> Self

Converts to this type from the input type.
source§

impl<T: PartialEq, P1: PinnedVec<T>, P2: PinnedVec<T>> PartialEq<ImpVec<T, P2>> for ImpVec<T, P1>

source§

fn eq(&self, other: &ImpVec<T, P2>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<T, P> RefUnwindSafe for ImpVec<T, P>

§

impl<T, P> Send for ImpVec<T, P>
where P: Send, T: Send,

§

impl<T, P> Sync for ImpVec<T, P>
where P: Sync, T: Sync,

§

impl<T, P> Unpin for ImpVec<T, P>
where P: Unpin, T: Unpin,

§

impl<T, P> UnwindSafe for ImpVec<T, P>
where P: UnwindSafe, T: UnwindSafe,

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V