Skip to content

feat: make T: /*snip */ impl Component a feature flag - #454

Draft
sanbox-irl wants to merge 1 commit into
Ralith:masterfrom
sanbox-irl:feat/blanket-component
Draft

feat: make T: /*snip */ impl Component a feature flag#454
sanbox-irl wants to merge 1 commit into
Ralith:masterfrom
sanbox-irl:feat/blanket-component

Conversation

@sanbox-irl

Copy link
Copy Markdown
Contributor

This is more of a discussion than a full PR -- I think more documentation would be needed to actually merge this.

This PR is about making hecs require a trait to allow a component to be in the ECS.

I explored three different methods of making hecs require a trait to allow a component to be added to the ECS:

  1. The first is this PR. It's kind of a hack, but it's relatively clean. The big loss is any library which uses hecs but then re-exports it, where feature unification will make someone unhappy.
  2. The second was making a macro which wraps hecs and allows a user to create "MyOwnHecs" by wrapping every method on hecs::World with a required feature. It was very boilerplate-y, but entirely external to hecs. That isn't such a bad thing! That may still be the best approach since it rocks the boat the least.
  3. The final approach was adding a bound which took a trait which was impled by a ZST directly into hecs, like this:
pub trait Satisfies<T: ?Sized> {}

pub struct DefaultPolicy;
impl<T: Send + Sync + 'static + ?Sized> Satisfies<T> for DefaultPolicy {}

pub struct World<P = DefaultPolicy>;

impl<P> World<P> {
    pub fn insert_one<T: Component>(&mut self, e: Entity, c: T) -> Result<(), NoSuchEntity>
    where
        P: Satisfies<T>,
    { /* ... */ }
}

This seemed promising at first, but the ergnomics that P = DefaultPolicy seem to offer really fall apart, and simple code which currently compiles fails to compile (notably let world = World::new()). I think this is the wrong approach right now.

I think it's reasonable to point that this is a non-goal of hecs, but at the same time, every mature game that I know of which uses hecs does seem to wrap hecs such that components must satisfy some trait to be added to the ECS.

@sanbox-irl
sanbox-irl force-pushed the feat/blanket-component branch from c852acf to 4563e3d Compare August 17, 2026 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant