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

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

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

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

Returns the number of elements in the vector.

👎 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.

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

Returns true if the vector contains no elements.

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

Returns a borrow of this VecAny as a SliceAny.

Returns a mutable borrow of this VecAny as a SliceAnyMut.

Attempts to downcast this VecAny to a concrete vector type.

Errors

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

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.

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

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

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Converts to this type from the input type.

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