Skip to content

Use marker trait pattern to simplify component delegation #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 20, 2023
16 changes: 16 additions & 0 deletions crates/cgp-component/src/macros/delegate_all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[macro_export]
macro_rules! delegate_all {
(
$source_marker:ident,
$source:ty,
$target:ty
$(,)?
) => {
impl<Component> DelegateComponent<Component> for $target
where
Self: $source_marker<Component>,
{
type Delegate = $source;
}
};
}
8 changes: 6 additions & 2 deletions crates/cgp-component/src/macros/delegate_component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#[macro_export]
macro_rules! delegate_component {
( $target:ident $( < $( $param:ident ),* $(,)? > )?;
$name:ty : $forwarded:ty $(,)?
(
$target:ident
$( < $( $param:ident ),* $(,)? > )?
{
$name:ty : $forwarded:ty $(,)?
}
) => {
impl< $( $( $param ),* )* >
$crate::traits::delegate_component::DelegateComponent< $name >
Expand Down
143 changes: 120 additions & 23 deletions crates/cgp-component/src/macros/delegate_components.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,141 @@
#[macro_export]
macro_rules! delegate_components {
( $target:ident $( < $( $param:ident ),* $(,)? > )? ; ) => {
(
#[mark_component( $marker:ident )]
#[mark_delegate( $delegate_marker:ident )]
$target:ident
$( < $( $param:ident ),* $(,)? > )?
{
$( $rest:tt )*
}
) => {
$crate::delegate_components!(
@mark_component( $marker )
@target( $target $( < $( $param ),* > )? )
@body( $( $rest )* )
);

pub trait $marker < Component > {}

$crate::expand_delegate_constraints!(
@target( $target $( < $( $param ),* > )? )
@body( $( $rest )* )
@head_buf(
pub trait $delegate_marker $( < $( $param ),* > )? : Sized
)
@tail_buf(
{}
)
);

$crate::expand_delegate_constraints!(
@target( $target $( < $( $param ),* > )? )
@body( $( $rest )* )
@head_buf(
impl<Components, $( $( $param ),* )? > $delegate_marker $( < $( $param ),* > )? for Components
where
Components: Sized
)
@tail_buf(
{}
)
);
};
( $target:ident $( < $( $param:ident ),* $(,)? > )?;
[ ] : $forwarded:ty
$( , $( $rest:tt )* )?
(
$( #[mark_component( $marker:ident )] )?
$target:ident
$( < $( $param:ident ),* $(,)? > )?
{
$( $rest:tt )*
}
) => {
$(
pub trait $marker < Component > {}
)?

$crate::delegate_components!(
$target $( < $( $param ),* > )* ;
$( $( $rest )* )?
$( @mark_component( $marker ) )?
@target( $target $( < $( $param ),* > )? )
@body( $( $rest )* )
);
};
( $target:ident $( < $( $param:ident ),* $(,)? > )? ;
[ $name:ty $(, $($names:tt)* )?] : $forwarded:ty
$( , $( $rest:tt )* )?
(
$( @mark_component( $marker:ident ) )?
@target(
$target:ident
$( < $( $param:ident ),* $(,)? > )?
)
@body( )
) => {
$crate::delegate_component!(
$target $( < $( $param ),* > )*;
$name : $forwarded
);

};
(
$( @mark_component( $marker:ident ) )?
@target(
$target:ident
$( < $( $param:ident ),* $(,)? > )?
)
@body(
[ ] : $forwarded:ty
$( , $( $rest:tt )* )?
)
) => {
$crate::delegate_components!(
$( @mark_component( $marker ) )?
@target( $target $( < $( $param ),* > )? )
@body(
$( $( $rest )* )?
)
);
};
(
$( @mark_component( $marker:ident ) )?
@target(
$target:ident
$( < $( $param:ident ),* $(,)? > )?
)
@body(
[ $name:ty $(, $($names:tt)* )?] : $forwarded:ty
$( , $( $rest:tt )* )?
)
) => {
$crate::delegate_components!(
$target $( < $( $param ),* > )* ;
[ $( $( $names )* )? ] : $forwarded
$( , $( $rest )* )?
$( @mark_component( $marker ) )?
@target( $target $( < $( $param ),* > )? )
@body(
$name : $forwarded,
[ $( $( $names )* )? ] : $forwarded
$( , $( $rest )* )?
)
);
};
( $target:ident $( < $( $param:ident ),* $(,)? > )? ;
$name:ty : $forwarded:ty
$( , $( $rest:tt )* )?
(
$( @mark_component( $marker:ident ) )?
@target(
$target:ident
$( < $( $param:ident ),* $(,)? > )?
)
@body(
$name:ty : $forwarded:ty
$( , $( $rest:tt )* )?
)
) => {
$crate::delegate_component!(
$target $( < $( $param ),* > )*;
$name : $forwarded
$target
$( < $( $param ),* > )*
{
$name : $forwarded
}
);

$( impl<T> $marker < $name > for T {} )?

$crate::delegate_components!(
$target $( < $( $param ),* > )* ;
$( $( $rest )* )?
$( @mark_component( $marker ) )?
@target( $target $( < $( $param ),* > )? )
@body(
$( $( $rest )* )?
)
);
};
}
71 changes: 71 additions & 0 deletions crates/cgp-component/src/macros/expand_delegate_constraints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#[macro_export]
macro_rules! expand_delegate_constraints {
(
@target( $target:ty )
@body( )
@head_buf( $( $head:tt )* )
@tail_buf( $( $tail:tt )* )
) => {
$( $head )*
$( $tail )*
};
(
@target( $target:ty )
@body(
[ ] : $forwarded:ty
$( , $( $rest:tt )* )?
)
@head_buf( $( $head:tt )* )
@tail_buf( $( $tail:tt )* )
) => {
$crate::expand_delegate_constraints!(
@target( $target )
@body(
$( $( $rest )* )?
)
@head_buf( $( $head )* )
@tail_buf( $( $tail )* )
);
};
(
@target( $target:ty )
@body(
[ $name:ty $(, $($names:tt)* )?] : $forwarded:ty
$( , $( $rest:tt )* )?
)
@head_buf( $( $head:tt )* )
@tail_buf( $( $tail:tt )* )
) => {
$crate::expand_delegate_constraints!(
@target( $target )
@body(
$name : $forwarded,
[ $( $( $names )* )? ] : $forwarded,
$( $( $rest )* )?
)
@head_buf( $( $head )* )
@tail_buf( $( $tail )* )
);
};
(
@target( $target:ty )
@body(
$name:ty : $forwarded:ty
$( , $( $rest:tt )* )?
)
@head_buf( $( $head:tt )* )
@tail_buf( $( $tail:tt )* )
) => {
$crate::expand_delegate_constraints!(
@target( $target )
@body(
$( $( $rest )* )?
)
@head_buf(
$( $head )*
+ $crate::traits::delegate_component::DelegateComponent< $name, Delegate = $target >
)
@tail_buf( $( $tail )* )
);
};
}
2 changes: 2 additions & 0 deletions crates/cgp-component/src/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod delegate_all;
pub mod delegate_component;
pub mod delegate_components;
pub mod expand_delegate_constraints;
3 changes: 2 additions & 1 deletion crates/cgp-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
pub mod prelude;

pub use cgp_component::{
delegate_component, delegate_components, derive_component, DelegateComponent, HasComponents,
delegate_all, delegate_component, delegate_components, derive_component, DelegateComponent,
HasComponents,
};

pub use cgp_async::{async_trait, Async};
Expand Down