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
#[cfg(feature = "ops")]
pub(crate) mod deref {
    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["Deref"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        Ok(derive_trait(data, parse_quote!(::core::ops::Deref), None, parse_quote! {
            trait Deref {
                type Target;
                #[inline]
                fn deref(&self) -> &Self::Target;
            }
        }))
    }
}

#[cfg(feature = "ops")]
pub(crate) mod deref_mut {
    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["DerefMut"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        Ok(derive_trait(
            data,
            parse_quote!(::core::ops::DerefMut),
            Some(format_ident!("Target")),
            parse_quote! {
                trait DerefMut: ::core::ops::Deref {
                    #[inline]
                    fn deref_mut(&mut self) -> &mut Self::Target;
                }
            },
        ))
    }
}

#[cfg(feature = "ops")]
pub(crate) mod index {
    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["Index"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        Ok(derive_trait(data, parse_quote!(::core::ops::Index), None, parse_quote! {
            trait Index<__Idx> {
                type Output;
                #[inline]
                fn index(&self, index: __Idx) -> &Self::Output;
            }
        }))
    }
}

#[cfg(feature = "ops")]
pub(crate) mod index_mut {
    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["IndexMut"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        Ok(derive_trait(
            data,
            parse_quote!(::core::ops::IndexMut),
            Some(format_ident!("Output")),
            parse_quote! {
                trait IndexMut<__Idx>: ::core::ops::Index<__Idx> {
                    #[inline]
                    fn index_mut(&mut self, index: __Idx) -> &mut Self::Output;
                }
            },
        ))
    }
}

#[cfg(feature = "ops")]
pub(crate) mod range_bounds {
    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["RangeBounds"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        Ok(derive_trait(data, parse_quote!(::core::ops::RangeBounds), None, parse_quote! {
            trait RangeBounds<__T: ?Sized> {
                #[inline]
                fn start_bound(&self) -> ::core::ops::Bound<&__T>;
                #[inline]
                fn end_bound(&self) -> ::core::ops::Bound<&__T>;
            }
        }))
    }
}

#[cfg(feature = "generator_trait")]
pub(crate) mod generator {
    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["Generator"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        Ok(derive_trait(data, parse_quote!(::core::ops::Generator), None, parse_quote! {
            trait Generator<R> {
                type Yield;
                type Return;
                #[inline]
                fn resume(
                    self: ::core::pin::Pin<&mut Self>,
                    arg: R,
                ) -> ::core::ops::GeneratorState<Self::Yield, Self::Return>;
            }
        }))
    }
}

#[cfg(feature = "fn_traits")]
pub(crate) mod fn_ {
    use derive_utils::EnumImpl;
    use syn::TypeParam;

    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["Fn"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        let trait_path = quote!(::core::ops::Fn);
        let trait_ = quote!(#trait_path(__T) -> __U);
        let fst = data.field_types().next();
        let mut impl_ = EnumImpl::new(data);

        impl_.set_trait(parse_quote!(#trait_path<(__T,)>));
        impl_.push_generic_param(TypeParam::from(format_ident!("__T")).into());
        impl_.push_generic_param(TypeParam::from(format_ident!("__U")).into());

        impl_.push_where_predicate(parse_quote!(#fst: #trait_));
        data.field_types()
            .skip(1)
            .for_each(|f| impl_.push_where_predicate(parse_quote!(#f: #trait_)));

        impl_.push_method(parse_quote! {
            #[inline]
            extern "rust-call" fn call(&self, args: (__T,)) -> Self::Output;
        });

        Ok(impl_.build())
    }
}

#[cfg(feature = "fn_traits")]
pub(crate) mod fn_mut {
    use derive_utils::EnumImpl;
    use syn::TypeParam;

    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["FnMut"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        let trait_path = quote!(::core::ops::FnMut);
        let trait_ = quote!(#trait_path(__T) -> __U);
        let fst = data.field_types().next();
        let mut impl_ = EnumImpl::new(data);

        impl_.set_trait(parse_quote!(#trait_path<(__T,)>));
        impl_.push_generic_param(TypeParam::from(format_ident!("__T")).into());
        impl_.push_generic_param(TypeParam::from(format_ident!("__U")).into());

        impl_.push_where_predicate(parse_quote!(#fst: #trait_));
        data.field_types()
            .skip(1)
            .for_each(|f| impl_.push_where_predicate(parse_quote!(#f: #trait_)));

        impl_.push_method(parse_quote! {
            #[inline]
            extern "rust-call" fn call_mut(&mut self, args: (__T,)) -> Self::Output;
        });

        Ok(impl_.build())
    }
}

#[cfg(feature = "fn_traits")]
pub(crate) mod fn_once {
    use derive_utils::EnumImpl;
    use syn::TypeParam;

    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["FnOnce"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        let trait_path = quote!(::core::ops::FnOnce);
        let trait_ = quote!(#trait_path(__T) -> __U);
        let fst = data.field_types().next();
        let mut impl_ = EnumImpl::new(data);

        impl_.set_trait(parse_quote!(#trait_path<(__T,)>));
        impl_.push_generic_param(TypeParam::from(format_ident!("__T")).into());
        impl_.push_generic_param(TypeParam::from(format_ident!("__U")).into());

        impl_.push_where_predicate(parse_quote!(#fst: #trait_));
        data.field_types()
            .skip(1)
            .for_each(|f| impl_.push_where_predicate(parse_quote!(#f: #trait_)));

        impl_.append_items_from_trait(parse_quote! {
            trait FnOnce {
                type Output;
                #[inline]
                extern "rust-call" fn call_once(self, args: (__T,)) -> Self::Output;
            }
        });

        Ok(impl_.build())
    }
}