#![doc = include_str!("../README.md")]
#![forbid(rust_2018_idioms, unsafe_op_in_unsafe_fn)]
pub mod init;
pub mod init_big;
#[doc(hidden)]
pub mod sync_unsafe_cell;
#[doc(hidden)]
pub mod token;
#[macro_export]
macro_rules! init {
{
$( #[doc = $($token_doc:tt)*] )*
$token_vis:vis token $token_name:ident;
$( #[doc = $($doc:tt)*] )*
$vis:vis static $name:ident : $type:ty = $init:expr;
} => {
$crate::token! {
$( #[doc = $($token_doc)*] )*
$token_vis token $token_name by init($name : $type);
}
$( #[doc = $($doc)*] )*
$vis static $name : $crate::init::Static<$type, $token_name> =
$crate::init::Static::new(|| $init);
};
}
#[macro_export]
macro_rules! init_big {
{
$( #[doc = $($token_doc:tt)*] )*
$token_vis:vis token $token_name:ident;
$( #[doc = $($doc:tt)*] )*
$vis:vis static $name:ident : $type:ty = $const_init:expr;
init($runtime_init_param_name:ident) { $($runtime_init:tt)+ }
} => {
$crate::token! {
$( #[doc = $($token_doc)*] )*
$token_vis token $token_name by init_big($name : $type);
}
$( #[doc = $($doc)*] )*
$vis static $name : $crate::init_big::Static<$type, $token_name> =
$crate::init_big::Static::new(
$const_init,
|value| {
#[allow(unsafe_code)]
let value = unsafe { $crate::sync_unsafe_cell::SyncUnsafeCell::get_mut(value) };
(|$runtime_init_param_name: &mut $type| { $($runtime_init)+ })(value)
},
);
};
}
#[cfg(doc)]
pub mod example {
init! {
pub token MyToken;
pub static MY_STATIC: i32 = std::env::var("MY_STATIC").unwrap().parse().unwrap();
}
init_big! {
pub token MyBigToken;
pub static MY_BIG_STATIC: i32 = 0;
init(s) {
*s = std::env::var("MY_STATIC").unwrap().parse().unwrap();
}
}
}