Introduce marshall_result_to_haskell_var

This commit is contained in:
Edsko de Vries 2023-03-21 12:51:32 +01:00
parent c46977354d
commit f406ec34a3

View file

@ -1,4 +1,5 @@
use std::{ use std::{
fmt::Display,
io::{Error, Write}, io::{Error, Write},
marker::PhantomData, marker::PhantomData,
}; };
@ -91,3 +92,20 @@ pub fn marshall_to_haskell_var<Tag, T>(
Err(e) => panic!("{}", e), 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);
}