From f406ec34a354d36052fc889f8efb1e6a51a4aa43 Mon Sep 17 00:00:00 2001 From: Edsko de Vries Date: Tue, 21 Mar 2023 12:51:32 +0100 Subject: [PATCH] Introduce `marshall_result_to_haskell_var` --- haskell-ffi/src/to_haskell.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/haskell-ffi/src/to_haskell.rs b/haskell-ffi/src/to_haskell.rs index da91378..986b326 100644 --- a/haskell-ffi/src/to_haskell.rs +++ b/haskell-ffi/src/to_haskell.rs @@ -1,4 +1,5 @@ use std::{ + fmt::Display, io::{Error, Write}, marker::PhantomData, }; @@ -91,3 +92,20 @@ pub fn marshall_to_haskell_var( Err(e) => panic!("{}", e), } } + +/// Wrapper around `marshall_to_haskell_var` that calls `format` for errors +pub fn marshall_result_to_haskell_var( + res: &Result, + out: *mut u8, + out_len: &mut usize, + tag: PhantomData, +) where + T: ToHaskell, + 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); +}