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

ZTimer: Input types for sleep{,_extended} functions #112

Open
chrysn opened this issue Aug 23, 2024 · 2 comments
Open

ZTimer: Input types for sleep{,_extended} functions #112

chrysn opened this issue Aug 23, 2024 · 2 comments

Comments

@chrysn
Copy link
Member

chrysn commented Aug 23, 2024

Right now (since #66 cleaned up a prior way more inconsistent state), sleep takes Ticks and sleep_extended takes a Duration.

Generally, either could take different kinds of times, eg. fugit types (as previously suggested in https://gitlab.com/etonomy/riot-wrappers/-/issues/6), possibly constructed from helper functions (sleep_seconds(5), sleep_milliseconds(500)). I think the expectation should be that anything accepted by sleep is convertible at build time, whereas sleep_extended may incur relatively costly transformations that may cause multiple sleeps on overflow.

@chrysn
Copy link
Member Author

chrysn commented Aug 23, 2024

Given fugit allows a lot of constness, we could allow .sleep() to accept ticks or a compatible fugit type. Then, clock_ms.sleep(5.secs()) would should build time converted (but can't be enforced), clock_ms.sleep(Duration::secs(5)) would be too, and clock_ms.sleep(const { Duration::secs(5) }) would make the conversion a build time error (which is otherwise, AIU, a panic inside secs()).

@chrysn chrysn changed the title ZTimer: Input types for _extended function ZTimer: Input types for sleep{,_extended} functions Aug 23, 2024
@chrysn
Copy link
Member Author

chrysn commented Aug 23, 2024

The const doesn't work quite that easily: The type can't be used for easy const construction because there there is also the parameter (u32/u64) which apparently can't be inferred even through a From/Into. Thus, the type needs annotations, and those can't be left incomplete without generic_arg_infer:

#![feature(generic_arg_infer)]
use fugit::ExtU32;

fn main() {
    let t1: T::<100000> = 32.secs().into();
    let t2: T::<100000> = const { fugit::Duration::<u32, _, _>::secs(1000000) }.into();
    println!("Hello, world! {:?}", t1);
    println!("Hello, world! {:?}", t2);
}

#[derive(Debug)]
struct T<const HZ: u32> { ticks: u32 }

impl<const HZ: u32> From<fugit::Duration<u32, 1, HZ>> for T<HZ> {
    fn from(orig: fugit::Duration<u32, 1, HZ>) -> Self {
        Self { ticks: orig.ticks() }
    }
}

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

1 participant