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 ? and return in the decorated function #4

Merged

Conversation

haxorcize
Copy link
Contributor

So far:

  1. BlockModifier implementation
  2. Named block in the decorated function

Local testing worked. Want to get main author's feedback, to discuss if this is the best approach.
Will require unit tests and added docs.
Perhaps split the block modifier to a separate file.

1. BlockModifier implementation
2. Named block in the decorated function

Anecdotal testing worked, need to write proper unit tests and add docs
false
}

#[retry(BACKOFF_CONFIG, retry_if)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails because the usage of ? on an Option gets modified by the block modifier, looking like below:

let result = 'block: {
    {
        break 'block Some(
            match i32::from_str(int).ok() {  // produces Option
                Ok(val) => val,                         // creates matching code for Result
                Err(err) => break 'block Err(err),
            },
        );
    }
};

@Brendan-Blanchard
Copy link
Owner

I've pushed a test that works for Result<T, E>, and one that fails for Option due to the modifier on try. I think I'm okay with ruling out using this on Option<T>-returning functions with the try operator as a first attempt, but definitely want to get feedback on your usage. At worst there could be separate macros that utilize different modifiers to make this work for now.

My initial thought is it's not possible to detect if the try operator is used on an Option or Result, as even parsing the return value of the function will fail if the user defines a custom type that isn't named Result or Option. Maybe I'm not aware of some darker magic that could work there though...

…on the Error object in the Err arm, to support what happens automatically in a block where anyhow::Error is used
@Brendan-Blanchard
Copy link
Owner

I haven't had much time to explore a full decorator function approach (parsing things like &mut self to this: &mut RealSelfType` and such), but want this to be in a usable-to-most state. Any hesitation with going ahead with your approach and ruling out using ?/Try on Options for now? (I can update docs, tests, etc)

@haxorcize
Copy link
Contributor Author

@Brendan-Blanchard sorry for going AWOL, broke my foot and work has been hectic.
Agree that this seems like a very valid compromise of not working if the retried function doesn't return Result.

I've been using it in my project based on my current branch and it has been great, so I think this is definitely a good step in the right direction.

@Brendan-Blanchard
Copy link
Owner

Wow that's quite a week, I hope you're on the mend now. I've pushed changes to tests and docs that represent how it works now given your changes. Thanks for taking an interest and driving this forward!

@Brendan-Blanchard Brendan-Blanchard merged commit f863131 into Brendan-Blanchard:master May 21, 2024
4 checks passed
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

Successfully merging this pull request may close these issues.

3 participants