// SPDX-FileCopyrightText: 2021 - 2023 Kamila Borowska <kamila@borowska.pw> // SPDX-FileCopyrightText: 2021 Bruno Corrêa Zimmermann <brunoczim@gmail.com> // // SPDX-License-Identifier: MIT OR Apache-2.0
usecrate::type_length; use proc_macro2::TokenStream; use quote::{format_ident, quote, ToTokens}; use syn::{DataEnum, Fields, FieldsNamed, FieldsUnnamed, Ident, Variant};
/// Total length is the sum of each variant's length. To represent a variant, its number is added to /// the sum of previous variant lengths. #[derive(Debug)] struct EnumGenerator {
length: Length,
from_usize_arms: TokenStream,
into_usize_arms: TokenStream,
}
/// Becomes simply `1` in counting, since this is the size of the unit. fn handle_unit_variant(&mutself, variant: &Ident) { letSelf {
length,
from_usize_arms,
into_usize_arms,
} = self;
*into_usize_arms = quote! { #into_usize_armsSelf::#variant => #length, };
*from_usize_arms = quote! { #from_usize_armsif value == #length { Self::#variant
} else
}; self.length.units += 1;
}
/// Its size is the product of the sizes of its members. To represent this variant, one can /// think of this as representing a little-endian number. First member is simply added, but /// next members are multiplied before being added. fn handle_unnamed_variant(&mutself, variant: &Ident, fields: &FieldsUnnamed) { letSelf {
length,
from_usize_arms,
into_usize_arms,
} = self; letmut expr_into = quote! { #length }; letmut fields_length = quote! { 1usize }; letmut params_from = quote! {}; for (i, field) in fields.unnamed.iter().enumerate() { let ident = format_ident!("p{}", i); let ty = &field.ty; let field_length = type_length(ty);
/// Its size is the product of the sizes of its members. To represent this variant, one can /// think of this as representing a little-endian number. First member is simply added, but /// next members are multiplied before being added. fn handle_named_variant(&mutself, variant: &Ident, fields: &FieldsNamed) { letSelf {
length,
from_usize_arms,
into_usize_arms,
} = self; letmut expr_into = quote! { #length }; letmut fields_length = quote! { 1usize }; letmut params_from = quote! {};
for field in fields.named.iter() { let ident = field.ident.as_ref().unwrap(); let ty = &field.ty; let field_length = type_length(ty);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.