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

suggestion: support a const initializer #29

Open
sbromling opened this issue Jun 30, 2022 · 1 comment
Open

suggestion: support a const initializer #29

sbromling opened this issue Jun 30, 2022 · 1 comment

Comments

@sbromling
Copy link

Since Durations can be created with const fns, it would be nice if humantime::Duration could also.

It would be ideal if From and Into could be const. Since I don't believe that's possible, perhaps the following added to humantime::Duration:

    pub const fn new(std: StdDuration) -> Self {
        Self(std)
    }

Alternatively, would it be feasible to make the inner StdDuration public (i.e. pub struct Duration(pub StdDuration))? Then the const initializer wouldn't be strictly necessary, and it would give other convenient ways to access the inner duration (i.e. without using the Into or Deref+Copy impls).

I was excited to discover this crate today, because it is a pretty good fit for clap derive-style argument parsing, thanks to the addition of FromStr and Display, plus making the command line fields "unit-less" and less error-prone.

@lilyball
Copy link

Another option is to just move the <Duration as From<StdDuration>>::from method into an inherent impl instead (and then have the trait just call the inherent method), which would have the effect of making a call to Duration::from() become constant.

Personally I like the idea of making the inner field public, because it feels simpler to say Duration(StdDuration::from_secs(2)) instead of Duration::from(StdDuration::from_secs(2)) and there doesn't seem to be any obvious reason why it would ever need a new field, it's just a newtype wrapper and does not impose any constraints on its wrapped value.

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

2 participants