Skip to content

Commit

Permalink
Don't expose unsafe recover handle functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tizoc committed Dec 27, 2023
1 parent 27f519f commit 71e36ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/compile_ok_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod test_immediate_ocamlrefs {

#[test]
fn test_immediate_ocamlrefs() {
let mut cr = unsafe { OCamlRuntime::recover_handle() };
assert!(test_immediate_ocamlref(&mut cr));
let cr = unsafe { OCamlRuntime::recover_handle_mut() };
assert!(test_immediate_ocamlref(cr));
}
}
31 changes: 11 additions & 20 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,17 @@ impl OCamlRuntime {
panic!("Rust code that is called from an OCaml program should not try to initialize the runtime.");
}

/// Recover the runtime handle.
///
/// This method is used internally, do not use directly in code, only when writing tests.
///
/// # Safety
///
/// This function is unsafe because the OCaml runtime handle should be obtained once
/// upon initialization of the OCaml runtime and then passed around. This method exists
/// only to ease the authoring of tests.
#[inline(always)]
pub unsafe fn recover_handle() -> &'static mut Self {
pub(crate) unsafe fn recover_handle_mut() -> &'static mut Self {
static mut RUNTIME: OCamlRuntime = OCamlRuntime { _private: () };
&mut RUNTIME
}

#[inline(always)]
pub(crate) unsafe fn recover_handle() -> &'static Self {
Self::recover_handle_mut()
}

/// Release the OCaml runtime lock, call `f`, and re-acquire the OCaml runtime lock.
pub fn releasing_runtime<T, F>(&mut self, f: F) -> T
where
Expand Down Expand Up @@ -136,18 +132,13 @@ impl OCamlDomainLock {
}

#[inline(always)]
pub fn perform<T, F>(self, f: F) -> T
where
F: FnOnce(&mut OCamlRuntime) -> T,
{
let cr = unsafe { OCamlRuntime::recover_handle() };
f(cr)
pub(crate) fn recover_handle<'a>(&self) -> &'a OCamlRuntime {
unsafe { OCamlRuntime::recover_handle() }
}

// FIXME: immutable reference but gets mut runtime
#[inline(always)]
pub fn recover_handle<'a>(&self) -> &'a mut OCamlRuntime {
unsafe { OCamlRuntime::recover_handle() }
pub(crate) fn recover_handle_mut<'a>(&self) -> &'a mut OCamlRuntime {
unsafe { OCamlRuntime::recover_handle_mut() }
}
}

Expand All @@ -170,7 +161,7 @@ impl Deref for OCamlDomainLock {

impl DerefMut for OCamlDomainLock {
fn deref_mut(&mut self) -> &mut OCamlRuntime {
self.recover_handle()
self.recover_handle_mut()
}
}

Expand Down

0 comments on commit 71e36ce

Please sign in to comment.