[−][src]Struct vec1::Vec1
std::vec::Vec
wrapper which guarantees to have at least 1 element.
Vec1<T>
dereferences to &[T]
and &mut [T]
as functionality
exposed through this can not change the length.
Methods of Vec
which can be called without reducing the length
(e.g. capacity()
, reserve()
) are exposed through wrappers
with the same function signature.
Methods of Vec
which could reduce the length to 0
are implemented with a try_
prefix returning a Result
.
(e.g. try_pop(&self)
, try_truncate()
, etc.).
Methods with returned Option<T>
with None
if the length was 0
(and do not reduce the length) now return T. (e.g. first
,
last
, first_mut
, etc.).
All stable traits and methods implemented on Vec<T>
should also
be implemented on Vec1<T>
(except if they make no sense to implement
due to the len 1 gurantee). Note that some small thinks are still missing
e.g. Vec1
does not implement drain currently as drains generic argument
is R: RangeArgument<usize>
and RangeArgument
is not stable.
Methods
impl<T> Vec1<T>
[src]
pub fn new(first: T) -> Self
[src]
Creates a new Vec1
instance containing a single element.
This is roughly Vec1(vec![first])
.
pub fn from_vec(vec: Vec<T>) -> StdResult<Self, Vec<T>>
[src]
does not work with ?
use Vec1::try_from_vec() instead
Tries to create a Vec1<T>
from a Vec<T>
.
The fact that the input is returned as error if it's empty,
means that it doesn't work well with the ?
operator. It naming
is also semantic sub-optimal as it's not a "from" but "try from"
conversion. Which is why this method is now deprecated. Instead
use try_from_vec
and once TryFrom
is stable it will be possible
to use try_from
, too.
Errors
If the input is empty the input is returned as error.
pub fn try_from_vec(vec: Vec<T>) -> StdResult<Self, Size0Error>
[src]
Tries to create a Vec1<T>
from a normal Vec<T>
.
Errors
This will fail if the input Vec<T>
is empty.
The returned error is a Size0Error
instance, as
such this means the input vector will be dropped if
it's empty. But this is normally fine as it only
happens if the Vec<T>
is empty.
Examples
let vec1 = Vec1::try_from_vec(vec![1u8, 2, 3]) .unwrap(); assert_eq!(vec1, vec![1u8, 2, 3]);
If you need to return a Option<Vec1<T>>
you
can use .ok()
on the returned result:
fn foobar(input: Vec<u8>) -> Option<Vec1<u8>> { let mut res = Vec1::try_from_vec(input).ok()?; for x in res.iter_mut() { *x *= 2; } Some(res) }
pub fn with_capacity(first: T, capacity: usize) -> Self
[src]
Creates a new Vec1
with a given capacity and a given "first" element.
pub fn into_vec(self) -> Vec<T>
[src]
Turns this Vec1
into a Vec
.
pub fn mapped<F, N>(self, map_fn: F) -> Vec1<N> where
F: FnMut(T) -> N,
[src]
F: FnMut(T) -> N,
Create a new Vec1
by consuming self
and mapping each element.
This is useful as it keeps the knowledge that the length is >= 1,
even through the old Vec1
is consumed and turned into an iterator.
Example
let data = vec1![1u8,2,3]; let data = data.mapped(|x|x*2); assert_eq!(data, vec![2,4,6]); // without mapped let data = Vec1::try_from_vec(data.into_iter().map(|x|x*2).collect::<Vec<_>>()).unwrap(); assert_eq!(data, vec![4,8,12]);
pub fn mapped_ref<F, N>(&self, map_fn: F) -> Vec1<N> where
F: FnMut(&T) -> N,
[src]
F: FnMut(&T) -> N,
Create a new Vec1
by mapping references to the elements of self
.
The benefit to this compared to Iterator::map
is that it's known
that the length will still be at least 1 when creating the new Vec1
.
pub fn mapped_mut<F, N>(&mut self, map_fn: F) -> Vec1<N> where
F: FnMut(&mut T) -> N,
[src]
F: FnMut(&mut T) -> N,
Create a new Vec1
by mapping mutable references to the elements of self
.
The benefit to this compared to Iterator::map
is that it's known
that the length will still be at least 1 when creating the new Vec1
.
pub fn try_mapped<F, N, E>(self, map_fn: F) -> Result<Vec1<N>, E> where
F: FnMut(T) -> Result<N, E>,
[src]
F: FnMut(T) -> Result<N, E>,
Create a new Vec1
by consuming self
and mapping each element
to a Result
.
This is useful as it keeps the knowledge that the length is >= 1,
even through the old Vec1
is consumed and turned into an iterator.
Example
let data = vec1![1,2,3]; let data: Result<Vec1<u8>, &'static str> = data.try_mapped(|x| Err("failed")); assert_eq!(data, Err("failed"));
pub fn try_mapped_ref<F, N, E>(&self, map_fn: F) -> Result<Vec1<N>, E> where
F: FnMut(&T) -> Result<N, E>,
[src]
F: FnMut(&T) -> Result<N, E>,
Create a new Vec1
by mapping references to the elements of self
to Result
s.
The benefit to this compared to Iterator::map
is that it's known
that the length will still be at least 1 when creating the new Vec1
.
pub fn try_mapped_mut<F, N, E>(&mut self, map_fn: F) -> Result<Vec1<N>, E> where
F: FnMut(&mut T) -> Result<N, E>,
[src]
F: FnMut(&mut T) -> Result<N, E>,
Create a new Vec1
by mapping mutable references to the elements of
self
to Result
s.
The benefit to this compared to Iterator::map
is that it's known
that the length will still be at least 1 when creating the new Vec1
.
pub fn last(&self) -> &T
[src]
Returns a reference to the last element.
As Vec1
always contains at least one element there is always a last element.
pub fn last_mut(&mut self) -> &mut T
[src]
Returns a mutable reference to the last element.
As Vec1
always contains at least one element there is always a last element.
pub fn first(&self) -> &T
[src]
Returns a reference to the first element.
As Vec1
always contains at least one element there is always a first element.
pub fn first_mut(&mut self) -> &mut T
[src]
Returns a mutable reference to the first element.
As Vec1
always contains at least one element there is always a first element.
pub fn try_truncate(&mut self, len: usize) -> StdResult<(), Size0Error>
[src]
pub fn try_swap_remove(&mut self, index: usize) -> StdResult<T, Size0Error>
[src]
pub fn try_remove(&mut self, index: usize) -> StdResult<T, Size0Error>
[src]
pub fn try_split_off(&mut self, at: usize) -> StdResult<Vec1<T>, Size0Error>
[src]
pub fn dedup_by_key<F, K>(&mut self, key: F) where
F: FnMut(&mut T) -> K,
K: PartialEq<K>,
[src]
F: FnMut(&mut T) -> K,
K: PartialEq<K>,
pub fn dedup_by<F>(&mut self, same_bucket: F) where
F: FnMut(&mut T, &mut T) -> bool,
[src]
F: FnMut(&mut T, &mut T) -> bool,
pub fn try_pop(&mut self) -> StdResult<T, Size0Error>
[src]
Tries to remove the last element from the Vec1
.
Returns an error if the length is currently 1 (so the try_pop
would reduce
the length to 0).
pub fn as_vec(&self) -> &Vec<T>
[src]
impl<T> Vec1<T>
[src]
pub fn reserve(&mut self, additional: usize)
[src]
pub fn reserve_exact(&mut self, additional: usize)
[src]
pub fn shrink_to_fit(&mut self)
[src]
pub fn as_mut_slice(&mut self) -> &mut [T]
[src]
pub fn push(&mut self, value: T)
[src]
pub fn append(&mut self, other: &mut Vec<T>)
[src]
pub fn insert(&mut self, idx: usize, val: T)
[src]
pub fn len(&self) -> usize
[src]
pub fn capacity(&self) -> usize
[src]
pub fn as_slice(&self) -> &[T]
[src]
impl<T> Vec1<T> where
T: Clone,
[src]
T: Clone,
pub fn try_resize(
&mut self,
new_len: usize,
value: T
) -> StdResult<(), Size0Error>
[src]
&mut self,
new_len: usize,
value: T
) -> StdResult<(), Size0Error>
pub fn extend_from_slice(&mut self, other: &[T])
[src]
impl<T> Vec1<T> where
T: PartialEq<T>,
[src]
T: PartialEq<T>,
impl<T> Vec1<T> where
T: PartialEq<T>,
[src]
T: PartialEq<T>,
Trait Implementations
impl<T> Into<Vec<T>> for Vec1<T>
[src]
impl<'a, T> Extend<&'a T> for Vec1<T> where
T: 'a + Copy,
[src]
T: 'a + Copy,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
[src]
I: IntoIterator<Item = &'a T>,
impl<T> Extend<T> for Vec1<T>
[src]
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = T>,
[src]
I: IntoIterator<Item = T>,
impl<T: PartialOrd> PartialOrd<Vec1<T>> for Vec1<T>
[src]
fn partial_cmp(&self, other: &Vec1<T>) -> Option<Ordering>
[src]
fn lt(&self, other: &Vec1<T>) -> bool
[src]
fn le(&self, other: &Vec1<T>) -> bool
[src]
fn gt(&self, other: &Vec1<T>) -> bool
[src]
fn ge(&self, other: &Vec1<T>) -> bool
[src]
impl<T> AsMut<[T]> for Vec1<T>
[src]
impl<T> AsMut<Vec1<T>> for Vec1<T>
[src]
impl<A, B> PartialEq<Vec1<B>> for Vec1<A> where
A: PartialEq<B>,
[src]
A: PartialEq<B>,
fn eq(&self, other: &Vec1<B>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<A, B> PartialEq<B> for Vec1<A> where
Vec<A>: PartialEq<B>,
[src]
Vec<A>: PartialEq<B>,
fn eq(&self, other: &B) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<T> AsRef<[T]> for Vec1<T>
[src]
impl<T> AsRef<Vec<T>> for Vec1<T>
[src]
impl<T> AsRef<Vec1<T>> for Vec1<T>
[src]
impl<T> IntoIterator for Vec1<T>
[src]
type Item = T
The type of the elements being iterated over.
type IntoIter = IntoIter<T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<'a, T> IntoIterator for &'a Vec1<T>
[src]
type Item = &'a T
The type of the elements being iterated over.
type IntoIter = Iter<'a, T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<'a, T> IntoIterator for &'a mut Vec1<T>
[src]
type Item = &'a mut T
The type of the elements being iterated over.
type IntoIter = IterMut<'a, T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<T: Clone> Clone for Vec1<T>
[src]
fn clone(&self) -> Vec1<T>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<T: Ord> Ord for Vec1<T>
[src]
fn cmp(&self, other: &Vec1<T>) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
Compares and returns the minimum of two values. Read more
fn clamp(self, min: Self, max: Self) -> Self
[src]
clamp
)Returns max if self is greater than max, and min if self is less than min. Otherwise this will return self. Panics if min > max. Read more
impl<T: Eq> Eq for Vec1<T>
[src]
impl<T> DerefMut for Vec1<T>
[src]
impl<T: Debug> Debug for Vec1<T>
[src]
impl<T: Hash> Hash for Vec1<T>
[src]
fn hash<__HT: Hasher>(&self, state: &mut __HT)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<T> Deref for Vec1<T>
[src]
impl<T, O, R> Index<R> for Vec1<T> where
Vec<T>: Index<R, Output = O>,
O: ?Sized,
[src]
Vec<T>: Index<R, Output = O>,
O: ?Sized,
impl<T, O, R> IndexMut<R> for Vec1<T> where
Vec<T>: IndexMut<R, Output = O>,
O: ?Sized,
[src]
Vec<T>: IndexMut<R, Output = O>,
O: ?Sized,
impl<T> Borrow<[T]> for Vec1<T>
[src]
impl<T> Borrow<Vec<T>> for Vec1<T>
[src]
impl<T> BorrowMut<[T]> for Vec1<T>
[src]
fn borrow_mut(&mut self) -> &mut [T]
[src]
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
impl<I> IntoIterator for I where
I: Iterator,
[src]
I: Iterator,
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
fn into_iter(self) -> I
[src]
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,