Skip to content

Commit

Permalink
Merge pull request #58 from RalfJung/abort_on_panic
Browse files Browse the repository at this point in the history
avoid abort_on_panic dependency
  • Loading branch information
yorickpeterse authored Sep 2, 2022
2 parents 3c82cc0 + 57afc1c commit c219779
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 0 additions & 1 deletion libffi-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ edition = "2018"

[dependencies]
libffi-sys = { path = "../libffi-sys-rs", version = "^2.0"}
abort_on_panic = "2.0.0"
libc = "0.2.65"

[features]
Expand Down
23 changes: 21 additions & 2 deletions libffi-rs/src/high/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@
//!
//! Invoking the closure a second time will panic.

use abort_on_panic::abort_on_panic;

pub use crate::middle::{ffi_abi_FFI_DEFAULT_ABI, FfiAbi};

pub mod types;
Expand All @@ -78,6 +76,27 @@ pub use types::{CType, Type};
pub mod call;
pub use call::*;

macro_rules! abort_on_panic {
($msg:literal, $body:expr) => {{
// Aborts when dropped (which will only happen due to an unwinding panic).
struct Bomb;
impl Drop for Bomb {
fn drop(&mut self) {
// We do our best to ignore errors that occur during printing.
// If this panics anyway, that'll still just be a double-panic which leads to abort.
let _ = writeln!(std::io::stderr(), $msg);
std::process::abort();
}
}

let b = Bomb;
// If this panics, `b` will be dropped, triggering the bomb.
$body;
// Defuse the bomb.
std::mem::forget(b);
}};
}

macro_rules! define_closure_mod {
(
$module:ident $cif:ident $fnptr:ident
Expand Down

0 comments on commit c219779

Please sign in to comment.