From 63a9734b46c3bb014122644b64f385f0cc56d28d Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Tue, 2 Jul 2024 20:53:20 -0400 Subject: [PATCH] first commit --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 8 ++++++++ src/lib.rs | 18 ++++++++++++++++++ src/main.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..aae9d01 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "loader" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..4a17ae6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "loader" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..8392a3e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,18 @@ +pub use std::sync::mpsc::TryRecvError; +use std::sync::mpsc::{self, Receiver}; + +pub struct Loader { + rx: Receiver, +} + +impl Loader { + pub fn new T + Send + 'static>(f: F) -> Loader { + let (tx, rx) = mpsc::sync_channel(0); + std::thread::spawn(move || tx.send(f()).unwrap()); + return Loader { rx }; + } + #[inline(always)] + pub fn try_recv(&self) -> Result { + self.rx.try_recv() + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9849edd --- /dev/null +++ b/src/main.rs @@ -0,0 +1,46 @@ +use loader::{Loader, TryRecvError}; +use std::{ + io::{self, stdout, Write}, + num::IntErrorKind, + time::{Duration, Instant}, +}; + +const FPS: u64 = 12; +const NSPF: u64 = ((1.0 / FPS as f64) * 1.0e9) as u64; +const FRAME_DUR: Duration = Duration::from_nanos(NSPF); + +fn main() { + let stdin = io::stdin(); + loop { + print!("How many seconds: "); + stdout().flush().unwrap(); + let mut input = String::new(); + stdin.read_line(&mut input).unwrap(); + let seconds: u64 = match input.strip_suffix("\n").unwrap_or(&input).parse() { + Ok(i) => i, + Err(e) if *e.kind() == IntErrorKind::Empty => break, + Err(e) => { + println!("Error parsing number: {:?}", e.kind()); + continue; + } + }; + + let loader: Loader = Loader::new(move || { + std::thread::sleep(Duration::from_secs(seconds)); + return format!("Hello World {}", seconds); + }); + let start = Instant::now(); + + let mut res = loader.try_recv(); + while res == Err(TryRecvError::Empty) { + print!("\rWaiting... {:.2}s", start.elapsed().as_secs_f32()); + std::io::stdout().flush().unwrap(); + std::thread::sleep(FRAME_DUR); + res = loader.try_recv(); + } + + let elapsed = start.elapsed(); + println!("\nDone! {:.2}s", elapsed.as_secs_f32()); + println!("{}\n", res.unwrap()); + } +}