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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Create static chains of objects with different types.
//!
//! In general, the chain starts (or ends, depending on your view) with a `Chain` element
//! and is built up from any number of `Link`s. This basic structure only allows you
//! to query the number of elements, but you can implement a more useful trait for both `Link` and
//! `Chain` to make this structure more useful.

mod private {
    pub trait Sealed {}

    impl<V> Sealed for super::Chain<V> {}
    impl<V, C: super::ChainElement> Sealed for super::Link<V, C> {}
}

/// A generic chain element
pub trait ChainElement: Sized + private::Sealed {
    /// Return the number of objects linked to this chain element
    fn len(&self) -> usize;
}

/// This piece of the chain contains some object
pub struct Link<V, C: ChainElement> {
    /// The current object
    pub object: V,

    /// The rest of the object chain
    pub parent: C,
}

impl<V, C: ChainElement> Link<V, C> {
    /// Append an object to the chain
    #[inline]
    pub fn append<T>(self, item: T) -> Link<T, Self> {
        Link {
            object: item,
            parent: self,
        }
    }
}

impl<V, C> Clone for Link<V, C>
where
    V: Clone,
    C: ChainElement + Clone,
{
    fn clone(&self) -> Self {
        Self {
            object: self.object.clone(),
            parent: self.parent.clone(),
        }
    }
}

impl<V, VC> ChainElement for Link<V, VC>
where
    VC: ChainElement,
{
    #[inline]
    fn len(&self) -> usize {
        self.parent.len() + 1
    }
}

/// This piece marks the end of a chain
pub struct Chain<V> {
    /// The wrapped object.
    pub object: V,
}

impl<V> Chain<V> {
    /// Append an object to the chain
    #[inline]
    pub fn append<T>(self, item: T) -> Link<T, Self> {
        Link {
            object: item,
            parent: self,
        }
    }
}

impl<V> Chain<V> {
    /// Create a new [`Chain`] by wrapping the given object.
    #[inline]
    pub const fn new(object: V) -> Self {
        Self { object }
    }
}

impl<V> Clone for Chain<V>
where
    V: Clone,
{
    fn clone(&self) -> Self {
        Self {
            object: self.object.clone(),
        }
    }
}

impl<V> ChainElement for Chain<V> {
    #[inline]
    fn len(&self) -> usize {
        1
    }
}

/// Internal implementation of chain macro
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! chain_impl {
    ($x:ty) => {
        Chain<$x>
    };
    ($x:ty,) => {
        Chain<$x>
    };
    ($x:ty, $($rest:tt)+) => {
        Link<$x, chain_impl! { $($rest)+ }>
    };
}

/// Reverse the argument list to generate object chain
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! reverse {
    ([] $($reversed:tt)+) => {
        chain_impl! { $($reversed)+ }
    };
    ([$first:ty] $($reversed:tt)*) => {
        reverse! { [ ] $first, $($reversed)* }
    };
    ([$first:ty, $($rest:ty),*] $($reversed:tt)*) => {
        reverse! { [ $($rest),* ] $first, $($reversed)* }
    };
}

/// Creates an object chain from the argument types.
///
/// This macro reduces the boilerplate required to describe the type of an object chain.
///
/// # Example:
///
/// Instead of writing this...
///
/// ```rust
/// use embedded_layout::prelude::*;
/// use embedded_graphics::primitives::{Circle, Rectangle, Triangle};
/// type Views = Link<Rectangle, Link<Circle, Chain<Triangle>>>;
/// ```
///
/// ... the `chain!` macro allows you to write this:
///
/// ```rust
/// use embedded_layout::prelude::*;
/// use embedded_graphics::primitives::{Circle, Rectangle, Triangle};
/// type Views = chain! { Triangle, Circle, Rectangle };
/// ```
///
/// Note also how the order of types follows the type of objects in the chain instead of being
/// reversed.
#[macro_export(local_inner_macros)]
macro_rules! chain {
    ( $($types:ty),+ ) => {
        reverse!{ [ $($types),+ ] }
    };
}

#[cfg(test)]
mod test {
    #![allow(dead_code)]
    use super::*;
    use core::marker::PhantomData;

    struct CompileTest {
        chain1: chain! {
            u8
        },
        generic_in_chain: chain! {
            Generic<'static, u32>
        },
        chain: chain! {
            u8, u16, u32
        },
    }

    struct Generic<'a, T> {
        field: PhantomData<&'a T>,
    }

    #[test]
    pub fn test() {
        fn f(_obj_chain: &chain! {u8, u16, u32}) {}

        let test = CompileTest {
            chain1: Chain::new(0),
            generic_in_chain: Chain::new(Generic { field: PhantomData }),
            chain: Chain::new(0u8).append(1u16).append(2u32),
        };

        f(&test.chain);
    }

    #[test]
    pub fn test_count() {
        assert_eq!(1, Chain::new(0).len());
        assert_eq!(3, Chain::new(0u8).append(1u16).append(2u32).len());
    }
}

#[cfg(test)]
#[allow(dead_code)]
mod test_macro {
    use crate::prelude::*;
    use embedded_graphics::primitives::{Rectangle, Triangle};

    type Views = chain! {
        Rectangle,
        Rectangle,
        Triangle
    };
}