Skip to content

Commit 3832ad7

Browse files
committed
Make ocaml! functions return results directly, instead of wrapped in Result.
panic! on unexpected exceptions.
1 parent 34c141b commit 3832ad7

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

src/closure.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl OCamlClosure {
6161
get_named(name).map(OCamlClosure)
6262
}
6363

64-
pub fn call<'a, T, R>(&self, cr: &'a mut OCamlRuntime, arg: &OCamlRooted<T>) -> Result<OCaml<'a, R>, OCamlError> {
64+
pub fn call<'a, T, R>(&self, cr: &'a mut OCamlRuntime, arg: &OCamlRooted<T>) -> OCaml<'a, R> {
6565
let result = unsafe { caml_callback_exn(*self.0, arg.get_raw()) };
6666
self.handle_call_result(cr, result)
6767
}
@@ -71,7 +71,7 @@ impl OCamlClosure {
7171
cr: &'a mut OCamlRuntime,
7272
arg1: &OCamlRooted<T>,
7373
arg2: &OCamlRooted<U>,
74-
) -> Result<OCaml<'a, R>, OCamlError> {
74+
) -> OCaml<'a, R> {
7575
let result = unsafe { caml_callback2_exn(*self.0, arg1.get_raw(), arg2.get_raw()) };
7676
self.handle_call_result(cr, result)
7777
}
@@ -82,25 +82,25 @@ impl OCamlClosure {
8282
arg1: &OCamlRooted<T>,
8383
arg2: &OCamlRooted<U>,
8484
arg3: &OCamlRooted<V>,
85-
) -> Result<OCaml<'a, R>, OCamlError> {
85+
) -> OCaml<'a, R> {
8686
let result = unsafe { caml_callback3_exn(*self.0, arg1.get_raw(), arg2.get_raw(), arg3.get_raw()) };
8787
self.handle_call_result(cr, result)
8888
}
8989

90-
pub fn call_n<'a, R>(&self, cr: &'a mut OCamlRuntime, args: &mut [RawOCaml]) -> Result<OCaml<'a, R>, OCamlError> {
90+
pub fn call_n<'a, R>(&self, cr: &'a mut OCamlRuntime, args: &mut [RawOCaml]) -> OCaml<'a, R> {
9191
let len = args.len();
9292
let result = unsafe { caml_callbackN_exn(*self.0, len, args.as_mut_ptr()) };
9393
self.handle_call_result(cr, result)
9494
}
9595

9696
#[inline]
97-
fn handle_call_result<'a, R>(&self, cr: &'a mut OCamlRuntime, result: RawOCaml) -> Result<OCaml<'a, R>, OCamlError> {
97+
fn handle_call_result<'a, R>(&self, cr: &'a mut OCamlRuntime, result: RawOCaml) -> OCaml<'a, R> {
9898
if is_exception_result(result) {
99-
let ex = extract_exception(result);
100-
Err(OCamlError::Exception(OCamlException::of(ex)))
99+
//let ex = extract_exception(result);
100+
//Err(OCamlError::Exception(OCamlException::of(ex)))
101+
panic!("OCaml exception")
101102
} else {
102-
let gv = unsafe { OCaml::new(cr, result) };
103-
Ok(gv)
103+
unsafe { OCaml::new(cr, result) }
104104
}
105105
}
106106
}

src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ macro_rules! ocaml {
8989
$vis fn $name<'a>(
9090
cr: &'a mut $crate::OCamlRuntime,
9191
$arg: &$crate::OCamlRooted<$typ>,
92-
) -> Result<$crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)>, $crate::OCamlError> {
92+
) -> $crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)> {
9393
$crate::ocaml_closure_reference!(F, $name);
9494
F.call(cr, $arg)
9595
}
@@ -105,7 +105,7 @@ macro_rules! ocaml {
105105
cr: &'a mut $crate::OCamlRuntime,
106106
$arg1: &$crate::OCamlRooted<$typ1>,
107107
$arg2: &$crate::OCamlRooted<$typ2>,
108-
) -> Result<$crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)>, $crate::OCamlError> {
108+
) -> $crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)> {
109109
$crate::ocaml_closure_reference!(F, $name);
110110
F.call2(cr, $arg1, $arg2)
111111
}
@@ -123,7 +123,7 @@ macro_rules! ocaml {
123123
$arg1: &$crate::OCamlRooted<$typ1>,
124124
$arg2: &$crate::OCamlRooted<$typ2>,
125125
$arg3: &$crate::OCamlRooted<$typ3>,
126-
) -> Result<$crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)>, $crate::OCamlError> {
126+
) -> $crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)> {
127127
$crate::ocaml_closure_reference!(F, $name);
128128
F.call3(cr, $arg1, $arg2, $arg3)
129129
}
@@ -137,7 +137,7 @@ macro_rules! ocaml {
137137
$vis fn $name<'a>(
138138
cr: &'a mut $crate::OCamlRuntime,
139139
$($arg: &$crate::OCamlRooted<$typ>),+
140-
) -> Result<$crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)>, $crate::OCamlError> {
140+
) -> $crate::OCaml<'a, $crate::default_to_unit!($($rtyp)?)> {
141141
$crate::ocaml_closure_reference!(F, $name);
142142
F.call_n(cr, &mut [$($arg.get_raw()),+])
143143
}

