1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! The example from [section 17.2 of the Rust book][0],
//! modified to use `UnsizedVec` instead of `Vec<Box<_>>>`.
//!
//! [0]: https://doc.rust-lang.org/book/ch17-02-trait-objects.html

#![allow(dead_code)]
#![feature(allocator_api, ptr_metadata, unsized_fn_params)]

use emplacable::by_value_str;
use unsized_vec::{unsize_vec, unsized_vec, UnsizedVec};

mod gui {
    //! lib.rs

    use unsized_vec::UnsizedVec;

    pub trait Draw {
        fn draw(&self);
    }
    pub struct Screen {
        pub components: UnsizedVec<dyn Draw>,
    }

    impl Screen {
        pub fn run(&self) {
            for component in self.components.iter() {
                component.draw();
            }
        }
    }
    pub struct Button {
        pub width: u32,
        pub height: u32,
        pub label: Box<str>,
    }

    impl Draw for Button {
        fn draw(&self) {
            // code to actually draw a button
        }
    }
}

// main.rs
use gui::Draw;

struct SelectBox {
    width: u32,
    height: u32,
    options: UnsizedVec<str>,
}

impl Draw for SelectBox {
    fn draw(&self) {
        // code to actually draw a select box
    }
}

use gui::{Button, Screen};

fn main() {
    let screen = Screen {
        components: unsize_vec![
            SelectBox {
                width: 75,
                height: 10,
                options: unsized_vec![
                    by_value_str!("Yes"),
                    by_value_str!("Maybe"),
                    by_value_str!("No"),
                ],
            },
            Button {
                width: 50,
                height: 10,
                label: Box::<str>::from("OK"),
            },
        ],
    };

    screen.run();
}