diff --git a/Cargo.toml b/Cargo.toml index 2615c98..a209c08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ chacha20stream = {version = "1.0", features=["async"]} openssl = "0.10.33" stackalloc = "1.1.0" pin-project = "1.0.6" -bytes = "0.5.6" \ No newline at end of file +bytes = "0.5.6" +log = {version = "0.4.14", optional=true} diff --git a/src/ext.rs b/src/ext.rs index 5734df0..a923e0b 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -202,6 +202,66 @@ where F: FnOnce(&mut [u8]) -> T } +#[macro_export] macro_rules! lazy_format { + ($msg:literal $($tt:tt)*) => { + { + use ::std::fmt::{self, Write, Formatter}; + use ::std::sync::Mutex; + use ::std::io; + + let pfn = move |fmt| { + write!(fmt, $msg $($tt)*)?; + let mut sfmt = String::new(); + write!(&mut sfmt, $msg $($tt)*)?; + Ok(sfmt) + }; + + enum LazyFormatInner + { + //todo: redo this entire thing + + Pending(F), + Complete(String), + Error(fmt::Error), + Panicked, + } + + struct LazyFormat(Mutex>); + + impl) -> io::Result> fmt::Display for LazyFormat + { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + //todo: redo this entire thing + /* + let mut inner = self.0.lock().unwrap(); + let this = std::mem::replace(inner, LazyFormatInner::Panicked); + //TODO: impl fmt::Write wrapper that multi-writes to 2 outputs + let string = match this { + LazyFormatInner::Pending(func) => func(f), + LazyFormatInner::Complete(string) => write!(f, "{}", string).map(move |_| string), + LazyFormatInner::Error(err) => return Err(err), + LazyFormatInner::Panicked => panic!(), + }; + match string { + Err(err) => { + *inner = LazyFormatInner::Error(err), + }, + }*/ + } + } + } + } +} + +#[cfg(not(feature="log"))] #[macro_export] macro_rules! trace { + ($fmt:literal $($tt:tt)*) => { + { + ((), $($tt)*); + } + } +} + pub mod bytes { use super::*; @@ -216,3 +276,4 @@ pub mod bytes mod slice; pub use slice::*; + diff --git a/src/lib.rs b/src/lib.rs index b13b3e1..295e336 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ #![allow(dead_code)] #[macro_use] extern crate pin_project; +#[cfg(feature="log")] #[macro_use] extern crate log; // Extensions & macros #[macro_use] mod ext;