Skip to content

add default implementations delegating to ticks functionality#4

Open
datdenkikniet wants to merge 2 commits into
tweedegolf:mainfrom
datdenkikniet:more-defaults
Open

add default implementations delegating to ticks functionality#4
datdenkikniet wants to merge 2 commits into
tweedegolf:mainfrom
datdenkikniet:more-defaults

Conversation

@datdenkikniet

Copy link
Copy Markdown

Showing what I mean in #3

Perhaps the division is too expensive, or we should pick a different function as base (i.e. base everything around micros instead, since that is a "more realistic" resolution?)

@datdenkikniet datdenkikniet changed the title add default implementations delegating to nanos functionality add default implementations delegating to ticks functionality May 23, 2026
Having a tickrate of zero seems nonsensical, and gives
rise to some cases where division could lead to div-by-zero.
@diondokter

Copy link
Copy Markdown
Member

I thought about doing this. This is probably alright, but somehow, somewhere I feel like there's something wrong or missing with it. But I can't figure out what. So maybe it's alright?

@datdenkikniet

datdenkikniet commented May 26, 2026

Copy link
Copy Markdown
Author

I feel like there's something wrong or missing with it

When time stuff is involved, I get often get this feeling too :P It really is just this straightforward, though. "Just" some time base conversion stuff.

I think the difficult part about this is that, by having any methods based on some "defined unit of time", we're picking the minimum resolution and maximum value for the time we can operate on.

Additionally, placing the responsibility of converting ns, ms, etc. completely with the Alarm or Timer can be odd, since all of the the time values are "the same thing", just in different time bases.

And thirdly, the u64s represent different things at different times: sometimes it's a Duration, sometimes it's an Instant.

An alternative approach would be to just have some sort of "fugit light", (sorry for bringing it up again :P):

#[derive(Clone, Copy)]
// Fugit equivalent: NanosDurationU64
struct Duration(u64);

impl Duration {
  fn from_nanos(nanos: u64) -> { Self(nanos) }
  fn from_micros(micros: u64) -> { todo!() }
  // Etc. etc.
  fn into_ticks(self, frequency: NonZeroU64) -> u64 { todo!() }
  fn into_ticks_const<FREQ: u64>(&self) -> u64 { todo!() }
}

impl Into<core::time::Duration> for Duration {}

// Fugit equivalent: Instant<u64, 1, 1_000_000_000>
struct Instant(u64);

impl Instant {
  fn from_ticks(ticks: u64, frequency: NonZeroU64) -> Self { todo!() }
}

impl Add<Duration> for Instant {}
impl Sub<Instant> for Instant {}

trait Timer {
  fn now(&self) -> Instant { todo!() }
}

trait Alarm {
  fn wait_until_ticks(&self, ticks: u64) { todo!() }
  fn wait_until(&self, instant: Instant) { todo!() }
}

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