Skip to content

Commit 194613c

Browse files
committed
Add new entrypoints for fetching and testing last result for FFI
1 parent f563eba commit 194613c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

bindings/src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,50 @@ mod handle;
2424

2525
pub use handle::*;
2626
pub use error::*;
27+
28+
29+
ffi_no_catch! {
30+
fn ffi_last_result(
31+
message_buf: Out<u8>,
32+
message_buf_len: usize,
33+
actual_message_len: Out<usize>,
34+
result: Out<FFIResult>
35+
) -> FFIResult {
36+
FFIResult::with_last_result(|last_result| {
37+
let (value, error) = last_result.unwrap_or((FFIResult::ok(), None));
38+
39+
unsafe_block!("The out pointer is valid and not mutably aliased elsewhere" => result.init(value));
40+
41+
if let Some(error) = error {
42+
let error = error.as_bytes();
43+
44+
unsafe_block!("The out pointer is valid and not mutably aliased elsewhere" => actual_message_len.init(error.len()));
45+
46+
if message_buf_len < error.len() {
47+
return FFIResult::buffer_too_small();
48+
}
49+
50+
unsafe_block!("The buffer is valid for writes and the length is within the buffer" => message_buf.init_bytes(error));
51+
} else {
52+
unsafe_block!("The out pointer is valid and not mutably aliased elsewhere" => actual_message_len.init(0));
53+
}
54+
55+
FFIResult::ok()
56+
})
57+
}
58+
}
59+
60+
/// These tests should be used for asserting that the wrapper code can see the expected
61+
/// error messages when it fails (or succeeds).
62+
#[cfg(debug_assertions)]
63+
ffi! {
64+
fn ffi_test_error() -> FFIResult {
65+
use std::io;
66+
67+
FFIResult::internal_error().context(io::Error::new(io::ErrorKind::Other, "A test error."))
68+
}
69+
70+
fn ffi_test_ok() -> FFIResult {
71+
FFIResult::ok()
72+
}
73+
}

0 commit comments

Comments
 (0)