Struct UniVec

Source
pub struct UniVec(/* private fields */);
Expand description

A dynamic vector that can store elements of any single type.

Implementations§

Source§

impl UniVec

Source

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

pub fn with_capacity<T: 'static>(capacity: usize) -> Self

Creates a new empty UniVec with the specified capacity and type.

§Arguments

This function requires the ‘turbofish’ notation to associate a type to the UniVec.

  • capacity - The capacity to reserve.
§Example
let univec = UniVec::with_capacity::<i32>(10);
assert!(univec.capacity() >= 10);
Source

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

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

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

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

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

pub fn push<T: 'static>(&mut self, value: T) -> Result<(), TypeErrorValue<T>>

Pushes a value of type T onto a UniVec.

§Arguments
  • value - A T 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));
Source

pub fn pop_any(&mut self) -> Option<Box<dyn Any>>

Pops a dynamic typed value from a UniVec.

§Returns

Returns Some(Box<dyn Any>) if a value is successfully popped. None when the univec was empty.

§Example
let mut univec = UniVec::new();
univec.push(42_i32);

let popped = univec.pop_any().unwrap();

assert_eq!(*popped.downcast::<i32>().unwrap(), 42);
Source

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

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

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 type T 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));
Source

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

pub fn len(&self) -> usize

Returns the length of a UniVec.

§Returns

Returns the length of the UniVec. A UniVec with no type associated is considered empty with length 0.

§Example
let mut univec = UniVec::new();
assert_eq!(univec.len(), 0);

univec.push(42_i32);

assert_eq!(univec.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns whether a UniVec is empty.

§Returns

Returns true when the UniVec is empty, otherwise returns false.

§Example
let univec = UniVec::new();

assert!(univec.is_empty());
Source

pub fn capacity(&self) -> usize

Returns the capacity of a UniVec.

§Returns

Returns the capacity of the UniVec. A UniVec with no type associated is considered empty with capacity 0.

§Example
let mut univec = UniVec::new();
assert_eq!(univec.capacity(), 0);

univec.push(42_i32);

assert!(univec.capacity() >= 1);
Source

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

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

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

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

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

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

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

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

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

pub fn as_vec<T: 'static>(&self) -> &Vec<T>

Get a reference to the underlying original Vec<T> of a UniVec.

§Panics

Panics if the UniVec does not store T's this includes the case when it has no associated type.

§Example
let mut univec = UniVec::new();

univec.push(42_i32);

let vec = univec.as_vec::<i32>();

assert_eq!(*vec, vec![42]);
Source

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

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

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

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

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

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

Trait Implementations§

Source§

impl<T: 'static> At<T> for UniVec

Source§

fn at(&self, index: impl IntoIndex) -> &T

Returns a reference to the element at the given index. Read more
Source§

fn try_at(&self, index: impl IntoIndex) -> Option<&T>

Returns Some(&T) to the element at the given index. Read more
Source§

impl<T: 'static> AtMut<T> for UniVec

Source§

fn at_mut(&mut self, index: impl IntoIndex) -> &mut T

Returns a mutable reference to the element at the given index. Read more
Source§

fn try_at_mut(&mut self, index: impl IntoIndex) -> Option<&mut T>

Returns Some(&mut T) to the element at the given index. Read more
Source§

impl Debug for UniVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for UniVec

Source§

fn default() -> UniVec

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

impl<T> From<Vec<T>> for UniVec
where T: 'static,

Source§

fn from(vec: Vec<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for UniVec

§

impl !RefUnwindSafe for UniVec

§

impl !Send for UniVec

§

impl !Sync for UniVec

§

impl Unpin for UniVec

§

impl !UnwindSafe for UniVec

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.