Unnamed repository; edit this file 'description' to name the repository.
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
//! Convenience macros.

/// Appends formatted string to a `String`.
#[macro_export]
macro_rules! format_to {
    ($buf:expr) => ();
    ($buf:expr, $lit:literal $($arg:tt)*) => {
        {
            use ::std::fmt::Write as _;
            // We can't do ::std::fmt::Write::write_fmt($buf, format_args!($lit $($arg)*))
            // unfortunately, as that loses out on autoref behavior.
            _ = $buf.write_fmt(format_args!($lit $($arg)*))
        }
    };
}

/// Appends formatted string to a `String` and returns the `String`.
///
/// Useful for folding iterators into a `String`.
#[macro_export]
macro_rules! format_to_acc {
    ($buf:expr, $lit:literal $($arg:tt)*) => {
        {
            use ::std::fmt::Write as _;
            // We can't do ::std::fmt::Write::write_fmt($buf, format_args!($lit $($arg)*))
            // unfortunately, as that loses out on autoref behavior.
            _ = $buf.write_fmt(format_args!($lit $($arg)*));
            $buf
        }
    };
}

/// Generates `From` impls that wrap values in, or map variants between, enums.
///
/// Enum mappings with `Source => Target` rename the variant and convert its payload with `.into()`.
///
/// # Examples
///
/// ```ignore
/// impl_from!(Struct, Union, Enum for Adt);
/// impl_from!(impl<'db> Item, InternedItem<'db> for ItemId<'db>);
/// impl_from!(AssocItem { Function, Const, TypeAlias } for Item);
/// impl_from!(AdtId { StructId => Struct, EnumId => Enum } for Adt);
/// ```
#[macro_export]
macro_rules! impl_from {
    (
        impl<$lifetime:lifetime>
        $source:ident $(<$source_lifetime:lifetime>)?
        { $($source_variant:ident $(=> $target_variant:ident)?),* $(,)? }
        for $target:ident<$target_lifetime:lifetime>
    ) => {
        impl<$lifetime> From<$source$(<$source_lifetime>)?> for $target<$target_lifetime> {
            fn from(value: $source$(<$source_lifetime>)?) -> Self {
                match value {
                    $(
                        $source::$source_variant(value) => $crate::impl_from!(
                            @map_enum_variant $target, value, $source_variant
                            $(=> $target_variant)?
                        ),
                    )*
                }
            }
        }
    };
    (
        $source:ident
        { $($source_variant:ident $(=> $target_variant:ident)?),* $(,)? }
        for $target:ident
    ) => {
        impl From<$source> for $target {
            fn from(value: $source) -> Self {
                match value {
                    $(
                        $source::$source_variant(value) => $crate::impl_from!(
                            @map_enum_variant $target, value, $source_variant
                            $(=> $target_variant)?
                        ),
                    )*
                }
            }
        }
    };
    (@map_enum_variant $target:ident, $value:ident, $variant:ident) => {
        $target::$variant($value)
    };
    (
        @map_enum_variant $target:ident, $value:ident,
        $source_variant:ident => $target_variant:ident
    ) => {
        $target::$target_variant($value.into())
    };
    (
        impl<$lifetime:lifetime>
        $(
            $variant:ident $(<$variant_lifetime:lifetime>)?
            $(($($sub_variant:ident $(<$sub_variant_lifetime:lifetime>)?),*))?
        ),*
        for $enum:ident<$enum_lifetime:lifetime>
    ) => {
        $(
            impl<$lifetime> From<$variant$(<$variant_lifetime>)?> for $enum<$enum_lifetime> {
                fn from(it: $variant$(<$variant_lifetime>)?) -> $enum<$enum_lifetime> {
                    $enum::$variant(it)
                }
            }
            $($(
                impl<$lifetime> From<$sub_variant$(<$sub_variant_lifetime>)?>
                    for $enum<$enum_lifetime>
                {
                    fn from(
                        it: $sub_variant$(<$sub_variant_lifetime>)?,
                    ) -> $enum<$enum_lifetime> {
                        $enum::$variant($variant::$sub_variant(it))
                    }
                }
            )*)?
        )*
    };
    ($($variant:ident $(($($sub_variant:ident),*))?),* for $enum:ident) => {
        $(
            impl From<$variant> for $enum {
                fn from(it: $variant) -> $enum {
                    $enum::$variant(it)
                }
            }
            $($(
                impl From<$sub_variant> for $enum {
                    fn from(it: $sub_variant) -> $enum {
                        $enum::$variant($variant::$sub_variant(it))
                    }
                }
            )*)?
        )*
    };
    ($($variant:ident$(<$V:ident>)?),* for $enum:ident) => {
        $(
            impl$(<$V>)? From<$variant$(<$V>)?> for $enum$(<$V>)? {
                fn from(it: $variant$(<$V>)?) -> $enum$(<$V>)? {
                    $enum::$variant(it)
                }
            }
        )*
    }
}