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

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

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

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

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

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

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

Appends an element to the back of the vector.

Panics

Panics if the new capacity exceeds isize::MAX bytes.

Examples
let mut vec = unempty::vec![1, 2];
vec.push(3);
assert_eq!(vec, unempty::vec![1, 2, 3]);

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

Extends a collection with the contents of an iterator. Read more

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

Extends a collection with exactly one element.

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

Reserves capacity in a collection for the given number of additional elements. Read more

Converts to this type from the input type.

Converts to this type from the input type.

The returned type after indexing.

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

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

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

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

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

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

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.