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>
impl<T, P: PinnedVec<T>> ImpVec<T, P>
Sourcepub fn into_inner(self) -> P
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]);
Sourcepub fn imp_push(&self, value: T)
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.
Sourcepub fn imp_push_get_ref(&self, value: T) -> &T
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');
Sourcepub fn imp_extend_from_slice(&self, slice: &[T])where
T: Clone,
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>
impl<T> ImpVec<T>
Sourcepub fn new() -> Self
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>>
impl<T> ImpVec<T, SplitVec<T, Doubling>>
Sourcepub fn with_doubling_growth() -> Self
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>>
impl<T> ImpVec<T, SplitVec<T, Recursive>>
Sourcepub fn with_recursive_growth() -> Self
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>>
impl<T> ImpVec<T, SplitVec<T, Linear>>
Sourcepub fn with_linear_growth(constant_fragment_capacity_exponent: usize) -> Self
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>>
impl<T> ImpVec<T, FixedVec<T>>
Sourcepub fn with_fixed_capacity(fixed_capacity: usize) -> Self
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 capacity
method.
Trait Implementationsยง
Sourceยงimpl<T, P> FromIterator<T> for ImpVec<T, P>where
P: FromIterator<T> + PinnedVec<T>,
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
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Sourceยงimpl<'a, T, P> IntoConcurrentIter for &'a ImpVec<T, P>
impl<'a, T, P> IntoConcurrentIter for &'a ImpVec<T, P>
Sourceยงtype IntoIter = <&'a P as IntoConcurrentIter>::IntoIter
type IntoIter = <&'a P as IntoConcurrentIter>::IntoIter
Sourceยงfn into_con_iter(self) -> Self::IntoIter
fn into_con_iter(self) -> Self::IntoIter
ConcurrentIter
,
using its into_con_iter
method. Read moreSourceยงimpl<T, P> IntoConcurrentIter for ImpVec<T, P>
impl<T, P> IntoConcurrentIter for ImpVec<T, P>
Sourceยงtype IntoIter = <P as IntoConcurrentIter>::IntoIter
type IntoIter = <P as IntoConcurrentIter>::IntoIter
Sourceยงfn into_con_iter(self) -> Self::IntoIter
fn into_con_iter(self) -> Self::IntoIter
ConcurrentIter
,
using its into_con_iter
method. Read moreSourceยงimpl<T, P: PinnedVec<T>> IntoIterator for ImpVec<T, P>
impl<T, P: PinnedVec<T>> IntoIterator for ImpVec<T, P>
Sourceยงimpl<T: PartialEq, P1: PinnedVec<T>, P2: PinnedVec<T>> PartialEq<ImpVec<T, P2>> for ImpVec<T, P1>
impl<T: PartialEq, P1: PinnedVec<T>, P2: PinnedVec<T>> PartialEq<ImpVec<T, P2>> for ImpVec<T, P1>
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>
impl<T, P = SplitVec<T>> !Sync for ImpVec<T, P>
impl<T, P> Unpin for ImpVec<T, P>
impl<T, P> UnwindSafe for ImpVec<T, P>where
P: UnwindSafe,
T: UnwindSafe,
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<X> ConcurrentCollection for Xwhere
X: IntoConcurrentIter,
&'a X: for<'a> IntoConcurrentIter<Item = &'a <X as IntoConcurrentIter>::Item>,
impl<X> ConcurrentCollection for Xwhere
X: IntoConcurrentIter,
&'a X: for<'a> IntoConcurrentIter<Item = &'a <X as IntoConcurrentIter>::Item>,
Sourceยงtype Item = <X as IntoConcurrentIter>::Item
type Item = <X as IntoConcurrentIter>::Item
Sourceยงtype Iterable<'i> = &'i X
where
X: 'i
type Iterable<'i> = &'i X where X: 'i
Sourceยงfn as_concurrent_iterable(&self) -> <X as ConcurrentCollection>::Iterable<'_>
fn as_concurrent_iterable(&self) -> <X as ConcurrentCollection>::Iterable<'_>
Sourceยงfn con_iter(&self) -> <Self::Iterable<'_> as ConcurrentIterable>::Iter
fn con_iter(&self) -> <Self::Iterable<'_> as ConcurrentIterable>::Iter
ConcurrentCollection
is a collection owning the elements such that Read more