#[diagnostic::on_unimplemented(
message = "this receiver does not implement Dispatcher for the requested argument tuple",
note = "use #[dispatch(prefix, (Carrier, ...), Ret)] with compatible typed handlers"
)]
pub trait Dispatcher<Args, Ret> {
fn dispatch_tuple(self, args: Args) -> Ret;
}
use crate::AnyView;
#[doc(hidden)]
#[diagnostic::on_unimplemented(
message = "the first dispatch argument must be AnyView or MutationView"
)]
pub trait DispatchInput {
type Source;
}
impl<'a> DispatchInput for AnyView<'a> {
type Source = crate::match_any_internal::ViewSource<'a>;
}
impl<'a, T> DispatchInput for crate::MutationView<'a, T> {
type Source = crate::match_any_internal::MutationSource<'a>;
}
use std::{borrow::BorrowMut, marker::PhantomData};
#[doc(hidden)]
pub mod marker {
use std::marker::PhantomData;
pub enum FromOwned {}
pub enum FromBorrowed {}
pub struct FromCallback<const N: usize, Shape>(PhantomData<fn() -> Shape>);
pub struct FromTuple<Markers, Args = CopyArgs>(PhantomData<fn() -> (Markers, Args)>);
pub enum CopyArgs {}
pub enum MutArg {}
pub struct Value<T>(PhantomData<fn() -> T>);
pub struct BorrowedObject<T>(PhantomData<fn() -> T>);
pub enum AnyView {}
pub struct Mutation<T = crate::Any>(PhantomData<fn() -> T>);
}
pub trait IntoDispatcher<Args, Ret, M> {
type Target;
type Output: BorrowMut<Self::Target>;
fn into_dispatcher(self) -> Self::Output;
}
#[doc(hidden)]
pub struct DispatchAdapter<F, M> {
input: F,
marker: PhantomData<fn() -> M>,
}
impl<D, Args, Ret> IntoDispatcher<Args, Ret, marker::FromOwned> for D
where
for<'s> &'s mut D: Dispatcher<Args, Ret>,
{
type Target = D;
type Output = D;
#[inline(always)]
fn into_dispatcher(self) -> Self::Output {
self
}
}
impl<'d, D, Args, Ret> IntoDispatcher<Args, Ret, marker::FromBorrowed> for &'d mut D
where
for<'s> &'s mut D: Dispatcher<Args, Ret>,
{
type Target = D;
type Output = &'d mut D;
#[inline(always)]
fn into_dispatcher(self) -> Self::Output {
self
}
}
impl<F, Args, Ret, const N: usize, Shape> IntoDispatcher<Args, Ret, marker::FromCallback<N, Shape>>
for F
where
F: DispatchLink<Args, Ret, marker::FromCallback<N, Shape>>,
{
type Target = DispatchAdapter<F, marker::FromCallback<N, Shape>>;
type Output = Self::Target;
#[inline(always)]
fn into_dispatcher(self) -> Self::Output {
DispatchAdapter {
input: self,
marker: PhantomData,
}
}
}
impl<F, Args, Ret, Markers, Reborrow>
IntoDispatcher<Args, Ret, marker::FromTuple<Markers, Reborrow>> for F
where
F: DispatchLink<Args, Ret, marker::FromTuple<Markers, Reborrow>>,
{
type Target = DispatchAdapter<F, marker::FromTuple<Markers, Reborrow>>;
type Output = Self::Target;
#[inline(always)]
fn into_dispatcher(self) -> Self::Output {
DispatchAdapter {
input: self,
marker: PhantomData,
}
}
}
#[doc(hidden)]
pub trait DispatchLink<Args, Ret, M> {
fn dispatch_link(&mut self, args: Args) -> Ret;
}
impl<F, Args, Ret, M> Dispatcher<Args, Ret> for &mut DispatchAdapter<F, M>
where
F: DispatchLink<Args, Ret, M>,
{
#[inline(always)]
fn dispatch_tuple(self, args: Args) -> Ret {
self.input.dispatch_link(args)
}
}
impl<D, Args, Ret> DispatchLink<Args, Ret, marker::FromOwned> for D
where
for<'s> &'s mut D: Dispatcher<Args, Ret>,
{
#[inline(always)]
fn dispatch_link(&mut self, args: Args) -> Ret {
self.dispatch_tuple(args)
}
}
impl<D, Args, Ret> DispatchLink<Args, Ret, marker::FromBorrowed> for &mut D
where
for<'s> &'s mut D: Dispatcher<Args, Ret>,
{
#[inline(always)]
fn dispatch_link(&mut self, args: Args) -> Ret {
(&mut **self).dispatch_tuple(args)
}
}
#[doc(hidden)]
pub trait CallbackResult<R> {
fn callback_result(self) -> R;
}
macro_rules! callback_prefix {
($n:literal; ($($A:ident:$a:ident),*); ($($B:ident:$b:ident),*)) => {
impl<'v, F, T, O, R, $($A,)*>
DispatchLink<(AnyView<'v>, $($A,)*), Option<R>, marker::FromCallback<$n, marker::Value<T>>> for F
where F: FnMut(T, $($B,)*) -> O, T: crate::type_traits::AnyCompatible,
O: CallbackResult<R>,
{
#[inline(always)]
fn dispatch_link(&mut self, (value, $($a,)*): (AnyView<'v>, $($A,)*)) -> Option<R> {
$(let _ = &$a;)*
let cell = value.as_raw_ffi_any();
let typed = unsafe {
(T::check_any_strict(cell) && T::match_constraints(cell))
.then(|| T::copy_from_any_view_after_check(cell))
};
typed.map(|typed| self(typed, $($b,)*).callback_result())
}
}
impl<'v, F, T, O, R, $($A,)*>
DispatchLink<(AnyView<'v>, $($A,)*), Option<R>, marker::FromCallback<$n, marker::BorrowedObject<T>>> for F
where F: for<'node> FnMut(&'node T, $($B,)*) -> O,
T: crate::type_traits::DowncastRefFromAny + 'static,
O: CallbackResult<R>,
{
#[inline(always)]
fn dispatch_link(&mut self, (value, $($a,)*): (AnyView<'v>, $($A,)*)) -> Option<R> {
$(let _ = &$a;)*
value.downcast_ref::<T>().map(|node| self(node, $($b,)*).callback_result())
}
}
impl<'v, F, O, R, $($A,)*>
DispatchLink<(AnyView<'v>, $($A,)*), Option<R>, marker::FromCallback<$n, marker::AnyView>> for F
where F: FnMut(AnyView<'v>, $($B,)*) -> O, O: CallbackResult<R>,
{
#[inline(always)]
fn dispatch_link(&mut self, (value, $($a,)*): (AnyView<'v>, $($A,)*)) -> Option<R> {
$(let _ = &$a;)*
Some(self(value, $($b,)*).callback_result())
}
}
};
}
callback_prefix!(1; (); ());
callback_prefix!(1; (A1:a1); ());
callback_prefix!(2; (A1:a1); (A1:a1));
callback_prefix!(1; (A1:a1, A2:a2); ());
callback_prefix!(2; (A1:a1, A2:a2); (A1:a1));
callback_prefix!(3; (A1:a1, A2:a2); (A1:a1, A2:a2));
callback_prefix!(1; (A1:a1, A2:a2, A3:a3); ());
callback_prefix!(2; (A1:a1, A2:a2, A3:a3); (A1:a1));
callback_prefix!(3; (A1:a1, A2:a2, A3:a3); (A1:a1, A2:a2));
callback_prefix!(4; (A1:a1, A2:a2, A3:a3); (A1:a1, A2:a2, A3:a3));
macro_rules! mutation_callback_prefix {
($n:literal; ($($A:ident:$a:ident),*); ($($B:ident:$b:ident),*)) => {
impl<'v, F, T, O, R, $($A,)*>
DispatchLink<(crate::MutationView<'v>, $($A,)*), Option<R>, marker::FromCallback<$n, marker::Mutation<T>>> for F
where F: FnMut(crate::MutationView<'v, T>, $($B,)*) -> O,
T: crate::type_traits::ContainerElement, O: CallbackResult<R>,
{
#[inline(always)]
fn dispatch_link(&mut self, (value, $($a,)*): (crate::MutationView<'v>, $($A,)*)) -> Option<R> {
$(let _ = &$a;)*
value.downcast::<T>().ok().map(|typed| self(typed, $($b,)*).callback_result())
}
}
};
}
mutation_callback_prefix!(1; (); ());
mutation_callback_prefix!(1; (A1:a1); ());
mutation_callback_prefix!(2; (A1:a1); (A1:a1));
mutation_callback_prefix!(1; (A1:a1, A2:a2); ());
mutation_callback_prefix!(2; (A1:a1, A2:a2); (A1:a1));
mutation_callback_prefix!(3; (A1:a1, A2:a2); (A1:a1, A2:a2));
mutation_callback_prefix!(1; (A1:a1, A2:a2, A3:a3); ());
mutation_callback_prefix!(2; (A1:a1, A2:a2, A3:a3); (A1:a1));
mutation_callback_prefix!(3; (A1:a1, A2:a2, A3:a3); (A1:a1, A2:a2));
mutation_callback_prefix!(4; (A1:a1, A2:a2, A3:a3); (A1:a1, A2:a2, A3:a3));
macro_rules! dispatch_tuple_links {
($(($F:ident, $M:ident, $idx:tt)),+) => {
impl<Args: Copy, R, $($F, $M,)+>
DispatchLink<Args, Option<R>, marker::FromTuple<($($M,)+)>> for ($($F,)+)
where $($F: DispatchLink<Args, Option<R>, $M>,)+
{
#[inline(always)]
fn dispatch_link(&mut self, args: Args) -> Option<R> {
$(if let Some(result) = self.$idx.dispatch_link(args) { return Some(result); })+
None
}
}
impl<'v, 'c, C, R, $($F, $M,)+>
DispatchLink<(AnyView<'v>, &'c mut C), Option<R>, marker::FromTuple<($($M,)+), marker::MutArg>> for ($($F,)+)
where $(for<'borrow> $F: DispatchLink<(AnyView<'v>, &'borrow mut C), Option<R>, $M>,)+
{
#[inline(always)]
fn dispatch_link(&mut self, (value, context): (AnyView<'v>, &'c mut C)) -> Option<R> {
$(if let Some(result) = self.$idx.dispatch_link((value, &mut *context)) { return Some(result); })+
None
}
}
impl<'v, 'c, C, R, $($F, $M,)+>
DispatchLink<(crate::MutationView<'v>, &'c mut C), Option<R>, marker::FromTuple<($($M,)+), marker::MutArg>> for ($($F,)+)
where $(for<'value, 'borrow> $F: DispatchLink<(crate::MutationView<'value>, &'borrow mut C), Option<R>, $M>,)+
{
#[inline(always)]
fn dispatch_link(&mut self, (mut value, context): (crate::MutationView<'v>, &'c mut C)) -> Option<R> {
$(if let Some(result) = self.$idx.dispatch_link((value.reborrow(), &mut *context)) { return Some(result); })+
None
}
}
};
}
macro_rules! impl_callback_chain_tuple_arities {
($impl_chain:ident) => {
impl_callback_chain_tuple_arities!(
@prefixes $impl_chain;
[];
(F0, M0, 0),
(F1, M1, 1),
(F2, M2, 2),
(F3, M3, 3),
(F4, M4, 4),
(F5, M5, 5),
(F6, M6, 6),
(F7, M7, 7),
(F8, M8, 8),
(F9, M9, 9),
(F10, M10, 10),
(F11, M11, 11)
);
};
(@prefixes $impl_chain:ident; [$($prefix:tt)*];) => {};
(
@prefixes $impl_chain:ident;
[$($prefix:tt)*];
$next:tt $(, $rest:tt)*
) => {
$impl_chain!($($prefix)* $next);
impl_callback_chain_tuple_arities!(
@prefixes $impl_chain;
[$($prefix)* $next,];
$($rest),*
);
};
}
impl_callback_chain_tuple_arities!(dispatch_tuple_links);