flax/metadata/
mod.rs

1use crate::{
2    buffer::ComponentBuffer,
3    component::{ComponentDesc, ComponentValue},
4    components::name,
5};
6
7mod debuggable;
8mod relation;
9
10pub use debuggable::*;
11pub use relation::*;
12
13/// Additional data that can attach itself to a component
14///
15/// Implementors of this trait are proxy types for attaching the proper
16/// components.
17pub trait Metadata<T: ComponentValue> {
18    /// Attach the metadata to the component buffer
19    fn attach(desc: ComponentDesc, buffer: &mut ComponentBuffer);
20}
21
22#[derive(Debug, Clone)]
23/// Provides a name for components
24pub struct Name;
25
26impl<T> Metadata<T> for Name
27where
28    T: ComponentValue,
29{
30    fn attach(desc: ComponentDesc, buffer: &mut ComponentBuffer) {
31        buffer.set(name(), desc.name().into());
32    }
33}
34
35#[cfg(test)]
36mod test {
37    use alloc::string::String;
38
39    use crate::component;
40
41    use super::*;
42
43    #[test]
44    fn metadata_attach() {
45        component! {
46            foo: String => [crate::Debuggable],
47        }
48
49        let meta = foo().desc().create_meta();
50
51        assert!(meta.get(debuggable()).is_some());
52        assert_eq!(meta.get(name()), Some(&"foo".into()));
53    }
54}