From b51029c6a1ca568730cb594da05f4aa62d156624 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Mon, 29 May 2023 22:29:41 +0200 Subject: [PATCH] revert breaking changes from v0.5.2 --- CHANGELOG.md | 5 ----- Cargo.toml | 2 +- src/lib.rs | 52 ++++++++++++++------------------------------- tests/basic.rs | 18 +--------------- tests/generics.rs | 15 ------------- tests/named.rs | 22 ------------------- tests/snake_case.rs | 12 ----------- tests/unit.rs | 12 ----------- tests/unnamed.rs | 24 --------------------- 9 files changed, 18 insertions(+), 144 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1817152..be78fb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,6 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -## 0.5.2 - -- Add is_* impl for non unit variants, #91 by goolmoos -- Fully qualify both Option and Result in generated code, #96 by kepler-5 - ## 0.5.1 - Generated functions now marked `#[inline]` to help with lto across crate boundaries for libraries, #87 by zxch3n diff --git a/Cargo.toml b/Cargo.toml index c5031de..92c0c3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,4 +35,4 @@ version = "1.0" version = "1.0" [dependencies.syn] -version = "2.0" +version = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 51b0948..2afb11e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,6 @@ //! //! let many = ManyVariants::Three(true, 1, 2); //! -//! assert!(many.is_three()); //! assert_eq!(many.as_three().unwrap(), (&true, &1_u32, &2_i64)); //! assert_eq!(many.into_three().unwrap(), (true, 1_u32, 2_i64)); //! ``` @@ -85,7 +84,6 @@ //! //! let many = ManyVariants::Three { one: true, two: 1, three: 2 }; //! -//! assert!(many.is_three()); //! assert_eq!(many.as_three().unwrap(), (&true, &1_u32, &2_i64)); //! assert_eq!(many.into_three().unwrap(), (true, 1_u32, 2_i64)); //! ``` @@ -123,7 +121,6 @@ fn unit_fields_return(variant_name: &syn::Ident, function_name: &Ident, doc: &st /// returns first the types to return, the match names, and then tokens to the field accesses fn unnamed_fields_return( variant_name: &syn::Ident, - (function_name_is, doc_is): (&Ident, &str), (function_name_mut_ref, doc_mut_ref): (&Ident, &str), (function_name_ref, doc_ref): (&Ident, &str), (function_name_val, doc_val): (&Ident, &str), @@ -167,32 +164,25 @@ fn unnamed_fields_return( }; quote!( - #[doc = #doc_is ] - #[inline] - #[allow(unused_variables)] - pub fn #function_name_is(&self) -> bool { - matches!(self, Self::#variant_name(#matches)) - } - #[doc = #doc_mut_ref ] #[inline] - pub fn #function_name_mut_ref(&mut self) -> ::core::option::Option<#returns_mut_ref> { + pub fn #function_name_mut_ref(&mut self) -> Option<#returns_mut_ref> { match self { Self::#variant_name(#matches) => { - ::core::option::Option::Some((#matches)) + Some((#matches)) } - _ => ::core::option::Option::None + _ => None } } #[doc = #doc_ref ] #[inline] - pub fn #function_name_ref(&self) -> ::core::option::Option<#returns_ref> { + pub fn #function_name_ref(&self) -> Option<#returns_ref> { match self { Self::#variant_name(#matches) => { - ::core::option::Option::Some((#matches)) + Some((#matches)) } - _ => ::core::option::Option::None + _ => None } } @@ -201,9 +191,9 @@ fn unnamed_fields_return( pub fn #function_name_val(self) -> ::core::result::Result<#returns_val, Self> { match self { Self::#variant_name(#matches) => { - ::core::result::Result::Ok((#matches)) + Ok((#matches)) }, - _ => ::core::result::Result::Err(self) + _ => Err(self) } } ) @@ -212,7 +202,6 @@ fn unnamed_fields_return( /// returns first the types to return, the match names, and then tokens to the field accesses fn named_fields_return( variant_name: &syn::Ident, - (function_name_is, doc_is): (&Ident, &str), (function_name_mut_ref, doc_mut_ref): (&Ident, &str), (function_name_ref, doc_ref): (&Ident, &str), (function_name_val, doc_val): (&Ident, &str), @@ -258,32 +247,25 @@ fn named_fields_return( }; quote!( - #[doc = #doc_is ] - #[inline] - #[allow(unused_variables)] - pub fn #function_name_is(&self) -> bool { - matches!(self, Self::#variant_name{ #matches }) - } - #[doc = #doc_mut_ref ] #[inline] - pub fn #function_name_mut_ref(&mut self) -> ::core::option::Option<#returns_mut_ref> { + pub fn #function_name_mut_ref(&mut self) -> Option<#returns_mut_ref> { match self { Self::#variant_name{ #matches } => { - ::core::option::Option::Some((#matches)) + Some((#matches)) } - _ => ::core::option::Option::None + _ => None } } #[doc = #doc_ref ] #[inline] - pub fn #function_name_ref(&self) -> ::core::option::Option<#returns_ref> { + pub fn #function_name_ref(&self) -> Option<#returns_ref> { match self { Self::#variant_name{ #matches } => { - ::core::option::Option::Some((#matches)) + Some((#matches)) } - _ => ::core::option::Option::None + _ => None } } @@ -292,9 +274,9 @@ fn named_fields_return( pub fn #function_name_val(self) -> ::core::result::Result<#returns_val, Self> { match self { Self::#variant_name{ #matches } => { - ::core::result::Result::Ok((#matches)) + Ok((#matches)) } - _ => ::core::result::Result::Err(self) + _ => Err(self) } } ) @@ -356,7 +338,6 @@ fn impl_all_as_fns(ast: &DeriveInput) -> TokenStream { syn::Fields::Unit => unit_fields_return(variant_name, &function_name_is, &doc_is), syn::Fields::Unnamed(unnamed) => unnamed_fields_return( variant_name, - (&function_name_is, &doc_is), (&function_name_mut_ref, &doc_mut_ref), (&function_name_ref, &doc_ref), (&function_name_val, &doc_val), @@ -364,7 +345,6 @@ fn impl_all_as_fns(ast: &DeriveInput) -> TokenStream { ), syn::Fields::Named(named) => named_fields_return( variant_name, - (&function_name_is, &doc_is), (&function_name_mut_ref, &doc_mut_ref), (&function_name_ref, &doc_ref), (&function_name_val, &doc_val), diff --git a/tests/basic.rs b/tests/basic.rs index 49e2d35..37ab85e 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -14,24 +14,12 @@ use enum_as_inner::EnumAsInner; -pub mod name_collisions { - #![allow(dead_code, missing_copy_implementations, missing_docs)] - pub struct Option; - pub struct Some; - pub struct None; - pub struct Result; - pub struct Ok; - pub struct Err; -} -#[allow(unused_imports)] -use name_collisions::*; - #[derive(Debug, EnumAsInner)] enum EmptyTest {} #[test] fn test_empty() { - let empty = std::option::Option::None::; + let empty = None::; assert!(empty.is_none()); } @@ -45,8 +33,6 @@ enum EmptyParendsTest { fn test_empty_parends() { let mut empty = EmptyParendsTest::Empty(); - assert!(empty.is_empty()); - empty .as_empty() .expect("should have been something and a unit"); @@ -68,7 +54,6 @@ enum OneTest { fn test_one() { let mut empty = OneTest::One(1); - assert!(empty.is_one()); assert_eq!(*empty.as_one().unwrap(), 1); assert_eq!(*empty.as_one_mut().unwrap(), 1); assert_eq!(empty.into_one().unwrap(), 1); @@ -83,7 +68,6 @@ enum MultiTest { fn test_multi() { let mut multi = MultiTest::Multi(1, 1); - assert!(multi.is_multi()); assert_eq!(multi.as_multi().unwrap(), (&1_u32, &1_u32)); assert_eq!(multi.as_multi_mut().unwrap(), (&mut 1_u32, &mut 1_u32)); assert_eq!(multi.into_multi().unwrap(), (1_u32, 1_u32)); diff --git a/tests/generics.rs b/tests/generics.rs index f7a515b..81d4f0d 100644 --- a/tests/generics.rs +++ b/tests/generics.rs @@ -14,18 +14,6 @@ use enum_as_inner::EnumAsInner; -pub mod name_collisions { - #![allow(dead_code, missing_copy_implementations, missing_docs)] - pub struct Option; - pub struct Some; - pub struct None; - pub struct Result; - pub struct Ok; - pub struct Err; -} -#[allow(unused_imports)] -use name_collisions::*; - #[allow(dead_code)] #[derive(Debug, Clone, Copy, EnumAsInner)] enum WithGenerics { @@ -37,9 +25,6 @@ enum WithGenerics { fn with_generics() { let mut with_generics = WithGenerics::A(100); - assert!(with_generics.is_a()); - assert!(!with_generics.is_b()); - assert!(with_generics.as_a().is_some()); assert!(with_generics.as_b().is_none()); diff --git a/tests/named.rs b/tests/named.rs index d642722..41ef86a 100644 --- a/tests/named.rs +++ b/tests/named.rs @@ -14,18 +14,6 @@ use enum_as_inner::EnumAsInner; -pub mod name_collisions { - #![allow(dead_code, missing_copy_implementations, missing_docs)] - pub struct Option; - pub struct Some; - pub struct None; - pub struct Result; - pub struct Ok; - pub struct Err; -} -#[allow(unused_imports)] -use name_collisions::*; - #[derive(Debug, EnumAsInner)] enum ManyVariants { One { one: u32 }, @@ -37,10 +25,6 @@ enum ManyVariants { fn test_one_named() { let mut many = ManyVariants::One { one: 1 }; - assert!(many.is_one()); - assert!(!many.is_two()); - assert!(!many.is_three()); - assert!(many.as_one().is_some()); assert!(many.as_two().is_none()); assert!(many.as_three().is_none()); @@ -57,9 +41,6 @@ fn test_one_named() { fn test_two_named() { let mut many = ManyVariants::Two { one: 1, two: 2 }; - assert!(!many.is_one()); - assert!(many.is_two()); - assert!(!many.is_three()); assert!(many.as_one().is_none()); assert!(many.as_two().is_some()); assert!(many.as_three().is_none()); @@ -80,9 +61,6 @@ fn test_three_named() { three: 2, }; - assert!(!many.is_one()); - assert!(!many.is_two()); - assert!(many.is_three()); assert!(many.as_one().is_none()); assert!(many.as_two().is_none()); assert!(many.as_three().is_some()); diff --git a/tests/snake_case.rs b/tests/snake_case.rs index 4b7d593..718d530 100644 --- a/tests/snake_case.rs +++ b/tests/snake_case.rs @@ -14,18 +14,6 @@ use enum_as_inner::EnumAsInner; -pub mod name_collisions { - #![allow(dead_code, missing_copy_implementations, missing_docs)] - pub struct Option; - pub struct Some; - pub struct None; - pub struct Result; - pub struct Ok; - pub struct Err; -} -#[allow(unused_imports)] -use name_collisions::*; - #[derive(Debug, EnumAsInner)] #[allow(non_camel_case_types)] #[allow(clippy::upper_case_acronyms)] diff --git a/tests/unit.rs b/tests/unit.rs index a1a92ff..2e66a8f 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -14,18 +14,6 @@ use enum_as_inner::EnumAsInner; -pub mod name_collisions { - #![allow(dead_code, missing_copy_implementations, missing_docs)] - pub struct Option; - pub struct Some; - pub struct None; - pub struct Result; - pub struct Ok; - pub struct Err; -} -#[allow(unused_imports)] -use name_collisions::*; - #[derive(EnumAsInner)] enum UnitVariants { Zero, diff --git a/tests/unnamed.rs b/tests/unnamed.rs index 4bf0ea5..e41022c 100644 --- a/tests/unnamed.rs +++ b/tests/unnamed.rs @@ -14,18 +14,6 @@ use enum_as_inner::EnumAsInner; -pub mod name_collisions { - #![allow(dead_code, missing_copy_implementations, missing_docs)] - pub struct Option; - pub struct Some; - pub struct None; - pub struct Result; - pub struct Ok; - pub struct Err; -} -#[allow(unused_imports)] -use name_collisions::*; - #[derive(Debug, EnumAsInner)] enum ManyVariants { One(u32), @@ -37,10 +25,6 @@ enum ManyVariants { fn test_one_unnamed() { let mut many = ManyVariants::One(1); - assert!(many.is_one()); - assert!(!many.is_two()); - assert!(!many.is_three()); - assert!(many.as_one().is_some()); assert!(many.as_two().is_none()); assert!(many.as_three().is_none()); @@ -58,10 +42,6 @@ fn test_one_unnamed() { fn test_two_unnamed() { let mut many = ManyVariants::Two(1, 2); - assert!(!many.is_one()); - assert!(many.is_two()); - assert!(!many.is_three()); - assert!(many.as_one().is_none()); assert!(many.as_two().is_some()); assert!(many.as_three().is_none()); @@ -79,10 +59,6 @@ fn test_two_unnamed() { fn test_three_unnamed() { let mut many = ManyVariants::Three(true, 1, 2); - assert!(!many.is_one()); - assert!(!many.is_two()); - assert!(many.is_three()); - assert!(many.as_one().is_none()); assert!(many.as_two().is_none()); assert!(many.as_three().is_some()); -- 2.40.1