Skip to content

Commit

Permalink
Impl Writer for tuple (#882)
Browse files Browse the repository at this point in the history
* Impl Writer for tuple

* Format Rust code using rustfmt

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
chrislearn and github-actions[bot] committed Aug 23, 2024
1 parent 2b65ccc commit 59783d5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 61 deletions.
63 changes: 2 additions & 61 deletions crates/core/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,64 +235,5 @@ macro_rules! skipper_tuple_impls {
}
}

macro_rules! __for_each_tuple {
($callback:ident) => {
$callback! {
1 {
(0) -> A,
}
2 {
(0) -> A,
(1) -> B,
}
3 {
(0) -> A,
(1) -> B,
(2) -> C,
}
4 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
}
5 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
(4) -> E,
}
6 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
(4) -> E,
(5) -> F,
}
7 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
(4) -> E,
(5) -> F,
(6) -> G,
}
8 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
(4) -> E,
(5) -> F,
(6) -> G,
(7) -> H,
}
}
};
}

__for_each_tuple!(handler_tuple_impls);
__for_each_tuple!(skipper_tuple_impls);
crate::for_each_tuple!(handler_tuple_impls);
crate::for_each_tuple!(skipper_tuple_impls);
60 changes: 60 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,63 @@ where
self.iter().map(|s| s.clone().into()).collect()
}
}

#[macro_export]
macro_rules! for_each_tuple {
($callback:ident) => {
$callback! {
1 {
(0) -> A,
}
2 {
(0) -> A,
(1) -> B,
}
3 {
(0) -> A,
(1) -> B,
(2) -> C,
}
4 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
}
5 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
(4) -> E,
}
6 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
(4) -> E,
(5) -> F,
}
7 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
(4) -> E,
(5) -> F,
(6) -> G,
}
8 {
(0) -> A,
(1) -> B,
(2) -> C,
(3) -> D,
(4) -> E,
(5) -> F,
(6) -> G,
(7) -> H,
}
}
};
}
20 changes: 20 additions & 0 deletions crates/core/src/writing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ impl Scribe for std::convert::Infallible {
fn render(self, _res: &mut Response) {}
}

macro_rules! writer_tuple_impls {
($(
$Tuple:tt {
$(($idx:tt) -> $T:ident,)+
}
)+) => {$(
#[async_trait::async_trait]
impl<$($T,)+> Writer for ($($T,)+) where $($T: Writer + Send,)+
{
async fn write(self, req: &mut Request, depot: &mut Depot, res: &mut Response) {
$(
self.$idx.write(req, depot, res).await;
)+
}
})+
}
}

crate::for_each_tuple!(writer_tuple_impls);

#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand Down

0 comments on commit 59783d5

Please sign in to comment.