| 1 | // Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | |
| 3 | use heckrust-analyzer cargo heck 0.5.0 crate/::ToKebabCaserust-analyzer cargo heck 0.5.0 kebab/ToKebabCase#; |
| 4 | use proc_macro2rust-analyzer cargo proc-macro2 1.0.106 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#; |
| 5 | use quoterust-analyzer cargo quote 1.0.44 crate/::{format_identrust-analyzer cargo quote 1.0.44 format/format_ident!, quoterust-analyzer cargo quote 1.0.44 quote!}; |
| 6 | use synrust-analyzer cargo syn 2.0.117 crate/::{Datarust-analyzer cargo syn 2.0.117 derive/Data#, DeriveInputrust-analyzer cargo syn 2.0.117 derive/DeriveInput#, Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#, FieldsNamedrust-analyzer cargo syn 2.0.117 data/FieldsNamed#, FieldsUnnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#, Genericsrust-analyzer cargo syn 2.0.117 generics/Generics#, Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, Typerust-analyzer cargo syn 2.0.117 ty/Type#}; |
| 7 | |
| 8 | use craterust-analyzer cargo glib-macros 0.22.6 crate/::utilsrust-analyzer cargo glib-macros 0.22.6 utils/::crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new().; |
| 9 | |
| 10 | pub fn impl_variantrust-analyzer cargo glib-macros 0.22.6 variant_derive/impl_variant().(input: DeriveInputrust-analyzer cargo syn 2.0.117 derive/DeriveInput#) -> synrust-analyzer cargo syn 2.0.117 crate/::Resultrust-analyzer cargo syn 2.0.117 error/Result#<TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#> { |
| 11 | match input.datarust-analyzer cargo syn 2.0.117 derive/DeriveInput#data. { |
| 12 | Datarust-analyzer cargo syn 2.0.117 derive/Data#::Structrust-analyzer cargo syn 2.0.117 derive/Data#Struct#(data_struct) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(derive_variant_for_structrust-analyzer cargo glib-macros 0.22.6 variant_derive/derive_variant_for_struct().( |
| 13 | input.identrust-analyzer cargo syn 2.0.117 derive/DeriveInput#ident., |
| 14 | input.genericsrust-analyzer cargo syn 2.0.117 derive/DeriveInput#generics., |
| 15 | data_struct, |
| 16 | )), |
| 17 | Datarust-analyzer cargo syn 2.0.117 derive/Data#::Enumrust-analyzer cargo syn 2.0.117 derive/Data#Enum#(data_enum) => { |
| 18 | let mode = get_enum_moderust-analyzer cargo glib-macros 0.22.6 variant_derive/get_enum_mode().(&input.attrsrust-analyzer cargo syn 2.0.117 derive/DeriveInput#attrs.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 19 | let has_data = data_enum |
| 20 | .variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants. |
| 21 | .iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().() |
| 22 | .anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|v| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(v.fieldsrust-analyzer cargo syn 2.0.117 data/Variant#fields., synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unitrust-analyzer cargo syn 2.0.117 data/Fields#Unit#)); |
| 23 | if has_data { |
| 24 | derive_variant_for_enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/derive_variant_for_enum().(input.identrust-analyzer cargo syn 2.0.117 derive/DeriveInput#ident., input.genericsrust-analyzer cargo syn 2.0.117 derive/DeriveInput#generics., data_enum, mode) |
| 25 | } else { |
| 26 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(derive_variant_for_c_enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/derive_variant_for_c_enum().( |
| 27 | input.identrust-analyzer cargo syn 2.0.117 derive/DeriveInput#ident., |
| 28 | input.genericsrust-analyzer cargo syn 2.0.117 derive/DeriveInput#generics., |
| 29 | data_enum, |
| 30 | mode, |
| 31 | )) |
| 32 | } |
| 33 | } |
| 34 | Datarust-analyzer cargo syn 2.0.117 derive/Data#::Unionrust-analyzer cargo syn 2.0.117 derive/Data#Union#(..) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.117 crate/::Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().( |
| 35 | input, |
| 36 | "#[derive(glib::Variant)] is not available for unions.", |
| 37 | )), |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn derive_variant_for_structrust-analyzer cargo glib-macros 0.22.6 variant_derive/derive_variant_for_struct().( |
| 42 | ident: Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, |
| 43 | generics: Genericsrust-analyzer cargo syn 2.0.117 generics/Generics#, |
| 44 | data_struct: synrust-analyzer cargo syn 2.0.117 crate/::DataStructrust-analyzer cargo syn 2.0.117 derive/DataStruct#, |
| 45 | ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream# { |
| 46 | let glib = crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new().(); |
| 47 | let (impl_generics, type_generics, where_clause) = generics.split_for_implrust-analyzer cargo syn 2.0.117 generics/impl#[Generics]split_for_impl().(); |
| 48 | let (static_variant_type, to_variant, from_variant) = match data_struct.fieldsrust-analyzer cargo syn 2.0.117 derive/DataStruct#fields. { |
| 49 | Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.117 data/Fields#Unnamed#(FieldsUnnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed# { unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed., .. }) => { |
| 50 | let types = unnamed |
| 51 | .into_pairsrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]into_pairs().() |
| 52 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|pair| pair.into_valuerust-analyzer cargo syn 2.0.117 punctuated/impl#[`Pair<T, P>`]into_value().()) |
| 53 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| field.tyrust-analyzer cargo syn 2.0.117 data/Field#ty.) |
| 54 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 55 | |
| 56 | let idents = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#types.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(synrust-analyzer cargo syn 2.0.117 crate/::Indexrust-analyzer cargo syn 2.0.117 expr/Index#::fromrust-analyzer cargo syn 2.0.117 expr/impl#[Index][`From<usize>`]from().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 57 | let idents_len = idents.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 58 | |
| 59 | let static_variant_type = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 60 | impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause { |
| 61 | #[inline] |
| 62 | fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> { |
| 63 | static TYP: ::std::sync::OnceLock<#glib::VariantType> = ::std::sync::OnceLock::new(); |
| 64 | ::std::borrow::Cow::Borrowed(TYP.get_or_init(|| { |
| 65 | |
| 66 | let mut builder = #glib::GStringBuilder::new("("); |
| 67 | |
| 68 | #( |
| 69 | { |
| 70 | let typ = <#types as #glib::variant::StaticVariantType>::static_variant_type(); |
| 71 | builder.append(typ.as_str()); |
| 72 | } |
| 73 | )* |
| 74 | builder.append_c(')'); |
| 75 | |
| 76 | #glib::VariantType::from_string(builder.into_string()).unwrap() |
| 77 | })) |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | let to_variant = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 83 | impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause { |
| 84 | fn to_variant(&self) -> #glib::Variant { |
| 85 | #glib::Variant::tuple_from_iter(::std::array::IntoIter::<#glib::Variant, #idents_len>::new([ |
| 86 | #( |
| 87 | #glib::variant::ToVariant::to_variant(&self.#idents) |
| 88 | ),* |
| 89 | ])) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause { |
| 94 | fn from(v: #ident #type_generics) -> #glib::Variant { |
| 95 | #glib::Variant::tuple_from_iter(::std::array::IntoIter::<#glib::Variant, #idents_len>::new([ |
| 96 | #( |
| 97 | <#glib::Variant as ::std::convert::From<_>>::from(v.#idents) |
| 98 | ),* |
| 99 | ])) |
| 100 | } |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | let from_variant = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 105 | impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause { |
| 106 | fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> { |
| 107 | if !variant.is_container() { |
| 108 | return ::core::option::Option::None; |
| 109 | } |
| 110 | ::core::option::Option::Some(Self( |
| 111 | #( |
| 112 | match variant.try_child_get::<#types>(#idents) { |
| 113 | ::core::result::Result::Ok(::core::option::Option::Some(field)) => field, |
| 114 | _ => return ::core::option::Option::None, |
| 115 | } |
| 116 | ),* |
| 117 | )) |
| 118 | } |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | (static_variant_type, to_variant, from_variant) |
| 123 | } |
| 124 | Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Namedrust-analyzer cargo syn 2.0.117 data/Fields#Named#(FieldsNamedrust-analyzer cargo syn 2.0.117 data/FieldsNamed# { namedrust-analyzer cargo syn 2.0.117 data/FieldsNamed#named., .. }) => { |
| 125 | let fields: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, Typerust-analyzer cargo syn 2.0.117 ty/Type#)> = named |
| 126 | .into_pairsrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]into_pairs().() |
| 127 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|pair| pair.into_valuerust-analyzer cargo syn 2.0.117 punctuated/impl#[`Pair<T, P>`]into_value().()) |
| 128 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| (field.identrust-analyzer cargo syn 2.0.117 data/Field#ident..expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Field ident is specified"), field.tyrust-analyzer cargo syn 2.0.117 data/Field#ty.)) |
| 129 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 130 | |
| 131 | let idents: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = fields.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(ident, _ty)| ident).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 132 | let types: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = fields.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_ident, ty)| ty).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 133 | let counts = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#types.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(synrust-analyzer cargo syn 2.0.117 crate/::Indexrust-analyzer cargo syn 2.0.117 expr/Index#::fromrust-analyzer cargo syn 2.0.117 expr/impl#[Index][`From<usize>`]from().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 134 | |
| 135 | let static_variant_type = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 136 | impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause { |
| 137 | #[inline] |
| 138 | fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> { |
| 139 | static TYP: ::std::sync::OnceLock<#glib::VariantType> = ::std::sync::OnceLock::new(); |
| 140 | ::std::borrow::Cow::Borrowed(TYP.get_or_init(|| unsafe { |
| 141 | let ptr = #glib::ffi::g_string_sized_new(16); |
| 142 | #glib::ffi::g_string_append_c(ptr, b'(' as _); |
| 143 | |
| 144 | #( |
| 145 | { |
| 146 | let typ = <#types as #glib::variant::StaticVariantType>::static_variant_type(); |
| 147 | #glib::ffi::g_string_append_len( |
| 148 | ptr, |
| 149 | typ.as_str().as_ptr() as *const _, |
| 150 | typ.as_str().len() as isize, |
| 151 | ); |
| 152 | } |
| 153 | )* |
| 154 | #glib::ffi::g_string_append_c(ptr, b')' as _); |
| 155 | |
| 156 | #glib::translate::from_glib_full( |
| 157 | #glib::ffi::g_string_free(ptr, #glib::ffi::GFALSE) as *mut #glib::ffi::GVariantType |
| 158 | ) |
| 159 | })) |
| 160 | } |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | let to_variant = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 165 | impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause { |
| 166 | fn to_variant(&self) -> #glib::Variant { |
| 167 | #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([ |
| 168 | #( |
| 169 | #glib::variant::ToVariant::to_variant(&self.#idents) |
| 170 | ),* |
| 171 | ])) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause { |
| 176 | fn from(v: #ident #type_generics) -> #glib::Variant { |
| 177 | #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([ |
| 178 | #( |
| 179 | <#glib::Variant as ::std::convert::From<_>>::from(v.#idents) |
| 180 | ),* |
| 181 | ])) |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | |
| 186 | let from_variant = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 187 | impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause { |
| 188 | fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> { |
| 189 | if !variant.is_container() { |
| 190 | return ::core::option::Option::None; |
| 191 | } |
| 192 | ::core::option::Option::Some(Self { |
| 193 | #( |
| 194 | #idents: match variant.try_child_get::<#types>(#counts) { |
| 195 | ::core::result::Result::Ok(::core::option::Option::Some(field)) => field, |
| 196 | _ => return ::core::option::Option::None, |
| 197 | } |
| 198 | ),* |
| 199 | }) |
| 200 | } |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | (static_variant_type, to_variant, from_variant) |
| 205 | } |
| 206 | Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unitrust-analyzer cargo syn 2.0.117 data/Fields#Unit# => { |
| 207 | let static_variant_type = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 208 | impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause { |
| 209 | #[inline] |
| 210 | fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> { |
| 211 | ::std::borrow::Cow::Borrowed(#glib::VariantTy::UNIT) |
| 212 | } |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | let to_variant = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 217 | impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause { |
| 218 | #[inline] |
| 219 | fn to_variant(&self) -> #glib::Variant { |
| 220 | #glib::variant::ToVariant::to_variant(&()) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause { |
| 225 | #[inline] |
| 226 | fn from(v: #ident #type_generics) -> #glib::Variant { |
| 227 | #glib::variant::ToVariant::to_variant(&()) |
| 228 | } |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | let from_variant = quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 233 | impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause { |
| 234 | fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> { |
| 235 | ::core::option::Option::Some(Self) |
| 236 | } |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | (static_variant_type, to_variant, from_variant) |
| 241 | } |
| 242 | }; |
| 243 | |
| 244 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 245 | #static_variant_type |
| 246 | |
| 247 | #to_variant |
| 248 | |
| 249 | #from_variant |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | enum EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode# { |
| 254 | Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String#, |
| 255 | Reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Repr#(Identrust-analyzer cargo proc-macro2 1.0.106 Ident#), |
| 256 | Enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum#repr.: bool }, |
| 257 | Flagsrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags#repr.: bool }, |
| 258 | } |
| 259 | |
| 260 | impl EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode# { |
| 261 | fn tag_typerust-analyzer cargo glib-macros 0.22.6 variant_derive/impl#[EnumMode]tag_type().(&self) -> char { |
| 262 | match self { |
| 263 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String# => 's', |
| 264 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Repr#(repr) => match repr.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() { |
| 265 | "i8" | "i16" => 'n', |
| 266 | "i32" => 'i', |
| 267 | "i64" => 'x', |
| 268 | "u8" => 'y', |
| 269 | "u16" => 'q', |
| 270 | "u32" => 'u', |
| 271 | "u64" => 't', |
| 272 | _ => unimplementedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unimplemented!!(), |
| 273 | }, |
| 274 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum#repr. } => { |
| 275 | if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().repr { |
| 276 | 'i' |
| 277 | } else { |
| 278 | 's' |
| 279 | } |
| 280 | } |
| 281 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Flagsrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags#repr. } => { |
| 282 | if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().repr { |
| 283 | 'u' |
| 284 | } else { |
| 285 | 's' |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | fn derive_variant_for_enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/derive_variant_for_enum().( |
| 293 | ident: Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, |
| 294 | generics: Genericsrust-analyzer cargo syn 2.0.117 generics/Generics#, |
| 295 | data_enum: synrust-analyzer cargo syn 2.0.117 crate/::DataEnumrust-analyzer cargo syn 2.0.117 derive/DataEnum#, |
| 296 | mode: EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#, |
| 297 | ) -> synrust-analyzer cargo syn 2.0.117 crate/::Resultrust-analyzer cargo syn 2.0.117 error/Result#<TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#> { |
| 298 | let glib = crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new().(); |
| 299 | let static_variant_type = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("({}v)", mode.tag_typerust-analyzer cargo glib-macros 0.22.6 variant_derive/impl#[EnumMode]tag_type().()); |
| 300 | let (impl_generics, type_generics, where_clause) = generics.split_for_implrust-analyzer cargo syn 2.0.117 generics/impl#[Generics]split_for_impl().(); |
| 301 | |
| 302 | let to = data_enum.variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants..iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(index, v)| { |
| 303 | let ident = &v.identrust-analyzer cargo syn 2.0.117 data/Variant#ident.; |
| 304 | let tag = match &mode { |
| 305 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String# => { |
| 306 | let nick = ToKebabCaserust-analyzer cargo heck 0.5.0 kebab/ToKebabCase#::to_kebab_caserust-analyzer cargo heck 0.5.0 kebab/impl#[str][ToKebabCase]to_kebab_case().(ident.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 307 | quoterust-analyzer cargo quote 1.0.44 quote!! { #nick } |
| 308 | }, |
| 309 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Repr#(repr) => quoterust-analyzer cargo quote 1.0.44 quote!! { #index as #repr }, |
| 310 | _ => unimplementedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unimplemented!!(), |
| 311 | }; |
| 312 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(v.fieldsrust-analyzer cargo syn 2.0.117 data/Variant#fields., synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unitrust-analyzer cargo syn 2.0.117 data/Fields#Unit#) { |
| 313 | match &mode { |
| 314 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum# { .. } => |
| 315 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.117 crate/::Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().(v, "#[variant_enum(enum) only allowed with C-style enums using #[derive(glib::Enum)]")), |
| 316 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Flagsrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags# { .. } => |
| 317 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.117 crate/::Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().(v, "#[variant_enum(flags) only allowed with bitflags using #[glib::flags]")), |
| 318 | _ => (), |
| 319 | } |
| 320 | } |
| 321 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(match &v.fieldsrust-analyzer cargo syn 2.0.117 data/Variant#fields. { |
| 322 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Namedrust-analyzer cargo syn 2.0.117 data/Fields#Named#(FieldsNamedrust-analyzer cargo syn 2.0.117 data/FieldsNamed# { namedrust-analyzer cargo syn 2.0.117 data/FieldsNamed#named., .. }) => { |
| 323 | let field_names = named.iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| f.identrust-analyzer cargo syn 2.0.117 data/Field#ident..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 324 | let field_names2 = field_names.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/map/impl#[`Map<I, F>`][Clone]clone().(); |
| 325 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 326 | Self::#ident { #(#field_names),* } => #glib::variant::ToVariant::to_variant(&( |
| 327 | #tag, |
| 328 | #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([ |
| 329 | #(#glib::variant::ToVariant::to_variant(&#field_names2)),* |
| 330 | ])) |
| 331 | )) |
| 332 | } |
| 333 | }, |
| 334 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.117 data/Fields#Unnamed#(FieldsUnnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed# { unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed., .. }) => { |
| 335 | let field_names = unnamed.iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(i, _)| { |
| 336 | format_identrust-analyzer cargo quote 1.0.44 format/format_ident!!("field{}", i) |
| 337 | }); |
| 338 | let field_names2 = field_names.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/map/impl#[`Map<I, F>`][Clone]clone().(); |
| 339 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 340 | Self::#ident(#(#field_names),*) => #glib::variant::ToVariant::to_variant(&( |
| 341 | #tag, |
| 342 | #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([ |
| 343 | #(#glib::variant::ToVariant::to_variant(&#field_names2)),* |
| 344 | ])) |
| 345 | )) |
| 346 | } |
| 347 | }, |
| 348 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unitrust-analyzer cargo syn 2.0.117 data/Fields#Unit# => { |
| 349 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 350 | Self::#ident => #glib::variant::ToVariant::to_variant(&( |
| 351 | #tag, |
| 352 | #glib::variant::ToVariant::to_variant(&()) |
| 353 | )) |
| 354 | } |
| 355 | }, |
| 356 | }) |
| 357 | }).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>, _>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 358 | let into = data_enum.variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants..iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(index, v)| { |
| 359 | let field_ident = &v.identrust-analyzer cargo syn 2.0.117 data/Variant#ident.; |
| 360 | let tag = match &mode { |
| 361 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String# => { |
| 362 | let nick = ToKebabCaserust-analyzer cargo heck 0.5.0 kebab/ToKebabCase#::to_kebab_caserust-analyzer cargo heck 0.5.0 kebab/impl#[str][ToKebabCase]to_kebab_case().(field_ident.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 363 | quoterust-analyzer cargo quote 1.0.44 quote!! { #nick } |
| 364 | } |
| 365 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Repr#(repr) => quoterust-analyzer cargo quote 1.0.44 quote!! { #index as #repr }, |
| 366 | _ => unimplementedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unimplemented!!(), |
| 367 | }; |
| 368 | match &v.fieldsrust-analyzer cargo syn 2.0.117 data/Variant#fields. { |
| 369 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Namedrust-analyzer cargo syn 2.0.117 data/Fields#Named#(FieldsNamedrust-analyzer cargo syn 2.0.117 data/FieldsNamed# { namedrust-analyzer cargo syn 2.0.117 data/FieldsNamed#named., .. }) => { |
| 370 | let field_names = named.iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| f.identrust-analyzer cargo syn 2.0.117 data/Field#ident..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 371 | let field_names2 = field_names.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/map/impl#[`Map<I, F>`][Clone]clone().(); |
| 372 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 373 | #ident::#field_ident { #(#field_names),* } => #glib::variant::ToVariant::to_variant(&( |
| 374 | #tag, |
| 375 | #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([ |
| 376 | #(<#glib::Variant as ::std::convert::From<_>>::from(#field_names2)),* |
| 377 | ])) |
| 378 | )) |
| 379 | } |
| 380 | } |
| 381 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.117 data/Fields#Unnamed#(FieldsUnnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed# { unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed., .. }) => { |
| 382 | let field_names = unnamed |
| 383 | .iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().() |
| 384 | .enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() |
| 385 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(i, _)| format_identrust-analyzer cargo quote 1.0.44 format/format_ident!!("field{}", i)); |
| 386 | let field_names2 = field_names.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/map/impl#[`Map<I, F>`][Clone]clone().(); |
| 387 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 388 | #ident::#field_ident(#(#field_names),*) => #glib::variant::ToVariant::to_variant(&( |
| 389 | #tag, |
| 390 | #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([ |
| 391 | #(<#glib::Variant as ::std::convert::From<_>>::from(#field_names2)),* |
| 392 | ])) |
| 393 | )) |
| 394 | } |
| 395 | } |
| 396 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unitrust-analyzer cargo syn 2.0.117 data/Fields#Unit# => { |
| 397 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 398 | #ident::#field_ident => #glib::variant::ToVariant::to_variant(&( |
| 399 | #tag, |
| 400 | #glib::variant::ToVariant::to_variant(&()) |
| 401 | )) |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | }); |
| 406 | let from = data_enum.variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants..iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(index, v)| { |
| 407 | let ident = &v.identrust-analyzer cargo syn 2.0.117 data/Variant#ident.; |
| 408 | let tag = match &mode { |
| 409 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String# => { |
| 410 | let nick = ToKebabCaserust-analyzer cargo heck 0.5.0 kebab/ToKebabCase#::to_kebab_caserust-analyzer cargo heck 0.5.0 kebab/impl#[str][ToKebabCase]to_kebab_case().(ident.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 411 | quoterust-analyzer cargo quote 1.0.44 quote!! { #nick } |
| 412 | } |
| 413 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Repr#(_) => quoterust-analyzer cargo quote 1.0.44 quote!! { #index }, |
| 414 | _ => unimplementedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unimplemented!!(), |
| 415 | }; |
| 416 | match &v.fieldsrust-analyzer cargo syn 2.0.117 data/Variant#fields. { |
| 417 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Namedrust-analyzer cargo syn 2.0.117 data/Fields#Named#(FieldsNamedrust-analyzer cargo syn 2.0.117 data/FieldsNamed# { namedrust-analyzer cargo syn 2.0.117 data/FieldsNamed#named., .. }) => { |
| 418 | let fields = named.iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(index, f)| { |
| 419 | let name = f.identrust-analyzer cargo syn 2.0.117 data/Field#ident..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 420 | let repr = &f.tyrust-analyzer cargo syn 2.0.117 data/Field#ty.; |
| 421 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 422 | #name: <#repr as #glib::variant::FromVariant>::from_variant( |
| 423 | &#glib::Variant::try_child_value(&value, #index)? |
| 424 | )? |
| 425 | } |
| 426 | }); |
| 427 | quoterust-analyzer cargo quote 1.0.44 quote!! { #tag => ::core::option::Option::Some(Self::#ident { #(#fields),* }), } |
| 428 | } |
| 429 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.117 data/Fields#Unnamed#(FieldsUnnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed# { unnamedrust-analyzer cargo syn 2.0.117 data/FieldsUnnamed#unnamed., .. }) => { |
| 430 | let indices = 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#unnamed.iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(); |
| 431 | let repr = unnamed.iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| &f.tyrust-analyzer cargo syn 2.0.117 data/Field#ty.); |
| 432 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 433 | #tag => ::core::option::Option::Some(Self::#ident( |
| 434 | #( |
| 435 | <#repr as #glib::variant::FromVariant>::from_variant( |
| 436 | &#glib::Variant::try_child_value(&value, #indices)? |
| 437 | )? |
| 438 | ),* |
| 439 | )), |
| 440 | } |
| 441 | } |
| 442 | synrust-analyzer cargo syn 2.0.117 crate/::Fieldsrust-analyzer cargo syn 2.0.117 data/Fields#::Unitrust-analyzer cargo syn 2.0.117 data/Fields#Unit# => { |
| 443 | quoterust-analyzer cargo quote 1.0.44 quote!! { #tag => ::core::option::Option::Some(Self::#ident), } |
| 444 | } |
| 445 | } |
| 446 | }); |
| 447 | |
| 448 | let (repr, tag_match) = match &mode { |
| 449 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String# => (quoterust-analyzer cargo quote 1.0.44 quote!! { String }, quoterust-analyzer cargo quote 1.0.44 quote!! { tag.as_str() }), |
| 450 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Repr#(repr) => (quoterust-analyzer cargo quote 1.0.44 quote!! { #repr }, quoterust-analyzer cargo quote 1.0.44 quote!! { tag as usize }), |
| 451 | _ => unimplementedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unimplemented!!(), |
| 452 | }; |
| 453 | |
| 454 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 455 | impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause { |
| 456 | #[inline] |
| 457 | fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> { |
| 458 | ::std::borrow::Cow::Borrowed( |
| 459 | unsafe { |
| 460 | #glib::VariantTy::from_str_unchecked(#static_variant_type) |
| 461 | } |
| 462 | ) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause { |
| 467 | fn to_variant(&self) -> #glib::Variant { |
| 468 | match self { |
| 469 | #(#to),* |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause { |
| 475 | fn from(v: #ident #type_generics) -> #glib::Variant { |
| 476 | match v { |
| 477 | #(#into),* |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause { |
| 483 | fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> { |
| 484 | let (tag, value) = <(#repr, #glib::Variant) as #glib::variant::FromVariant>::from_variant(&variant)?; |
| 485 | if !#glib::VariantTy::is_tuple(#glib::Variant::type_(&value)) { |
| 486 | return ::core::option::Option::None; |
| 487 | } |
| 488 | match #tag_match { |
| 489 | #(#from)* |
| 490 | _ => ::core::option::Option::None |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | }) |
| 495 | } |
| 496 | |
| 497 | fn derive_variant_for_c_enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/derive_variant_for_c_enum().( |
| 498 | ident: Identrust-analyzer cargo proc-macro2 1.0.106 Ident#, |
| 499 | generics: Genericsrust-analyzer cargo syn 2.0.117 generics/Generics#, |
| 500 | data_enum: synrust-analyzer cargo syn 2.0.117 crate/::DataEnumrust-analyzer cargo syn 2.0.117 derive/DataEnum#, |
| 501 | mode: EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#, |
| 502 | ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream# { |
| 503 | let glib = crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new().(); |
| 504 | let static_variant_type = mode.tag_typerust-analyzer cargo glib-macros 0.22.6 variant_derive/impl#[EnumMode]tag_type().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 505 | let (impl_generics, type_generics, where_clause) = generics.split_for_implrust-analyzer cargo syn 2.0.117 generics/impl#[Generics]split_for_impl().(); |
| 506 | |
| 507 | let (to_variant, from_variant) = match mode { |
| 508 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String# => { |
| 509 | let idents = data_enum.variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants..iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| &v.identrust-analyzer cargo syn 2.0.117 data/Variant#ident.); |
| 510 | let nicks = data_enum.variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants..iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| { |
| 511 | let nick = ToKebabCaserust-analyzer cargo heck 0.5.0 kebab/ToKebabCase#::to_kebab_caserust-analyzer cargo heck 0.5.0 kebab/impl#[str][ToKebabCase]to_kebab_case().(v.identrust-analyzer cargo syn 2.0.117 data/Variant#ident..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 512 | quoterust-analyzer cargo quote 1.0.44 quote!! { #nick } |
| 513 | }); |
| 514 | let idents2 = idents.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/map/impl#[`Map<I, F>`][Clone]clone().(); |
| 515 | let nicks2 = nicks.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/map/impl#[`Map<I, F>`][Clone]clone().(); |
| 516 | ( |
| 517 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 518 | #glib::variant::ToVariant::to_variant(match self { |
| 519 | #(Self::#idents => #nicks),* |
| 520 | }) |
| 521 | }, |
| 522 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 523 | let tag = #glib::Variant::str(&variant)?; |
| 524 | match tag { |
| 525 | #(#nicks2 => ::core::option::Option::Some(Self::#idents2),)* |
| 526 | _ => ::core::option::Option::None |
| 527 | } |
| 528 | }, |
| 529 | ) |
| 530 | } |
| 531 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Repr#(repr) => { |
| 532 | let idents = data_enum.variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants..iterrust-analyzer cargo syn 2.0.117 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| &v.identrust-analyzer cargo syn 2.0.117 data/Variant#ident.); |
| 533 | ( |
| 534 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 535 | #glib::variant::ToVariant::to_variant(&(*self as #repr)) |
| 536 | }, |
| 537 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 538 | let value = <#repr as #glib::variant::FromVariant>::from_variant(&variant)?; |
| 539 | #(if value == Self::#idents as #repr { |
| 540 | return ::core::option::Option::Some(Self::#idents); |
| 541 | })* |
| 542 | ::core::option::Option::None |
| 543 | }, |
| 544 | ) |
| 545 | } |
| 546 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum#repr.: true } => ( |
| 547 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 548 | #glib::variant::ToVariant::to_variant(&(*self as i32)) |
| 549 | }, |
| 550 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 551 | let value = <i32 as #glib::variant::FromVariant>::from_variant(&variant)?; |
| 552 | unsafe { #glib::translate::try_from_glib(value) }.ok() |
| 553 | }, |
| 554 | ), |
| 555 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum#repr.: false } => ( |
| 556 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 557 | let enum_class = #glib::EnumClass::new::<Self>(); |
| 558 | let value = <Self as #glib::translate::IntoGlib>::into_glib(*self); |
| 559 | let value = #glib::EnumClass::value(&enum_class, value); |
| 560 | let value = ::core::option::Option::unwrap(value); |
| 561 | let nick = #glib::EnumValue::nick(&value); |
| 562 | #glib::variant::ToVariant::to_variant(nick) |
| 563 | }, |
| 564 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 565 | let enum_class = #glib::EnumClass::new::<Self>(); |
| 566 | let tag = #glib::Variant::str(&variant)?; |
| 567 | let value = #glib::EnumClass::value_by_nick(&enum_class, tag)?; |
| 568 | let value = #glib::EnumValue::value(&value); |
| 569 | unsafe { #glib::translate::try_from_glib(value) }.ok() |
| 570 | }, |
| 571 | ), |
| 572 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Flagsrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags#repr.: true } => ( |
| 573 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 574 | #glib::variant::ToVariant::to_variant(&self.bits()) |
| 575 | }, |
| 576 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 577 | let value = <u32 as #glib::variant::FromVariant>::from_variant(&variant)?; |
| 578 | Self::from_bits(value) |
| 579 | }, |
| 580 | ), |
| 581 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Flagsrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags#repr.: false } => ( |
| 582 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 583 | let flags_class = #glib::FlagsClass::new::<Self>(); |
| 584 | let value = <Self as #glib::translate::IntoGlib>::into_glib(*self); |
| 585 | let s = #glib::FlagsClass::to_nick_string(&flags_class, value); |
| 586 | #glib::variant::ToVariant::to_variant(&s) |
| 587 | }, |
| 588 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 589 | let flags_class = #glib::FlagsClass::new::<Self>(); |
| 590 | let s = #glib::Variant::str(&variant)?; |
| 591 | let value = #glib::FlagsClass::from_nick_string(&flags_class, s).ok()?; |
| 592 | ::core::option::Option::Some(unsafe { #glib::translate::from_glib(value) }) |
| 593 | }, |
| 594 | ), |
| 595 | }; |
| 596 | |
| 597 | quoterust-analyzer cargo quote 1.0.44 quote!! { |
| 598 | impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause { |
| 599 | #[inline] |
| 600 | fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> { |
| 601 | ::std::borrow::Cow::Borrowed( |
| 602 | unsafe { |
| 603 | #glib::VariantTy::from_str_unchecked(#static_variant_type) |
| 604 | } |
| 605 | ) |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause { |
| 610 | fn to_variant(&self) -> #glib::Variant { |
| 611 | #to_variant |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause { |
| 616 | #[inline] |
| 617 | fn from(v: #ident #type_generics) -> #glib::Variant { |
| 618 | <#ident #type_generics as #glib::variant::ToVariant>::to_variant(&v) |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause { |
| 623 | fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> { |
| 624 | #from_variant |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | fn get_enum_moderust-analyzer cargo glib-macros 0.22.6 variant_derive/get_enum_mode().(attrs: &[synrust-analyzer cargo syn 2.0.117 crate/::Attributerust-analyzer cargo syn 2.0.117 attr/Attribute#]) -> synrust-analyzer cargo syn 2.0.117 crate/::Resultrust-analyzer cargo syn 2.0.117 error/Result#<EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#> { |
| 631 | let attr = attrs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|a| a.pathrust-analyzer cargo syn 2.0.117 attr/impl#[Attribute]path().().is_identrust-analyzer cargo syn 2.0.117 path/impl#[Path]is_ident().("variant_enum")); |
| 632 | |
| 633 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(attr) = attr else { |
| 634 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String#); |
| 635 | }; |
| 636 | |
| 637 | let mut repr_attr = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 638 | let mut mode = EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String#; |
| 639 | attr.parse_nested_metarust-analyzer cargo syn 2.0.117 attr/impl#[Attribute]parse_nested_meta().(|meta| { |
| 640 | match meta.pathrust-analyzer cargo syn 2.0.117 meta/ParseNestedMeta#path..get_identrust-analyzer cargo syn 2.0.117 path/impl#[Path]get_ident().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|id| id.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()).as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().() { |
| 641 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("repr") => { |
| 642 | repr_attr = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(meta.pathrust-analyzer cargo syn 2.0.117 meta/ParseNestedMeta#path.); |
| 643 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 644 | } |
| 645 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("enum") => { |
| 646 | mode = EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum#repr.: false }; |
| 647 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 648 | } |
| 649 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("flags") => { |
| 650 | mode = EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Flagsrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags# { reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags#repr.: false }; |
| 651 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 652 | } |
| 653 | _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.117 crate/::Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().( |
| 654 | meta.pathrust-analyzer cargo syn 2.0.117 meta/ParseNestedMeta#path., |
| 655 | "unknown type in #[variant_enum] attribute", |
| 656 | )), |
| 657 | } |
| 658 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 659 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(match mode { |
| 660 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Stringrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#String# if repr_attr.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() => { |
| 661 | let repr_attr = repr_attr.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 662 | let repr = get_reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/get_repr().(attrs).ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| { |
| 663 | synrust-analyzer cargo syn 2.0.117 crate/::Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().( |
| 664 | repr_attr, |
| 665 | "Must have #[repr] attribute with one of i8, i16, i32, i64, u8, u16, u32, u64", |
| 666 | ) |
| 667 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 668 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Repr#(repr) |
| 669 | } |
| 670 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum# { .. } => EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Enumrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum# { |
| 671 | reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Enum#repr.: repr_attr.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().(), |
| 672 | }, |
| 673 | EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Flagsrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags# { .. } => EnumModerust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#::Flagsrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags# { |
| 674 | reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/EnumMode#Flags#repr.: repr_attr.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().(), |
| 675 | }, |
| 676 | e => e, |
| 677 | }) |
| 678 | } |
| 679 | |
| 680 | fn get_reprrust-analyzer cargo glib-macros 0.22.6 variant_derive/get_repr().(attrs: &[synrust-analyzer cargo syn 2.0.117 crate/::Attributerust-analyzer cargo syn 2.0.117 attr/Attribute#]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Identrust-analyzer cargo proc-macro2 1.0.106 Ident#> { |
| 681 | let attr = attrs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|a| a.pathrust-analyzer cargo syn 2.0.117 attr/impl#[Attribute]path().().is_identrust-analyzer cargo syn 2.0.117 path/impl#[Path]is_ident().("repr"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 682 | let mut repr_ty = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 683 | attr.parse_nested_metarust-analyzer cargo syn 2.0.117 attr/impl#[Attribute]parse_nested_meta().(|meta| { |
| 684 | repr_ty = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(meta.pathrust-analyzer cargo syn 2.0.117 meta/ParseNestedMeta#path..get_identrust-analyzer cargo syn 2.0.117 path/impl#[Path]get_ident().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().clonerust-analyzer cargo proc-macro2 1.0.106 impl#[Ident][Clone]clone().()); |
| 685 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 686 | }) |
| 687 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 688 | match repr_ty.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() { |
| 689 | "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(repr_ty?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().), |
| 690 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 691 | } |
| 692 | } |