use std::rc::Rc;
use icydb_core as core;
pub struct RequestExecutionRoot {
inner: Rc<core::db::RequestExecutionRoot>,
}
impl RequestExecutionRoot {
fn new() -> Self {
Self {
inner: Rc::new(core::db::RequestExecutionRoot::__new_or_current_runtime_root()),
}
}
#[doc(hidden)]
#[must_use]
pub fn __core(&self) -> &core::db::RequestExecutionRoot {
self.inner.as_ref()
}
}
pub fn with_request_execution<T>(run: impl FnOnce() -> T) -> T {
let root = RequestExecutionRoot::new();
root.inner.__with_current_scope(run)
}
pub fn with_request_execution_root<T>(run: impl FnOnce(RequestExecutionRoot) -> T) -> T {
let root = RequestExecutionRoot::new();
let ambient = Rc::clone(&root.inner);
ambient.__with_current_scope(|| run(root))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_entry_runs_one_synchronous_scoped_callback() {
let called = with_request_execution(|| 1_u8);
assert_eq!(called, 1);
}
#[test]
fn explicit_request_root_can_outlive_callback_construction() {
let retained = with_request_execution_root(std::convert::identity);
let _retained = std::hint::black_box(retained);
}
}