Merge pull request #1 from BeFunctional/edsko/marshall-result

Introduce `marshall_result_to_haskell_var`
This commit is contained in:
Edsko de Vries 2023-03-21 11:59:17 +00:00 committed by GitHub
commit f034cbe5d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
use std::{
fmt::Display,
io::{Error, Write},
marker::PhantomData,
};
@ -91,3 +92,20 @@ pub fn marshall_to_haskell_var<Tag, T>(
Err(e) => panic!("{}", e),
}
}
/// Wrapper around `marshall_to_haskell_var` that calls `format` for errors
pub fn marshall_result_to_haskell_var<Tag, T, E>(
res: &Result<T, E>,
out: *mut u8,
out_len: &mut usize,
tag: PhantomData<Tag>,
) where
T: ToHaskell<Tag>,
E: Display,
{
let res: Result<&T, String> = match res {
Ok(t) => Ok(t),
Err(e) => Err(format!("{}", e)),
};
marshall_to_haskell_var(&res, out, out_len, tag);
}