From a4dae76c23ab26d4a374c1e7f3c14c2d1a411b79 Mon Sep 17 00:00:00 2001 From: Jasper Visser Date: Fri, 26 Mar 2021 09:41:39 +0100 Subject: [PATCH 1/3] Add qualified option --- example/some_proc_crate/src/lib.rs | 4 ++-- example/src/lib.rs | 3 ++- src/lib.rs | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/example/some_proc_crate/src/lib.rs b/example/some_proc_crate/src/lib.rs index 8e5c3e8..e85610c 100644 --- a/example/some_proc_crate/src/lib.rs +++ b/example/some_proc_crate/src/lib.rs @@ -8,13 +8,13 @@ pub fn for_struct(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let fields = proc_macro2_helper::named_struct_fields_from_data(input.data); let attrs = proc_macro2_helper::filter_attributes_from_fields(&fields, "someattr"); - // Only 1 field should have the option type + // Only 2 fields should have the option type let options = fields .iter() .filter(|field| proc_macro2_helper::has_first_type_option(field)) .count(); - assert_eq!(1, options); + assert_eq!(2, options); // Only 2 attributes have an attribute 'someattr' assert_eq!(2, attrs.len()); diff --git a/example/src/lib.rs b/example/src/lib.rs index 25e84c6..7a0a517 100644 --- a/example/src/lib.rs +++ b/example/src/lib.rs @@ -9,7 +9,8 @@ struct SomeStruct { field1: &'static str, #[someattr] field2: String, - nullable: Option + nullable: Option, + qualified_option: std::option::Option, } #[allow(dead_code)] diff --git a/src/lib.rs b/src/lib.rs index 777b95f..bcdd687 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,21 @@ pub fn has_first_type_option(field: &Field) -> bool { pub fn has_first_type(field: &Field, ty: &str) -> bool { match &field.ty { - Type::Path(path) => path.path.segments[0].ident == ty, + Type::Path(path) => { + let ident = &path.path.segments[0].ident; + + if ident == ty { + true + } else if ident == "std" { + if path.path.segments.len() < 3 { + return false; + } + + path.path.segments[2].ident == ty + } else { + false + } + }, _ => false, } } From e0495d3d05d11c3e59db848e295269cc28218c91 Mon Sep 17 00:00:00 2001 From: Jasper Visser Date: Fri, 26 Mar 2021 09:41:57 +0100 Subject: [PATCH 2/3] Add qualified option --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5ef4d96..9c76eb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "proc_macro2_helper" -version = "0.2.8" +version = "0.2.9" authors = ["Jasper Visser "] edition = "2018" description = "Various utility code to extract data that can be used with proc macro2" From ae988ae9d2682009f3c2d0010b914ca1b2de129e Mon Sep 17 00:00:00 2001 From: Jasper Visser Date: Fri, 26 Mar 2021 09:47:23 +0100 Subject: [PATCH 3/3] Improved formatting --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index bcdd687..2fb6aa2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,7 +48,7 @@ pub fn has_first_type(field: &Field, ty: &str) -> bool { } else { false } - }, + } _ => false, } }