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
impl LTAny
Sourcepub const fn fits<T>() -> bool
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);
}Sourcepub fn new<T>(value: T) -> Selfwhere
T: 'static,
pub fn new<T>(value: T) -> Selfwhere
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);Sourcepub fn from_box<T: 'static>(boxed: Box<T>) -> Self
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]);Sourcepub fn from_any(boxed: Box<dyn Any>) -> Self
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);Sourcepub fn type_id(&self) -> TypeId
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>());Sourcepub fn is<T>(&self) -> boolwhere
T: 'static,
pub fn is<T>(&self) -> boolwhere
T: 'static,
Returns true if the stored value is of type T.
§Example
let a: LTAny = LTAny::new(42u32);
assert!(a.is::<u32>());Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: 'static,
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);Sourcepub fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: 'static,
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);Sourcepub fn downcast<T>(self) -> Result<T, LTAny>where
T: 'static,
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!();
};