Skip to content

Commit

Permalink
Verbose error message on boxroot_create failure (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lupus authored Jan 15, 2025
1 parent 0f8a8ca commit a1016f8
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,45 @@ impl core::fmt::Debug for Root {

impl Eq for Root {}

// Adaptation from C++: https://gitlab.com/ocaml-rust/ocaml-boxroot/-/blob/c119eb0c88f3f628e683ee90f1b694b51c60a0cb/boxroot/cpp/boxroot.cpp
fn boxroot_raise_error() -> ! {
use ocaml_boxroot_sys::Status;
let status = unsafe { ocaml_boxroot_sys::boxroot_status() };
let error_message = match status {
Status::ToreDown => "boxroot_teardown has previously been called",
Status::Invalid => "With systhreads, boxroot_setup must be called after caml_thread_initialize but before any thread is created",
Status::Running | Status::NotSetup => {
#[cfg(not(feature = "no-std"))]
{
use std::io::{Error, ErrorKind};
match Error::last_os_error().kind() {
ErrorKind::PermissionDenied => {
"You tried calling boxroot_create or boxroot_modify without holding the domain lock"
}
ErrorKind::OutOfMemory => {
"Allocation failure of the backing store"
}
_ => "Unknown Error::last_os_error().kind()",
}
}
#[cfg(feature = "no-std")]
{
"Unknown error (details unavailable in no-std environment)"
}
}
_ => "Unknown ocaml_boxroot_sys::Status",
};

panic!("Boxroot error: {}", error_message);
}

impl Root {
/// Create a new root
pub unsafe fn new(v: sys::Value) -> Root {
Root(ocaml_boxroot_sys::boxroot_create(v).expect("boxroot_create failed"))
match ocaml_boxroot_sys::boxroot_create(v) {
Some(root) => Root(root),
None => boxroot_raise_error(),
}
}

/// Get value from root
Expand Down

0 comments on commit a1016f8

Please sign in to comment.