External Publication
Visit Post

`Instant::PAST_VALUE` constant?

Rust Internals [Unofficial] May 13, 2026
Source

The most common use-case I have is if I'm setting up a struct where, whenever a method gets called, it needs to do something if it hasn't recently enough. So I end up with something like this:

pub struct FooDoer {
    last_bar_timestamp: Instant,
    ..
}

impl FooDoer {
    pub fn do_foo(&mut self) {
        if self.last_bar_timestamp.elapsed() >= Duration::from_secs(1) { self.do_bar(); }
        ..
    }
    fn do_bar(&mut self) {
        self.last_bar_timestamp = Instant::now();
        ..
    }
}

But then, when I go to construct a FooDoer, then I have a few options for how to handle constructing the timestamp:

  1. I can use Instant::now(), which has a syscall and so it's impossible to do it at const-time (and maybe subtract the interval if I need it to happen on the first time).
  2. I can use None to accurately indicate that it hasn't happened yet, but then I have to rewrite everything to handle an option that will only be an option on the first time.
  3. I can use something like the unsafe casting GP mentioned to construct an Instant which I know is valid and in the past, but then I'm going against the stdlib public API and that's liable to be broken on an update.

And also, it often doesn't matter much when exactly the first call to do_bar() happens, so I could get around all of that if I just had a constant which is guaranteed to not be in the future, hence why I suggested just that as the guarantee.

Discussion in the ATmosphere

Loading comments...