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
56 stars 16 forks source link

short functions which return fixed types #26

Closed burrbull closed 2 years ago

burrbull commented 2 years ago

Now we have 2 main ways to create value

  1. DurarionType::from_ticks(val) - fixed type result, can be used in any context, but long call
  2. Ext traits: 1.millis() - generic type result, non-const. Can fail to resolve result type if information is not enough.

Need short variant of 1 which looks similar to 2. cc @korken89

TDHolmes commented 2 years ago

Can you elaborate? I'm not sure I fully understand the request.

burrbull commented 2 years ago

We have ExtU32 which give us millis<NOM, DEMOM>() -> Duration<NOM, DEMOM>. And in most cases it works very well. But often it can't detect expected coefs. In those cases simple millis() -> MillisDuration would work better. Example:

let time: MillisDurationU32 = ...;
if time > 5.millis() { // <- error here: unknown type
   ...
}

It would be ideal if Rust could default types in trait methods, but it can't even in nightly.

trait ExtU32 {
   fn millis<const NOM: u32 = 1, const DENOM: u32 = 1_000>() -> DurationU32<NOM, DEMOM>;
}
korken89 commented 2 years ago

const defaults comes in the next stable if I'm not mistaken, this should be fixable then without adding something new.

korken89 commented 2 years ago

It seems my hopes were too high for the new const generics defaults, the currently release version is too crippled and one only runs into this wherever one would like to use it:

error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
   --> src/duration.rs:684:76
    |
684 | impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32 = L_NOM, const R_DENOM: u32 = L_DENOM>
    |                                                                            ^^^^^^^

So the questions then is, how to tackle this issue. For now does this work? @burrbull

let millis = DurarionType::millis(val);

This is what the trait leans on to get its generic superpowers (it just takes it a step extra and tries to find DurationType automatically, however this can fail as you have seen). But it should work just fine by itself and it gets its generic parameters from the DurationType.

burrbull commented 2 years ago

I don't see good solve for now.

And yes, now I use Hertz::MHz(val), etc. It is not ideal, but better then nothing.