cido 0.2.0

Core traits and implementations for indexing with cido
Documentation
#[_impls]
use crate::prelude::*;
use cido_macros::_impls;

#[_impls]
/// A unique reference to an entity that is already cached
///
/// The lifetime of this gets disassociated from the context so that the context can be used to
/// load other entities or events
#[must_use = "RefMut is expensive to load from the database"]
pub struct RefMut<'a, T: Transformer> {
  inner: std::cell::RefMut<'a, T>,
}

#[_impls]
impl<'a, T: Transformer> RefMut<'a, T> {
  /// Saves the state of the transformer
  pub async fn save(self) -> Result<(), CidomapError> {
    Ok(())
  }

  /// Performs a logical delete of an entity.
  /// That means it closes its "liveness" range and is marked as removed from the current value table.
  pub async fn delete(self) -> Result<(), CidomapError> {
    Ok(())
  }

  /// Resets to a completely new instance and ensures it is saved
  pub fn reset(&mut self, new: T) {
    let _ = new;
  }

  /// Convenient way to not save without running into issues with not calling drop
  pub fn consume(self) {}
}

#[_impls]
impl<'a, T: Transformer> ::core::ops::Deref for RefMut<'a, T> {
  type Target = T;
  fn deref(&self) -> &Self::Target {
    &*self.inner
  }
}

#[_impls]
impl<'a, T: Transformer> ::core::ops::DerefMut for RefMut<'a, T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut *self.inner
  }
}

#[_impls]
/// A shared reference to an entity that is already cached
///
/// The lifetime of this gets disassociated from the context so that the context can be used to
/// load other entities or events
#[must_use = "Ref is expensive to load from the database"]
pub struct Ref<'a, T: Transformer> {
  inner: std::cell::Ref<'a, T>,
}

#[_impls]
impl<T: Transformer> ::core::ops::Deref for Ref<'_, T> {
  type Target = T;
  fn deref(&self) -> &Self::Target {
    &*self.inner
  }
}

#[_impls]
impl<T: Transformer> Clone for Ref<'_, T> {
  fn clone(&self) -> Self {
    Self {
      inner: std::cell::Ref::clone(&self.inner),
    }
  }
}