mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-09-21 00:48:04 +03:00
f738cd4d97
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.
31 lines
669 B
Rust
31 lines
669 B
Rust
#[derive(Debug)]
|
|
pub enum Error {
|
|
IOError(std::io::Error),
|
|
Misc(String),
|
|
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;
|
|
}
|