1
0
mirror of https://github.com/danog/strum.git synced 2024-11-29 20:18:58 +01:00

implement Debug for EnumIter (#240)

Co-authored-by: Peter Glotfelty <peter@glotfelty.us>
This commit is contained in:
Peter Glotfelty 2022-10-15 12:00:20 -07:00 committed by GitHub
parent dd0c2fa785
commit e772e203da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -65,11 +65,14 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
arms.push(quote! { _ => ::core::option::Option::None });
let iter_name = syn::parse_str::<Ident>(&format!("{}Iter", name)).unwrap();
// Create a string literal "MyEnumIter" to use in the debug impl.
let iter_name_debug_struct =
syn::parse_str::<syn::LitStr>(&format!("\"{}\"", iter_name)).unwrap();
Ok(quote! {
#[doc = #doc_comment]
#[allow(
missing_copy_implementations,
missing_debug_implementations,
)]
#vis struct #iter_name #ty_generics {
idx: usize,
@ -77,6 +80,16 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
marker: ::core::marker::PhantomData #phantom_data,
}
impl #impl_generics core::fmt::Debug for #iter_name #ty_generics #where_clause {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// We don't know if the variants implement debug themselves so the only thing we
// can really show is how many elements are left.
f.debug_struct(#iter_name_debug_struct)
.field("len", &self.len())
.finish()
}
}
impl #impl_generics #iter_name #ty_generics #where_clause {
fn get(&self, idx: usize) -> Option<#name #ty_generics> {
match idx {