@@ -446,14 +446,14 @@ struct ReplicationBuffer {
446
446
/// The number of empty arrays at the end. Can be removed using [`Self::trim_empty_arrays`]
447
447
trailing_empty_arrays : usize ,
448
448
449
- /// Position of the entity map from last call of [`Self::start_entity_data`] or [`Self::write_current_entity `].
449
+ /// Position of entity after [`Self::start_entity_data`] or its data after [`Self::write_data_entity `].
450
450
entity_data_pos : u64 ,
451
451
452
- /// Length of the map that updated automatically after writing data.
452
+ /// Length of the data for entity that updated automatically after writing data.
453
453
entity_data_len : u8 ,
454
454
455
455
/// Entity from last call of [`Self::start_entity_data`].
456
- current_entity : Entity ,
456
+ data_entity : Entity ,
457
457
}
458
458
459
459
impl ReplicationBuffer {
@@ -476,7 +476,7 @@ impl ReplicationBuffer {
476
476
trailing_empty_arrays : Default :: default ( ) ,
477
477
entity_data_pos : Default :: default ( ) ,
478
478
entity_data_len : Default :: default ( ) ,
479
- current_entity : Entity :: PLACEHOLDER ,
479
+ data_entity : Entity :: PLACEHOLDER ,
480
480
} )
481
481
}
482
482
@@ -547,7 +547,7 @@ impl ReplicationBuffer {
547
547
self . trailing_empty_arrays = 0 ;
548
548
}
549
549
550
- /// Starts writing entity and its data by remembering [`Entity`] .
550
+ /// Starts writing entity and its data by remembering `entity` .
551
551
///
552
552
/// Arrays can contain component changes or removals inside.
553
553
/// Length will be increased automatically after writing data.
@@ -556,14 +556,14 @@ impl ReplicationBuffer {
556
556
fn start_entity_data ( & mut self , entity : Entity ) {
557
557
debug_assert_eq ! ( self . entity_data_len, 0 ) ;
558
558
559
- self . current_entity = entity;
559
+ self . data_entity = entity;
560
560
}
561
561
562
562
/// Writes entity for current data and updates remembered position for it to write length later.
563
563
///
564
564
/// Should be called only after first data write.
565
- fn write_current_entity ( & mut self ) -> Result < ( ) , bincode:: Error > {
566
- DefaultOptions :: new ( ) . serialize_into ( & mut self . message , & self . current_entity ) ?;
565
+ fn write_data_entity ( & mut self ) -> Result < ( ) , bincode:: Error > {
566
+ self . write_entity ( self . data_entity ) ?;
567
567
self . entity_data_pos = self . message . position ( ) ;
568
568
self . message
569
569
. set_position ( self . entity_data_pos + mem:: size_of_val ( & self . entity_data_len ) as u64 ) ;
@@ -595,7 +595,7 @@ impl ReplicationBuffer {
595
595
Ok ( ( ) )
596
596
}
597
597
598
- /// Serializes [`ReplicationId`] and component into the buffer data.
598
+ /// Serializes `replication_id` and component from `ptr` into the buffer data.
599
599
///
600
600
/// Should be called only inside entity data.
601
601
/// Increases entity data length by 1.
@@ -607,7 +607,7 @@ impl ReplicationBuffer {
607
607
ptr : Ptr ,
608
608
) -> Result < ( ) , bincode:: Error > {
609
609
if self . entity_data_len == 0 {
610
- self . write_current_entity ( ) ?;
610
+ self . write_data_entity ( ) ?;
611
611
}
612
612
613
613
DefaultOptions :: new ( ) . serialize_into ( & mut self . message , & replication_id) ?;
@@ -617,14 +617,14 @@ impl ReplicationBuffer {
617
617
Ok ( ( ) )
618
618
}
619
619
620
- /// Serializes [`ReplicationId`] of the removed component into the buffer data.
620
+ /// Serializes `replication_id` of the removed component into the buffer data.
621
621
///
622
622
/// Should be called only inside entity data.
623
623
/// Increases entity data length by 1.
624
624
/// See also [`Self::start_entity_data`].
625
625
fn write_removal ( & mut self , replication_id : ReplicationId ) -> Result < ( ) , bincode:: Error > {
626
626
if self . entity_data_len == 0 {
627
- self . write_current_entity ( ) ?;
627
+ self . write_data_entity ( ) ?;
628
628
}
629
629
630
630
DefaultOptions :: new ( ) . serialize_into ( & mut self . message , & replication_id) ?;
@@ -633,18 +633,26 @@ impl ReplicationBuffer {
633
633
Ok ( ( ) )
634
634
}
635
635
636
- /// Serializes despawned [`Entity`] .
636
+ /// Serializes despawned `entity` .
637
637
///
638
638
/// Should be called only inside array.
639
639
/// Increases array length by 1.
640
640
/// See also [`Self::start_array`].
641
641
fn write_despawn ( & mut self , entity : Entity ) -> Result < ( ) , bincode:: Error > {
642
- DefaultOptions :: new ( ) . serialize_into ( & mut self . message , & entity) ?;
642
+ self . write_entity ( entity) ?;
643
643
self . array_len = self
644
644
. array_len
645
645
. checked_add ( 1 )
646
646
. ok_or ( bincode:: ErrorKind :: SizeLimit ) ?;
647
647
648
648
Ok ( ( ) )
649
649
}
650
+
651
+ /// Serializes `entity` in optimal way by writing its index and generation as separate u32 varints.
652
+ fn write_entity ( & mut self , entity : Entity ) -> Result < ( ) , bincode:: Error > {
653
+ DefaultOptions :: new ( ) . serialize_into ( & mut self . message , & entity. index ( ) ) ?;
654
+ DefaultOptions :: new ( ) . serialize_into ( & mut self . message , & entity. generation ( ) ) ?;
655
+
656
+ Ok ( ( ) )
657
+ }
650
658
}
0 commit comments