Expand description

Mixing hecs entity builder with legion intoComponentSource

let mut world = legion::World::new(Default::default());
let mut components = EntityBuilder::new();
components.add(42i32);
components.add(true);
components.add(vec![0, 1, 2, 3]);
components.add("hello");
components.add(0u64);
let components = components.build();
let entity = world.extend(components)[0];
assert_eq!(Ok(&42), world.entry(entity).unwrap().get_component::<i32>());
assert_eq!(Ok(&vec![0, 1, 2, 3]), world.entry(entity).unwrap().get_component::<Vec<i32>>());

Possible build facilities

struct Builder<T, S> {
    inner: BuiltEntity,
    phantom: PhantomData<T,S>
}
fn new<T:TypeTrait, H:NodeHashs>(t:T, h:H) -> Builder<T,Typed> {
    let mut inner = BuiltEntity::default();
    inner.add(t);
    inner.add(h);
    Builder {
        inner,
        phantom: PhantomData
    }
}
 
trait Final {}
struct Typed;
struct Keyword;
impl Final for Keyword {}
struct Labeled;
impl Final for Labeled {}
struct WithChildren;
impl Final for WithChildren {}
 
// use a bound on T to know if it can have a label ?
impl<T> Builder<T,Typed> {
    pub fn label(self, l: LabelIdentifier) -> Builder<T, Labeled> {
        let mut inner = self.inner;
        inner.add(l);
        Builder {
            inner,
            phantom: PhantomData
        }
    }
    pub fn children(self, cs: Children) -> Builder<T, WithChildren> {
        let mut inner = self.inner;
        inner.add(cs);
        Builder {
            inner,
            phantom: PhantomData
        }
    }
    pub fn add_metadata(self, md: MD) -> Builder<T, Keyword> {
        let mut inner = self.inner;
        inner.add(md);
        Builder {
            inner,
            phantom: PhantomData
        }
    }
}
 
impl<T, S:Final> Builder<T,S> {
    pub fn add_metadata(self, md: MD) -> Builder<T, S> {
        let mut inner = self.inner;
        inner.add(md);
        Builder {
            inner,
            phantom: PhantomData
        }
    }
    pub fn build(self) -> BuiltEntity {
        self.inner.build()
    }
}
 

Structs