diff --git a/src/log/lvl.rs b/src/log/lvl.rs index 2419312..1e60d5f 100644 --- a/src/log/lvl.rs +++ b/src/log/lvl.rs @@ -52,7 +52,7 @@ impl LogLvl { "fatal" | "1" => Some(LogLvl::Fatal), "error" | "2" => Some(LogLvl::Error), "warn" | "3" => Some(LogLvl::Warn), - "info" | "4" => Some(LogLvl::Info), + "info" | "4" => Some(LogLvl::Info), "debug" | "5" => Some(LogLvl::Debug), "all" | "6" | "" | "*" => Some(LogLvl::All), _ => None, diff --git a/src/log/mod.rs b/src/log/mod.rs index dc5a5a9..54a349b 100644 --- a/src/log/mod.rs +++ b/src/log/mod.rs @@ -37,7 +37,7 @@ impl Logger { pub fn set_info_all() { Self::initialized_set(true); Self::enable_set(true); - let mut logger = LOGGER.as_mut().unwrap(); + let mut logger = LOGGER.as_mut(); logger.mod_paths.insert("*".to_string()); logger.max_lvl = LogLvl::Info; } @@ -50,29 +50,32 @@ impl Logger { Self::enable_set(false); } pub fn initialized() -> bool { - LOGGER.get().initialized.load(Ordering::Relaxed) + LOGGER.as_ref().initialized.load(Ordering::Relaxed) } pub fn initialized_set(b: bool) { - LOGGER.get().initialized.store(b, Ordering::SeqCst); + LOGGER.as_ref().initialized.store(b, Ordering::SeqCst); } pub fn enable() -> bool { - LOGGER.get().enabled.load(Ordering::Relaxed) + LOGGER.as_ref().enabled.load(Ordering::Relaxed) } pub fn enable_set(b: bool) { - LOGGER.get().enabled.store(b, Ordering::SeqCst); + LOGGER.as_ref().enabled.store(b, Ordering::SeqCst); } pub fn without_cli_options() -> bool { - LOGGER.get().without_cli_options.load(Ordering::Relaxed) + LOGGER.as_ref().without_cli_options.load(Ordering::Relaxed) } pub fn without_cli_options_set(b: bool) { - LOGGER.get().without_cli_options.store(b, Ordering::SeqCst); + LOGGER + .as_ref() + .without_cli_options + .store(b, Ordering::SeqCst); } pub fn max_lvl() -> &'static LogLvl { - &LOGGER.get().max_lvl + &LOGGER.as_ref().max_lvl } /// `mod_path[s]` pub fn mps() -> Iter<'static, String> { - LOGGER.get().mod_paths.iter() + LOGGER.as_ref().mod_paths.iter() } /// The current time in the local timezone pub fn now() -> Tm { @@ -131,7 +134,7 @@ impl Logger { // no_input -> info/* // / -> all/pkg // lvl/mods -> lvl/mods - let mut logger = LOGGER.as_mut().unwrap(); + let mut logger = LOGGER.as_mut(); // avoid init second if logger.initialized.load(Ordering::Relaxed) { return; @@ -180,7 +183,7 @@ impl Logger { } ///Log message occurs at current module and current LogLvl whether need output pub fn filter(mod_path: &str, lvl: LogLvl) -> bool { - let logger = LOGGER.get(); + let logger = LOGGER.as_ref(); // println!("LOGER::filter(mp: {:?},lvl: {:?}): {:?}", // mod_path, // lvl, @@ -300,11 +303,11 @@ impl LogFmter { return; } LogFmterInitialized.store(true, Ordering::SeqCst); - LogFmterDefault.set(f.into()).unwrap() + LogFmterDefault.set(f.into()) } /// Format `&LogMesg` by `LogFmter` pub fn call(msg: &LogMsg) -> String { - (LogFmterDefault.get().0)(msg) + (LogFmterDefault.as_ref().0)(msg) } } diff --git a/src/static_mut.rs b/src/static_mut.rs index 0e82eca..fd36e61 100644 --- a/src/static_mut.rs +++ b/src/static_mut.rs @@ -32,13 +32,13 @@ fn main() { }; // But when write it, operate it Concurrent is unsafe. { - STRING.set(str).unwrap(); // update by setting value - USIZE.as_mut().unwrap().0 = 123; // update by modifying field + STRING.set(str); // update by setting value + USIZE.as_mut().0 = 123; // update by modifying field } // After write, You can read it Concurrent safely. - println!("{:?}", STRING.get()); - println!("{:?}", USIZE.get()); + println!("{:?}", STRING.as_ref()); + println!("{:?}", USIZE.as_ref()); } #[derive(Debug)] @@ -62,30 +62,31 @@ use std::cell::UnsafeCell; unsafe impl Sync for StaticMut {} impl StaticMut { + #[inline] pub fn new(value: T) -> Self { StaticMut(UnsafeCell::new(value)) } /// read it #[inline] #[allow(unknown_lints,should_implement_trait)] - pub fn as_ref(&self) -> Option<&T> { - unsafe { self.0.get().as_ref() } - } - /// read it with `unwrap()` - #[inline] - pub fn get(&self) -> &T { - self.as_ref().unwrap() + pub fn as_ref(&self) -> &T { + unsafe { self.0.get().as_ref().unwrap() } } /// write it #[allow(unknown_lints,mut_from_ref)] #[inline] - pub fn as_mut(&self) ->Option<&mut T> { - unsafe { self.0.get().as_mut() } + pub fn as_mut(&self) -> &mut T { + unsafe { self.0.get().as_mut().unwrap() } + } + /// update it + #[inline] + pub fn set(&self, value:T) { + *self.as_mut() = value } - /// write it(returns value `is_some()` if update success) + ///Unwraps the value #[inline] - pub fn set(&self, value:T)-> Option<()> { - self.as_mut().map(|v|*v =value) + pub fn into_inner(self)->T { + unsafe {self.0.into_inner()} } }