diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2019-09-11 01:15:20 +0200 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2019-11-26 22:07:28 +0100 |
commit | f738cd4d976f4f72159bbcbfa7b451c33f0ea74a (patch) | |
tree | 2c20a500f84c967aec1f74bca4d2383c673e1b62 /nix-rust/src/lib.rs | |
parent | 8110b4ebb29174ecd4b22510da0285abf604b8a7 (diff) |
More Rust FFI adventures
We can now convert Rust Errors to C++ exceptions. At the Rust->C++ FFI
boundary, Result<T, Error> will cause Error to be converted to and
thrown as a C++ exception.
Diffstat (limited to 'nix-rust/src/lib.rs')
-rw-r--r-- | nix-rust/src/lib.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/nix-rust/src/lib.rs b/nix-rust/src/lib.rs index 192ca29e4..b6b0d746d 100644 --- a/nix-rust/src/lib.rs +++ b/nix-rust/src/lib.rs @@ -4,7 +4,30 @@ mod tarfile; pub use error::Error; +pub struct CBox<T> { + ptr: *mut libc::c_void, + phantom: std::marker::PhantomData<T>, +} + +impl<T> CBox<T> { + fn new(t: T) -> Self { + unsafe { + let size = std::mem::size_of::<T>(); + let ptr = libc::malloc(size); + eprintln!("PTR = {:?}, SIZE = {}", ptr, size); + *(ptr as *mut T) = t; // FIXME: probably UB + Self { + ptr, + phantom: std::marker::PhantomData, + } + } + } +} + #[no_mangle] -pub extern "C" fn unpack_tarfile(source: foreign::Source, dest_dir: &str) { - tarfile::unpack_tarfile(source, dest_dir).unwrap(); +pub extern "C" fn unpack_tarfile( + source: foreign::Source, + dest_dir: &str, +) -> CBox<Result<(), error::CppException>> { + CBox::new(tarfile::unpack_tarfile(source, dest_dir).map_err(|err| err.into())) } |