impl-trait-for-tuples icon indicating copy to clipboard operation
impl-trait-for-tuples copied to clipboard

Creating variable bindings inside repetitions

Open Ploppz opened this issue 3 years ago • 4 comments
trafficstars

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?

Ploppz avatar May 04 '22 15:05 Ploppz

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

Isn't this working?

bkchr avatar May 14 '22 07:05 bkchr

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

Ploppz avatar May 14 '22 09:05 Ploppz

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

bkchr avatar May 16 '22 19:05 bkchr

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

Ploppz avatar May 16 '22 20:05 Ploppz