simple/
simple.rs

1use sized::SizedBox;
2
3extern crate sized;
4
5#[repr(C)]
6#[derive(Default)]
7struct Implementation1 {
8    _padding: [u8; 16],
9}
10
11impl DoesSomething for Implementation1 {
12    fn do_the_thing(&mut self) {
13        println!("Implementation1")
14    }
15}
16
17#[repr(C)]
18#[derive(Default)]
19struct Implementation2 {
20    _padding: [u8; 31],
21}
22
23impl DoesSomething for Implementation2 {
24    fn do_the_thing(&mut self) {
25        println!("Implementation2")
26    }
27}
28
29#[repr(C)]
30#[derive(Default)]
31struct TooLarge {
32    _padding: [u8; 32],
33}
34
35impl DoesSomething for TooLarge {
36    fn do_the_thing(&mut self) {
37        println!("Implementation2")
38    }
39}
40
41trait DoesSomething {
42    fn do_the_thing(&mut self);
43}
44
45struct StoreDynType {
46    dyn_type: SizedBox<dyn DoesSomething, 31>,
47}
48
49fn main() {
50    let mut state = StoreDynType {
51        dyn_type: SizedBox::new(Implementation1::default()),
52    };
53
54    state.dyn_type.do_the_thing();
55
56    state.dyn_type = SizedBox::new(Implementation2::default());
57
58    state.dyn_type.do_the_thing();
59
60    // This should panic at compile time
61    //state.dyn_type = SizedBox::new(TooLarge::default());
62
63    //state.dyn_type.do_the_thing();
64}