clay_core/object/
mapper.rs

1use std::collections::HashSet;
2use crate::{
3    prelude::*,
4    map::*,
5    object::*,
6};
7
8/// A new object obtained by applying some mapping to another object.
9pub struct ObjectMapper<O: Object, M: Map> {
10    pub object: O,
11    pub map: M,
12}
13
14impl<O: Object, M: Map> ObjectMapper<O, M> {
15    pub fn new(object: O, map: M) -> Self {
16        Self { object, map }
17    }
18}
19
20impl<O: Object, M: Map> Object for ObjectMapper<O, M> {}
21
22impl<O: Object, M: Map> Instance<ObjectClass> for ObjectMapper<O, M> {
23    fn source(cache: &mut HashSet<u64>) -> String {
24        if !cache.insert(Self::type_hash()) {
25            return String::new()
26        }
27        [
28            O::source(cache),
29            M::source(cache),
30            "#include <clay_core/object/mapper.h>".to_string(),
31            format!(
32                "MAP_OBJECT_FN_DEF({}, {}, {}, {}, {})",
33                Self::inst_name(),
34                O::inst_name(),
35                M::inst_name(),
36                O::size_int(), O::size_float(),
37            ),
38        ].join("\n")
39    }
40    fn inst_name() -> String {
41        format!(
42            "__mapper_{:x}",
43            Self::type_hash(),
44        )
45    }
46}
47
48
49impl<O: Object, M: Map> Pack for ObjectMapper<O, M> {
50    fn size_int() -> usize {
51        O::size_int() + M::size_int()
52    }
53    fn size_float() -> usize {
54        O::size_float() + M::size_float()
55    }
56    fn pack_to(&self, buffer_int: &mut [i32], buffer_float: &mut [f32]) {
57        Packer::new(buffer_int, buffer_float)
58        .pack(&self.object)
59        .pack(&self.map);
60    }
61}