pub fn register<C: Component + serde::Serialize + for<'de> serde::Deserialize<'de>>(
        &mut self,
        mapped_type_id: T,
    ) {
        let type_id = ComponentTypeId::of::<C>();
        let serialize_slice_fn =
            |storage: &dyn UnknownComponentStorage,
             archetype,
             serialize: &mut dyn FnMut(&dyn erased_serde::Serialize)| unsafe {
                let (ptr, len) = storage.get_raw(archetype).unwrap();
                let slice = std::slice::from_raw_parts(ptr as *const C, len);
                (serialize)(&slice);
            };
        let serialize_fn = |ptr, serialize: &mut dyn FnMut(&dyn erased_serde::Serialize)| {
            let component = unsafe { &*(ptr as *const C) };
            (serialize)(component);
        };
        let deserialize_slice_fn =
            |storage: UnknownComponentWriter<'_>,
             deserializer: &mut dyn erased_serde::Deserializer<'_>| {
                // todo avoid temp vec
                ComponentSeq::<C> {
                    storage,
                    _phantom: PhantomData,
                }
                .deserialize(deserializer)?;
                Ok(())
            };
        let deserialize_single_boxed_fn =
            |deserializer: &mut dyn erased_serde::Deserializer<'_>| {
                let component = erased_serde::deserialize::<C>(deserializer)?;
                unsafe {
                    let vec = std::slice::from_raw_parts(
                        &component as *const C as *const u8,
                        std::mem::size_of::<C>(),
                    )
                    .to_vec();
                    std::mem::forget(component);
                    Ok(vec.into_boxed_slice())
                }
            };
        let constructor_fn = |layout: &mut EntityLayout| layout.register_component::<C>();
        self.serialize_fns.insert(
            type_id,
            (
                mapped_type_id.clone(),
                serialize_slice_fn,
                serialize_fn,
                deserialize_slice_fn,
                deserialize_single_boxed_fn,
            ),
        );
        self.constructors
            .insert(mapped_type_id, (type_id, constructor_fn));
    }
/// Registers a component type and its key with the registry.
Registers a component type and its key with the registry.
Registers a component type and its key with the registry .
pub fn register<C: Component + serde::Serialize + for<'de> serde::Deserialize<'de>>(
        &mut self,
        mapped_type_id: T,
    ) {
        let type_id = ComponentTypeId::of::<C>();
        let serialize_slice_fn =
            |storage: &dyn UnknownComponentStorage,
             archetype,
             serialize: &mut dyn FnMut(&dyn erased_serde::Serialize)| unsafe {
                let (ptr, len) = storage.get_raw(archetype).unwrap();
                let slice = std::slice::from_raw_parts(ptr as *const C, len);
                (serialize)(&slice);
            };
        let serialize_fn = |ptr, serialize: &mut dyn FnMut(&dyn erased_serde::Serialize)| {
            let component = unsafe { &*(ptr as *const C) };
            (serialize)(component);
        };
        let deserialize_slice_fn =
            |storage: UnknownComponentWriter<'_>,
             deserializer: &mut dyn erased_serde::Deserializer<'_>| {
                // todo avoid temp vec
                ComponentSeq::<C> {
                    storage,
                    _phantom: PhantomData,
                }
                .deserialize(deserializer)?;
                Ok(())
            };
        let deserialize_single_boxed_fn =
            |deserializer: &mut dyn erased_serde::Deserializer<'_>| {
                let component = erased_serde::deserialize::<C>(deserializer)?;
                unsafe {
                    let vec = std::slice::from_raw_parts(
                        &component as *const C as *const u8,
                        std::mem::size_of::<C>(),
                    )
                    .to_vec();
                    std::mem::forget(component);
                    Ok(vec.into_boxed_slice())
                }
            };
        let constructor_fn = |layout: &mut EntityLayout| layout.register_component::<C>();
        self.serialize_fns.insert(
            type_id,
            (
                mapped_type_id.clone(),
                serialize_slice_fn,
                serialize_fn,
                deserialize_slice_fn,
                deserialize_single_boxed_fn,
            ),
        );
        self.constructors
            .insert(mapped_type_id, (type_id, constructor_fn));
    }
pub fn register < C : Component + serde :: Serialize + for < ' de > serde :: Deserialize < ' de > > ( & mut self , mapped_type_id : T , ) { let type_id = ComponentTypeId :: of :: < C > ( ) ; let serialize_slice_fn = | storage : & dyn UnknownComponentStorage , archetype , serialize : & mut dyn FnMut ( & dyn erased_serde :: Serialize ) | unsafe { let ( ptr , len ) = storage . get_raw ( archetype ) . unwrap ( ) ; let slice = std :: slice :: from_raw_parts ( ptr as * const C , len ) ; ( serialize ) ( & slice ) ; } ; let serialize_fn = | ptr , serialize : & mut dyn FnMut ( & dyn erased_serde :: Serialize ) | { let component = unsafe { & * ( ptr as * const C ) } ; ( serialize ) ( component ) ; } ; let deserialize_slice_fn = | storage : UnknownComponentWriter < ' _ > , deserializer : & mut dyn erased_serde :: Deserializer < ' _ > | { // todo avoid temp vec ComponentSeq :: < C > { storage , _phantom : PhantomData , } . deserialize ( deserializer ) ? ; Ok ( ( ) ) } ; let deserialize_single_boxed_fn = | deserializer : & mut dyn erased_serde :: Deserializer < ' _ > | { let component = erased_serde :: deserialize :: < C > ( deserializer ) ? ; unsafe { let vec = std :: slice :: from_raw_parts ( & component as * const C as * const u8 , std :: mem :: size_of :: < C > ( ) , ) . to_vec ( ) ; std :: mem :: forget ( component ) ; Ok ( vec . into_boxed_slice ( ) ) } } ; let constructor_fn = | layout : & mut EntityLayout | layout . register_component :: < C > ( ) ; self . serialize_fns . insert ( type_id , ( mapped_type_id . clone ( ) , serialize_slice_fn , serialize_fn , deserialize_slice_fn , deserialize_single_boxed_fn , ) , ) ; self . constructors . insert ( mapped_type_id , ( type_id , constructor_fn ) ) ; }
Registers a component type and its key with the registry.
Registers a component type and its key with the registry .
&mut self
mapped_type_id
T
&mut self
mapped_type_id
T