Skip to content

Decouple C++ exception representation from Rust str #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/result.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use crate::exception::Exception;
use crate::rust_str::RustStr;
use alloc::boxed::Box;
use alloc::string::{String, ToString};
use core::fmt::Display;
use core::ptr;
use core::ptr::{self, NonNull};
use core::result::Result as StdResult;
use core::slice;
use core::str;

#[repr(C)]
#[derive(Copy, Clone)]
struct PtrLen {
ptr: NonNull<u8>,
len: usize,
}

#[repr(C)]
pub union Result {
err: RustStr,
err: PtrLen,
ok: *const u8, // null
}

Expand Down Expand Up @@ -41,7 +47,10 @@ unsafe fn to_c_error(msg: String) -> Result {
let copy = error(ptr, len);
let slice = slice::from_raw_parts(copy, len);
let string = str::from_utf8_unchecked(slice);
let err = RustStr::from(string);
let err = PtrLen {
ptr: NonNull::from(string).cast::<u8>(),
len: string.len(),
};
Result { err }
}

Expand Down