Struct enso_prelude::non_empty_vec::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);

Append an element to the back of a collection.

Panics

Panics if the number of elements in the vector overflows a usize.

Examples

use enso_prelude::NonEmptyVec;
let mut vec = NonEmptyVec::new(0,vec![1,2]);
vec.push(3);
assert_eq!(vec.len(),4);

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])

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)

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

Returns true if the vector contains no elements.

Examples

let mut v = Vec::new();
assert!(v.is_empty());

v.push(1);
assert!(!v.is_empty());

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

Checks if self is actually part of its subset T (and can be converted to it).

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

The inclusion map: converts self to the equivalent element of its superset.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.