-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from dtolnay/result
Implement conversion of Result⟶exception
- Loading branch information
Showing
9 changed files
with
153 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::rust_str::RustStr; | ||
use std::fmt::Display; | ||
use std::ptr; | ||
use std::result::Result as StdResult; | ||
use std::slice; | ||
use std::str; | ||
|
||
#[repr(C)] | ||
pub union Result { | ||
err: RustStr, | ||
ok: *const u8, // null | ||
} | ||
|
||
pub unsafe fn r#try<T, E>(ret: *mut T, result: StdResult<T, E>) -> Result | ||
where | ||
E: Display, | ||
{ | ||
match result { | ||
Ok(ok) => { | ||
ptr::write(ret, ok); | ||
Result { ok: ptr::null() } | ||
} | ||
Err(err) => to_c_error(err.to_string()), | ||
} | ||
} | ||
|
||
unsafe fn to_c_error(msg: String) -> Result { | ||
let mut msg = msg; | ||
msg.as_mut_vec().push(b'\0'); | ||
let ptr = msg.as_ptr(); | ||
let len = msg.len(); | ||
|
||
extern "C" { | ||
#[link_name = "cxxbridge02$error"] | ||
fn error(ptr: *const u8, len: usize) -> *const u8; | ||
} | ||
|
||
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); | ||
Result { err } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters