use std::any::Any;
pub struct Value {
inner: Box<Any>,
}
impl Value {
pub fn new(value: Box<Any>) -> Self {
Self { inner: value }
}
#[inline]
pub fn is<T: 'static>(&self) -> bool {
self.inner.is::<T>()
}
#[inline]
pub fn get<T: 'static>(&self) -> Option<&T> {
self.inner.downcast_ref::<T>()
}
#[inline]
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.inner.downcast_mut::<T>()
}
#[inline]
#[allow(clippy::wrong_self_convention)]
pub fn into_cloned<T: 'static>(&self) -> Option<T>
where
T: Clone,
{
self.get::<T>().cloned()
}
#[inline]
pub fn unwrap<T: 'static>(&self) -> &T {
self.get::<T>().unwrap()
}
#[inline]
pub fn unwrap_mut<T: 'static>(&mut self) -> &mut T {
self.get_mut::<T>().unwrap()
}
#[inline]
pub fn consume<T: 'static>(self) -> T
where
T: Clone,
{
self.into_cloned::<T>().unwrap()
}
}