use std::cmp;
macro_rules! trace {
( $TRACE:expr, $fmt:expr, $($pargs:expr),* ) => {
if $TRACE {
eprintln!($fmt, $($pargs),*);
}
};
( $TRACE:expr, $fmt:expr ) => {
trace!($TRACE, $fmt, );
};
}
pub(crate) fn indent(i: isize) -> &'static str {
let s = " ";
&s[0..cmp::min(usize::try_from(i).unwrap_or(0), s.len())]
}
macro_rules! tracer {
( $TRACE:expr, $func:expr ) => {
tracer!($TRACE, $func, 0)
};
( $TRACE:expr, $func:expr, $indent:expr ) => {
#[allow(unused_macros)]
macro_rules! t {
( $fmt:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, $fmt) };
( $fmt:expr, $a:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a)) };
( $fmt:expr, $a:expr, $b:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j)) };
( $fmt:expr, $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr, $k:expr ) =>
{ trace!($TRACE, "{}{}: {}", crate::macros::indent($indent), $func, format!($fmt, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k)) };
}
}
}
#[allow(unused_macros)]
macro_rules! time_it {
( $label:expr, $ms_threshold:expr, $body:expr ) => {{
use std::time::{SystemTime, Duration};
struct Timer {
start: SystemTime,
}
impl Drop for Timer {
fn drop(&mut self) {
let elapsed = self.start.elapsed();
if elapsed.clone().unwrap_or(Duration::from_millis($ms_threshold))
>= Duration::from_millis($ms_threshold)
{
if $label.len() > 0 {
eprint!("{}:", $label);
}
eprintln!("{}:{}: {:?}", file!(), line!(), elapsed);
}
}
}
let _start = Timer { start: SystemTime::now() };
$body
}};
( $label:expr, $body:expr ) => {
time_it!($label, 0, $body)
};
( $body:expr ) => {
time_it!("", $body)
};
}
#[cfg(test)]
mod test_time_it {
#[test]
fn time_it() {
let result = time_it!("Some code", 10, {
5
});
assert_eq!(result, 5);
}
}
macro_rules! assert_send_and_sync {
( $x:ty where $( $g:ident$( : $a:path )? $(,)?)*) => {
impl<$( $g ),*> crate::types::Sendable for $x
where $( $g: Send + Sync $( + $a )? ),*
{}
impl<$( $g ),*> crate::types::Syncable for $x
where $( $g: Send + Sync $( + $a )? ),*
{}
};
( $x:ty where $( $g:ident$( : $a:ident $( + $b:ident )* )? $(,)?)*) => {
impl<$( $g ),*> crate::types::Sendable for $x
where $( $g: Send + Sync $( + $a $( + $b )* )? ),*
{}
impl<$( $g ),*> crate::types::Syncable for $x
where $( $g: Send + Sync $( + $a $( + $b )* )? ),*
{}
};
( $x:ty ) => {
impl crate::types::Sendable for $x {}
impl crate::types::Syncable for $x {}
};
}
#[cfg(test)]
#[allow(dead_code)]
mod test_assert_send_and_sync {
pub struct MyStruct {}
assert_send_and_sync!(MyStruct);
pub struct WithLifetime<'a> { _p: std::marker::PhantomData<&'a ()> }
assert_send_and_sync!(WithLifetime<'_>);
pub struct MyWriter<W: std::io::Write> { _p: std::marker::PhantomData<W> }
assert_send_and_sync!(MyWriter<W> where W: std::io::Write);
pub struct MyWriterWithLifetime<'a, C, W: std::io::Write> {
_p: std::marker::PhantomData<&'a (C, W)>,
}
assert_send_and_sync!(MyWriterWithLifetime<'_, C, W> where C, W: std::io::Write);
}
#[allow(unused_macros)]
macro_rules! zero_stack {
($n:literal bytes after running || -> $t:ty $code:block) => {
crate::crypto::mem::zero_stack_after::<$n, _>(
#[inline(never)] || -> $t { $code })
};
($n:literal bytes after running $code:block) => {
crate::crypto::mem::zero_stack_after::<$n, _>(
#[inline(never)] || $code)
};
}