use crate::runtime::{Downcast, Either, Origin, Provider, RuntimeResult, TypeHint, Upcast};
impl<'a, T> Downcast<'a> for Option<T>
where
T: Downcast<'a>,
{
#[inline(always)]
fn downcast(origin: Origin, provider: Provider<'a>) -> RuntimeResult<Self> {
match &provider {
Provider::Owned(cell) if cell.is_nil() => return Ok(None),
Provider::Borrowed(cell) if cell.is_nil() => return Ok(None),
_ => (),
}
let inner = <T as Downcast<'a>>::downcast(origin, provider)?;
Ok(Some(inner))
}
#[inline(always)]
fn hint() -> TypeHint {
<T as Downcast<'a>>::hint()
}
}
impl<'a, T> Upcast<'a> for Option<T>
where
T: Upcast<'a>,
{
type Output = Either<(), <T as Upcast<'a>>::Output>;
#[inline(always)]
fn upcast(origin: Origin, this: Self) -> RuntimeResult<Self::Output> {
match this {
Some(inner) => Ok(Either::Right(<T as Upcast<'a>>::upcast(origin, inner)?)),
None => Ok(Either::Left(())),
}
}
#[inline(always)]
fn hint() -> TypeHint {
<T as Upcast<'a>>::hint()
}
}
impl<'a, T> Upcast<'a> for &'a Option<T>
where
&'a T: Upcast<'a>,
{
type Output = Either<(), <&'a T as Upcast<'a>>::Output>;
#[inline(always)]
fn upcast(origin: Origin, this: Self) -> RuntimeResult<Self::Output> {
match this {
Some(inner) => Ok(Either::Right(<&'a T as Upcast<'a>>::upcast(origin, inner)?)),
None => Ok(Either::Left(())),
}
}
#[inline(always)]
fn hint() -> TypeHint {
<&'a T as Upcast<'a>>::hint()
}
}
impl<'a, T> Upcast<'a> for &'a mut Option<T>
where
&'a mut T: Upcast<'a>,
{
type Output = Either<(), <&'a mut T as Upcast<'a>>::Output>;
#[inline(always)]
fn upcast(origin: Origin, this: Self) -> RuntimeResult<Self::Output> {
match this {
Some(inner) => Ok(Either::Right(<&'a mut T as Upcast<'a>>::upcast(
origin, inner,
)?)),
None => Ok(Either::Left(())),
}
}
#[inline(always)]
fn hint() -> TypeHint {
<&'a mut T as Upcast<'a>>::hint()
}
}