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
//! This crates provides `new_flattened!` macro which enables you to write
//! ```rust,ignore
//! new_flattened!(42, Box, Box, Box)
//! ```
//! instead of
//! ```rust,ignore
//! Box::new(Box::new(Box::new(42)))
//! ```

/// Macro to flatten nested calls of `new`s.
///
/// # Examples
///
/// ```rust
/// use new_flattened::new_flattened;
/// assert_eq!(
///     Box::new(Box::new(Box::new(42))),
///     new_flattened!(42, Box, Box, Box),
/// )
/// ```
#[macro_export]
macro_rules! new_flattened {
    ($value:expr, $container:ident) => {
        $container::new($value)
    };
    ($value:expr, $container:ident, $($containers:ident),+) => {
        $container::new(new_flattened!($value, $($containers),+))
    };
}

#[cfg(test)]
mod tests {
    #[test]
    fn simple() {
        assert_eq!(
            Box::new(Box::new(Box::new(5))),
            new_flattened!(5, Box, Box, Box),
        );
    }
}