-
Notifications
You must be signed in to change notification settings - Fork 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
FormatInto object safety - or how can I make a collection of Token Streams? #30
Comments
Interesting. Have you tried to write your code so that you use a If you want to use it multiple times you could also do: I appreciate you reporting on your experience and please continue doing so! This might feed into future choices on API design. I can definitely see how support for trait objects might be legitimately useful. |
Actually looking over the docs of I'd suggest adding those impls ASAP. I'll look closer into this tomorrow. |
Also of you have the time: posting a representative example of how you are trying to use genco would also be helpful. I might be able to suggest a different way to write it. Looking at this repo and how it uses genco might also be helpful! |
Adding the I was able to get my code working by using a for loop calling Overall my experience with genco has been great. No surprises, it is well documented and does what it says. |
Cheers!
So maybe this is helpful. The typical way I'd do that pattern is instead of doing something like this: let mut out = Tokens::new();
for thing in things {
match thing {
Thing::First => quote_in!(out => /* do first*/),
Thing::Second => quote_in!(out => /* do second */),
}
} I'd do: let mut out = Tokens::new();
quote_in! { out =>
$(for thing in things join ($['\n']) => match thing {
Thing::First => /* do first */,
Thing::Second => /* do second */,
})
} And if one of them was a bit more involved, I'd break it out into a function: fn do_third() -> impl FormatInto<Rust> {
quote_fn! {
/* do third */
}
}
quote_in! { out =>
$(for thing in things => match thing {
Thing::First => /* do first */,
Thing::Second => /* do second */,
Thing::Third => $(do_third()),
})
} Note that due to the design of the I hope this helps! |
I don't know what the match cases are at compile time, only after I parse them out of the schema. So I have this: let mut fn_matches = rust::Tokens::new();
for (idx, op) in ops.iter().enumerate() {
let fun = to_snake_case(op.name.as_str());
let input = &op.input;
if input == "()" {
quote_in!{ fn_matches =>
$idx => {
$fun(bucket, key, req)
.and_then($into_resp::into_response).await
},$['\n']
};
}else {
quote_in!{ fn_matches =>
$idx => {
$shapes::$input::from_request(req)
.and_then(|req| $fun(bucket, key, req))
.and_then($into_resp::into_response).await
},$['\n']
};
}
}
``` |
I am creating a custom HTTP handler class from a schema specification which has about 100 routes.
Because of reasons, my main dispatcher is basically a
match
from a usize to an function invocation:e.g.
I want to create a
Vec<Box<dyn FormatInto<Rust>>
of handlers and then iterate through them and create each match block.However, I can't, because
format_into
takesself
It would be nice if there was an alternative to format_into that didn't consume self. Or maybe there is a better overall approach?
What is the recommended way to create a vector of token streams that I can inject into here?
The text was updated successfully, but these errors were encountered: