default_constructor/
lib.rs

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
#![doc = include_str!("../README.md")]
#![no_std]
mod convert;
pub use convert::{infer_into, InferInto, StandardConverters};
pub use default_constructor_macros::meta_default_constructor;
/// Standard intermediate functions that can be called via the `@name` syntax.
///
/// This enables conversion chains like `&str --into--> String --@some--> Option<String>`.
pub mod effects {
    extern crate alloc;

    /// Construct a `Box`.
    #[cfg(feature = "std")]
    pub fn boxed<T>(item: T) -> alloc::boxed::Box<T> {
        alloc::boxed::Box::new(item)
    }

    /// Construct an `Rc`.
    #[cfg(feature = "std")]
    pub fn rc<T>(item: T) -> alloc::rc::Rc<T> {
        alloc::rc::Rc::new(item)
    }

    /// Construct an `Arc`.
    #[cfg(feature = "std")]
    pub fn arc<T>(item: T) -> alloc::sync::Arc<T> {
        alloc::sync::Arc::new(item)
    }

    /// Construct a `Cow`.
    #[cfg(feature = "std")]
    pub fn cow<T: Clone>(item: T) -> alloc::borrow::Cow<'static, T> {
        alloc::borrow::Cow::Owned(item)
    }

    /// Construct a `Some`.
    pub fn some<T>(item: T) -> Option<T> {
        Option::Some(item)
    }

    /// Construct an iterator from an array.
    ///
    /// This is magic since this also enables macro recursion on array literals.
    ///
    /// `[a, b, c] -> [a.into(), b.into(), c.into()].into_iter().collect()`.
    pub fn arr<T, I: IntoIterator<Item = T> + FromIterator<T>, const N: usize>(item: [T; N]) -> I {
        item.into_iter().collect()
    }
}

/// A standard constructor that uses [`Into`].
///
/// # Syntax
///
/// ```
/// # /*
/// construct! {
///     Student {
///         name: "Timmy",
///         age: 10,
///         father : {
///             name: "Tommy",
///             age: 35,
///         }
///     }
/// }
/// # */
/// ```
#[macro_export]
macro_rules! construct {
    ($($tt: tt)*) => {
        {
            use $crate::effects::*;
            $crate::meta_default_constructor! {
                [::std::convert::Into::into]
                $($tt)*
            }
        }
    };
}

/// A standard constructor that uses [`InferInto`].
///
/// [`InferInto`] is inference based and will fail if multiple implementations
/// of the same conversion exists.
///
/// # Syntax
///
/// ```
/// # /*
/// infer_construct! {
///     Student {
///         name: "Timmy",
///         age: 10,
///         father : {
///             name: "Tommy",
///             age: 35,
///         }
///     }
/// }
/// # */
/// ```
#[macro_export]
macro_rules! infer_construct {
    ($($tt: tt)*) => {
        {
            use $crate::effects::*;
            $crate::meta_default_constructor! {
                [$crate::infer_into]
                $($tt)*
            }
        }
    };
}