testing/rust-caller/src/lib.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn increment_bytes(cr: &mut OCamlRuntime, bytes: &str, first_n: usize) -> St
6565
ocaml_frame!(cr, (bytes_root, first_n_root), {
6666
let bytes = to_ocaml!(cr, bytes, bytes_root);
6767
let first_n = to_ocaml!(cr, first_n as i64, first_n_root);
68-
let result = ocaml::increment_bytes(cr, &bytes, &first_n).unwrap();
68+
let result = ocaml::increment_bytes(cr, &bytes, &first_n);
6969
result.to_rust()
7070
})
7171
}
@@ -74,8 +74,6 @@ pub fn increment_ints_list(cr: &mut OCamlRuntime, ints: &Vec<i64>) -> Vec<i64> {
7474
ocaml_frame!(cr, (root), {
7575
let ints = to_ocaml!(cr, ints, root);
7676
let result = ocaml::increment_ints_list(cr, &ints);
77-
let result: OCaml<OCamlList<OCamlInt>> =
78-
result.expect("Error in 'increment_ints_list' call result");
7977
result.to_rust()
8078
})
8179
}
@@ -85,7 +83,6 @@ pub fn twice(cr: &mut OCamlRuntime, num: i64) -> i64 {
8583
let num = unsafe { OCaml::of_i64_unchecked(num) };
8684
let num = root.keep(num);
8785
let result = ocaml::twice(cr, &num);
88-
let result: OCaml<OCamlInt> = result.expect("Error in 'twice' call result");
8986
result.to_rust()
9087
})
9188
}
@@ -96,7 +93,6 @@ pub fn make_tuple(cr: &mut OCamlRuntime, fst: String, snd: i64) -> (String, i64)
9693
let num = num_root.keep(num);
9794
let str = to_ocaml!(cr, fst, str_root);
9895
let result = ocaml::make_tuple(cr, &str, &num);
99-
let result: OCaml<(String, OCamlInt)> = result.expect("Error in 'make_tuple' call result");
10096
result.to_rust()
10197
})
10298
}
@@ -105,7 +101,6 @@ pub fn make_some(cr: &mut OCamlRuntime, value: String) -> Option<String> {
105101
ocaml_frame!(cr, (root), {
106102
let str = to_ocaml!(cr, value, root);
107103
let result = ocaml::make_some(cr, &str);
108-
let result: OCaml<Option<String>> = result.expect("Error in 'make_some' call result");
109104
result.to_rust()
110105
})
111106
}
@@ -114,8 +109,6 @@ pub fn make_ok(cr: &mut OCamlRuntime, value: i64) -> Result<i64, String> {
114109
ocaml_frame!(cr, (root), {
115110
let result = to_ocaml!(cr, value, root);
116111
let result = ocaml::make_ok(cr, &result);
117-
let result: OCaml<Result<OCamlInt, String>> =
118-
result.expect("Error in 'make_ok' call result");
119112
result.to_rust()
120113
})
121114
}
@@ -124,8 +117,6 @@ pub fn make_error(cr: &mut OCamlRuntime, value: String) -> Result<i64, String> {
124117
ocaml_frame!(cr, (root), {
125118
let result = to_ocaml!(cr, value, root);
126119
let result = ocaml::make_error(cr, &result);
127-
let result: OCaml<Result<OCamlInt, String>> =
128-
result.expect("Error in 'make_error' call result");
129120
result.to_rust()
130121
})
131122
}
@@ -134,7 +125,6 @@ pub fn verify_record_test(cr: &mut OCamlRuntime, record: ocaml::TestRecord) -> S
134125
ocaml_frame!(cr, (root), {
135126
let ocaml_record = to_ocaml!(cr, record, root);
136127
let result = ocaml::stringify_record(cr, &ocaml_record);
137-
let result: OCaml<String> = result.expect("Error in 'stringify_record' call result");
138128
result.to_rust()
139129
})
140130
}
@@ -143,7 +133,6 @@ pub fn verify_variant_test(cr: &mut OCamlRuntime, variant: ocaml::Movement) -> S
143133
ocaml_frame!(cr, (root), {
144134
let ocaml_variant = to_ocaml!(cr, variant, root);
145135
let result = ocaml::stringify_variant(cr, &ocaml_variant);
146-
let result: OCaml<String> = result.expect("Error in 'stringify_variant' call result");
147136
result.to_rust()
148137
})
149138
}

0 commit comments

Comments
 (0)