Skip to content

Commit

Permalink
Merge pull request #132 from dtolnay/repr
Browse files Browse the repository at this point in the history
Support round tripping anyhow::Error over C FFI as void*
  • Loading branch information
dtolnay committed Dec 18, 2020
2 parents 2a82468 + 3105a73 commit 1e70b58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -361,6 +361,7 @@ pub use anyhow as format_err;
/// # Ok(())
/// }
/// ```
#[repr(transparent)]
pub struct Error {
inner: ManuallyDrop<Box<ErrorImpl<()>>>,
}
Expand Down
18 changes: 18 additions & 0 deletions tests/test_ffi.rs
@@ -0,0 +1,18 @@
#![deny(improper_ctypes, improper_ctypes_definitions)]

use anyhow::anyhow;

#[no_mangle]
pub extern "C" fn anyhow1(err: anyhow::Error) {
println!("{:?}", err);
}

#[no_mangle]
pub extern "C" fn anyhow2(err: &mut Option<anyhow::Error>) {
*err = Some(anyhow!("ffi error"));
}

#[no_mangle]
pub extern "C" fn anyhow3() -> Option<anyhow::Error> {
Some(anyhow!("ffi error"))
}

0 comments on commit 1e70b58

Please sign in to comment.