Thanks to visit codestin.com
Credit goes to docs.rs

ImpVec

Struct ImpVec 

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

ImpVec, stands 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::*;

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::*;

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::*;

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_push_get_ref(&self, value: T) -> &T

Pushes the value to the vector and returns a reference to it.

It is the composition of vec.imp_push(value) call followed by &vec[vec.len() - 1].

ยงExamples

This method provides a shorthand for the following common use case.

use orx_imp_vec::*;

let vec = ImpVec::new();

vec.imp_push('a');
let a = &vec[vec.len() - 1];
assert_eq!(a, &'a');

// or with imp_push_get_ref

let b = vec.imp_push_get_ref('b');
assert_eq!(b, &'b');
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::*;

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::*;

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.

Default underlying pinned vector is a new SplitVec<T, Doubling>.

ยงExample
use orx_imp_vec::*;

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

impl<T> ImpVec<T, SplitVec<T, Doubling>>

Source

pub fn with_doubling_growth() -> Self

Creates a new ImpVec by creating and wrapping up a new SplitVec<T, Doubling> as the underlying storage.

Sourceยง

impl<T> ImpVec<T, SplitVec<T, Recursive>>

Source

pub fn with_recursive_growth() -> Self

Creates a new ImpVec by creating and wrapping up a new SplitVec<T, Recursive> as the underlying storage.

Sourceยง

impl<T> ImpVec<T, SplitVec<T, Linear>>

Source

pub fn with_linear_growth(constant_fragment_capacity_exponent: usize) -> Self

Creates a new ImpVec by creating and wrapping up a new SplitVec<T, Linear> as the underlying storage.

  • Each fragment of the underlying split vector will have a capacity of 2 ^ constant_fragment_capacity_exponent.
Sourceยง

impl<T> ImpVec<T, FixedVec<T>>

Source

pub fn with_fixed_capacity(fixed_capacity: usize) -> Self

Creates a new ImpVec by creating and wrapping up a new FixedVec<T> as the underlying storage.

ยงSafety

Note that a FixedVec cannot grow beyond the given fixed_capacity. In other words, has a hard upper bound on the number of elements it can hold, which is the fixed_capacity.

Pushing to the vector beyond this capacity leads to โ€œout-of-capacityโ€ error.

This maximum capacity can be accessed by the capacitymethod.

Trait Implementationsยง

Sourceยง

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

Sourceยง

fn clone(&self) -> Self

Returns a duplicate 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: Debug, 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::*;

let imp_vec: ImpVec<usize> = ImpVec::default();
assert!(imp_vec.is_empty());
Sourceยง

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

Sourceยง

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, P> FromIterator<T> for ImpVec<T, P>
where P: FromIterator<T> + PinnedVec<T>,

Sourceยง

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Sourceยง

impl<T, P: PinnedVec<T>> Index<usize> for ImpVec<T, P>

Sourceยง

type Output = T

The returned type after indexing.
Sourceยง

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Sourceยง

impl<T, P: PinnedVec<T>> IndexMut<usize> for ImpVec<T, P>

Sourceยง

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Sourceยง

impl<'a, T, P> IntoConcurrentIter for &'a ImpVec<T, P>
where P: PinnedVec<T>, &'a P: IntoConcurrentIter<Item = &'a T>, T: Send + Sync,

Sourceยง

type Item = &'a T

Type of the element that the concurrent iterator yields.
Sourceยง

type IntoIter = <&'a P as IntoConcurrentIter>::IntoIter

Type of the concurrent iterator that this type can be converted into.
Sourceยง

fn into_con_iter(self) -> Self::IntoIter

Trait to convert a source (collection or generator) into a concurrent iterator; i.e., ConcurrentIter, using its into_con_iter method. Read more
Sourceยง

impl<T, P> IntoConcurrentIter for ImpVec<T, P>
where P: PinnedVec<T> + IntoConcurrentIter<Item = T>, T: Send + Sync,

Sourceยง

type Item = T

Type of the element that the concurrent iterator yields.
Sourceยง

type IntoIter = <P as IntoConcurrentIter>::IntoIter

Type of the concurrent iterator that this type can be converted into.
Sourceยง

fn into_con_iter(self) -> Self::IntoIter

Trait to convert a source (collection or generator) into a concurrent iterator; i.e., ConcurrentIter, using its into_con_iter method. Read more
Sourceยง

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

Sourceยง

type Item = T

The type of the elements being iterated over.
Sourceยง

type IntoIter = <P as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Sourceยง

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Sourceยง

impl<T: PartialEq, P: PinnedVec<T>> PartialEq<[T]> for ImpVec<T, P>

Sourceยง

fn eq(&self, other: &[T]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<T: PartialEq, P: PinnedVec<T>> PartialEq<FixedVec<T>> for ImpVec<T, P>

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<T: PartialEq, P: PinnedVec<T>> PartialEq<ImpVec<T, P>> for [T]

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<T: PartialEq, P: PinnedVec<T>> PartialEq<ImpVec<T, P>> for FixedVec<T>

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<T: PartialEq, P: PinnedVec<T>, G: Growth> PartialEq<ImpVec<T, P>> for SplitVec<T, G>

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<T: PartialEq, P: PinnedVec<T>> PartialEq<ImpVec<T, P>> for Vec<T>

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<T: PartialEq, P: PinnedVec<T>, G: Growth> PartialEq<SplitVec<T, G>> for ImpVec<T, P>

Sourceยง

fn eq(&self, other: &SplitVec<T, G>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<T: PartialEq, P: PinnedVec<T>> PartialEq<Vec<T>> for ImpVec<T, P>

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

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

Auto Trait Implementationsยง

ยง

impl<T, P = SplitVec<T>> !Freeze for ImpVec<T, P>

ยง

impl<T, P = SplitVec<T>> !RefUnwindSafe for ImpVec<T, P>

ยง

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

ยง

impl<T, P = SplitVec<T>> !Sync for ImpVec<T, P>

ยง

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

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<X> ConcurrentCollection for X
where X: IntoConcurrentIter, &'a X: for<'a> IntoConcurrentIter<Item = &'a <X as IntoConcurrentIter>::Item>,

Sourceยง

type Item = <X as IntoConcurrentIter>::Item

Type of the element that the concurrent iterator yields.
Sourceยง

type Iterable<'i> = &'i X where X: 'i

Type of the ConcurrentIterable that reference of this type implements.
Sourceยง

fn as_concurrent_iterable(&self) -> <X as ConcurrentCollection>::Iterable<'_>

Returns the ConcurrentIterable that a reference of this type can create.
Sourceยง

fn con_iter(&self) -> <Self::Iterable<'_> as ConcurrentIterable>::Iter

A type implementing ConcurrentCollection is a collection owning the elements such that 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Sourceยง

type Target = T

๐Ÿ”ฌThis is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Sourceยง

impl<T> SoM<T> for T

Sourceยง

fn get_ref(&self) -> &T

Returns a reference to self.
Sourceยง

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

Returns a mutable reference to self.
Sourceยง

impl<T> SoR<T> for T

Sourceยง

fn get_ref(&self) -> &T

Returns a reference to self.
Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

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

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.