pub struct UniVec(/* private fields */);
Expand description
A dynamic vector that can store elements of any single type.
Implementations§
Source§impl UniVec
impl UniVec
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new empty UniVec
. A new UniVec
is empty and has no type associated to it,
the type becomes associated when adding the first data to it.
§Example
let univec = UniVec::new();
assert!(univec.is_empty());
Sourcepub fn with_capacity<T: 'static>(capacity: usize) -> Self
pub fn with_capacity<T: 'static>(capacity: usize) -> Self
Sourcepub fn try_get<T: 'static>(
&self,
index: impl IntoIndex,
) -> Result<Option<&T>, TypeError>
pub fn try_get<T: 'static>( &self, index: impl IntoIndex, ) -> Result<Option<&T>, TypeError>
Try to get a reference to an element of type T
by index.
§Arguments
index
- The index of the element to retrieve.
§Returns
Returns Ok(Some(&T))
if the element is found, Ok(None)
when the index is out of
range. This includes indices that can’t be converted to usize
.
§Errors
Returns Err(TypeError)
when this UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let element = univec.try_get::<i32>(0).unwrap();
assert_eq!(element, Some(&42));
Sourcepub fn get<T: 'static>(&self, index: impl IntoIndex) -> Option<&T>
pub fn get<T: 'static>(&self, index: impl IntoIndex) -> Option<&T>
Get a reference to an element of type T
by index. This resembles the the Vec::get
method.
§Arguments
index
- The index of the element to retrieve.
§Returns
Returns Some(&T)
it is found and None
when the index is out of range.
This includes indices that can’t be converted to usize
.
§Panics
Panics if this UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let element = univec.get::<i32>(0).unwrap();
assert_eq!(element, &42);
Sourcepub fn try_get_mut<T: 'static>(
&mut self,
index: impl IntoIndex,
) -> Result<Option<&mut T>, TypeError>
pub fn try_get_mut<T: 'static>( &mut self, index: impl IntoIndex, ) -> Result<Option<&mut T>, TypeError>
Try to get a mutable reference to an element of type T
by index.
§Arguments
index
- The index of the element to retrieve.
§Returns
Returns Ok(Some(&mut T))
if the element is found, Ok(None) when the index is out of
range. This includes indices that can’t be converted to usize
.
§Errors
Returns Err(TypeError)
when this UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let mut element = univec.try_get_mut::<i32>(0).unwrap();
if let Some(elem) = element {
*elem += 10;
}
assert_eq!(univec.try_get::<i32>(0).unwrap(), Some(&52));
Sourcepub fn get_mut<T: 'static>(&mut self, index: impl IntoIndex) -> Option<&mut T>
pub fn get_mut<T: 'static>(&mut self, index: impl IntoIndex) -> Option<&mut T>
Get a mutable reference to an element of type T
by index. Will associate Self
with
a empty Vec<T>
when the UniVec
is not associated to a type.
§Arguments
index
- The index of the element to retrieve.
§Returns
Returns Some(&mut T)
it is found and None
when the index is out of range.
This includes indices that can’t be converted to usize
.
§Panics
Panics if this UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let mut element = univec.get_mut::<i32>(0).unwrap();
*element += 10;
assert_eq!(univec.get::<i32>(0), Some(&52));
Sourcepub fn ensure<T: 'static>(&mut self, capacity: usize) -> Result<(), TypeError>
pub fn ensure<T: 'static>(&mut self, capacity: usize) -> Result<(), TypeError>
Ensures that the UniVec
stores T's
. Possibly associate the requested type and
reserving the specified capacity.
§Arguments
capacity
- The capacity to reserve.
§Returns
Returns Ok(())
when the UniVec
was empty or T
was already is associated.
The requested capacity is reserved then.
§Errors
Returns Err(TypeError)
when this UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.ensure::<i32>(10).unwrap();
assert!(univec.capacity() >= 10);
assert_eq!(univec.associated_type(), Some(std::any::TypeId::of::<i32>()));
Sourcepub fn push<T: 'static>(&mut self, value: T) -> Result<(), TypeErrorValue<T>>
pub fn push<T: 'static>(&mut self, value: T) -> Result<(), TypeErrorValue<T>>
Pushes a value of type T
onto a UniVec
.
§Arguments
value
- AT
to push onto self.
§Returns
Returns Ok(())
when the value was successfully pushed.
§Errors
Returns Err(TypeErrorValue(value))
when this UniVec
can not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let mut element = univec.get_mut::<i32>(0);
if let Some(elem) = element {
*elem += 10;
}
assert_eq!(univec.get::<i32>(0), Some(&52));
Sourcepub fn pop<T: 'static>(&mut self) -> Result<Option<T>, TypeError>
pub fn pop<T: 'static>(&mut self) -> Result<Option<T>, TypeError>
Pops a value of type T
from a UniVec
.
§Returns
Returns Ok(Some(T))
with the value is successfully popped.
Returns Ok(None)
when the univec is empty.
§Errors
Returns Err(TypeError)
when the univec does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let popped = univec.pop().unwrap();
assert_eq!(popped, Some(42));
assert!(univec.is_empty());
Sourcepub fn drop_last(&mut self) -> bool
pub fn drop_last(&mut self) -> bool
Drops the last value from a UniVec
. This is more efficient than the other pop
variants when the value is not required anymore.
§Returns
Returns true
if a value is successfully dropped, otherwise returns false
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
assert!(univec.drop_last());
assert!(univec.is_empty());
Sourcepub fn insert<T: 'static>(
&mut self,
index: impl IntoIndex,
value: T,
) -> Result<(), TypeErrorValue<T>>
pub fn insert<T: 'static>( &mut self, index: impl IntoIndex, value: T, ) -> Result<(), TypeErrorValue<T>>
Inserts a value of type T
into the UniVec
at the specified index.
§Arguments
index
- The index at which to insert the value.value
- A reference to the value of typeT
to insert into the vector.
§Errors
Returns Err(TypeErrorValue(value))
when this UniVec
does not store T's
.
§Panics
Panics if the index is out of bounds.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
univec.insert(0, 10_i32).unwrap();
assert_eq!(univec.get::<i32>(0), Some(&10));
assert_eq!(univec.get::<i32>(1), Some(&42));
Sourcepub fn remove<T: 'static>(
&mut self,
index: impl IntoIndex,
) -> Result<T, TypeError>
pub fn remove<T: 'static>( &mut self, index: impl IntoIndex, ) -> Result<T, TypeError>
Removes a value of type T
from an UniVec
at the specified index.
§Arguments
index
- The index of the element to remove.
§Panics
Panics if index is out of bounds.
§Returns
Returns Ok(T)
if a value is successfully removed.
§Errors
Returns Err(TypeError)
when this UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let removed = univec.remove::<i32>(0).unwrap();
assert_eq!(removed, 42);
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted in the given
UniVec
. The collection may reserve more space to avoid frequent reallocations. Note
that reserve()
will not reserve elements on a UniVec
that has no type associated,
use UniVec::with_capacity()
to initialize a Univec
or UniVec::ensure()
to
associate a type while reserving some elements.
§Arguments
additional
- The number of additional elements to reserve space for.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
univec.reserve(10);
assert!(univec.capacity() >= 11);
Sourcepub fn replace<T: 'static>(
&mut self,
index: impl IntoIndex,
value: T,
) -> Result<T, TypeErrorValue<T>>
pub fn replace<T: 'static>( &mut self, index: impl IntoIndex, value: T, ) -> Result<T, TypeErrorValue<T>>
Replaces the element at the specified index with a new value.
§Arguments
index
- The index of the element to replace.value
- The new value to replace the element with.
§Panics
Panics if index is out of bounds.
§Returns
Returns Ok(T)
with the old value if a value is successfully replaced.
§Errors
Returns Err(TypeErrorValue(value))
when this UniVec
does not store T's
.
§Panics
Panics if index is out of bounds.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let removed = univec.replace(0, 10_i32).unwrap();
assert_eq!(removed, 42);
assert_eq!(univec.get::<i32>(0), Some(&10));
Sourcepub fn try_set_last<T: 'static>(
&mut self,
value: T,
) -> Result<Option<T>, TypeErrorValue<T>>
pub fn try_set_last<T: 'static>( &mut self, value: T, ) -> Result<Option<T>, TypeErrorValue<T>>
Tries to sets the last element of an UniVec
to a new value. When the UniVec
is
empty or not associated to a type, an initial value is pushed.
§Arguments
value
- the new value to replace the last element with.
§Returns
Returns Ok(Some(T))
with the old last element or Ok(None)
when the univec was
empty.
§Errors
Returns Err(TypeErrorValue(value))
when this UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.try_set_last(10_i32).unwrap();
assert_eq!(univec.get::<i32>(0), Some(&10));
univec.try_set_last(20_i32).unwrap();
assert_eq!(univec.get::<i32>(0), Some(&20));
Sourcepub fn set_last<T: 'static>(&mut self, value: T) -> Option<T>
pub fn set_last<T: 'static>(&mut self, value: T) -> Option<T>
Sets the last element of an UniVec
to a new value. When the UniVec
is
empty or not associated to a type, an initial value is pushed.
§Arguments
value
- The new value to replace the last element with.
§Returns
Returns Some(T)
with the old last element or None
when the univec was empty.
§Panics
Panics if this UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.set_last(10_i32);
assert_eq!(univec.get::<i32>(0), Some(&10));
univec.set_last(20_i32);
assert_eq!(univec.get::<i32>(0), Some(&20));
Sourcepub fn associated_type(&self) -> Option<TypeId>
pub fn associated_type(&self) -> Option<TypeId>
Returns an Option<TypeId>
the type is associated to a UniVec
.
§Returns
Returns Some(TypeId)
if a type is associated to this UniVec
, otherwise returns None
.
§Example
let mut univec = UniVec::new();
assert_eq!(univec.associated_type(), None);
univec.push(42_i32);
assert_eq!(univec.associated_type(), Some(std::any::TypeId::of::<i32>()));
Sourcepub fn associated_type_name(&self) -> Option<&'static str>
pub fn associated_type_name(&self) -> Option<&'static str>
Returns the name of the type is associated to a UniVec
.
§Returns
Returns Some('static str)
if a type is associated to this UniVec
, otherwise returns None
.
§Example
let mut univec = UniVec::new();
assert_eq!(univec.associated_type_name(), None);
univec.push(42_i32);
assert_eq!(univec.associated_type_name(), Some("i32"));
Sourcepub fn take<T: 'static>(&mut self) -> Result<Option<Vec<T>>, TypeError>
pub fn take<T: 'static>(&mut self) -> Result<Option<Vec<T>>, TypeError>
Takes the underlying original Vec<T>
out of the UniVec
and returns it.
self
is left non associated and can be used to store a different type.
§Returns
Returns Ok(Some(Vec<T>))
when the underlying vector is successfully taken.
Returns Ok(None)
when self
had no type associated.
§Errors
Returns Err(TypeError)
when the UniVec
does not store T's
.
The UniVec
is left unchanged then.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let vec = univec.take::<i32>().unwrap().unwrap();
assert_eq!(vec, vec![42]);
Sourcepub fn into_inner<T: 'static>(
self,
) -> Result<Option<Vec<T>>, TypeErrorIntoInner>
pub fn into_inner<T: 'static>( self, ) -> Result<Option<Vec<T>>, TypeErrorIntoInner>
Returns the underlying original Vec<T>
of a UniVec
. Consumes the UniVec
.
§Returns
Returns Ok(Some(Vec<T>))
if the underlying vector was successfully taken.
Returns Ok(None)
when the UniVec
was empty.
§Errors
Returns Err(TypeErrorIntoInner(self))
when the UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let vec = univec.into_inner::<i32>().unwrap().unwrap();
assert_eq!(vec, vec![42]);
Sourcepub fn try_as_vec<T: 'static>(&self) -> Result<Option<&Vec<T>>, TypeError>
pub fn try_as_vec<T: 'static>(&self) -> Result<Option<&Vec<T>>, TypeError>
Try to get a reference to the underlying original Vec<T>
of a UniVec
.
§Returns
Returns Ok(Some(&Vec<T>))
when the underlying Vec<T>
is available.
Returns Ok(None)
when the UniVec
is not associated with any type.
§Errors
Returns Err(TypeError)
when the UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let vec = univec.try_as_vec::<i32>().unwrap().unwrap();
assert_eq!(*vec, vec![42]);
Sourcepub fn try_as_vec_mut<T: 'static>(
&mut self,
) -> Result<Option<&mut Vec<T>>, TypeError>
pub fn try_as_vec_mut<T: 'static>( &mut self, ) -> Result<Option<&mut Vec<T>>, TypeError>
Try to get a mutable reference to the underlying original Vec<T>
of a UniVec
.
§Returns
Returns Ok(Some(&mut Vec<T>))
if the underlying Vec<T>
is available.
Returns Ok(None)
when the UniVec
is not associated to a type.
§Errors
Returns Err(TypeError)
when the UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let vec = univec.try_as_vec_mut::<i32>().unwrap().unwrap();
vec.push(10);
assert_eq!(*vec, vec![42, 10]);
Sourcepub fn as_vec_mut<T: 'static>(&mut self) -> &mut Vec<T>
pub fn as_vec_mut<T: 'static>(&mut self) -> &mut Vec<T>
Get a mutable reference to the underlying original Vec<T>
of a UniVec
. Will
associate Self
with a empty Vec<T>
when the UniVec
is not associated to a type.
§Panics
Panics if the UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let vec = univec.as_vec_mut::<i32>();
vec.push(10);
assert_eq!(*vec, vec![42, 10]);
Sourcepub fn try_as_slice<T: 'static>(&self) -> Result<Option<&[T]>, TypeError>
pub fn try_as_slice<T: 'static>(&self) -> Result<Option<&[T]>, TypeError>
Try to get a reference to a slice of underlying original Vec<T>
of a UniVec
.
§Returns
Returns Ok(Some(&[T]))
if the underlying Vec<T>
is available.
Returns Ok(None)
when the UniVec
is not associated to a type.
§Errors
Returns Err(TypeError)
when the UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let slice = univec.try_as_slice::<i32>().unwrap().unwrap();
assert_eq!(slice, &[42]);
Sourcepub fn as_slice<T: 'static>(&self) -> &[T]
pub fn as_slice<T: 'static>(&self) -> &[T]
Get a reference to a slice of underlying original Vec<T>
of a UniVec
.
§Returns
Returns &[T]
if the underlying Vec<T>
is available.
§Panics
Panics if the UniVec
does not store T's
or has no associated type.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let slice = univec.as_slice::<i32>();
assert_eq!(slice, &[42]);
Sourcepub fn try_as_mut_slice<T: 'static>(
&mut self,
) -> Result<Option<&mut [T]>, TypeError>
pub fn try_as_mut_slice<T: 'static>( &mut self, ) -> Result<Option<&mut [T]>, TypeError>
Try to get a mutable reference to a slice of underlying original Vec<T>
of a
UniVec
.
§Returns
Returns Ok(Some(&mut [T]))
if the underlying Vec<T>
is available.
Returns Ok(None)
when the UniVec
is not associated to a type.
§Errors
Returns Err(TypeError)
when the UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let slice = univec.try_as_mut_slice::<i32>().unwrap().unwrap();
slice[0] = 10;
assert_eq!(slice, &[10]);
assert_eq!(univec.get::<i32>(0), Some(&10));
Sourcepub fn as_mut_slice<T: 'static>(&mut self) -> &mut [T]
pub fn as_mut_slice<T: 'static>(&mut self) -> &mut [T]
Get a mutable reference to a slice of underlying original Vec<T>
of a UniVec
.
Will associate Self
with a empty Vec<T>
when the UniVec
is not associated to a
type.
§Returns
Returns the underlying &mut [T]
.
§Panics
Panics if the UniVec
does not store T's
.
§Example
let mut univec = UniVec::new();
univec.push(42_i32);
let slice = univec.as_mut_slice::<i32>();
slice[0] = 10;
assert_eq!(slice, &[10]);
assert_eq!(univec.get::<i32>(0), Some(&10));