SliceAny

Struct SliceAny 

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

Source§

impl<'a> SliceAny<'a>

Source

pub fn opaque() -> Self

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

pub fn deferred() -> Self

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(&[][..]));
Source§

impl<'a, B: ?Sized> SliceAny<'a, B>

Source

pub const fn len(&self) -> usize

Returns the number of elements in the slice.

Source

pub const fn is_empty(&self) -> bool

Returns true if the slice has a length of 0.

Source

pub fn type_id_of_element(&self) -> TypeId

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

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

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

Trait Implementations§

Source§

impl<'a, B: Debug + ?Sized> Debug for SliceAny<'a, B>

Source§

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

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

impl<'a, B: ?Sized, T: AnyBound<B>> From<&'a [T]> for SliceAny<'a, B>

Source§

fn from(slice: &[T]) -> Self

Converts to this type from the input type.
Source§

impl<'a, B: ?Sized + Send + Sync> Send for SliceAny<'a, B>

Source§

impl<'a, B: ?Sized + Sync> Sync for SliceAny<'a, B>

Auto Trait Implementations§

§

impl<'a, B> Freeze for SliceAny<'a, B>
where B: ?Sized,

§

impl<'a, B> RefUnwindSafe for SliceAny<'a, B>
where B: RefUnwindSafe + ?Sized,

§

impl<'a, B> Unpin for SliceAny<'a, B>
where B: ?Sized,

§

impl<'a, B> UnwindSafe for SliceAny<'a, B>
where B: RefUnwindSafe + ?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 + Sync + Send> for T
where T: Any + Send + Sync,

Source§

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