Skip to content

Commit

Permalink
Some modify about StaticMut
Browse files Browse the repository at this point in the history
  • Loading branch information
biluohc committed Aug 9, 2017
1 parent 54d6e5a commit f03f566
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/log/lvl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 16 additions & 13 deletions src/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down
33 changes: 17 additions & 16 deletions src/static_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -62,30 +62,31 @@ use std::cell::UnsafeCell;
unsafe impl<T> Sync for StaticMut<T> {}

impl<T> StaticMut<T> {
#[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()}
}
}

0 comments on commit f03f566

Please sign in to comment.