Skip to content

Commit

Permalink
refactor away some unsafe usages
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Mar 7, 2024
1 parent 41af9bd commit 384613e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/pathlang/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, U> = IResult<T, U, VerboseError<T>>;

Expand Down Expand Up @@ -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()))
Expand Down
10 changes: 6 additions & 4 deletions src/utils/log.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)*))
}
);
Expand All @@ -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)
}
Expand All @@ -30,5 +32,5 @@ macro_rules! debug_err {
}

pub fn set_verbose(flag: bool) {
unsafe { VERBOSE = flag }
VERBOSE.store(flag, Ordering::SeqCst);
}

0 comments on commit 384613e

Please sign in to comment.