Make `Insert` more flexible

This commit is contained in:
Hanno Braun 2023-05-04 11:20:36 +02:00
parent 761f9eff14
commit da5bd11057
2 changed files with 11 additions and 3 deletions

View File

@ -69,7 +69,7 @@ pub trait TransformObject: Sized {
impl<T> TransformObject for Handle<T>
where
T: Clone + Insert + TransformObject + 'static,
T: Clone + Insert<Inserted = Handle<T>> + TransformObject + 'static,
{
fn transform_with_cache(
self,

View File

@ -12,15 +12,23 @@ use crate::{
/// This is the only primitive operation that is directly understood by
/// `Service<Objects>`. All other operations are built on top of it.
pub trait Insert: Sized {
/// The type of `Self`, once it has been inserted
///
/// Usually this is just `Handle<Self>`, but there are some more complex
/// cases where this type needs to be customized.
type Inserted;
/// Insert the object into its respective store
fn insert(self, services: &mut Services) -> Handle<Self>;
fn insert(self, services: &mut Services) -> Self::Inserted;
}
macro_rules! impl_insert {
($($ty:ty, $store:ident;)*) => {
$(
impl Insert for $ty {
fn insert(self, services: &mut Services) -> Handle<Self> {
type Inserted = Handle<Self>;
fn insert(self, services: &mut Services) -> Self::Inserted {
let handle = services.objects.$store.reserve();
let object = (handle.clone(), self).into();
services.insert_object(object);