use std::sync::Arc;
use tokio::sync::OnceCell;
use crate::{backend::Backend, RexecutorError};
pub struct GlobalBackend;
static GLOBAL_BACKEND: OnceCell<Arc<dyn Backend + 'static + Sync + Send>> = OnceCell::const_new();
impl GlobalBackend {
pub(crate) fn set(backend: impl Backend + Send + Sync + 'static) -> Result<(), RexecutorError> {
GLOBAL_BACKEND.set(Arc::new(backend)).map_err(|err| {
tracing::error!(%err, "Couldn't set global backend {err}");
RexecutorError::GlobalBackend
})?;
Ok(())
}
#[doc(hidden)]
pub fn as_ref() -> Result<&'static (dyn Backend + Send + Sync), RexecutorError> {
Ok(GLOBAL_BACKEND
.get()
.ok_or(RexecutorError::GlobalBackend)?
.as_ref())
}
}