mirror of
https://github.com/NixOS/nix
synced 2025-06-25 23:11:16 +02:00
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.
This commit is contained in:
parent
8110b4ebb2
commit
f738cd4d97
6 changed files with 133 additions and 18 deletions
|
@ -4,7 +4,30 @@ mod tarfile;
|
|||
|
||||
pub use error::Error;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn unpack_tarfile(source: foreign::Source, dest_dir: &str) {
|
||||
tarfile::unpack_tarfile(source, dest_dir).unwrap();
|
||||
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,
|
||||
) -> CBox<Result<(), error::CppException>> {
|
||||
CBox::new(tarfile::unpack_tarfile(source, dest_dir).map_err(|err| err.into()))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue