mod log {
use crate::{OnceInit, StaticDefault};
pub trait Logger: Send + Sync {
fn log(&self, msg: &str);
}
pub static LOGGER: OnceInit<dyn Logger> = OnceInit::uninit();
struct DefaultLogger;
impl Logger for DefaultLogger {
fn log(&self, _msg: &str) {
}
}
unsafe impl StaticDefault for dyn Logger {
fn static_default() -> &'static Self {
static NOP: DefaultLogger = DefaultLogger;
&NOP
}
}
}
mod a_logger {
extern crate alloc;
use crate::OnceInitError;
use super::log::{Logger, LOGGER};
pub struct ALogger;
impl Logger for ALogger {
fn log(&self, msg: &str) {
println!("{msg}");
}
}
impl ALogger {
pub fn init() -> Result<(), OnceInitError> {
LOGGER.init_boxed(Box::new(ALogger))
}
}
}
mod hello_world {
use crate::tests::log::LOGGER;
pub fn hello_world() {
LOGGER.log("Hello, world!");
}
}
#[test]
fn test_logger() {
a_logger::ALogger::init().unwrap();
hello_world::hello_world();
}