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
|
@ -1,5 +1,30 @@
|
|||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
IOError(std::io::Error),
|
||||
Misc(String),
|
||||
Foreign(libc::c_void), // == std::exception_ptr
|
||||
Foreign(CppException),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
Error::IOError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for CppException {
|
||||
fn from(err: Error) -> Self {
|
||||
match err {
|
||||
Error::Foreign(ex) => ex,
|
||||
Error::Misc(s) => unsafe { make_error(&s) },
|
||||
Error::IOError(err) => unsafe { make_error(&err.to_string()) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct CppException(*const libc::c_void); // == std::exception_ptr*
|
||||
|
||||
extern "C" {
|
||||
fn make_error(s: &str) -> CppException;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue