1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// #![doc(html_logo_url = "https://rin.rs/logo.svg")]
// #![doc(html_favicon_url = "https://rin.rs/favicon.ico")]

pub use self::lazy_value::Lazy;
pub use self::value_cache::*;
pub use self::error::Error;
pub use self::error::Result;
pub use self::error::LogErr;

#[cfg(not(target_os="unknown"))]
pub use self::autoloader::AutoLoader;
#[cfg(feature="async")]
pub use self::async_fs::Promise;
#[cfg(not(target_os="unknown"))]
pub mod autoloader;

mod lazy_value;
pub mod idtree;
mod value_cache;
mod error;
#[cfg(feature="async")]
pub mod async_fs;

// time

/// Converts nanoseconds to seconds
pub fn ns_to_s(ns: u64) -> f64{
    (ns as f64) / 1000000000.0
}

/// Converts seconds to nanoseconds
pub fn s_to_ns(s: f64) -> u64{
    (s * 1000000000.0) as u64
}

/// fps from time interval and frames passed
pub fn calc_fps(curr_time: u64, prev_time: u64, frames: u64) -> f64{
    frames as f64 / ns_to_s(curr_time - prev_time)
}



// share

/*/// Trait implemented by classes that wrap some kind of shared pointer allowing
/// for the instances to be owned by several owners instead of one as is the
/// norm in rust
pub trait Share{

    /// returns a copy of the instance, which is actually a reference to an
    /// internal object containing the data
    fn share(&self) -> Self;
}*/
#[macro_export]
macro_rules! to_c_str{
	($string: expr) => (
        {
            use std::ffi::CString;
            CString::new($string).unwrap().as_ptr()
        }
	);
}

#[macro_export]
macro_rules! from_c_str{
	($c_string: expr) => (
        {
            use std::ffi::CStr;
            use std::str;
	        str::from_utf8(CStr::from_ptr($c_string).to_bytes()).unwrap()
        }
	);
}


#[macro_export]
macro_rules! time{
    ($descr: expr, $func: block) => (
        {
            use time;
            let then = time::precise_time_ns() as f32/1000000f32;
            let ret = $func;
            let now = time::precise_time_ns() as f32/1000000f32;
            println!("{} time: {}ms", $descr, now - then);
            ret
        }
    );
}


#[macro_export]
macro_rules! time_ns{
    ($descr: expr, $func: block) => (
        {
            use time;
            let then = time::precise_time_ns();
            let ret = $func;
            let now = time::precise_time_ns();
            println!("{} time: {}ns", $descr, now - then);
            ret
        }
    );
}

#[macro_export]
macro_rules! dont_time{
    ($descr: expr, $func: block) => ( $func );
}

// logging
// TODO: not working
/*pub fn set_log_level(module: &str, log_level: u32){
    os::setenv("RUST_LOG",vec!(module.to_string(), "=".to_string(), log_level.to_string()).concat().as_slice());
}

pub fn enable_logging(module: &str){
    os::setenv("RUST_LOG", module);
}

pub fn disable_logging(){
    os::setenv("RUST_LOG", "");
}*/