pub(crate) mod inner;
pub(crate) use inner::*;
#[cfg(feature = "experimental-async")]
use crate::AsStoreAsync;
use crate::{AsStoreMut, AsStoreRef, StoreMut, StoreRef, macros::backend::match_rt};
use std::{any::Any, fmt::Debug, marker::PhantomData};
#[derive(Debug, derive_more::From)]
pub struct FunctionEnv<T>(pub(crate) BackendFunctionEnv<T>);
impl<T> Clone for FunctionEnv<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T> FunctionEnv<T> {
pub fn new(store: &mut impl AsStoreMut, value: T) -> Self
where
T: Any + Send + 'static + Sized,
{
Self(BackendFunctionEnv::new(store, value))
}
pub fn as_ref<'a>(&self, store: &'a impl AsStoreRef) -> &'a T
where
T: Any + Send + 'static + Sized,
{
self.0.as_ref(store)
}
pub fn as_mut<'a>(&self, store: &'a mut impl AsStoreMut) -> &'a mut T
where
T: Any + Send + 'static + Sized,
{
self.0.as_mut(store)
}
pub fn into_mut(self, store: &mut impl AsStoreMut) -> FunctionEnvMut<'_, T>
where
T: Any + Send + 'static + Sized,
{
self.0.into_mut(store)
}
}
#[derive(derive_more::From)]
pub struct FunctionEnvMut<'a, T: 'a>(pub(crate) BackendFunctionEnvMut<'a, T>);
impl<T: Send + 'static> FunctionEnvMut<'_, T> {
pub fn data(&self) -> &T {
self.0.data()
}
pub fn data_mut(&mut self) -> &mut T {
self.0.data_mut()
}
pub fn as_ref(&self) -> FunctionEnv<T> {
self.0.as_ref()
}
pub fn as_mut(&mut self) -> FunctionEnvMut<'_, T> {
self.0.as_mut()
}
pub fn data_and_store_mut(&mut self) -> (&mut T, StoreMut<'_>) {
self.0.data_and_store_mut()
}
#[cfg(feature = "experimental-async")]
pub fn as_store_async(&self) -> Option<impl AsStoreAsync + 'static> {
self.0.as_store_async()
}
}
impl<T> AsStoreRef for FunctionEnvMut<'_, T> {
fn as_store_ref(&self) -> StoreRef<'_> {
self.0.as_store_ref()
}
}
impl<T> AsStoreMut for FunctionEnvMut<'_, T> {
fn as_store_mut(&mut self) -> StoreMut<'_> {
self.0.as_store_mut()
}
fn objects_mut(&mut self) -> &mut crate::StoreObjects {
self.0.objects_mut()
}
}
impl<T> std::fmt::Debug for FunctionEnvMut<'_, T>
where
T: Send + std::fmt::Debug + 'static,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[cfg(feature = "experimental-async")]
pub struct AsyncFunctionEnvMut<T>(pub(crate) BackendAsyncFunctionEnvMut<T>);
#[cfg(feature = "experimental-async")]
pub struct AsyncFunctionEnvHandle<T>(pub(crate) BackendAsyncFunctionEnvHandle<T>);
#[cfg(feature = "experimental-async")]
pub struct AsyncFunctionEnvHandleMut<T>(pub(crate) BackendAsyncFunctionEnvHandleMut<T>);
#[cfg(feature = "experimental-async")]
impl<T: 'static> AsyncFunctionEnvMut<T> {
pub async fn read(&self) -> AsyncFunctionEnvHandle<T> {
AsyncFunctionEnvHandle(self.0.read().await)
}
pub async fn write(&self) -> AsyncFunctionEnvHandleMut<T> {
AsyncFunctionEnvHandleMut(self.0.write().await)
}
pub fn as_ref(&self) -> FunctionEnv<T> {
FunctionEnv(self.0.as_ref())
}
pub fn as_mut(&mut self) -> Self {
Self(self.0.as_mut())
}
pub fn as_store_async(&self) -> impl AsStoreAsync + 'static {
self.0.as_store_async()
}
}
#[cfg(feature = "experimental-async")]
impl<T: 'static> AsyncFunctionEnvHandle<T> {
pub fn data(&self) -> &T {
self.0.data()
}
pub fn data_and_store(&self) -> (&T, &impl AsStoreRef) {
self.0.data_and_store()
}
}
#[cfg(feature = "experimental-async")]
impl<T: 'static> AsStoreRef for AsyncFunctionEnvHandle<T> {
fn as_store_ref(&self) -> StoreRef<'_> {
AsStoreRef::as_store_ref(&self.0)
}
}
#[cfg(feature = "experimental-async")]
impl<T: 'static> AsyncFunctionEnvHandleMut<T> {
pub fn data_mut(&mut self) -> &mut T {
self.0.data_mut()
}
pub fn data_and_store_mut(&mut self) -> (&mut T, &mut impl AsStoreMut) {
self.0.data_and_store_mut()
}
pub fn as_function_env_mut(&mut self) -> FunctionEnvMut<'_, T> {
FunctionEnvMut(self.0.as_function_env_mut())
}
}
#[cfg(feature = "experimental-async")]
impl<T: 'static> AsStoreRef for AsyncFunctionEnvHandleMut<T> {
fn as_store_ref(&self) -> StoreRef<'_> {
AsStoreRef::as_store_ref(&self.0)
}
}
#[cfg(feature = "experimental-async")]
impl<T: 'static> AsStoreMut for AsyncFunctionEnvHandleMut<T> {
fn as_store_mut(&mut self) -> StoreMut<'_> {
AsStoreMut::as_store_mut(&mut self.0)
}
fn objects_mut(&mut self) -> &mut crate::StoreObjects {
AsStoreMut::objects_mut(&mut self.0)
}
}