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:
Eelco Dolstra
2019-11-26 22:07:28 +01:00
parent 8110b4ebb2
commit f738cd4d97
6 changed files with 132 additions and 17 deletions
+26 -1
View File
@@ -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;
}