Struct enso_prelude::NonEmptyVec [−][src]
pub struct NonEmptyVec<T> { /* fields omitted */ }
Expand description
A version of std::vec::Vec
that can’t be empty.
Implementations
Construct a new non-empty vector.
The vector will not allocate more than the space required to contain first
and rest
.
Examples
#![allow(unused_mut)] use enso_prelude::NonEmptyVec; let mut vec: NonEmptyVec<usize> = NonEmptyVec::new(0,vec![]);
Construct a NonEmptyVec
containing a single element.
Examples
use enso_prelude::NonEmptyVec; let vec = NonEmptyVec::singleton(0); assert_eq!(vec.get(0),Some(&0)); assert_eq!(vec.len(),1);
Construct a new, NonEmptyVec<T>
containing the provided element and with the provided
capacity
.
If capacity
is 0, then the vector will be allocated with capacity for the provided first
element. The vector will be able to hold exactly capacity
elements without reallocating.
It is important to note that although the returned vector has the capacity specified, the vector will have a length of 1.
Panics
Panics if capacity
is not > 0.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::with_capacity(0, 10); // The vector contains one item, even though it has capacity for more assert_eq!(vec.len(), 1); // These are all done without reallocating... for i in 1..10 { vec.push(i); } // ...but this may make the vector reallocate vec.push(11);
Reserve capacity for at least additional
more elements to be inserted in the given
Vec<T>
.
The collection may reserve more space to avoid frequent reallocations. After calling
reserve
, capacity will be greater than or equal to self.len() + additional
. Does nothing
if capacity is already sufficient.
Panics
Panics if the new capacity overflows usize
.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::new(0,vec![]); vec.reserve(10); assert!(vec.capacity() >= 11);
Shrinks the capacity of the NonEmotyVec
as much as possible.
It will drop down as close as possible to the length, but the allocator may still inform the vector that there is space for a few more elements.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::with_capacity(0, 10); assert_eq!(vec.capacity(),10); vec.shrink_to_fit(); assert!(vec.capacity() < 10);
Remove an element from the back of the collection, returning it.
Will not pop any item if there is only one item left in the vector.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::new(0,vec![1]); assert!(vec.pop().is_some()); assert!(vec.pop().is_none()); assert_eq!(vec.len(),1);
Obtain a mutable reference to teh element in the vector at the specified index
.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::new(0,vec![1,2]); let reference = vec.get_mut(0); assert!(reference.is_some()); assert_eq!(*reference.unwrap(),0);
Obtain an immutable reference to the head of the NonEmptyVec
.
Examples
use enso_prelude::NonEmptyVec; let vec = NonEmptyVec::new(0,vec![1,2]); assert_eq!(*vec.first(), 0);
Obtain a mutable reference to the head of the NonEmptyVec
.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::new(0,vec![1,2]); assert_eq!(*vec.first_mut(), 0);
Obtain an immutable reference to the last element in the NonEmptyVec
.
Examples
use enso_prelude::NonEmptyVec; let vec = NonEmptyVec::new(0,vec![1,2]); assert_eq!(*vec.last(),2)
Obtain a mutable reference to the last element in the NonEmptyVec
.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::new(0,vec![1,2]); assert_eq!(*vec.last_mut(),2)
Create a draining iterator that removes the specified range in the vector and yields the removed items.
It will never remove the root element of the vector.
Panics
Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::new(0,vec![1,2,3,4,5]); let drained:Vec<i32> = vec.drain(1..=5).collect(); assert_eq!(drained,[1,2,3,4,5])
pub fn splice<R, I>(
&mut self,
range: R,
replace_with: I
) -> Splice<'_, <I as IntoIterator>::IntoIter>ⓘ where
I: IntoIterator<Item = T>,
R: RangeBounds<usize>,
pub fn splice<R, I>(
&mut self,
range: R,
replace_with: I
) -> Splice<'_, <I as IntoIterator>::IntoIter>ⓘ where
I: IntoIterator<Item = T>,
R: RangeBounds<usize>,
Creates a splicing iterator that replaces the specified range in the vector with the given 4
replace_with
iterator and yields the removed items.
replace_with
does not need to be the same length as range. The element range is removed
even if the iterator is not consumed until the end.
It is unspecified how many elements are removed from the vector if the Splice value is leaked.
The input iterator replace_with is only consumed when the Splice value is dropped.
Panics
Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
Examples
use enso_prelude::NonEmptyVec; let mut vec = NonEmptyVec::new(0,vec![1,2,3,4,5]); let replacements = [10,20,30,40]; let yielded:Vec<_> = vec.splice(..2,replacements.iter().cloned()).collect(); assert_eq!(vec.as_slice(),&[10,20,30,40,2,3,4,5]); assert_eq!(yielded,&[0,1])
Methods from Deref<Target = Vec<T>>
Returns the number of elements the vector can hold without reallocating.
Examples
let vec: Vec<i32> = Vec::with_capacity(10); assert_eq!(vec.capacity(), 10);
Extracts a slice containing the entire vector.
Equivalent to &s[..]
.
Examples
use std::io::{self, Write}; let buffer = vec![1, 2, 3, 5, 8]; io::sink().write(buffer.as_slice()).unwrap();
Returns a raw pointer to the vector’s buffer.
The caller must ensure that the vector outlives the pointer this function returns, or else it will end up pointing to garbage. Modifying the vector may cause its buffer to be reallocated, which would also make any pointers to it invalid.
The caller must also ensure that the memory the pointer (non-transitively) points to
is never written to (except inside an UnsafeCell
) using this pointer or any pointer
derived from it. If you need to mutate the contents of the slice, use as_mut_ptr
.
Examples
let x = vec![1, 2, 4]; let x_ptr = x.as_ptr(); unsafe { for i in 0..x.len() { assert_eq!(*x_ptr.add(i), 1 << i); } }
🔬 This is a nightly-only experimental API. (allocator_api
)
allocator_api
)Returns a reference to the underlying allocator.
Returns the number of elements in the vector, also referred to as its ‘length’.
Examples
let a = vec![1, 2, 3]; assert_eq!(a.len(), 3);
Trait Implementations
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
Auto Trait Implementations
impl<T> RefUnwindSafe for NonEmptyVec<T> where
T: RefUnwindSafe,
impl<T> Send for NonEmptyVec<T> where
T: Send,
impl<T> Sync for NonEmptyVec<T> where
T: Sync,
impl<T> Unpin for NonEmptyVec<T> where
T: Unpin,
impl<T> UnwindSafe for NonEmptyVec<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
type Output = T
type Output = T
Should always be Self
The inverse inclusion map: attempts to construct self
from the equivalent element of its
superset. Read more
pub fn is_in_subset(&self) -> bool
pub fn is_in_subset(&self) -> bool
Checks if self
is actually part of its subset T
(and can be converted to it).
pub fn to_subset_unchecked(&self) -> SS
pub fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset
but without any property checks. Always succeeds.
pub fn from_subset(element: &SS) -> SP
pub fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.