impl-trait-for-tuples
impl-trait-for-tuples copied to clipboard
Creating variable bindings inside repetitions
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?
for_tuples!(
Ok(( #( Tuple.log(record, logger_value)? ),* ))
)
Isn't this working?
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
How do you ensure that either all succeed or all are failing?
I don't. Either all succeed, or at least one fails, in which case I create an Err with a tuple of Results.