Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fields::iter and IntoIterator for &Fields #324

Merged
merged 3 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// except according to those terms.

use super::*;
use punctuated::Punctuated;
use punctuated::{Iter, Punctuated};

ast_struct! {
/// Data structure sent to a `proc_macro_derive` macro.
Expand Down Expand Up @@ -75,6 +75,26 @@ ast_enum_of_structs! {
do_not_generate_to_tokens
}

impl Fields {
/// Returns an iterator over the fields
pub fn iter(&self) -> Iter<Field, Token![,]> {
match *self {
Fields::Unit => Iter::empty(),
Fields::Named(FieldsNamed { ref named, .. }) => named.iter(),
Fields::Unnamed(FieldsUnnamed { ref unnamed, .. }) => unnamed.iter(),
}
}
}

impl<'a> IntoIterator for &'a Fields {
type Item = &'a Field;
type IntoIter = Iter<'a, Field, Token![,]>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
Expand Down
8 changes: 8 additions & 0 deletions src/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ pub struct Iter<'a, T: 'a, P: 'a> {
inner: slice::Iter<'a, (T, Option<P>)>,
}

impl<'a, T: 'a, P: 'a> Iter<'a, T, P> {
pub(crate) fn empty() -> Self {
Self {
inner: [].iter(),
}
}
}

impl<'a, T, P> Iterator for Iter<'a, T, P> {
type Item = &'a T;

Expand Down
76 changes: 76 additions & 0 deletions tests/test_derive_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,79 @@ fn test_pub_restricted_in_super() {

assert_eq!(expected, actual);
}

#[test]
fn test_fields_on_unit_struct() {
let raw = "struct S;";
let struct_body = match syn::parse_str::<DeriveInput>(raw).unwrap().data {
Data::Struct(body) => body,
_ => panic!("expected a struct"),
};

assert_eq!(0, struct_body.fields.iter().count());
}

#[test]
fn test_fields_on_named_struct() {
let raw = "struct S {
foo: i32,
pub bar: String,
}";
let struct_body = match syn::parse_str::<DeriveInput>(raw).unwrap().data {
Data::Struct(body) => body,
_ => panic!("expected a struct"),
};

let expected = vec![
Field {
attrs: vec![],
vis: Visibility::Inherited,
ident: Some(Ident::from("foo")),
colon_token: Some(Default::default()),
ty: syn::parse_str("i32").unwrap(),
},
Field {
attrs: vec![],
vis: Visibility::Public(VisPublic {
pub_token: Default::default(),
}),
ident: Some(Ident::from("bar")),
colon_token: Some(Default::default()),
ty: syn::parse_str("String").unwrap(),
},
];
let expected = expected.iter().collect::<Vec<_>>();

assert_eq!(expected, struct_body.fields.iter().collect::<Vec<_>>());
}

#[test]
fn test_fields_on_tuple_struct() {
let raw = "struct S(i32, pub String);";
let struct_body = match syn::parse_str::<DeriveInput>(raw).unwrap().data {
Data::Struct(body) => body,
_ => panic!("expected a struct"),
};

let expected = vec![
Field {
attrs: vec![],
vis: Visibility::Inherited,
ident: None,
colon_token: None,
ty: syn::parse_str("i32").unwrap(),
},
Field {
attrs: vec![],
vis: Visibility::Public(VisPublic {
pub_token: Default::default(),
}),
ident: None,
colon_token: None,
ty: syn::parse_str("String").unwrap(),
},
];
let expected = expected.iter().collect::<Vec<_>>();

assert_eq!(expected, struct_body.fields.iter().collect::<Vec<_>>());
}