Skip to content

Conversation

@Mysvac
Copy link
Contributor

@Mysvac Mysvac commented Sep 22, 2025

Objective

This change allows direct use of .serialize(value, serializer) instead of the more verbose .get_serializable(value).serialize(serializer).

Currently, ReflectDeserialize, ReflectDeserializeWithRegistry, and ReflectSerializeWithRegistry all provide a deserialize/serialize method directly. It is inconsistent and somewhat odd that ReflectSerialize only provides get_serializable.

While the returned Serializable object does expose a serialize method, I believe ReflectSerialize should offer a direct serialize method for better ergonomics.

The existing get_serializable method can still be used to obtain the Serializable struct for testing or other specialized use cases. However, for typical serialization tasks, the direct .serialize(...) approach is clearer and more concise.

Solution

Add serialize function for ReflectSerialize.

Testing

  • cd crates/bevy_reflect; cargo test
  • cd crates/bevy_ecs; cargo test
  • cd crates/bevy_asset; cargo test
  • cargo run --example serialization

Showcase

Click to view showcase
// bevy_reflect  type_registry.rs
#[derive(Clone)]
pub struct ReflectSerialize {
    get_serializable: fn(value: &dyn Reflect) -> Serializable,
}

impl ReflectSerialize {
    // keep function `get_serializable()` unchanged .

    /// Serializes a reflected value.
    pub fn serialize<S>(&self, value: &dyn Reflect, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        (self.get_serializable)(value).serialize(serializer)
    }
}

// bevy_reflect  custom_serialization.rs
/*
Ok(reflect_serialize
    .get_serializable(value)
    .borrow()
    .serialize(serializer))
*/
Ok(reflect_serialize.serialize(value, serializer))

@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Reflection Runtime information about types X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 22, 2025
@james7132 james7132 removed the S-Needs-Review Needs reviewer attention (from anyone!) to move forward label Sep 22, 2025
@james7132 james7132 added this to the 0.18 milestone Sep 22, 2025
@james7132 james7132 added this pull request to the merge queue Sep 22, 2025
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Sep 22, 2025
Merged via the queue into bevyengine:main with commit fc9d37e Sep 22, 2025
46 checks passed
@Mysvac Mysvac deleted the mysvac_reflect branch September 27, 2025 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Reflection Runtime information about types C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants