onceinit

Crates.ioonceinit
lib.rsonceinit
version
sourcesrc
created_at2024-10-17 09:33:06.346191
updated_at2024-11-07 15:23:54.479776
description仅可设置一次数据的类型。
homepage
repositoryhttps://github.com/worksoup/onceinit
max_upload_size
id1412826
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
汤意工坊 (worksoup)

documentation

https://docs.rs/onceinit

README

OnceInit

#[cfg(test)]
mod tests {

    // log 门面库的类似实现。
    mod log {
        use crate::{
            OnceInit,
            StaticDefault,
        };
        pub trait Logger: Send + Sync {
            fn log(&self, msg: &str);
        }
        pub static LOGGER: OnceInit<dyn Logger> = OnceInit::new();
        
        // 只有 `T` 实现了 `StaticDefault`, `OnceInit<T>` 才会实现 `Deref<Target = T>`.
        struct DefaultLogger;
        impl Logger for DefaultLogger {
            fn log(&self, _msg: &str) {
                // do nothing.
            }
        }
        impl StaticDefault for dyn Logger {
            fn static_default() -> &'static Self {
                static NOP: DefaultLogger = DefaultLogger;
                &NOP
            }
        }
    }
    mod a_logger {
        use crate::OnceInitError;
        // 一个简单的 a_logger crate.
        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.set_boxed_data(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();
    }
}

LICENSE

LICENSE

Commit count: 7

cargo fmt