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

Support fallible iteration methods #63

Open
eggyal opened this issue Nov 4, 2023 · 0 comments
Open

Support fallible iteration methods #63

eggyal opened this issue Nov 4, 2023 · 0 comments

Comments

@eggyal
Copy link

eggyal commented Nov 4, 2023

If errors need to be reported during processing, e.g. in the callbacks passed to Structure::each_variant and VariantInfo::construct, one's only real option is to embed compile_error!() macro invocations into the returned TokenStream. However this makes testing a little complex. Would it be possible to provide fallible versions of such methods instead:

impl Structure<'_> {
    pub fn try_each_variant<F, R, E>(&self, f: F) -> Result<TokenStream, E>
    where
        F: FnMut(&VariantInfo<'_>) -> Result<R, E>,
        R: ToTokens,
    { todo!() }
}

and

impl VariantInfo<'_> {
    pub fn try_construct<F, T, E>(&self, func: F) -> Result<TokenStream, E>
    where
        F: FnMut(&Field, usize) -> Result<T, E>,
        T: ToTokens,
    { todo!() }
}

(obviously there would be quite a few such methods across the library).

I think the closest users can currently get is by manually iterating the variants/bindings, collecting into a Result<Vec<_>, _> and then (on the happy path) calling the relevant infallible method that takes from such collected vector—which needlessly allocates and is just a tad tedious.

If this would be acceptable, I'd be happy to submit a PR.

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

1 participant