Add basic timing library
This commit is contained in:
@@ -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"]
|
||||
|
||||
|
||||
@@ -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
30
crates/lb/src/time.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user