pub struct SliceAny<'a, B: ?Sized = dyn Any + Send + Sync> { /* private fields */ }
Expand description

A type-erased slice.

Semantically &[dyn Any].

Examples

use list_any::SliceAny;
let data: &[u8] = b"hello";
let slice_any: SliceAny = SliceAny::from(data);
assert_eq!(slice_any.downcast::<u8>(), Some(data));

Implementations

Create a SliceAny with a length of 0, for which downcasting will always return None.

use list_any::SliceAny;
let slice = SliceAny::opaque();
assert_eq!(slice.downcast::<f64>(), None);
assert_eq!(slice.downcast::<u32>(), None);
assert_eq!(slice.downcast::<()>(), None);

Create a SliceAny with a length of 0, for which downcasting will always return Some.

use list_any::SliceAny;
let slice = SliceAny::deferred();
assert_eq!(slice.downcast::<f64>(), Some(&[][..]));
assert_eq!(slice.downcast::<u32>(), Some(&[][..]));
assert_eq!(slice.downcast::<()>(), Some(&[][..]));

Returns the number of elements in the slice.

Returns true if the slice has a length of 0.

Returns the TypeId of the elements contained in this slice.

Examples
use core::any::TypeId;
use list_any::SliceAny;
let data: &[u8] = b"hello";
let slice_any: SliceAny = SliceAny::from(data);
assert_eq!(slice_any.type_id_of_element(), TypeId::of::<u8>());

Returns some reference to the original slice if the elements are of type T, or None if they are not.

Trait Implementations

Formats the value using the given formatter. 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.