mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
use RocCallResult and pass-by-reference in all examples
This commit is contained in:
parent
89e9549d85
commit
5897a07962
6 changed files with 61 additions and 8 deletions
|
@ -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
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue