Skip to content
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

Creating variable bindings inside repetitions #10

Open
Ploppz opened this issue May 4, 2022 · 4 comments
Open

Creating variable bindings inside repetitions #10

Ploppz opened this issue May 4, 2022 · 4 comments

Comments

@Ploppz
Copy link

Ploppz commented May 4, 2022

I'm not sure if impl-trait-for-tuples can do what I need.

I need to implement something that looks like https://docs.rs/slog/latest/src/slog/lib.rs.html#1898 but for tuples instead. How far I got:

#[impl_for_tuples(1, 12)]
impl ListOfDrains for Tuple {
    for_tuples!( where #( Tuple: Drain )* );

    for_tuples!( type Ok = ( #( <Tuple as Drain>::Ok ),* ); );
    for_tuples!( type Err = ( #( <Tuple as Drain>::Err ),* ); );
    fn log(&self, record: &Record<'_>, logger_values: &OwnedKVList) -> Result<Self::Ok, Self::Err> {
        for_tuples!(

            match ( #( Tuple.log(record, logger_values) ),* ) {

                ( #(Ok()),* )
            }
            )
    }
}

But in the match block, I'm not sure how to proceed. I would need to create N new variable bindings?
Is this possible, if not, is it in scope? Or is there another way to achieve the same?

@bkchr
Copy link
Owner

bkchr commented May 14, 2022

for_tuples!(
   Ok(( #( Tuple.log(record, logger_value)? ),* ))
)

Isn't this working?

@Ploppz
Copy link
Author

Ploppz commented May 14, 2022

I need something like this code, but generalized for tuples:

        match (res1, res2) {
            (Ok(o1), Ok(o2)) => Ok((o1, o2)),
            (r1, r2) => Err((r1, r2)),
        }

That requires N variables that are bound in the pattern and used in the body

@bkchr
Copy link
Owner

bkchr commented May 16, 2022

How do you ensure that either all succeed or all are failing?

@Ploppz
Copy link
Author

Ploppz commented May 16, 2022

I don't. Either all succeed, or at least one fails, in which case I create an Err with a tuple of Results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants