use std::{
pin::Pin,
task::{Context, Poll},
};
use topcoat_core::error::Result;
use crate::{
BoxView, View, ViewFirst, ViewSwap,
internal::{LiveView, MoveView, ScopeView},
};
pub struct Child<'a> {
view: BoxView<'a>,
}
impl<'a> Child<'a> {
#[must_use]
pub fn new(view: impl View + 'a) -> Self {
Self {
view: Box::pin(view),
}
}
}
impl Default for Child<'_> {
fn default() -> Self {
Self::new(())
}
}
impl<'a> From<BoxView<'a>> for Child<'a> {
fn from(view: BoxView<'a>) -> Self {
Self { view }
}
}
macro_rules! child_from_view {
($(#[$doc:meta])* impl<$($param:tt),*> for $ty:ty) => {
$(#[$doc])*
impl<'a, $($param),*> From<$ty> for Child<'a>
where
$ty: View + 'a,
{
fn from(view: $ty) -> Self {
Self::new(view)
}
}
};
}
child_from_view! {
impl<V> for ScopeView<V>
}
child_from_view! {
impl<Fut> for LiveView<Fut>
}
child_from_view! {
impl<Fut> for MoveView<Fut>
}
impl View for Child<'_> {
fn poll_first(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<ViewFirst>> {
self.get_mut().view.as_mut().poll_first(cx)
}
fn poll_swap(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<Option<ViewSwap>>> {
self.get_mut().view.as_mut().poll_swap(cx)
}
}