From 384613e182985844aa01f97aa449080c03b04f15 Mon Sep 17 00:00:00 2001 From: Emanuel Dima Date: Thu, 7 Mar 2024 09:38:42 +0100 Subject: [PATCH] refactor away some unsafe usages --- src/pathlang/parse.rs | 10 +++++----- src/utils/log.rs | 10 ++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/pathlang/parse.rs b/src/pathlang/parse.rs index a301155..5c72e52 100644 --- a/src/pathlang/parse.rs +++ b/src/pathlang/parse.rs @@ -11,7 +11,7 @@ use nom::{ sequence::{delimited, terminated, tuple}, IResult, }; -use std::str::{from_utf8_unchecked, FromStr}; +use std::str::{from_utf8, FromStr}; pub type NomRes = IResult>; @@ -202,10 +202,10 @@ fn path_item_start(input: &str) -> NomRes<&str, char> { context( "path_item_start", alt(( - tag(unsafe { from_utf8_unchecked(&[Relation::Attr as u8]) }), - tag(unsafe { from_utf8_unchecked(&[Relation::Sub as u8]) }), - tag(unsafe { from_utf8_unchecked(&[Relation::Interpretation as u8]) }), - tag(unsafe { from_utf8_unchecked(&[Relation::Field as u8]) }), + tag(from_utf8(&[Relation::Attr as u8]).unwrap()), + tag(from_utf8(&[Relation::Sub as u8]).unwrap()), + tag(from_utf8(&[Relation::Interpretation as u8]).unwrap()), + tag(from_utf8(&[Relation::Field as u8]).unwrap()), )), )(input) .map(|(next_input, res)| (next_input, res.chars().next().unwrap())) diff --git a/src/utils/log.rs b/src/utils/log.rs index fcefad2..470d483 100644 --- a/src/utils/log.rs +++ b/src/utils/log.rs @@ -1,4 +1,6 @@ -pub static mut VERBOSE: bool = false; +use std::sync::atomic::{AtomicBool, Ordering}; + +pub static VERBOSE: AtomicBool = AtomicBool::new(false); #[macro_export] macro_rules! warning { @@ -10,7 +12,7 @@ macro_rules! debug { ( $($arg:tt)* ) => ( - if unsafe{$crate::utils::log::VERBOSE} { + if $crate::utils::log::VERBOSE.load(std::sync::atomic::Ordering::SeqCst) { println!("‣ {}", format!($($arg)*)) } ); @@ -21,7 +23,7 @@ macro_rules! debug_err { ( $arg:expr ) => { - if unsafe { $crate::utils::log::VERBOSE } { + if $crate::utils::log::VERBOSE.load(std::sync::atomic::Ordering::SeqCst) { if $arg.kind != HErrKind::None { println!("‣Error: {:?}", $arg) } @@ -30,5 +32,5 @@ macro_rules! debug_err { } pub fn set_verbose(flag: bool) { - unsafe { VERBOSE = flag } + VERBOSE.store(flag, Ordering::SeqCst); }