Add basic timing library

This commit is contained in:
2025-06-29 18:06:56 +10:00
parent fcdee34b42
commit 7768c5ec56
6 changed files with 57 additions and 13 deletions

View File

@@ -10,6 +10,7 @@ repository.workspace = true
[features]
runtime = ["tokio/rt"]
task = ["tokio/rt", "tokio/time"]
time = []
fs = ["tokio/fs", "dep:walkdir", "dep:globset", "dep:tempfile"]
net = ["tokio/net", "tokio/io-util"]

View File

@@ -9,3 +9,5 @@ pub mod net;
pub mod runtime;
#[cfg(feature = "task")]
pub mod task;
#[cfg(feature = "time")]
pub mod time;

30
crates/lb/src/time.rs Normal file
View File

@@ -0,0 +1,30 @@
use luaffi::{cdef, metatype};
#[cdef(module = "lb:time")]
pub struct lb_timelib;
#[metatype]
impl lb_timelib {
#[new]
extern "Lua-C" fn new() -> Self {
Self
}
pub extern "Lua-C" fn instant() -> lb_instant {
lb_instant::new(std::time::Instant::now())
}
}
#[cdef]
pub struct lb_instant(#[opaque] std::time::Instant);
#[metatype]
impl lb_instant {
fn new(instant: std::time::Instant) -> Self {
Self(instant)
}
pub extern "Lua-C" fn elapsed(&self) -> f64 {
self.0.elapsed().as_secs_f64()
}
}