Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ComponentStorage and associated types #12311

Merged
merged 3 commits into from
Mar 5, 2024

Conversation

james7132
Copy link
Member

@james7132 james7132 commented Mar 5, 2024

Objective

When doing a final pass for #3362, it appeared that ComponentStorage as a trait, the two types implementing it, and the associated type on Component aren't really necessary anymore. This likely was due to an earlier constraint on the use of consts in traits, but that definitely doesn't seem to be a problem in Rust 1.76.

Solution

Remove them.


Changelog

Changed: Component::Storage has been replaced with Component::STORAGE_TYPE as a const.
Removed: bevy::ecs::component::ComponentStorage trait
Removed: bevy::ecs::component::TableStorage struct
Removed: bevy::ecs::component::SparseSetStorage struct

Migration Guide

If you were manually implementing Component instead of using the derive macro, replace the associated Storage associated type with the STORAGE_TYPE const:

// in Bevy 0.13
impl Component for MyComponent {
    type Storage = TableStorage;
}
// in Bevy 0.14
impl Component for MyComponent {
    const STORAGE_TYPE: StorageType = StorageType::Table;
}

Component is no longer object safe. If you were relying on &dyn Component, Box<dyn Component>, etc. please file an issue to get this change reverted.

@james7132 james7132 added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Mar 5, 2024
@james7132 james7132 added this to the 0.14 milestone Mar 5, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Does this mean that we can store Box<dyn Component> without specifying an associated storage type finally? That would be really cool if so.

@james7132
Copy link
Member Author

james7132 commented Mar 5, 2024

I don't think so. Actually I think that was the reason why it's still that way. Associated consts make the type not object safe. However, do we want to support Component trait objects? I don't think we can do anything with it, not even downcast.

crates/bevy_ecs/src/component.rs Outdated Show resolved Hide resolved
@alice-i-cecile
Copy link
Member

I don't think so. Actually I think that was the reason why it's still that way. Associated consts make the type not object safe. However, do we want to support Component trait objects? I don't think we can do anything with it, not even downcast.

Yeah, it's fundamentally not object safe. I'd still maybe like an object-safe subtrait for Bundle at some point for a "if X, insert bundle A, otherwise bundle B" but I don't think that's vital (and this change is orthogonal to it).

james7132 and others added 2 commits March 4, 2024 19:13
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@james7132 james7132 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 Mar 5, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 5, 2024
Merged via the queue into bevyengine:main with commit dc40cd1 Mar 5, 2024
26 checks passed
spectria-limina pushed a commit to spectria-limina/bevy that referenced this pull request Mar 9, 2024
# Objective
When doing a final pass for bevyengine#3362, it appeared that `ComponentStorage`
as a trait, the two types implementing it, and the associated type on
`Component` aren't really necessary anymore. This likely was due to an
earlier constraint on the use of consts in traits, but that definitely
doesn't seem to be a problem in Rust 1.76.

## Solution
Remove them.

---

## Changelog
Changed: `Component::Storage` has been replaced with
`Component::STORAGE_TYPE` as a const.
Removed: `bevy::ecs::component::ComponentStorage` trait
Removed: `bevy::ecs::component::TableStorage` struct
Removed: `bevy::ecs::component::SparseSetStorage` struct

## Migration Guide
If you were manually implementing `Component` instead of using the
derive macro, replace the associated `Storage` associated type with the
`STORAGE_TYPE` const:

```rust
// in Bevy 0.13
impl Component for MyComponent {
    type Storage = TableStorage;
}
// in Bevy 0.14
impl Component for MyComponent {
    const STORAGE_TYPE: StorageType = StorageType::Table;
}
```

Component is no longer object safe. If you were relying on `&dyn
Component`, `Box<dyn Component>`, etc. please [file an issue
](https://github.com/bevyengine/bevy/issues) to get [this
change](bevyengine#12311) reverted.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@james7132 james7132 deleted the remove-component-storage branch March 10, 2024 07:27
zhaop added a commit to zhaop/renet2 that referenced this pull request Jun 24, 2024
@christianlarrabure
Copy link

I was relying on

Box through the following methods:

pub trait SaveableComponent: bevy::prelude::Component<Storage = TableStorage> {
    fn to_json(&self, oidb: Res<ObjectIdMap>) -> Option<serde_json::Value>;
    fn from_json(
        &mut self,
        json: serde_json::Value,
        commands: &mut EntityCommands,
        oidb: &Res<ObjectIdMap>,
    );
    fn db_component_type(&self) -> SaveableComponentTypes;
}

pub fn to_component(&self) -> Box<dyn SaveableComponent> {
...
}

Is there an alternative to this now? I was using this type to selectively save some of my objects. My use case is very unique; I am making a text-based MUD game. So the normal saving methods that exist in the Bevy community are not appliable to me. I had to develop my own methods for it! Any help appreciated so that I can migrate to 0.14.

@alice-i-cecile
Copy link
Member

Do scenes work for you? I don't see any particular reason why the typical reflection-based approach wouldn't be applicable in a text-based game.

Anyways, maybe make a Discussion so this can be seen more broadly?

@christianlarrabure
Copy link

We have a system which saves it to SQL, I believe that the one that is used saves it to a file? Is there any documentation I can review on the matter? I've been using the ECS more than other parts, so I am not so familiar.

@alice-i-cecile
Copy link
Member

Scene::write_to_world_with is the core bit you need. There's an example for scenes, but it's not particularly useful for you. Instead, I'd recommend digging into the source there and getting a sense of how the type registry and reflection is used to list and then serialize components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants