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

Refactor builders to be const #172

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Refactor builders to be const #172

wants to merge 1 commit into from

Conversation

Matt3o12
Copy link

Hello thank you for your work.

I have made changes so that all builders are now const. They should still be 100% backwards compadible. I had to introduce a constructor, new, as Default cannot be const just yet.

This allows me to define a const builder at the top and use it like this:

    const BACKOFF_BUILDER: ExponentialBuilder = ExponentialBuilder::new().with_min_delay(Duration::from_millis(1));

    fn test() {
        my_fn().retry(BACKOFF_BUILDER).call();
    }

All changes:

  • Refactored all methods inside FibonacciBuilder, ExponentialBuilder, and ConstantBuilder to be const.
  • Added a new const new constructor for each builder which uses the values from default()
  • Updated the default method to call new().
  • Added unit tests for the const builders to ensure they have the correct values.

@Matt3o12
Copy link
Author

It seems like debug_asserts are causing problems with the MSRV:

    /// Set the factor for the backoff.
    ///
    /// # Panics
    ///
    /// This function will panic if the input factor is less than `1.0`.
    pub const fn with_factor(mut self, factor: f32) -> Self {
        debug_assert!(factor >= 1.0, "invalid factor that lower than 1");

        self.factor = factor;
        self
    }

When do you plan to bump the MSRV or do you have an idea how to fix that in the meantime? :)

- Refactored all methods inside FibonacciBuilder, ExponentialBuilder, and ConstantBuilder to be const.
- Added a new const `new` constructor for each builder which uses the values from default()
- Updated the default method to call `new()`.
- Added unit tests for the const builders to ensure they have the correct values.
@Xuanwo
Copy link
Owner

Xuanwo commented Dec 18, 2024

Hi @Matt3o12, thank you for your work first! I believe we can remove that debug_assert. Would you like to update the documentation for with_factor to emphasize the importance of ensuring that factor >= 1?

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.

2 participants