use crate::error::StateError;
use crate::state::State;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use core::mem::MaybeUninit;
impl<Q> State<&Q> {
pub fn cloned(&self) -> State<Q>
where
Q: Clone,
{
State(self.0.clone())
}
pub const fn copied(&self) -> State<Q>
where
Q: Copy,
{
State(*self.0)
}
}
impl<Q> State<&mut Q> {
pub fn cloned(&self) -> State<Q>
where
Q: Clone,
{
State(self.0.clone())
}
pub const fn copied(&self) -> State<Q>
where
Q: Copy,
{
State(*self.0)
}
}
impl<Q> State<*const Q> {
pub fn from_ptr(ptr: *const Q) -> Self {
Self(ptr)
}
}
impl<Q> State<*mut Q> {
pub fn from_mut_ptr(ptr: *mut Q) -> Self {
Self(ptr)
}
}
impl<Q> State<MaybeUninit<Q>> {
pub const fn initialized(value: Q) -> Self {
Self(MaybeUninit::new(value))
}
pub const fn uninit() -> Self {
Self(MaybeUninit::uninit())
}
#[allow(clippy::missing_safety_doc)]
pub unsafe fn assume_init(self) -> State<Q> {
State(unsafe { self.value().assume_init() })
}
pub fn is_null(&self) -> bool {
self.get().as_ptr().is_null()
}
pub fn write(&mut self, value: Q) -> &mut Q {
self.get_mut().write(value)
}
}
impl State<()> {
pub const fn empty() -> Self {
Self(())
}
}
impl State<bool> {
pub const fn yes() -> Self {
Self(true)
}
pub const fn no() -> Self {
Self(false)
}
pub fn is_true(&self) -> bool {
self.value()
}
pub fn is_false(&self) -> bool {
!self.value()
}
}
#[cfg(feature = "alloc")]
impl State<Box<dyn core::any::Any>> {
pub fn downcast<Q>(self) -> Result<State<Box<Q>>, StateError>
where
Q: core::any::Any,
{
self.0
.downcast()
.map(State)
.map_err(|_| StateError::DowncastFailure)
}
pub fn downcast_ref<Q>(&self) -> Option<State<&Q>>
where
Q: core::any::Any,
{
self.0.downcast_ref().map(State)
}
pub fn downcast_mut<Q>(&mut self) -> Option<State<&mut Q>>
where
Q: core::any::Any,
{
self.0.downcast_mut().map(State)
}
}
impl<Q> State<Option<Q>> {
pub const fn none() -> Self {
Self(None)
}
pub const fn some(value: Q) -> Self {
Self(Some(value))
}
}