Skip to content

Commit

Permalink
Remove unnecessary transmute
Browse files Browse the repository at this point in the history
  • Loading branch information
benfogle committed Nov 4, 2021
1 parent 7150a9a commit be69bd4
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions libbpf-rs/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,12 @@ fn default_callback(lvl: PrintLevel, msg: &str) {
let _ = io::stderr().write(msg.as_bytes());
}

// not allowed to use function pointers in const functions, so it needs to be a macro
macro_rules! to_ptr {
($x:expr) => {
unsafe { std::mem::transmute::<PrintCallback, *mut ()>($x) }
};
}

fn from_ptr(ptr: *mut ()) -> PrintCallback {
unsafe { std::mem::transmute::<*mut (), PrintCallback>(ptr) }
}

// There is no AtomicFnPtr. This is a workaround.
static PRINT_CB: AtomicPtr<()> = AtomicPtr::new(to_ptr!(default_callback));
static PRINT_CB: AtomicPtr<()> = AtomicPtr::new(default_callback as *mut ());

/// libbpf's default cb uses vfprintf's return code...which is ignored everywhere. Mimic that for
/// completeness
Expand Down Expand Up @@ -81,7 +74,7 @@ extern "C" fn outer_print_cb(
///
/// This overrides (and is overridden by) [`ObjectBuilder::debug`]
pub fn set_print(func: PrintCallback) -> PrintCallback {
let cb = to_ptr!(func);
let cb = func as *mut ();
let prev = PRINT_CB.swap(cb, Ordering::Relaxed);
unsafe { libbpf_sys::libbpf_set_print(Some(outer_print_cb)) };
from_ptr(prev)
Expand Down

0 comments on commit be69bd4

Please sign in to comment.