korken89 / fugit

`fugit` provides a comprehensive library of `Duration` and `Instant` for the handling of time in embedded systems, doing all it can at compile time.
Apache License 2.0
55 stars 16 forks source link

Reexport aliases #3

Closed andresv closed 2 years ago

andresv commented 2 years ago

Currently I have something like this:

use fugit::{ExtU32, aliases::MillisDurationU32};

pub struct SysTimer{
    start: fugit::Instant::<u32, 1, 1_000>,
    duration: MillisDurationU32,
}

I would prefer:

pub struct SysTimer{
    start: fugit::Instant::<u32, 1, 1_000>,
    duration: fugit::MillisDurationU32,
}

Actually maybe it makes sense to create aliases also for Instants?

korken89 commented 2 years ago

Good catch, missed that!

korken89 commented 2 years ago

On the other hand, does it make sense? Usually Instants have the speed of hardware timers which are far and spread. While for an API Duration can be used to set the granularity you want in the API. I have no problem adding the aliases, but I don't think Instant would see as much use as Duration.

What are your thoughts? @andresv

korken89 commented 2 years ago

Maybe something like this makes more sense?

/// Alias for instants that come from timers with a specific frequency
pub type TimerInstant<T, const FREQ_HZ: u32> = Instant<T, 1, FREQ_HZ>;

/// Alias for instants that come from timers with a specific frequency (`u32` backing storage)
pub type TimerInstantU32<const FREQ_HZ: u32> = Instant<u32, 1, FREQ_HZ>;

/// Alias for instants that come from timers with a specific frequency (`u64` backing storage)
pub type TimerInstantU64<const FREQ_HZ: u32> = Instant<u64, 1, FREQ_HZ>;
andresv commented 2 years ago
const TIMER_FREQ_HZ: u32 = 1000;

// looking this first time might take some time to figure out what is going on here
Instant::<u32, 1, 1_000>

// I think this is more clear for a reader
TimerInstant::<u32, TIMER_FREQ_HZ>

Of course users can defines these aliases by themselves. Maybe there should be just an example in doc that shows it where it makes sense to define your own aliases. Because indeed Instant can be what ever depending on hardware. Like Timer4Instant64Khz or something. So I think you are right and we do not need ready made aliases for them.

andresv commented 2 years ago

Seems to be fixed now.