From 42d5251f246c4fefc587e2a73075eecb0167f75c Mon Sep 17 00:00:00 2001 From: Kai Ren Date: Wed, 27 Mar 2024 17:42:38 +0100 Subject: [PATCH] Refactor `::derive_more` absolute paths to just `derive_more` (#344, #338) ## Synopsis At the moment, the `derive_more` macros cannot be re-exported or the `derive_more` crate cannot be renamed as the dependency, because expansions require `::derive_more` module be present in the scope. See #338 for details. ## Solution Use `derive_more::*` paths instead of `::derive_more::*` in expansions, allowing to provide this module in the scope on the call site even when `derive_more` is transitive dependency of the crate. --- README.md | 27 ++++++- impl/doc/add.md | 18 ++--- impl/doc/add_assign.md | 4 +- impl/doc/as_mut.md | 6 +- impl/doc/as_ref.md | 6 +- impl/doc/deref.md | 8 +- impl/doc/deref_mut.md | 16 ++-- impl/doc/from_str.md | 14 ++-- impl/doc/index.md | 8 +- impl/doc/index_mut.md | 15 ++-- impl/doc/into_iterator.md | 24 +++--- impl/doc/mul.md | 16 ++-- impl/doc/mul_assign.md | 8 +- impl/doc/not.md | 14 ++-- impl/doc/sum.md | 13 ++-- impl/doc/try_into.md | 14 ++-- impl/src/add_assign_like.rs | 2 +- impl/src/add_like.rs | 20 ++--- impl/src/as/mod.rs | 10 +-- impl/src/error.rs | 26 ++++--- impl/src/fmt/debug.rs | 77 ++++++++++--------- impl/src/fmt/display.rs | 20 ++--- impl/src/from.rs | 12 +-- impl/src/from_str.rs | 10 +-- impl/src/into.rs | 4 +- impl/src/not_like.rs | 14 ++-- impl/src/sum_like.rs | 6 +- impl/src/try_from.rs | 12 +-- impl/src/try_into.rs | 18 ++--- impl/src/try_unwrap.rs | 34 +++++--- impl/src/utils.rs | 15 ++-- .../as_mut/renamed_generic.stderr | 5 +- .../as_ref/renamed_generic.stderr | 5 +- tests/deref_mut.rs | 16 ++-- tests/index_mut.rs | 4 +- tests/into_iterator.rs | 2 +- tests/sum.rs | 3 +- 37 files changed, 289 insertions(+), 237 deletions(-) diff --git a/README.md b/README.md index a3b92835..383f773d 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,32 @@ This crate also re-exports all the standard library traits that it adds derives for. So, both the `Display` derive and the `Display` trait will be in scope when you add the following code: ```rust -use derive_more::Display; +use derive_more::Display; // also imports `core::fmt::Display` +``` + +For derive macros only, without the corresponding traits, do import them from +the `derive` module: +```rust +use derive_more::derive::Display; // imports macro only +``` + +#### Hygiene + +For hygiene purposes, macros use `derive_more::*` absolute paths in their expansions. +This might introduce a trouble, if you want to re-export `derive_more` macros in your +own crate without using the `derive_more` as a direct dependency in downstream crates: +```rust,ignore +use my_lib::Display; // re-exported in `my_lib` crate + +#[derive(Display)] // error: could not find `derive_more` in the list of imported crates +struct MyInt(i32); +``` +In such case, you should re-export the `derive_more` module too: +```rust,ignore +use my_lib::{derive_more, Display}; // re-exported in `my_lib` crate + +#[derive(Display)] // works fine now! +struct MyInt(i32); ``` diff --git a/impl/doc/add.md b/impl/doc/add.md index 23fab973..c02eeb7c 100644 --- a/impl/doc/add.md +++ b/impl/doc/add.md @@ -26,7 +26,7 @@ Code like this will be generated: ```rust # struct MyInts(i32, i32); -impl ::core::ops::Add for MyInts { +impl derive_more::Add for MyInts { type Output = MyInts; fn add(self, rhs: MyInts) -> MyInts { MyInts(self.0.add(rhs.0), self.1.add(rhs.1)) @@ -60,7 +60,7 @@ Code like this will be generated: # x: i32, # y: i32, # } -impl ::core::ops::Add for Point2D { +impl derive_more::Add for Point2D { type Output = Point2D; fn add(self, rhs: Point2D) -> Point2D { Point2D { @@ -112,9 +112,9 @@ Code like this will be generated: # UnsignedTwo(u32), # Unit, # } -impl ::core::ops::Add for MixedInts { - type Output = Result; - fn add(self, rhs: MixedInts) -> Result { +impl derive_more::Add for MixedInts { + type Output = Result; + fn add(self, rhs: MixedInts) -> Result { match (self, rhs) { (MixedInts::SmallInt(__l_0), MixedInts::SmallInt(__r_0)) => { Ok(MixedInts::SmallInt(__l_0.add(__r_0))) @@ -138,11 +138,11 @@ impl ::core::ops::Add for MixedInts { (MixedInts::UnsignedTwo(__l_0), MixedInts::UnsignedTwo(__r_0)) => { Ok(MixedInts::UnsignedTwo(__l_0.add(__r_0))) } - (MixedInts::Unit, MixedInts::Unit) => Err(::derive_more::BinaryError::Unit( - ::derive_more::UnitError::new("add"), + (MixedInts::Unit, MixedInts::Unit) => Err(derive_more::BinaryError::Unit( + derive_more::UnitError::new("add"), )), - _ => Err(::derive_more::BinaryError::Mismatch( - ::derive_more::WrongVariantError::new("add"), + _ => Err(derive_more::BinaryError::Mismatch( + derive_more::WrongVariantError::new("add"), )), } } diff --git a/impl/doc/add_assign.md b/impl/doc/add_assign.md index 04a85a18..4eab9aad 100644 --- a/impl/doc/add_assign.md +++ b/impl/doc/add_assign.md @@ -22,7 +22,7 @@ Code like this will be generated: ```rust # struct MyInts(i32, i32); -impl ::core::ops::AddAssign for MyInts { +impl derive_more::AddAssign for MyInts { fn add_assign(&mut self, rhs: MyInts) { self.0.add_assign(rhs.0); self.1.add_assign(rhs.1); @@ -56,7 +56,7 @@ Code like this will be generated: # x: i32, # y: i32, # } -impl ::core::ops::AddAssign for Point2D { +impl derive_more::AddAssign for Point2D { fn add_assign(&mut self, rhs: Point2D) { self.x.add_assign(rhs.x); self.y.add_assign(rhs.y); diff --git a/impl/doc/as_mut.md b/impl/doc/as_mut.md index bee04f19..180674a7 100644 --- a/impl/doc/as_mut.md +++ b/impl/doc/as_mut.md @@ -24,7 +24,7 @@ Generates: ```rust # struct MyWrapper(String); -impl AsMut for MyWrapper { +impl derive_more::AsMut for MyWrapper { fn as_mut(&mut self) -> &mut String { &mut self.0 } @@ -50,9 +50,9 @@ This generates code equivalent to: ```rust # struct SingleFieldForward(Vec); -impl AsMut for SingleFieldForward +impl derive_more::AsMut for SingleFieldForward where - Vec: AsMut, + Vec: derive_more::AsMut, { #[inline] fn as_mut(&mut self) -> &mut T { diff --git a/impl/doc/as_ref.md b/impl/doc/as_ref.md index d6cee1d4..aedded5a 100644 --- a/impl/doc/as_ref.md +++ b/impl/doc/as_ref.md @@ -24,7 +24,7 @@ Generates: ```rust # struct MyWrapper(String); -impl AsRef for MyWrapper { +impl derive_more::AsRef for MyWrapper { fn as_ref(&self) -> &String { &self.0 } @@ -50,9 +50,9 @@ This generates code equivalent to: ```rust # struct SingleFieldForward(Vec); -impl AsRef for SingleFieldForward +impl derive_more::AsRef for SingleFieldForward where - Vec: AsRef, + Vec: derive_more::AsRef, { #[inline] fn as_ref(&self) -> &T { diff --git a/impl/doc/deref.md b/impl/doc/deref.md index f1719e9b..2a48886d 100644 --- a/impl/doc/deref.md +++ b/impl/doc/deref.md @@ -67,7 +67,7 @@ Code like this will be generated: # cool: bool, # vec: Vec, # } -impl ::core::ops::Deref for CoolVec { +impl derive_more::Deref for CoolVec { type Target = Vec; #[inline] fn deref(&self) -> &Self::Target { @@ -90,11 +90,11 @@ Code like this will be generated: ```rust # struct MyBoxedInt(Box); -impl ::core::ops::Deref for MyBoxedInt { - type Target = as ::core::ops::Deref>::Target; +impl derive_more::Deref for MyBoxedInt { + type Target = as derive_more::Deref>::Target; #[inline] fn deref(&self) -> &Self::Target { - as ::core::ops::Deref>::deref(&self.0) + as derive_more::Deref>::deref(&self.0) } } ``` diff --git a/impl/doc/deref_mut.md b/impl/doc/deref_mut.md index 8cde33d0..32853379 100644 --- a/impl/doc/deref_mut.md +++ b/impl/doc/deref_mut.md @@ -73,18 +73,19 @@ struct CoolVec { Code like this will be generated: ```rust +# use ::core::ops::Deref; # struct CoolVec { # cool: bool, # vec: Vec, # } -# impl ::core::ops::Deref for CoolVec { +# impl Deref for CoolVec { # type Target = Vec; # #[inline] # fn deref(&self) -> &Self::Target { # &self.vec # } # } -impl ::core::ops::DerefMut for CoolVec { +impl derive_more::DerefMut for CoolVec { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.vec @@ -106,18 +107,19 @@ struct MyBoxedInt(Box); When deriving a forwarded `DerefMut` for a struct: ```rust +# use ::core::ops::Deref; # struct MyBoxedInt(Box); -# impl ::core::ops::Deref for MyBoxedInt { -# type Target = as ::core::ops::Deref>::Target; +# impl Deref for MyBoxedInt { +# type Target = as Deref>::Target; # #[inline] # fn deref(&self) -> &Self::Target { -# as ::core::ops::Deref>::deref(&self.0) +# as Deref>::deref(&self.0) # } # } -impl ::core::ops::DerefMut for MyBoxedInt { +impl derive_more::DerefMut for MyBoxedInt { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - as ::core::ops::DerefMut>::deref_mut(&mut self.0) + as derive_more::DerefMut>::deref_mut(&mut self.0) } } ``` diff --git a/impl/doc/from_str.md b/impl/doc/from_str.md index d888e825..bf9d1e73 100644 --- a/impl/doc/from_str.md +++ b/impl/doc/from_str.md @@ -44,8 +44,8 @@ Code like this will be generated: ```rust # struct MyInt(i32); -impl ::core::str::FromStr for MyInt { - type Err = ::Err; +impl derive_more::FromStr for MyInt { + type Err = ::Err; fn from_str(src: &str) -> Result { return Ok(MyInt(i32::from_str(src)?)); } @@ -74,8 +74,8 @@ Code like this will be generated: # struct Point1D { # x: i32, # } -impl ::core::str::FromStr for Point1D { - type Err = ::Err; +impl derive_more::FromStr for Point1D { + type Err = ::Err; fn from_str(src: &str) -> Result { return Ok(Point1D { x: i32::from_str(src)?, @@ -121,14 +121,14 @@ Code like this will be generated: # Baz, # } # -impl ::core::str::FromStr for EnumNoFields { - type Err = ::derive_more::FromStrError; +impl derive_more::FromStr for EnumNoFields { + type Err = derive_more::FromStrError; fn from_str(src: &str) -> Result { Ok(match src.to_lowercase().as_str() { "foo" => EnumNoFields::Foo, "bar" => EnumNoFields::Bar, "baz" => EnumNoFields::Baz, - _ => return Err(::derive_more::FromStrError::new("EnumNoFields")), + _ => return Err(derive_more::FromStrError::new("EnumNoFields")), }) } } diff --git a/impl/doc/index.md b/impl/doc/index.md index c45fb46d..47f70921 100644 --- a/impl/doc/index.md +++ b/impl/doc/index.md @@ -54,14 +54,14 @@ Code like this will be generated: # numbers: Vec, # useless: bool, # } -impl<__IdxT> ::core::ops::Index<__IdxT> for Numbers +impl<__IdxT> derive_more::Index<__IdxT> for Numbers where - Vec: ::core::ops::Index<__IdxT>, + Vec: derive_more::Index<__IdxT>, { - type Output = as ::core::ops::Index<__IdxT>>::Output; + type Output = as derive_more::Index<__IdxT>>::Output; #[inline] fn index(&self, idx: __IdxT) -> &Self::Output { - as ::core::ops::Index<__IdxT>>::index(&self.numbers, idx) + as derive_more::Index<__IdxT>>::index(&self.numbers, idx) } } ``` diff --git a/impl/doc/index_mut.md b/impl/doc/index_mut.md index ff234779..c1e86974 100644 --- a/impl/doc/index_mut.md +++ b/impl/doc/index_mut.md @@ -58,27 +58,28 @@ struct Numbers { Code like this will be generated to implement `IndexMut`: ```rust +# use ::core::ops::Index; # struct Numbers { # numbers: Vec, # useless: bool, # } -# impl<__IdxT> ::core::ops::Index<__IdxT> for Numbers +# impl<__IdxT> Index<__IdxT> for Numbers # where -# Vec: ::core::ops::Index<__IdxT>, +# Vec: Index<__IdxT>, # { -# type Output = as ::core::ops::Index<__IdxT>>::Output; +# type Output = as Index<__IdxT>>::Output; # #[inline] # fn index(&self, idx: __IdxT) -> &Self::Output { -# as ::core::ops::Index<__IdxT>>::index(&self.numbers, idx) +# as Index<__IdxT>>::index(&self.numbers, idx) # } # } -impl<__IdxT> ::core::ops::IndexMut<__IdxT> for Numbers +impl<__IdxT> derive_more::IndexMut<__IdxT> for Numbers where - Vec: ::core::ops::IndexMut<__IdxT>, + Vec: derive_more::IndexMut<__IdxT>, { #[inline] fn index_mut(&mut self, idx: __IdxT) -> &mut Self::Output { - as ::core::ops::IndexMut<__IdxT>>::index_mut(&mut self.numbers, idx) + as derive_more::IndexMut<__IdxT>>::index_mut(&mut self.numbers, idx) } } ``` diff --git a/impl/doc/into_iterator.md b/impl/doc/into_iterator.md index c9abe66c..c660a908 100644 --- a/impl/doc/into_iterator.md +++ b/impl/doc/into_iterator.md @@ -63,30 +63,30 @@ Code like this will be generated: # numbers: Vec, # useless: bool, # } -impl ::core::iter::IntoIterator for Numbers { - type Item = as ::core::iter::IntoIterator>::Item; - type IntoIter = as ::core::iter::IntoIterator>::IntoIter; +impl derive_more::IntoIterator for Numbers { + type Item = as derive_more::IntoIterator>::Item; + type IntoIter = as derive_more::IntoIterator>::IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter { - as ::core::iter::IntoIterator>::into_iter(self.numbers) + as derive_more::IntoIterator>::into_iter(self.numbers) } } -impl<'__deriveMoreLifetime> ::core::iter::IntoIterator for &'__deriveMoreLifetime Numbers { - type Item = <&'__deriveMoreLifetime Vec as ::core::iter::IntoIterator>::Item; - type IntoIter = <&'__deriveMoreLifetime Vec as ::core::iter::IntoIterator>::IntoIter; +impl<'__deriveMoreLifetime> derive_more::IntoIterator for &'__deriveMoreLifetime Numbers { + type Item = <&'__deriveMoreLifetime Vec as derive_more::IntoIterator>::Item; + type IntoIter = <&'__deriveMoreLifetime Vec as derive_more::IntoIterator>::IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter { - <&'__deriveMoreLifetime Vec as ::core::iter::IntoIterator>::into_iter(&self.numbers) + <&'__deriveMoreLifetime Vec as derive_more::IntoIterator>::into_iter(&self.numbers) } } -impl<'__deriveMoreLifetime> ::core::iter::IntoIterator for &'__deriveMoreLifetime mut Numbers { - type Item = <&'__deriveMoreLifetime mut Vec as ::core::iter::IntoIterator>::Item; - type IntoIter = <&'__deriveMoreLifetime mut Vec as ::core::iter::IntoIterator>::IntoIter; +impl<'__deriveMoreLifetime> derive_more::IntoIterator for &'__deriveMoreLifetime mut Numbers { + type Item = <&'__deriveMoreLifetime mut Vec as derive_more::IntoIterator>::Item; + type IntoIter = <&'__deriveMoreLifetime mut Vec as derive_more::IntoIterator>::IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter { - <&'__deriveMoreLifetime mut Vec as ::core::iter::IntoIterator>::into_iter( + <&'__deriveMoreLifetime mut Vec as derive_more::IntoIterator>::into_iter( &mut self.numbers, ) } diff --git a/impl/doc/mul.md b/impl/doc/mul.md index a1068bef..a9055158 100644 --- a/impl/doc/mul.md +++ b/impl/doc/mul.md @@ -35,8 +35,8 @@ Code like this will be generated: ```rust # struct MyInt(i32); -impl<__RhsT> ::core::ops::Mul<__RhsT> for MyInt - where i32: ::core::ops::Mul<__RhsT, Output = i32> +impl<__RhsT> derive_more::Mul<__RhsT> for MyInt + where i32: derive_more::Mul<__RhsT, Output = i32> { type Output = MyInt; fn mul(self, rhs: __RhsT) -> MyInt { @@ -60,8 +60,8 @@ Code like this will be generated: ```rust # struct MyInts(i32, i32); -impl<__RhsT: ::core::marker::Copy> ::core::ops::Mul<__RhsT> for MyInts - where i32: ::core::ops::Mul<__RhsT, Output = i32> +impl<__RhsT: Copy> derive_more::Mul<__RhsT> for MyInts + where i32: derive_more::Mul<__RhsT, Output = i32> { type Output = MyInts; fn mul(self, rhs: __RhsT) -> MyInts { @@ -94,8 +94,8 @@ Code like this will be generated: # struct Point1D { # x: i32, # } -impl<__RhsT> ::core::ops::Mul<__RhsT> for Point1D - where i32: ::core::ops::Mul<__RhsT, Output = i32> +impl<__RhsT> derive_more::Mul<__RhsT> for Point1D + where i32: derive_more::Mul<__RhsT, Output = i32> { type Output = Point1D; fn mul(self, rhs: __RhsT) -> Point1D { @@ -125,8 +125,8 @@ Code like this will be generated: # x: i32, # y: i32, # } -impl<__RhsT: ::core::marker::Copy> ::core::ops::Mul<__RhsT> for Point2D - where i32: ::core::ops::Mul<__RhsT, Output = i32> +impl<__RhsT: Copy> derive_more::Mul<__RhsT> for Point2D + where i32: derive_more::Mul<__RhsT, Output = i32> { type Output = Point2D; fn mul(self, rhs: __RhsT) -> Point2D { diff --git a/impl/doc/mul_assign.md b/impl/doc/mul_assign.md index e5d9225e..aedefdfd 100644 --- a/impl/doc/mul_assign.md +++ b/impl/doc/mul_assign.md @@ -27,8 +27,8 @@ Code like this will be generated: ```rust # struct MyInts(i32, i32); -impl<__RhsT: ::core::marker::Copy> ::core::ops::MulAssign<__RhsT> for MyInts - where i32: ::core::ops::MulAssign<__RhsT> +impl<__RhsT: Copy> derive_more::MulAssign<__RhsT> for MyInts + where i32: derive_more::MulAssign<__RhsT> { fn mul_assign(&mut self, rhs: __RhsT) { self.0.mul_assign(rhs); @@ -64,8 +64,8 @@ Code like this will be generated: # x: i32, # y: i32, # } -impl<__RhsT: ::core::marker::Copy> ::core::ops::MulAssign<__RhsT> for Point2D - where i32: ::core::ops::MulAssign<__RhsT> +impl<__RhsT: Copy> derive_more::MulAssign<__RhsT> for Point2D + where i32: derive_more::MulAssign<__RhsT> { fn mul_assign(&mut self, rhs: __RhsT) { self.x.mul_assign(rhs); diff --git a/impl/doc/not.md b/impl/doc/not.md index affb7900..98834472 100644 --- a/impl/doc/not.md +++ b/impl/doc/not.md @@ -23,7 +23,7 @@ Code like this will be generated: ```rust # struct MyInts(i32, i32); -impl ::core::ops::Not for MyInts { +impl derive_more::Not for MyInts { type Output = MyInts; fn not(self) -> MyInts { MyInts(self.0.not(), self.1.not()) @@ -57,7 +57,7 @@ Code like this will be generated: # x: i32, # y: i32, # } -impl ::core::ops::Not for Point2D { +impl derive_more::Not for Point2D { type Output = Point2D; fn not(self) -> Point2D { Point2D { @@ -104,7 +104,7 @@ Code like this will be generated: # UnsignedOne(u32), # UnsignedTwo(u32), # } -impl ::core::ops::Not for MixedInts { +impl derive_more::Not for MixedInts { type Output = MixedInts; fn not(self) -> MixedInts { match self { @@ -147,12 +147,12 @@ Code like this will be generated: # SmallInt(i32), # Unit, # } -impl ::core::ops::Not for EnumWithUnit { - type Output = Result; - fn not(self) -> Result { +impl derive_more::Not for EnumWithUnit { + type Output = Result; + fn not(self) -> Result { match self { EnumWithUnit::SmallInt(__0) => Ok(EnumWithUnit::SmallInt(__0.not())), - EnumWithUnit::Unit => Err(::derive_more::UnitError::new("not")), + EnumWithUnit::Unit => Err(derive_more::UnitError::new("not")), } } } diff --git a/impl/doc/sum.md b/impl/doc/sum.md index ffb5ea6b..8219c55f 100644 --- a/impl/doc/sum.md +++ b/impl/doc/sum.md @@ -42,23 +42,24 @@ struct MyInts(i32, i64); Code like this will be generated for the `Sum` implementation: ```rust +# use ::core::ops::Add; # struct MyInts(i32, i64); -# impl ::core::ops::Add for MyInts { +# impl Add for MyInts { # type Output = MyInts; # #[inline] # fn add(self, rhs: MyInts) -> MyInts { # MyInts(self.0.add(rhs.0), self.1.add(rhs.1)) # } # } -impl ::core::iter::Sum for MyInts { +impl derive_more::Sum for MyInts { #[inline] - fn sum>(iter: I) -> Self { + fn sum>(iter: I) -> Self { iter.fold( MyInts( - ::core::iter::empty::().sum(), - ::core::iter::empty::().sum(), + derive_more::core::iter::empty::().sum(), + derive_more::core::iter::empty::().sum(), ), - ::core::ops::Add::add, + derive_more::core::ops::Add::add, ) } } diff --git a/impl/doc/try_into.md b/impl/doc/try_into.md index 252a33bf..3953fbf7 100644 --- a/impl/doc/try_into.md +++ b/impl/doc/try_into.md @@ -95,7 +95,7 @@ Code like this will be generated: # UnsignedOne(u32), # UnsignedTwo(u32), # } -impl ::core::convert::TryFrom for (i32) { +impl derive_more::TryFrom for (i32) { type Error = &'static str; fn try_from(value: MixedInts) -> Result { match value { @@ -104,7 +104,7 @@ impl ::core::convert::TryFrom for (i32) { } } } -impl ::core::convert::TryFrom for (i64) { +impl derive_more::TryFrom for (i64) { type Error = &'static str; fn try_from(value: MixedInts) -> Result { match value { @@ -113,7 +113,7 @@ impl ::core::convert::TryFrom for (i64) { } } } -impl ::core::convert::TryFrom for (i32, i32) { +impl derive_more::TryFrom for (i32, i32) { type Error = &'static str; fn try_from(value: MixedInts) -> Result { match value { @@ -122,7 +122,7 @@ impl ::core::convert::TryFrom for (i32, i32) { } } } -impl ::core::convert::TryFrom for (i64, i64) { +impl derive_more::TryFrom for (i64, i64) { type Error = &'static str; fn try_from(value: MixedInts) -> Result { match value { @@ -131,7 +131,7 @@ impl ::core::convert::TryFrom for (i64, i64) { } } } -impl ::core::convert::TryFrom for (u32) { +impl derive_more::TryFrom for (u32) { type Error = &'static str; fn try_from(value: MixedInts) -> Result { match value { @@ -161,7 +161,7 @@ Code like this will be generated: # SmallInt(i32), # Unit, # } -impl ::core::convert::TryFrom for (i32) { +impl derive_more::TryFrom for (i32) { type Error = &'static str; fn try_from(value: EnumWithUnit) -> Result { match value { @@ -170,7 +170,7 @@ impl ::core::convert::TryFrom for (i32) { } } } -impl ::core::convert::TryFrom for () { +impl derive_more::TryFrom for () { type Error = &'static str; fn try_from(value: EnumWithUnit) -> Result { match value { diff --git a/impl/src/add_assign_like.rs b/impl/src/add_assign_like.rs index ad2be2a6..49510eb7 100644 --- a/impl/src/add_assign_like.rs +++ b/impl/src/add_assign_like.rs @@ -29,7 +29,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { quote! { #[automatically_derived] - impl #impl_generics ::derive_more::#trait_ident for #input_type #ty_generics #where_clause { + impl #impl_generics derive_more::#trait_ident for #input_type #ty_generics #where_clause { #[inline] fn #method_ident(&mut self, rhs: #input_type #ty_generics) { #( #exprs; )* diff --git a/impl/src/add_like.rs b/impl/src/add_like.rs index 8cc1a9c4..095bf37f 100644 --- a/impl/src/add_like.rs +++ b/impl/src/add_like.rs @@ -32,7 +32,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { }, Data::Enum(ref data_enum) => ( quote! { - ::derive_more::core::result::Result<#input_type #ty_generics, ::derive_more::BinaryError> + derive_more::core::result::Result<#input_type #ty_generics, derive_more::BinaryError> }, enum_content(input_type, data_enum, &method_ident), ), @@ -42,7 +42,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { quote! { #[automatically_derived] - impl #impl_generics ::derive_more::#trait_ident for #input_type #ty_generics #where_clause { + impl #impl_generics derive_more::#trait_ident for #input_type #ty_generics #where_clause { type Output = #output_type; #[inline] @@ -98,7 +98,9 @@ fn enum_content( let matcher = quote! { (#subtype(#(#l_vars),*), #subtype(#(#r_vars),*)) => { - ::derive_more::core::result::Result::Ok(#subtype(#(#l_vars.#method_iter(#r_vars)),*)) + derive_more::core::result::Result::Ok( + #subtype(#(#l_vars.#method_iter(#r_vars)),*) + ) } }; matches.push(matcher); @@ -117,7 +119,7 @@ fn enum_content( let matcher = quote! { (#subtype{#(#field_names: #l_vars),*}, #subtype{#(#field_names: #r_vars),*}) => { - ::derive_more::core::result::Result::Ok(#subtype{ + derive_more::core::result::Result::Ok(#subtype{ #(#field_names: #l_vars.#method_iter(#r_vars)),* }) } @@ -127,9 +129,9 @@ fn enum_content( Fields::Unit => { let operation_name = method_ident.to_string(); matches.push(quote! { - (#subtype, #subtype) => ::derive_more::core::result::Result::Err( - ::derive_more::BinaryError::Unit( - ::derive_more::UnitError::new(#operation_name) + (#subtype, #subtype) => derive_more::core::result::Result::Err( + derive_more::BinaryError::Unit( + derive_more::UnitError::new(#operation_name) ) ) }); @@ -142,8 +144,8 @@ fn enum_content( // match. let operation_name = method_ident.to_string(); matches.push(quote! { - _ => ::derive_more::core::result::Result::Err(::derive_more::BinaryError::Mismatch( - ::derive_more::WrongVariantError::new(#operation_name) + _ => derive_more::core::result::Result::Err(derive_more::BinaryError::Mismatch( + derive_more::WrongVariantError::new(#operation_name) )) }); } diff --git a/impl/src/as/mod.rs b/impl/src/as/mod.rs index 8c49bbdb..fd631aa3 100644 --- a/impl/src/as/mod.rs +++ b/impl/src/as/mod.rs @@ -240,7 +240,7 @@ impl<'a> ToTokens for Expansion<'a> { }; let trait_ty = quote! { - ::derive_more::#trait_ident <#return_ty> + derive_more::#trait_ident <#return_ty> }; let generics = match &impl_kind { @@ -253,7 +253,7 @@ impl<'a> ToTokens for Expansion<'a> { if is_blanket { generics .params - .push(parse_quote! { #return_ty: ?::derive_more::core::marker::Sized }); + .push(parse_quote! { #return_ty: ?derive_more::core::marker::Sized }); } Cow::Owned(generics) } @@ -270,11 +270,11 @@ impl<'a> ToTokens for Expansion<'a> { <#field_ty as #trait_ty>::#method_ident(#field_ref) }), ImplKind::Specialized => Cow::Owned(quote! { - use ::derive_more::__private::ExtractRef as _; + use derive_more::__private::ExtractRef as _; let conv = - <::derive_more::__private::Conv<& #mut_ #field_ty, #return_ty> - as ::derive_more::core::default::Default>::default(); + + as derive_more::core::default::Default>::default(); (&&conv).__extract_ref(#field_ref) }), }; diff --git a/impl/src/error.rs b/impl/src/error.rs index f02f90a4..61c2e9ad 100644 --- a/impl/src/error.rs +++ b/impl/src/error.rs @@ -40,8 +40,8 @@ pub fn expand( // Not using `#[inline]` here on purpose, since this is almost never part // of a hot codepath. quote! { - fn source(&self) -> Option<&(dyn ::derive_more::Error + 'static)> { - use ::derive_more::__private::AsDynError; + fn source(&self) -> Option<&(dyn derive_more::Error + 'static)> { + use derive_more::__private::AsDynError; #source } } @@ -51,7 +51,10 @@ pub fn expand( // Not using `#[inline]` here on purpose, since this is almost never part // of a hot codepath. quote! { - fn provide<'_request>(&'_request self, request: &mut ::derive_more::core::error::Request<'_request>) { + fn provide<'_request>( + &'_request self, + request: &mut derive_more::core::error::Request<'_request>, + ) { #provide } } @@ -65,7 +68,8 @@ pub fn expand( &generics, quote! { where - #ident #ty_generics: ::derive_more::core::fmt::Debug + ::derive_more::core::fmt::Display + #ident #ty_generics: derive_more::core::fmt::Debug + + derive_more::core::fmt::Display }, ); } @@ -76,9 +80,9 @@ pub fn expand( &generics, quote! { where #( - #bounds: ::derive_more::core::fmt::Debug - + ::derive_more::core::fmt::Display - + ::derive_more::Error + #bounds: derive_more::core::fmt::Debug + + derive_more::core::fmt::Display + + derive_more::Error + 'static ),* }, @@ -89,7 +93,7 @@ pub fn expand( let render = quote! { #[automatically_derived] - impl #impl_generics ::derive_more::Error for #ident #ty_generics #where_clause { + impl #impl_generics derive_more::Error for #ident #ty_generics #where_clause { #source #provide } @@ -213,7 +217,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> { let source_provider = self.source.map(|source| { let source_expr = &self.data.members[source]; quote! { - ::derive_more::Error::provide(&#source_expr, request); + derive_more::Error::provide(&#source_expr, request); } }); let backtrace_provider = self @@ -243,7 +247,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> { let pattern = self.data.matcher(&[source], &[quote! { source }]); Some(quote! { #pattern => { - ::derive_more::Error::provide(source, request); + derive_more::Error::provide(source, request); } }) } @@ -255,7 +259,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> { Some(quote! { #pattern => { request.provide_ref::<::std::backtrace::Backtrace>(backtrace); - ::derive_more::Error::provide(source, request); + derive_more::Error::provide(source, request); } }) } diff --git a/impl/src/fmt/debug.rs b/impl/src/fmt/debug.rs index b09c9c35..68e873e1 100644 --- a/impl/src/fmt/debug.rs +++ b/impl/src/fmt/debug.rs @@ -46,11 +46,11 @@ pub fn expand(input: &syn::DeriveInput, _: &str) -> syn::Result { Ok(quote! { #[automatically_derived] - impl #impl_gens ::derive_more::Debug for #ident #ty_gens #where_clause { + impl #impl_gens derive_more::Debug for #ident #ty_gens #where_clause { #[inline] fn fmt( - &self, __derive_more_f: &mut ::derive_more::core::fmt::Formatter<'_> - ) -> ::derive_more::core::fmt::Result { + &self, __derive_more_f: &mut derive_more::core::fmt::Formatter<'_> + ) -> derive_more::core::fmt::Result { #body } } @@ -228,9 +228,9 @@ impl<'a> Expansion<'a> { fn generate_body(&self) -> syn::Result { if let Some(fmt) = &self.attr.fmt { return Ok(if let Some((expr, trait_ident)) = fmt.transparent_call() { - quote! { ::derive_more::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) } + quote! { derive_more::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) } } else { - quote! { ::derive_more::core::write!(__derive_more_f, #fmt) } + quote! { derive_more::core::write!(__derive_more_f, #fmt) } }); }; @@ -238,7 +238,7 @@ impl<'a> Expansion<'a> { syn::Fields::Unit => { let ident = self.ident.to_string(); Ok(quote! { - ::derive_more::core::fmt::Formatter::write_str( + derive_more::core::fmt::Formatter::write_str( __derive_more_f, #ident, ) @@ -249,40 +249,41 @@ impl<'a> Expansion<'a> { let ident_str = self.ident.to_string(); let out = quote! { - &mut ::derive_more::__private::debug_tuple( + &mut derive_more::__private::debug_tuple( __derive_more_f, #ident_str, ) }; let out = unnamed.unnamed.iter().enumerate().try_fold( out, - |out, (i, field)| { - match FieldAttribute::parse_attrs(&field.attrs, self.attr_name)? - .map(Spanning::into_inner) - { - Some(FieldAttribute::Left(_skip)) => { - exhaustive = false; - Ok::<_, syn::Error>(out) - } - Some(FieldAttribute::Right(fmt_attr)) => Ok(quote! { - ::derive_more::__private::DebugTuple::field( - #out, - &::derive_more::core::format_args!(#fmt_attr), - ) - }), - None => { - let ident = format_ident!("_{i}"); - Ok(quote! { - ::derive_more::__private::DebugTuple::field(#out, #ident) - }) - } + |out, (i, field)| match FieldAttribute::parse_attrs( + &field.attrs, + self.attr_name, + )? + .map(Spanning::into_inner) + { + Some(FieldAttribute::Left(_skip)) => { + exhaustive = false; + Ok::<_, syn::Error>(out) + } + Some(FieldAttribute::Right(fmt_attr)) => Ok(quote! { + derive_more::__private::DebugTuple::field( + #out, + &derive_more::core::format_args!(#fmt_attr), + ) + }), + None => { + let ident = format_ident!("_{i}"); + Ok(quote! { + derive_more::__private::DebugTuple::field(#out, #ident) + }) } }, )?; Ok(if exhaustive { - quote! { ::derive_more::__private::DebugTuple::finish(#out) } + quote! { derive_more::__private::DebugTuple::finish(#out) } } else { - quote! { ::derive_more::__private::DebugTuple::finish_non_exhaustive(#out) } + quote! { derive_more::__private::DebugTuple::finish_non_exhaustive(#out) } }) } syn::Fields::Named(named) => { @@ -290,7 +291,7 @@ impl<'a> Expansion<'a> { let ident = self.ident.to_string(); let out = quote! { - &mut ::derive_more::core::fmt::Formatter::debug_struct( + &mut derive_more::core::fmt::Formatter::debug_struct( __derive_more_f, #ident, ) @@ -308,21 +309,21 @@ impl<'a> Expansion<'a> { Ok::<_, syn::Error>(out) } Some(FieldAttribute::Right(fmt_attr)) => Ok(quote! { - ::derive_more::core::fmt::DebugStruct::field( + derive_more::core::fmt::DebugStruct::field( #out, #field_str, - &::derive_more::core::format_args!(#fmt_attr), + &derive_more::core::format_args!(#fmt_attr), ) }), None => Ok(quote! { - ::derive_more::core::fmt::DebugStruct::field(#out, #field_str, #field_ident) + derive_more::core::fmt::DebugStruct::field(#out, #field_str, #field_ident) }), } })?; Ok(if exhaustive { - quote! { ::derive_more::core::fmt::DebugStruct::finish(#out) } + quote! { derive_more::core::fmt::DebugStruct::finish(#out) } } else { - quote! { ::derive_more::core::fmt::DebugStruct::finish_non_exhaustive(#out) } + quote! { derive_more::core::fmt::DebugStruct::finish_non_exhaustive(#out) } }) } } @@ -336,7 +337,7 @@ impl<'a> Expansion<'a> { out.extend(fmt.bounded_types(self.fields).map(|(ty, trait_name)| { let trait_ident = format_ident!("{trait_name}"); - parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident } + parse_quote! { #ty: derive_more::core::fmt::#trait_ident } })); Ok(out) } else { @@ -350,12 +351,12 @@ impl<'a> Expansion<'a> { |(ty, trait_name)| { let trait_ident = format_ident!("{trait_name}"); - parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident } + parse_quote! { #ty: derive_more::core::fmt::#trait_ident } }, )); } Some(FieldAttribute::Left(_skip)) => {} - None => out.extend([parse_quote! { #ty: ::derive_more::Debug }]), + None => out.extend([parse_quote! { #ty: derive_more::Debug }]), } Ok(out) }) diff --git a/impl/src/fmt/display.rs b/impl/src/fmt/display.rs index 7c891c1e..e41dcbd1 100644 --- a/impl/src/fmt/display.rs +++ b/impl/src/fmt/display.rs @@ -50,10 +50,10 @@ pub fn expand(input: &syn::DeriveInput, trait_name: &str) -> syn::Result - ) -> ::derive_more::core::fmt::Result { + &self, __derive_more_f: &mut derive_more::core::fmt::Formatter<'_> + ) -> derive_more::core::fmt::Result { #body } } @@ -188,7 +188,7 @@ fn expand_union( Ok(( attrs.bounds.0.clone().into_iter().collect(), - quote! { ::derive_more::core::write!(__derive_more_f, #fmt) }, + quote! { derive_more::core::write!(__derive_more_f, #fmt) }, )) } @@ -229,16 +229,16 @@ impl<'a> Expansion<'a> { match &self.attrs.fmt { Some(fmt) => { Ok(if let Some((expr, trait_ident)) = fmt.transparent_call() { - quote! { ::derive_more::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) } + quote! { derive_more::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) } } else { - quote! { ::derive_more::core::write!(__derive_more_f, #fmt) } + quote! { derive_more::core::write!(__derive_more_f, #fmt) } }) } None if self.fields.is_empty() => { let ident_str = self.ident.to_string(); Ok(quote! { - ::derive_more::core::write!(__derive_more_f, #ident_str) + derive_more::core::write!(__derive_more_f, #ident_str) }) } None if self.fields.len() == 1 => { @@ -251,7 +251,7 @@ impl<'a> Expansion<'a> { let trait_ident = self.trait_ident; Ok(quote! { - ::derive_more::core::fmt::#trait_ident::fmt(#ident, __derive_more_f) + derive_more::core::fmt::#trait_ident::fmt(#ident, __derive_more_f) }) } _ => Err(syn::Error::new( @@ -275,7 +275,7 @@ impl<'a> Expansion<'a> { .map(|f| { let ty = &f.ty; let trait_ident = &self.trait_ident; - vec![parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }] + vec![parse_quote! { #ty: derive_more::core::fmt::#trait_ident }] }) .unwrap_or_default(); }; @@ -284,7 +284,7 @@ impl<'a> Expansion<'a> { .map(|(ty, trait_name)| { let trait_ident = format_ident!("{trait_name}"); - parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident } + parse_quote! { #ty: derive_more::core::fmt::#trait_ident } }) .chain(self.attrs.bounds.0.clone()) .collect() diff --git a/impl/src/from.rs b/impl/src/from.rs index 3b55fa3c..c784d972 100644 --- a/impl/src/from.rs +++ b/impl/src/from.rs @@ -165,7 +165,7 @@ impl<'a> Expansion<'a> { let index = index.into_iter(); let from_ty = from_tys.next().unwrap_or_else(|| unreachable!()); quote! { - #( #ident: )* <#ty as ::derive_more::From<#from_ty>>::from( + #( #ident: )* <#ty as derive_more::From<#from_ty>>::from( value #( .#index )* ), } @@ -173,7 +173,7 @@ impl<'a> Expansion<'a> { Ok(quote! { #[automatically_derived] - impl #impl_gens ::derive_more::From<#ty> for #ident #ty_gens #where_clause { + impl #impl_gens derive_more::From<#ty> for #ident #ty_gens #where_clause { #[inline] fn from(value: #ty) -> Self { #ident #( :: #variant )* #init @@ -193,7 +193,7 @@ impl<'a> Expansion<'a> { Ok(quote! { #[automatically_derived] - impl #impl_gens ::derive_more::From<(#( #field_tys ),*)> for #ident #ty_gens #where_clause { + impl #impl_gens derive_more::From<(#( #field_tys ),*)> for #ident #ty_gens #where_clause { #[inline] fn from(value: (#( #field_tys ),*)) -> Self { #ident #( :: #variant )* #init @@ -209,7 +209,7 @@ impl<'a> Expansion<'a> { let index = index.into_iter(); let gen_ident = format_ident!("__FromT{i}"); let out = quote! { - #( #ident: )* <#ty as ::derive_more::From<#gen_ident>>::from( + #( #ident: )* <#ty as derive_more::From<#gen_ident>>::from( value #( .#index )* ), }; @@ -223,7 +223,7 @@ impl<'a> Expansion<'a> { let mut generics = self.generics.clone(); for (ty, ident) in field_tys.iter().zip(&gen_idents) { generics.make_where_clause().predicates.push( - parse_quote! { #ty: ::derive_more::From<#ident> }, + parse_quote! { #ty: derive_more::From<#ident> }, ); generics .params @@ -235,7 +235,7 @@ impl<'a> Expansion<'a> { Ok(quote! { #[automatically_derived] - impl #impl_gens ::derive_more::From<(#( #gen_idents ),*)> for #ident #ty_gens #where_clause { + impl #impl_gens derive_more::From<(#( #gen_idents ),*)> for #ident #ty_gens #where_clause { #[inline] fn from(value: (#( #gen_idents ),*)) -> Self { #ident #(:: #variant)* #init diff --git a/impl/src/from_str.rs b/impl/src/from_str.rs index 308b63fa..16ccbffb 100644 --- a/impl/src/from_str.rs +++ b/impl/src/from_str.rs @@ -42,8 +42,8 @@ pub fn struct_from(state: &State, trait_name: &'static str) -> TokenStream { type Err = <#field_type as #trait_path>::Err; #[inline] - fn from_str(src: &str) -> ::derive_more::core::result::Result { - Ok(#body) + fn from_str(src: &str) -> derive_more::core::result::Result { + derive_more::core::result::Result::Ok(#body) } } } @@ -94,13 +94,13 @@ fn enum_from( quote! { impl #trait_path for #input_type { - type Err = ::derive_more::FromStrError; + type Err = derive_more::FromStrError; #[inline] - fn from_str(src: &str) -> ::derive_more::core::result::Result { + fn from_str(src: &str) -> derive_more::core::result::Result { Ok(match src.to_lowercase().as_str() { #(#cases)* - _ => return Err(::derive_more::FromStrError::new(#input_type_name)), + _ => return Err(derive_more::FromStrError::new(#input_type_name)), }) } } diff --git a/impl/src/into.rs b/impl/src/into.rs index f2125fc8..f426beca 100644 --- a/impl/src/into.rs +++ b/impl/src/into.rs @@ -179,13 +179,13 @@ impl<'a> Expansion<'a> { Ok(quote! { #[automatically_derived] - impl #impl_gens ::derive_more::core::convert::From<#r #lf #m #input_ident #ty_gens> + impl #impl_gens derive_more::core::convert::From<#r #lf #m #input_ident #ty_gens> for ( #( #r #lf #m #tys ),* ) #where_clause { #[inline] fn from(value: #r #lf #m #input_ident #ty_gens) -> Self { (#( - <#r #m #tys as ::derive_more::core::convert::From<_>>::from( + <#r #m #tys as derive_more::core::convert::From<_>>::from( #r #m value. #fields_idents ) ),*) diff --git a/impl/src/not_like.rs b/impl/src/not_like.rs index 8186e980..da8a3457 100644 --- a/impl/src/not_like.rs +++ b/impl/src/not_like.rs @@ -36,7 +36,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { quote! { #[automatically_derived] - impl #impl_generics ::derive_more::#trait_ident for #input_type #ty_generics #where_clause { + impl #impl_generics derive_more::#trait_ident for #input_type #ty_generics #where_clause { type Output = #output_type; #[inline] @@ -108,7 +108,7 @@ fn enum_output_type_and_content( let method_iter = method_iter.by_ref(); let mut body = quote! { #subtype(#(#vars.#method_iter()),*) }; if has_unit_type { - body = quote! { ::derive_more::core::result::Result::Ok(#body) } + body = quote! { derive_more::core::result::Result::Ok(#body) } } let matcher = quote! { #subtype(#(#vars),*) => { @@ -135,7 +135,7 @@ fn enum_output_type_and_content( #subtype{#(#field_names: #vars.#method_iter()),*} }; if has_unit_type { - body = quote! { ::derive_more::core::result::Result::Ok(#body) } + body = quote! { derive_more::core::result::Result::Ok(#body) } } let matcher = quote! { #subtype{#(#field_names: #vars),*} => { @@ -147,8 +147,8 @@ fn enum_output_type_and_content( Fields::Unit => { let operation_name = method_ident.to_string(); matches.push(quote! { - #subtype => ::derive_more::core::result::Result::Err( - ::derive_more::UnitError::new(#operation_name) + #subtype => derive_more::core::result::Result::Err( + derive_more::UnitError::new(#operation_name) ) }); } @@ -162,7 +162,9 @@ fn enum_output_type_and_content( }; let output_type = if has_unit_type { - quote! { ::derive_more::core::result::Result<#input_type #ty_generics, ::derive_more::UnitError> } + quote! { + derive_more::core::result::Result<#input_type #ty_generics, derive_more::UnitError> + } } else { quote! { #input_type #ty_generics } }; diff --git a/impl/src/sum_like.rs b/impl/src/sum_like.rs index 2256223b..40926598 100644 --- a/impl/src/sum_like.rs +++ b/impl/src/sum_like.rs @@ -18,7 +18,7 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result Result = field_types .iter() .map(|field_type| { - quote! { #trait_path::#method_ident(::derive_more::core::iter::empty::<#field_type>()) } + quote! { #trait_path::#method_ident(derive_more::core::iter::empty::<#field_type>()) } }) .collect(); let identity = multi_field_data.initializer(&initializers); @@ -45,7 +45,7 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result>(iter: I) -> Self { + fn #method_ident>(iter: I) -> Self { iter.fold(#identity, #op_path::#op_method_ident) } } diff --git a/impl/src/try_from.rs b/impl/src/try_from.rs index d3e4d5e0..f50c321f 100644 --- a/impl/src/try_from.rs +++ b/impl/src/try_from.rs @@ -121,16 +121,18 @@ impl ToTokens for Expansion { quote! { #[automatically_derived] - impl #impl_generics ::derive_more::TryFrom<#repr_ty #ty_generics> for #ident #where_clause { - type Error = ::derive_more::TryFromReprError<#repr_ty>; + impl #impl_generics derive_more::TryFrom<#repr_ty #ty_generics> for #ident #where_clause { + type Error = derive_more::TryFromReprError<#repr_ty>; #[allow(non_upper_case_globals)] #[inline] - fn try_from(val: #repr_ty) -> ::derive_more::core::result::Result { + fn try_from(val: #repr_ty) -> derive_more::core::result::Result { #( const #consts: #repr_ty = #discriminants; )* match val { - #(#consts => ::derive_more::core::result::Result::Ok(#ident::#variants),)* - _ => ::derive_more::core::result::Result::Err(::derive_more::TryFromReprError::new(val)), + #(#consts => derive_more::core::result::Result::Ok(#ident::#variants),)* + _ => derive_more::core::result::Result::Err( + derive_more::TryFromReprError::new(val) + ), } } } diff --git a/impl/src/try_into.rs b/impl/src/try_into.rs index 1da075ab..46478270 100644 --- a/impl/src/try_into.rs +++ b/impl/src/try_into.rs @@ -101,21 +101,19 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result for - (#(#reference_with_lifetime #original_types),*) - #where_clause - { - type Error = ::derive_more::TryIntoError<#reference_with_lifetime #input_type>; + impl #impl_generics derive_more::core::convert::TryFrom< + #reference_with_lifetime #input_type #ty_generics + > for (#(#reference_with_lifetime #original_types),*) #where_clause { + type Error = derive_more::TryIntoError<#reference_with_lifetime #input_type>; #[inline] fn try_from( value: #reference_with_lifetime #input_type #ty_generics, - ) -> ::derive_more::core::result::Result { + ) -> derive_more::core::result::Result { match value { - #(#matchers)|* => ::derive_more::core::result::Result::Ok(#vars), - _ => ::derive_more::core::result::Result::Err( - ::derive_more::TryIntoError::new(value, #variant_names, #output_type), + #(#matchers)|* => derive_more::core::result::Result::Ok(#vars), + _ => derive_more::core::result::Result::Err( + derive_more::TryIntoError::new(value, #variant_names, #output_type), ), } } diff --git a/impl/src/try_unwrap.rs b/impl/src/try_unwrap.rs index 67d3793d..b90ba714 100644 --- a/impl/src/try_unwrap.rs +++ b/impl/src/try_unwrap.rs @@ -18,7 +18,7 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result Result Result<(#(#data_types),*), ::derive_more::TryUnwrapError> { + pub fn #fn_name(self) -> derive_more::core::result::Result< + (#(#data_types),*), derive_more::TryUnwrapError + > { match self { - #pattern => Ok(#ret_value), + #pattern => derive_more::core::result::Result::Ok(#ret_value), val @ _ => #failed_block, } } @@ -84,9 +86,11 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result Result<(#(&#data_types),*), ::derive_more::TryUnwrapError<&Self>> { + pub fn #ref_fn_name(&self) -> derive_more::core::result::Result< + (#(&#data_types),*), derive_more::TryUnwrapError<&Self> + > { match self { - #pattern => Ok(#ret_value), + #pattern => derive_more::core::result::Result::Ok(#ret_value), val @ _ => #failed_block_ref, } } @@ -97,9 +101,11 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result Result<(#(&mut #data_types),*), ::derive_more::TryUnwrapError<&mut Self>> { + pub fn #mut_fn_name(&mut self) -> derive_more::core::result::Result< + (#(&mut #data_types),*), derive_more::TryUnwrapError<&mut Self> + > { match self { - #pattern => Ok(#ret_value), + #pattern => derive_more::core::result::Result::Ok(#ret_value), val @ _ => #failed_block_mut, } } @@ -154,8 +160,18 @@ fn failed_block(state: &State, enum_name: &Ident, func_name: &Ident) -> TokenStr Fields::Unit => quote! {}, }; let variant_ident = &variant.ident; - let error = quote! { ::derive_more::TryUnwrapError::<_>::new(val, stringify!(#enum_name), stringify!(#variant_ident), stringify!(#func_name)) }; - quote! { val @ #enum_name :: #variant_ident #data_pattern => Err(#error) } + let error = quote! { + derive_more::TryUnwrapError::<_>::new( + val, + stringify!(#enum_name), + stringify!(#variant_ident), + stringify!(#func_name), + ) + }; + quote! { + val @ #enum_name :: #variant_ident #data_pattern + => derive_more::core::result::Result::Err(#error) + } }); quote! { diff --git a/impl/src/utils.rs b/impl/src/utils.rs index fabb217c..73a4bbcf 100644 --- a/impl/src/utils.rs +++ b/impl/src/utils.rs @@ -144,7 +144,7 @@ pub fn add_extra_type_param_bound_op_output<'a>( for type_param in &mut generics.type_params_mut() { let type_ident = &type_param.ident; let bound: TypeParamBound = parse_quote! { - ::derive_more::core::ops::#trait_ident + derive_more::core::ops::#trait_ident }; type_param.bounds.push(bound) } @@ -156,10 +156,7 @@ pub fn add_extra_ty_param_bound_op<'a>( generics: &'a Generics, trait_ident: &'a Ident, ) -> Generics { - add_extra_ty_param_bound( - generics, - "e! { ::derive_more::core::ops::#trait_ident }, - ) + add_extra_ty_param_bound(generics, "e! { derive_more::core::ops::#trait_ident }) } pub fn add_extra_ty_param_bound<'a>( @@ -229,11 +226,11 @@ pub fn add_where_clauses_for_new_ident<'a>( sized: bool, ) -> Generics { let generic_param = if fields.len() > 1 { - quote! { #type_ident: ::derive_more::core::marker::Copy } + quote! { #type_ident: derive_more::core::marker::Copy } } else if sized { quote! { #type_ident } } else { - quote! { #type_ident: ?::derive_more::core::marker::Sized } + quote! { #type_ident: ?derive_more::core::marker::Sized } }; let generics = add_extra_where_clauses(generics, type_where_clauses); @@ -377,7 +374,7 @@ impl<'input> State<'input> { let trait_name = trait_name.trim_end_matches("ToInner"); let trait_ident = format_ident!("{trait_name}"); let method_ident = format_ident!("{trait_attr}"); - let trait_path = quote! { ::derive_more::#trait_ident }; + let trait_path = quote! { derive_more::#trait_ident }; let (derive_type, fields, variants): (_, Vec<_>, Vec<_>) = match input.data { Data::Struct(ref data_struct) => match data_struct.fields { Fields::Unnamed(ref fields) => { @@ -520,7 +517,7 @@ impl<'input> State<'input> { let trait_name = trait_name.trim_end_matches("ToInner"); let trait_ident = format_ident!("{trait_name}"); let method_ident = format_ident!("{trait_attr}"); - let trait_path = quote! { ::derive_more::#trait_ident }; + let trait_path = quote! { derive_more::#trait_ident }; let (derive_type, fields): (_, Vec<_>) = match variant.fields { Fields::Unnamed(ref fields) => { (DeriveType::Unnamed, unnamed_to_vec(fields)) diff --git a/tests/compile_fail/as_mut/renamed_generic.stderr b/tests/compile_fail/as_mut/renamed_generic.stderr index f0838843..190faaed 100644 --- a/tests/compile_fail/as_mut/renamed_generic.stderr +++ b/tests/compile_fail/as_mut/renamed_generic.stderr @@ -5,10 +5,7 @@ error[E0599]: the method `as_mut` exists for struct `Baz`, but its trait bo | ------------- doesn't satisfy `Foo: AsMut>` ... 7 | struct Baz(Foo); - | ------------- - | | - | method `as_mut` not found for this struct - | doesn't satisfy `Baz: AsMut>` + | ------------- method `as_mut` not found for this struct because it doesn't satisfy `Baz: AsMut>` ... 11 | let _: &mut Bar = item.as_mut(); | ^^^^^^ method cannot be called on `Baz` due to unsatisfied trait bounds diff --git a/tests/compile_fail/as_ref/renamed_generic.stderr b/tests/compile_fail/as_ref/renamed_generic.stderr index 22fd78a5..07311720 100644 --- a/tests/compile_fail/as_ref/renamed_generic.stderr +++ b/tests/compile_fail/as_ref/renamed_generic.stderr @@ -5,10 +5,7 @@ error[E0599]: the method `as_ref` exists for struct `Baz`, but its trait bo | ------------- doesn't satisfy `Foo: AsRef>` ... 7 | struct Baz(Foo); - | ------------- - | | - | method `as_ref` not found for this struct - | doesn't satisfy `Baz: AsRef>` + | ------------- method `as_ref` not found for this struct because it doesn't satisfy `Baz: AsRef>` ... 11 | let _: &Bar = item.as_ref(); | ^^^^^^ method cannot be called on `Baz` due to unsatisfied trait bounds diff --git a/tests/deref_mut.rs b/tests/deref_mut.rs index fb1fe87e..2f0e8929 100644 --- a/tests/deref_mut.rs +++ b/tests/deref_mut.rs @@ -12,7 +12,8 @@ use derive_more::DerefMut; #[derive(DerefMut)] #[deref_mut(forward)] struct MyBoxedInt(Box); -// Deref implementation is needed for DerefMut + +// `Deref` implementation is required for `DerefMut`. impl ::core::ops::Deref for MyBoxedInt { type Target = as ::core::ops::Deref>::Target; #[inline] @@ -26,7 +27,8 @@ struct NumRef<'a> { #[deref_mut(forward)] num: &'a mut i32, } -// Deref implementation is needed for DerefMut + +// `Deref` implementation is required for `DerefMut`. impl<'a> ::core::ops::Deref for NumRef<'a> { type Target = <&'a mut i32 as ::core::ops::Deref>::Target; #[inline] @@ -43,7 +45,7 @@ struct NumRef2<'a> { useless: bool, } -// Deref implementation is needed for DerefMut +// `Deref` implementation is required for `DerefMut`. impl<'a> ::core::ops::Deref for NumRef2<'a> { type Target = <&'a mut i32 as ::core::ops::Deref>::Target; #[inline] @@ -55,7 +57,7 @@ impl<'a> ::core::ops::Deref for NumRef2<'a> { #[derive(DerefMut)] struct MyInt(i32); -// Deref implementation is needed for DerefMutToInner +// `Deref` implementation is required for `DerefMut`. impl ::core::ops::Deref for MyInt { type Target = i32; #[inline] @@ -69,7 +71,7 @@ struct Point1D { x: i32, } -// Deref implementation is needed for DerefMutToInner +// `Deref` implementation is required for `DerefMut`. impl ::core::ops::Deref for Point1D { type Target = i32; #[inline] @@ -84,6 +86,8 @@ struct CoolVec { #[deref_mut] vec: Vec, } + +// `Deref` implementation is required for `DerefMut`. impl ::core::ops::Deref for CoolVec { type Target = Vec; #[inline] @@ -95,6 +99,7 @@ impl ::core::ops::Deref for CoolVec { #[derive(DerefMut)] struct GenericVec(Vec); +// `Deref` implementation is required for `DerefMut`. impl ::core::ops::Deref for GenericVec { type Target = Vec; #[inline] @@ -112,6 +117,7 @@ fn deref_mut_generic() { #[derive(DerefMut)] struct GenericBox(#[deref_mut(forward)] Box); +// `Deref` implementation is required for `DerefMut`. impl ::core::ops::Deref for GenericBox where Box: ::core::ops::Deref, diff --git a/tests/index_mut.rs b/tests/index_mut.rs index 0aba15ad..cd16cb53 100644 --- a/tests/index_mut.rs +++ b/tests/index_mut.rs @@ -4,7 +4,7 @@ use derive_more::IndexMut; #[derive(IndexMut)] struct MyVec(Vec); -//Index implementation is required for IndexMut +// `Index` implementation is required for `IndexMut`. impl<__IdxT> ::core::ops::Index<__IdxT> for MyVec where Vec: ::core::ops::Index<__IdxT>, @@ -23,7 +23,7 @@ struct Numbers { useless: bool, } -//Index implementation is required for IndexMut +// `Index` implementation is required for `IndexMut`. impl<__IdxT> ::core::ops::Index<__IdxT> for Numbers where Vec: ::core::ops::Index<__IdxT>, diff --git a/tests/into_iterator.rs b/tests/into_iterator.rs index 6458ec12..7a876c96 100644 --- a/tests/into_iterator.rs +++ b/tests/into_iterator.rs @@ -77,7 +77,7 @@ struct Numbers3 { useless2: bool, } -// Test that owned is not enabled when ref/ref_mut are enabled without owned +// Test that `owned` is not enabled when `ref`/`ref_mut` are enabled without `owned`. impl ::core::iter::IntoIterator for Numbers3 { type Item = as ::core::iter::IntoIterator>::Item; type IntoIter = as ::core::iter::IntoIterator>::IntoIter; diff --git a/tests/sum.rs b/tests/sum.rs index d220fd5a..a273bf80 100644 --- a/tests/sum.rs +++ b/tests/sum.rs @@ -5,7 +5,7 @@ use derive_more::Sum; #[derive(Sum)] struct MyInts(i32, i64); -// Add implementation is needed for Sum +// `Add` implementation is required for `Sum`. impl ::core::ops::Add for MyInts { type Output = MyInts; #[inline] @@ -20,6 +20,7 @@ struct Point2D { y: i32, } +// `Add` implementation is required for `Sum`. impl ::core::ops::Add for Point2D { type Output = Point2D; #[inline]