LTAny

Struct LTAny 

Source
pub struct LTAny { /* private fields */ }
Expand description

dyn Any with fixed-size inlined storage. Types that fit in the storage are stored without allocation. Types that are too large are boxed. Stored types may not implement Send and Sync. For thread-safe version see TAny.

Implementations§

Source§

impl LTAny

Source

pub const fn fits<T>() -> bool

Returns true if the type T fits and can be stored without allocation. If true, then LTAny::new::<T> is guaranteed to not allocate.

§Example
if LTAny::fits::<u32>() {
  // Guaranteed to not allocate.
  LTAny::new(42u32);
}
Source

pub fn new<T>(value: T) -> Self
where T: 'static,

Construct new LTAny with the given value.

If the type T fits in the storage, it is stored without allocation. Otherwise, it will be boxed.

Type of value may not implement Send or Sync. But LTAny itself does not implement Send and Sync. For Send and Sync container see TAny.

§Example
let mut a: LTAny = LTAny::new(42u32);
 
assert_eq!(a.downcast_ref::<u32>().unwrap(), &42);
Source

pub fn from_box<T: 'static>(boxed: Box<T>) -> Self

Construct new LTAny from the given boxed value.

If type fits in the storage, value will be unboxed. Otherwise it will be stored as boxed, but no allocation will be performed.

Type of value may not implement Send or Sync. But LTAny itself does not implement Send and Sync. For Send and Sync container see TAny.

§Example
let boxed = Box::new([1u32; 42]);
 
// No additional allocation is performed.
let a: LTAny = LTAny::from_box(boxed);
 
assert_eq!(a.downcast_ref::<[u32; 42]>().unwrap(), &[1u32; 42]);
Source

pub fn from_any(boxed: Box<dyn Any>) -> Self

Construct new LTAny with the given boxed any. Since type is unknown, it will be stored as boxed.

Allows Any without Send or Sync. But LTAny itself does not implement Send and Sync. For Send and Sync container see TAny.

§Example
use core::any::Any;
 
let any: Box<dyn Any> = Box::new(42u32);
 
let a: LTAny = LTAny::from_any(any);
 
assert_eq!(a.downcast_ref::<u32>().unwrap(), &42);
Source

pub fn type_id(&self) -> TypeId

Returns the type id of the stored value.

§Example
use core::any::TypeId;
 
let a: LTAny = LTAny::new(42u32);
 
assert_eq!(a.type_id(), TypeId::of::<u32>());
Source

pub fn is<T>(&self) -> bool
where T: 'static,

Returns true if the stored value is of type T.

§Example
let a: LTAny = LTAny::new(42u32);
assert!(a.is::<u32>());
Source

pub fn downcast_ref<T>(&self) -> Option<&T>
where T: 'static,

Returns some reference to the stored value if it is of type T. Otherwise returns none.

§Example
let a: LTAny = LTAny::new(42u32);
 
assert_eq!(a.downcast_ref::<u32>().unwrap(), &42);
Source

pub fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: 'static,

Returns some mutable reference to the stored value if it is of type T. Otherwise returns none.

§Example
let mut a: LTAny = LTAny::new(42u32);
 
assert_eq!(a.downcast_mut::<u32>().unwrap(), &mut 42);
Source

pub fn downcast<T>(self) -> Result<T, LTAny>
where T: 'static,

Returns the stored value if it is of type T. Otherwise return self back.

This will unbox the value if it was stored as boxed.

§Example
let a: LTAny = LTAny::new(42u32);
 
let Ok(a) = a.downcast::<u32>() else {
   panic!();
};
Source

pub unsafe fn downcast_ref_unchecked<T>(&self) -> &T
where T: 'static,

Returns reference to the stored value without type checking.

§Safety

The caller must ensure that the type is correct.

§Example
let a: LTAny = LTAny::new(42u32);
 
unsafe {
   assert_eq!(a.downcast_ref_unchecked::<u32>(), &42);
}
Source

pub unsafe fn downcast_mut_unchecked<T>(&mut self) -> &mut T
where T: 'static,

Returns mutable reference to the stored value without type checking.

§Safety

The caller must ensure that the type is correct.

§Example
let mut a: LTAny = LTAny::new(42u32);
 
unsafe {
  assert_eq!(a.downcast_mut_unchecked::<u32>(), &mut 42);
}
Source

pub unsafe fn downcast_unchecked<T>(self) -> T
where T: 'static,

Returns the stored value without type checking.

This will unbox the value if it was stored as boxed.

§Safety

The caller must ensure that the type is correct.

§Example
let a: LTAny = LTAny::new(42u32);
 
unsafe {
 assert_eq!(a.downcast_unchecked::<u32>(), 42);
}

Trait Implementations§

Source§

impl Drop for LTAny

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<Box<dyn Any>> for LTAny

Source§

fn from(boxed: Box<dyn Any>) -> Self

Converts to this type from the input type.
Source§

impl From<TAny> for LTAny

Source§

fn from(value: TAny) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for LTAny

§

impl RefUnwindSafe for LTAny

§

impl !Send for LTAny

§

impl !Sync for LTAny

§

impl Unpin for LTAny

§

impl UnwindSafe for LTAny

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.