Skip to content

Commit

Permalink
Use more descriptive names for default functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Apr 19, 2024
1 parent 341bba0 commit 6a50ffa
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 45 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Writing to entities on client now done via `EntityMut` and `Commands` instead of `EntityWorldMut`. It was needed to support the mentioned in-place deserialization and will possibly allow batching insertions in the future (for details see https://github.com/bevyengine/bevy/issues/10154).
- Move `Replication` to `core` module.
- Move all functions-related logic from `ReplicationRules` into a new `ReplicationFns`.
- Rename `serialize_component` into `serialize` and move into `serde_fns` module.
- Rename `deserialize_component` into `deserialize` and move into `serde_fns` module.
- Rename `deserialize_mapped_component` into `deserialize_mapped` and move into `serde_fns` module.
- Rename `remove_component` into `remove` and move into `command_fns` module.
- Rename `serialize_component` into `default_serialize` and move into `serde_fns` module.
- Rename `deserialize_component` into `default_deserialize` and move into `serde_fns` module.
- Rename `deserialize_mapped_component` into `default_deserialize_mapped` and move into `serde_fns` module.
- Rename `remove_component` into `default_remove` and move into `command_fns` module.
- Move `despawn_recursive` into `replication_fns` module.

### Removed
Expand Down
14 changes: 7 additions & 7 deletions src/core/command_markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub trait AppMarkerExt {
If this marker is present on an entity and its priority is the highest,
then these functions will be called for this component during replication
instead of default [`write`](super::replication_fns::command_fns::write) and
[`remove`](super::replication_fns::command_fns::remove).
instead of [`default_write`](super::replication_fns::command_fns::default_write) and
[`default_remove`](super::replication_fns::command_fns::default_remove).
See also [`Self::set_command_fns`].
# Safety
Expand Down Expand Up @@ -127,9 +127,9 @@ pub trait AppMarkerExt {
/// Sets default functions for a component when there are no markers.
///
/// If there are no markers are present on an entity, then these functions will
/// be called for this component during replication instead of default
/// [`write`](super::replication_fns::command_fns::write) and
/// [`remove`](super::replication_fns::command_fns::remove).
/// be called for this component during replication instead of
/// [`default_write`](super::replication_fns::command_fns::default_write) and
/// [`default_remove`](super::replication_fns::command_fns::default_remove).
/// See also [`Self::set_marker_fns`].
///
/// # Safety
Expand Down Expand Up @@ -269,8 +269,8 @@ mod tests {
// SAFETY: `write` can be safely called with a `SerdeFns` created for `DummyComponent`.
unsafe {
app.set_marker_fns::<DummyMarkerA, DummyComponent>(
command_fns::write::<DummyComponent>,
command_fns::remove::<DummyComponent>,
command_fns::default_write::<DummyComponent>,
command_fns::default_remove::<DummyComponent>,
);
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/core/replication_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ impl ReplicationFns {
{
self.register_serde_fns(
world,
serde_fns::serialize::<C>,
serde_fns::deserialize::<C>,
serde_fns::default_serialize::<C>,
serde_fns::default_deserialize::<C>,
serde_fns::in_place_as_deserialize::<C>,
)
}
Expand All @@ -127,8 +127,8 @@ impl ReplicationFns {
{
self.register_serde_fns(
world,
serde_fns::serialize::<C>,
serde_fns::deserialize_mapped::<C>,
serde_fns::default_serialize::<C>,
serde_fns::default_deserialize_mapped::<C>,
serde_fns::in_place_as_deserialize::<C>,
)
}
Expand Down Expand Up @@ -255,8 +255,8 @@ mod tests {
replication_fns.set_marker_fns::<ComponentA>(
&mut world,
marker_a,
command_fns::write::<ComponentA>,
command_fns::remove::<ComponentA>,
command_fns::default_write::<ComponentA>,
command_fns::default_remove::<ComponentA>,
);
}

Expand Down Expand Up @@ -289,14 +289,14 @@ mod tests {
replication_fns.set_marker_fns::<ComponentA>(
&mut world,
marker_a,
command_fns::write::<ComponentA>,
command_fns::remove::<ComponentA>,
command_fns::default_write::<ComponentA>,
command_fns::default_remove::<ComponentA>,
);
replication_fns.set_marker_fns::<ComponentB>(
&mut world,
marker_b,
command_fns::write::<ComponentB>,
command_fns::remove::<ComponentB>,
command_fns::default_write::<ComponentB>,
command_fns::default_remove::<ComponentB>,
);
}

Expand Down
17 changes: 10 additions & 7 deletions src/core/replication_fns/command_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ impl CommandFns {
/// Creates a new instance for `C` with default functions and the specified number of empty marker function slots.
pub(super) fn new<C: Component>(marker_slots: usize) -> Self {
Self {
read: read::<C>,
write: write::<C>,
remove: remove::<C>,
read: default_read::<C>,
write: default_write::<C>,
remove: default_remove::<C>,
markers: vec![None; marker_slots],
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ impl CommandFns {
/// Entity markers store information about which markers are present on an entity.
/// The first-found write function whose marker is present on the entity will be selected
/// (the functions are sorted by priority).
/// If there is no such function, it will use the default [`write()`].
/// If there is no such function, it will use the [`default_write`].
///
/// # Safety
///
Expand Down Expand Up @@ -189,7 +189,7 @@ pub type RemoveFn = fn(EntityCommands, RepliconTick);
/// # Safety
///
/// The caller must ensure that `ptr` and `serde_fns` were created for `C`.
pub unsafe fn read<C: Component>(
pub unsafe fn default_read<C: Component>(
serde_fns: &SerdeFns,
ptr: Ptr,
cursor: &mut Cursor<Vec<u8>>,
Expand All @@ -205,7 +205,7 @@ pub unsafe fn read<C: Component>(
/// # Safety
///
/// The caller must ensure that `serde_fns` was created for `C`.
pub unsafe fn write<C: Component>(
pub unsafe fn default_write<C: Component>(
serde_fns: &SerdeFns,
commands: &mut Commands,
entity: &mut EntityMut,
Expand All @@ -229,6 +229,9 @@ pub unsafe fn write<C: Component>(
}

/// Default component removal function.
pub fn remove<C: Component>(mut entity_commands: EntityCommands, _replicon_tick: RepliconTick) {
pub fn default_remove<C: Component>(
mut entity_commands: EntityCommands,
_replicon_tick: RepliconTick,
) {
entity_commands.remove::<C>();
}
10 changes: 5 additions & 5 deletions src/core/replication_fns/serde_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ pub type DeserializeInPlaceFn<C> =
fn(DeserializeFn<C>, &mut C, &mut Cursor<&[u8]>, &mut ClientMapper) -> bincode::Result<()>;

/// Default component serialization function.
pub fn serialize<C: Component + Serialize>(
pub fn default_serialize<C: Component + Serialize>(
component: &C,
cursor: &mut Cursor<Vec<u8>>,
) -> bincode::Result<()> {
DefaultOptions::new().serialize_into(cursor, component)
}

/// Default component deserialization function.
pub fn deserialize<C: Component + DeserializeOwned>(
pub fn default_deserialize<C: Component + DeserializeOwned>(
cursor: &mut Cursor<&[u8]>,
_mapper: &mut ClientMapper,
) -> bincode::Result<C> {
DefaultOptions::new().deserialize_from(cursor)
}

/// Like [`deserialize`], but also maps entities before insertion.
pub fn deserialize_mapped<C: Component + DeserializeOwned + MapEntities>(
pub fn default_deserialize_mapped<C: Component + DeserializeOwned + MapEntities>(
cursor: &mut Cursor<&[u8]>,
mapper: &mut ClientMapper,
) -> bincode::Result<C> {
Expand Down Expand Up @@ -160,8 +160,8 @@ mod tests {
#[should_panic]
fn packing() {
let serde_fns = SerdeFns::new(
serialize::<ComponentA>,
deserialize::<ComponentA>,
default_serialize::<ComponentA>,
default_deserialize::<ComponentA>,
in_place_as_deserialize::<ComponentA>,
);

Expand Down
8 changes: 4 additions & 4 deletions src/core/replication_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub trait AppRuleExt {
C: Component + Serialize + DeserializeOwned,
{
self.replicate_with::<C>(
serde_fns::serialize::<C>,
serde_fns::deserialize::<C>,
serde_fns::default_serialize::<C>,
serde_fns::default_deserialize::<C>,
serde_fns::in_place_as_deserialize::<C>,
)
}
Expand All @@ -46,8 +46,8 @@ pub trait AppRuleExt {
C: Component + Serialize + DeserializeOwned + MapEntities,
{
self.replicate_with::<C>(
serde_fns::serialize::<C>,
serde_fns::deserialize_mapped::<C>,
serde_fns::default_serialize::<C>,
serde_fns::default_deserialize_mapped::<C>,
serde_fns::in_place_as_deserialize::<C>,
)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn command_fns() {
unsafe {
app.set_command_fns::<OriginalComponent>(
replace,
command_fns::remove::<ReplacedComponent>,
command_fns::default_remove::<ReplacedComponent>,
);
}
}
Expand Down Expand Up @@ -190,7 +190,7 @@ fn marker() {
unsafe {
app.set_marker_fns::<ReplaceMarker, OriginalComponent>(
replace,
command_fns::remove::<ReplacedComponent>,
command_fns::default_remove::<ReplacedComponent>,
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn command_fns() {
unsafe {
app.set_command_fns::<OriginalComponent>(
replace,
command_fns::remove::<ReplacedComponent>,
command_fns::default_remove::<ReplacedComponent>,
);
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ fn marker() {
unsafe {
app.set_marker_fns::<ReplaceMarker, OriginalComponent>(
replace,
command_fns::remove::<ReplacedComponent>,
command_fns::default_remove::<ReplacedComponent>,
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/removal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ fn command_fns() {
// SAFETY: `replace` can be safely called with a `SerdeFns` created for `DummyComponent`.
unsafe {
app.set_command_fns::<DummyComponent>(
command_fns::write::<DummyComponent>,
command_fns::remove::<RemovingComponent>,
command_fns::default_write::<DummyComponent>,
command_fns::default_remove::<RemovingComponent>,
);
}
}
Expand Down Expand Up @@ -120,8 +120,8 @@ fn marker() {
// SAFETY: `replace` can be safely called with a `SerdeFns` created for `DummyComponent`.
unsafe {
app.set_marker_fns::<RemoveMarker, DummyComponent>(
command_fns::write::<DummyComponent>,
command_fns::remove::<RemovingComponent>,
command_fns::default_write::<DummyComponent>,
command_fns::default_remove::<RemovingComponent>,
);
}
}
Expand Down

0 comments on commit 6a50ffa

Please sign in to comment.