VecAny

Struct VecAny 

Source
pub struct VecAny<B: ?Sized = dyn Any + Send + Sync> { /* private fields */ }
Expand description

A type-erased Vec.

Semantically Vec<dyn Any>.

§Examples

use list_any::VecAny;
let data: Vec<u8> = b"hello".to_vec();
let vec_any: VecAny = VecAny::from(data);
let data_returned = vec_any.downcast::<u8>().unwrap();
assert_eq!(data_returned, b"hello");

Implementations§

Source§

impl VecAny

Source

pub fn opaque() -> Self

Create a new, empty, VecAny for which downcasting will always return None.

Source

pub fn deferred() -> Self

Create a new, empty, VecAny with a internal type deferred until the first mutable downcast. Note that, until this type is otherwise downcast, downcast_slice will always succeed.

use list_any::VecAny;
let mut v = VecAny::deferred();
assert_eq!(v.downcast_slice::<f64>(), Some(&[][..]));
assert_eq!(v.downcast_slice_mut::<u32>(), Some(&mut [][..]));
assert_eq!(v.downcast_slice::<f64>(), None);
Source§

impl<B: ?Sized> VecAny<B>

Source

pub fn new<T: AnyBound<B>>() -> Self

Create a new, empty, VecAny with a given internal type.

Source

pub fn len(&self) -> usize

Returns the number of elements in the vector.

Source

pub fn capcaity(&self) -> usize

👎Deprecated since 0.2.1: method name was misspelled, use the correct spelling instead

Returns the number of elements the vector can hold without reallocating.

Source

pub fn capacity(&self) -> usize

Returns the number of elements the vector can hold without reallocating.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

Source

pub fn type_id_of_element(&self) -> TypeId

Returns the TypeId of the elements contained within the vector.

use core::any::TypeId;
use list_any::VecAny;
let data: Vec<u8> = b"hello".to_vec();
let vec_any: VecAny = VecAny::from(data);
assert_eq!(vec_any.type_id_of_element(), TypeId::of::<u8>());
Source

pub fn as_slice_any(&self) -> SliceAny<'_>

Returns a borrow of this VecAny as a SliceAny.

Source

pub fn as_slice_any_mut(&mut self) -> SliceAnyMut<'_>

Returns a mutable borrow of this VecAny as a SliceAnyMut.

Source

pub fn downcast<T: AnyBound<B>>(self) -> Result<Vec<T>, Self>

Attempts to downcast this VecAny to a concrete vector type.

§Errors

Returns self unchanged if the VecAny did not contain elements of type T.

Source

pub fn downcast_mut<T: AnyBound<B>>(&mut self) -> Option<VecAnyGuard<'_, T, B>>

Returns some mutable guard over the original vector if the elements are of type T, or None if they are not.

Leaking this guard (through forget or similar) is safe, however it will have the effect of leaking all of the elements in the vector, and the source VecAny will be left empty.

Source

pub fn downcast_slice<T: AnyBound<B>>(&self) -> Option<&[T]>

Returns some reference to the contained vector as a slice if the contained elements are of type T, or None if they are not.

Source

pub fn downcast_slice_mut<T: AnyBound<B>>(&mut self) -> Option<&mut [T]>

Returns some mutable reference to the contained vector as a slice if the contained elements are of type T, or None if they are not.

Trait Implementations§

Source§

impl<B: Debug + ?Sized> Debug for VecAny<B>

Source§

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

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

impl<B: ?Sized> Drop for VecAny<B>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<B: ?Sized, T: AnyBound<B>> From<Vec<T>> for VecAny<B>

Source§

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

Converts to this type from the input type.
Source§

impl<B: ?Sized + Send> Send for VecAny<B>

Source§

impl<B: ?Sized + Sync> Sync for VecAny<B>

Auto Trait Implementations§

§

impl<B> Freeze for VecAny<B>
where B: ?Sized,

§

impl<B> RefUnwindSafe for VecAny<B>
where B: RefUnwindSafe + ?Sized,

§

impl<B> Unpin for VecAny<B>
where B: Unpin + ?Sized,

§

impl<B> UnwindSafe for VecAny<B>
where B: UnwindSafe + ?Sized,

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.
Source§

impl<T> AnyBound<dyn Any> for T
where T: Any,

Source§

impl<T> AnyBound<dyn Any + Send> for T
where T: Any + Send,

Source§

impl<T> AnyBound<dyn Any + Send + Sync> for T
where T: Any + Send + Sync,

Source§

impl<T> AnyBound<dyn Any + Sync> for T
where T: Any + Sync,