pub struct Vec<T> { /* private fields */ }
Expand description
A non-empty vector of items.
The first entry is statically stored. Additional items are dynamically stored with
std::vec::Vec<T>
; for memory and performance characteristics please review the documentation
for that module and type.
Completeness
std::vec::Vec
has many methods. These are being implemented as needed!
Please submit a PR or create an issue if you need a method!
Unstable/nightly features
Does not currently support customizable allocators, nightly features, or unstable features. If any of these are desired, please submit a PR for the parts you need!
Implementations
sourceimpl<T> Vec<T>
impl<T> Vec<T>
sourcepub fn new(first: T) -> Self
pub fn new(first: T) -> Self
Efficiently constructs a new instance with a single item.
The underlying std::vec::Vec
does not allocate unless more items are pushed.
Examples
let vec: unempty::Vec<usize> = unempty::Vec::new(1);
sourcepub fn with_capacity(first: T, capacity: Capacity<1>) -> Self
pub fn with_capacity(first: T, capacity: Capacity<1>) -> Self
Constructs a new instance with a single item and the specified capacity.
Capacity is in two parts: the guaranteed portion of this data structure consumes 1 “capacity”, and the dynamic portion of this data structure consumes the rest (the “additional capacity”).
“Additional capacity” follows the same rules as std::vec::Vec
:
The vector will be able to hold at least additional capacity elements without reallocating. This method is allowed to allocate for more elements than capacity. If additional capacity is 0, the vector will not allocate.
It is important to note that although the returned vector has the minimum additional capacity specified,
the vector will have a length of 1.
For an explanation of the difference between length and capacity, see std::vec::Vec::with_capacity
.
If it is imporant to know the exact allocated capacity, always use the capacity
method after construction.
When T
is a zero-sized type, there will be no allocation and the additional capacity will always be usize::MAX
.
Panics
Panics if the additional capacity exceeds isize::MAX
bytes.
Examples
let v = unempty::Vec::with_capacity("abc", Capacity::new_total(10));
sourcepub fn capacity(&self) -> Capacity<1>
pub fn capacity(&self) -> Capacity<1>
Returns the number of elements the Vec
can hold without reallocating.
Examples
let cap = Capacity::new_total(10);
let v = unempty::Vec::with_capacity("abc", cap);
assert_eq!(v.capacity(), cap);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the data structure, also referred to as its ‘length’. Includes both static and dynamic portions of the data structure.
Examples
let a = unempty::vec![1, 2, 3];
assert_eq!(a.len(), 3);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the vector contains no elements.
This method always returns false
, because by defition an unempty::Vec
cannot be empty.
This method is included for API completeness and to make Clippy happy.
Examples
let mut v = unempty::Vec::new("abcd");
assert!(!v.is_empty());
sourcepub fn pop(self) -> (Option<Self>, T)
pub fn pop(self) -> (Option<Self>, T)
Removes the last element from a vector and returns it.
If you’d like to pop the first element, consider using VecDeque::pop_front
instead.
Consuming self
Since this method may pop when there is only one item in the vector, it consumes the vector and optionally returns the vector with its new size.
This is necessary because if the vector is popped when there is only one item,
the “non-empty” guarantee of unempty::Vec
is no longer possible.
If this API is not desired, convert the instance to a std::vec::Vec
and use that;
std::vec::Vec
has no such guarantee.
Examples
let vec = unempty::vec![1, 2];
let (vec, last) = vec.pop();
assert_eq!(last, 2);
assert_eq!(vec, Some(unempty::vec![1]));
let (vec, last) = vec.expect("already tested").pop();
assert_eq!(last, 1);
assert_eq!(vec, None);
Trait Implementations
sourceimpl<T> Extend<T> for Vec<T>
impl<T> Extend<T> for Vec<T>
sourcefn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T: PartialEq> PartialEq<Vec<T>> for Vec<T>
impl<T: PartialEq> PartialEq<Vec<T>> for Vec<T>
impl<T: Eq> Eq for Vec<T>
impl<T> StructuralEq for Vec<T>
impl<T> StructuralPartialEq for Vec<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Vec<T> where
T: RefUnwindSafe,
impl<T> Send for Vec<T> where
T: Send,
impl<T> Sync for Vec<T> where
T: Sync,
impl<T> Unpin for Vec<T> where
T: Unpin,
impl<T> UnwindSafe for Vec<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more