use RocCallResult and pass-by-reference in all examples

This commit is contained in:
Folkert 2020-11-01 15:00:11 +01:00
parent 89e9549d85
commit 5897a07962
6 changed files with 61 additions and 8 deletions

View file

@ -426,3 +426,39 @@ impl Drop for RocStr {
}
}
}
#[allow(non_camel_case_types)]
type c_char = u8;
#[repr(u64)]
pub enum RocCallResult<T> {
Success(T),
Failure(*mut c_char),
}
impl<T: Sized> Into<Result<T, &'static str>> for RocCallResult<T> {
fn into(self) -> Result<T, &'static str> {
use RocCallResult::*;
match self {
Success(value) => Ok(value),
Failure(failure) => Err({
let msg = unsafe {
let mut null_byte_index = 0;
loop {
if *failure.offset(null_byte_index) == 0 {
break;
}
null_byte_index += 1;
}
let bytes = core::slice::from_raw_parts(failure, null_byte_index as usize);
core::str::from_utf8_unchecked(bytes)
};
msg
}),
}
}
}