semantics/
lib.rs

1#[cfg(feature = "semantics-derive")]
2pub use semantics_derive::Semantics;
3
4pub trait Semantics {
5  type Name;
6  type Index: Copy;
7
8  fn name(&self) -> Self::Name;
9
10  fn index(&self) -> Self::Index;
11}
12
13pub trait HasSemantics<Name = &'static str, Index = u8> {
14  type Sem: Semantics<Name = Name, Index = Index>;
15
16  const SEMANTICS: Self::Sem;
17}
18
19#[cfg(test)]
20mod test {
21  use super::*;
22
23  #[derive(Debug, Eq, PartialEq)]
24  enum VertexSemantics {
25    Position,
26    Normal,
27    Color,
28  }
29
30  impl Semantics for VertexSemantics {
31    type Name = &'static str;
32
33    type Index = u8;
34
35    fn name(&self) -> Self::Name {
36      match *self {
37        VertexSemantics::Position => "position",
38        VertexSemantics::Normal => "normal",
39        VertexSemantics::Color => "color",
40      }
41    }
42
43    fn index(&self) -> Self::Index {
44      match *self {
45        VertexSemantics::Position => 0,
46        VertexSemantics::Normal => 1,
47        VertexSemantics::Color => 2,
48      }
49    }
50  }
51
52  struct VPos(pub [f32; 3]);
53
54  impl HasSemantics for VPos {
55    type Sem = VertexSemantics;
56
57    const SEMANTICS: Self::Sem = VertexSemantics::Position;
58  }
59
60  struct VNor(pub [f32; 3]);
61
62  impl HasSemantics for VNor {
63    type Sem = VertexSemantics;
64
65    const SEMANTICS: Self::Sem = VertexSemantics::Normal;
66  }
67
68  struct VCol(pub [f32; 3]);
69
70  impl HasSemantics for VCol {
71    type Sem = VertexSemantics;
72
73    const SEMANTICS: Self::Sem = VertexSemantics::Color;
74  }
75
76  struct Vertex {
77    pos: VPos,
78    nor: VNor,
79    col: VCol,
80  }
81
82  #[test]
83  fn test_vertex_semantics() {
84    assert_eq!(VertexSemantics::Position.name(), "position");
85    assert_eq!(VertexSemantics::Normal.name(), "normal");
86    assert_eq!(VertexSemantics::Color.name(), "color");
87
88    assert_eq!(VertexSemantics::Position.index(), 0);
89    assert_eq!(VertexSemantics::Normal.index(), 1);
90    assert_eq!(VertexSemantics::Color.index(), 2);
91
92    assert_eq!(VPos::SEMANTICS, VertexSemantics::Position);
93  }
94}