Skip to content

Commit

Permalink
Refactor ::derive_more absolute paths to just derive_more (#344, #…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
tyranron committed Mar 27, 2024
1 parent 2a001d6 commit 42d5251
Show file tree
Hide file tree
Showing 37 changed files with 289 additions and 237 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```


Expand Down
18 changes: 9 additions & 9 deletions impl/doc/add.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -112,9 +112,9 @@ Code like this will be generated:
# UnsignedTwo(u32),
# Unit,
# }
impl ::core::ops::Add for MixedInts {
type Output = Result<MixedInts, ::derive_more::BinaryError>;
fn add(self, rhs: MixedInts) -> Result<MixedInts, ::derive_more::BinaryError> {
impl derive_more::Add for MixedInts {
type Output = Result<MixedInts, derive_more::BinaryError>;
fn add(self, rhs: MixedInts) -> Result<MixedInts, derive_more::BinaryError> {
match (self, rhs) {
(MixedInts::SmallInt(__l_0), MixedInts::SmallInt(__r_0)) => {
Ok(MixedInts::SmallInt(__l_0.add(__r_0)))
Expand All @@ -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"),
)),
}
}
Expand Down
4 changes: 2 additions & 2 deletions impl/doc/add_assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions impl/doc/as_mut.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Generates:

```rust
# struct MyWrapper(String);
impl AsMut<String> for MyWrapper {
impl derive_more::AsMut<String> for MyWrapper {
fn as_mut(&mut self) -> &mut String {
&mut self.0
}
Expand All @@ -50,9 +50,9 @@ This generates code equivalent to:

```rust
# struct SingleFieldForward(Vec<i32>);
impl<T: ?Sized> AsMut<T> for SingleFieldForward
impl<T: ?Sized> derive_more::AsMut<T> for SingleFieldForward
where
Vec<i32>: AsMut<T>,
Vec<i32>: derive_more::AsMut<T>,
{
#[inline]
fn as_mut(&mut self) -> &mut T {
Expand Down
6 changes: 3 additions & 3 deletions impl/doc/as_ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Generates:

```rust
# struct MyWrapper(String);
impl AsRef<String> for MyWrapper {
impl derive_more::AsRef<String> for MyWrapper {
fn as_ref(&self) -> &String {
&self.0
}
Expand All @@ -50,9 +50,9 @@ This generates code equivalent to:

```rust
# struct SingleFieldForward(Vec<i32>);
impl<T: ?Sized> AsRef<T> for SingleFieldForward
impl<T: ?Sized> derive_more::AsRef<T> for SingleFieldForward
where
Vec<i32>: AsRef<T>,
Vec<i32>: derive_more::AsRef<T>,
{
#[inline]
fn as_ref(&self) -> &T {
Expand Down
8 changes: 4 additions & 4 deletions impl/doc/deref.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Code like this will be generated:
# cool: bool,
# vec: Vec<i32>,
# }
impl ::core::ops::Deref for CoolVec {
impl derive_more::Deref for CoolVec {
type Target = Vec<i32>;
#[inline]
fn deref(&self) -> &Self::Target {
Expand All @@ -90,11 +90,11 @@ Code like this will be generated:

```rust
# struct MyBoxedInt(Box<i32>);
impl ::core::ops::Deref for MyBoxedInt {
type Target = <Box<i32> as ::core::ops::Deref>::Target;
impl derive_more::Deref for MyBoxedInt {
type Target = <Box<i32> as derive_more::Deref>::Target;
#[inline]
fn deref(&self) -> &Self::Target {
<Box<i32> as ::core::ops::Deref>::deref(&self.0)
<Box<i32> as derive_more::Deref>::deref(&self.0)
}
}
```
Expand Down
16 changes: 9 additions & 7 deletions impl/doc/deref_mut.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,19 @@ struct CoolVec {
Code like this will be generated:

```rust
# use ::core::ops::Deref;
# struct CoolVec {
# cool: bool,
# vec: Vec<i32>,
# }
# impl ::core::ops::Deref for CoolVec {
# impl Deref for CoolVec {
# type Target = Vec<i32>;
# #[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
Expand All @@ -106,18 +107,19 @@ struct MyBoxedInt(Box<i32>);
When deriving a forwarded `DerefMut` for a struct:

```rust
# use ::core::ops::Deref;
# struct MyBoxedInt(Box<i32>);
# impl ::core::ops::Deref for MyBoxedInt {
# type Target = <Box<i32> as ::core::ops::Deref>::Target;
# impl Deref for MyBoxedInt {
# type Target = <Box<i32> as Deref>::Target;
# #[inline]
# fn deref(&self) -> &Self::Target {
# <Box<i32> as ::core::ops::Deref>::deref(&self.0)
# <Box<i32> 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 {
<Box<i32> as ::core::ops::DerefMut>::deref_mut(&mut self.0)
<Box<i32> as derive_more::DerefMut>::deref_mut(&mut self.0)
}
}
```
Expand Down
14 changes: 7 additions & 7 deletions impl/doc/from_str.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Code like this will be generated:

```rust
# struct MyInt(i32);
impl ::core::str::FromStr for MyInt {
type Err = <i32 as ::core::str::FromStr>::Err;
impl derive_more::FromStr for MyInt {
type Err = <i32 as derive_more::FromStr>::Err;
fn from_str(src: &str) -> Result<Self, Self::Err> {
return Ok(MyInt(i32::from_str(src)?));
}
Expand Down Expand Up @@ -74,8 +74,8 @@ Code like this will be generated:
# struct Point1D {
# x: i32,
# }
impl ::core::str::FromStr for Point1D {
type Err = <i32 as ::core::str::FromStr>::Err;
impl derive_more::FromStr for Point1D {
type Err = <i32 as derive_more::FromStr>::Err;
fn from_str(src: &str) -> Result<Self, Self::Err> {
return Ok(Point1D {
x: i32::from_str(src)?,
Expand Down Expand Up @@ -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<Self, Self::Err> {
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")),
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions impl/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ Code like this will be generated:
# numbers: Vec<i32>,
# useless: bool,
# }
impl<__IdxT> ::core::ops::Index<__IdxT> for Numbers
impl<__IdxT> derive_more::Index<__IdxT> for Numbers
where
Vec<i32>: ::core::ops::Index<__IdxT>,
Vec<i32>: derive_more::Index<__IdxT>,
{
type Output = <Vec<i32> as ::core::ops::Index<__IdxT>>::Output;
type Output = <Vec<i32> as derive_more::Index<__IdxT>>::Output;
#[inline]
fn index(&self, idx: __IdxT) -> &Self::Output {
<Vec<i32> as ::core::ops::Index<__IdxT>>::index(&self.numbers, idx)
<Vec<i32> as derive_more::Index<__IdxT>>::index(&self.numbers, idx)
}
}
```
Expand Down
15 changes: 8 additions & 7 deletions impl/doc/index_mut.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,28 @@ struct Numbers {
Code like this will be generated to implement `IndexMut`:

```rust
# use ::core::ops::Index;
# struct Numbers {
# numbers: Vec<i32>,
# useless: bool,
# }
# impl<__IdxT> ::core::ops::Index<__IdxT> for Numbers
# impl<__IdxT> Index<__IdxT> for Numbers
# where
# Vec<i32>: ::core::ops::Index<__IdxT>,
# Vec<i32>: Index<__IdxT>,
# {
# type Output = <Vec<i32> as ::core::ops::Index<__IdxT>>::Output;
# type Output = <Vec<i32> as Index<__IdxT>>::Output;
# #[inline]
# fn index(&self, idx: __IdxT) -> &Self::Output {
# <Vec<i32> as ::core::ops::Index<__IdxT>>::index(&self.numbers, idx)
# <Vec<i32> 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<i32>: ::core::ops::IndexMut<__IdxT>,
Vec<i32>: derive_more::IndexMut<__IdxT>,
{
#[inline]
fn index_mut(&mut self, idx: __IdxT) -> &mut Self::Output {
<Vec<i32> as ::core::ops::IndexMut<__IdxT>>::index_mut(&mut self.numbers, idx)
<Vec<i32> as derive_more::IndexMut<__IdxT>>::index_mut(&mut self.numbers, idx)
}
}
```
Expand Down
24 changes: 12 additions & 12 deletions impl/doc/into_iterator.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,30 @@ Code like this will be generated:
# numbers: Vec<i32>,
# useless: bool,
# }
impl ::core::iter::IntoIterator for Numbers {
type Item = <Vec<i32> as ::core::iter::IntoIterator>::Item;
type IntoIter = <Vec<i32> as ::core::iter::IntoIterator>::IntoIter;
impl derive_more::IntoIterator for Numbers {
type Item = <Vec<i32> as derive_more::IntoIterator>::Item;
type IntoIter = <Vec<i32> as derive_more::IntoIterator>::IntoIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
<Vec<i32> as ::core::iter::IntoIterator>::into_iter(self.numbers)
<Vec<i32> as derive_more::IntoIterator>::into_iter(self.numbers)
}
}

impl<'__deriveMoreLifetime> ::core::iter::IntoIterator for &'__deriveMoreLifetime Numbers {
type Item = <&'__deriveMoreLifetime Vec<i32> as ::core::iter::IntoIterator>::Item;
type IntoIter = <&'__deriveMoreLifetime Vec<i32> as ::core::iter::IntoIterator>::IntoIter;
impl<'__deriveMoreLifetime> derive_more::IntoIterator for &'__deriveMoreLifetime Numbers {
type Item = <&'__deriveMoreLifetime Vec<i32> as derive_more::IntoIterator>::Item;
type IntoIter = <&'__deriveMoreLifetime Vec<i32> as derive_more::IntoIterator>::IntoIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
<&'__deriveMoreLifetime Vec<i32> as ::core::iter::IntoIterator>::into_iter(&self.numbers)
<&'__deriveMoreLifetime Vec<i32> as derive_more::IntoIterator>::into_iter(&self.numbers)
}
}

impl<'__deriveMoreLifetime> ::core::iter::IntoIterator for &'__deriveMoreLifetime mut Numbers {
type Item = <&'__deriveMoreLifetime mut Vec<i32> as ::core::iter::IntoIterator>::Item;
type IntoIter = <&'__deriveMoreLifetime mut Vec<i32> as ::core::iter::IntoIterator>::IntoIter;
impl<'__deriveMoreLifetime> derive_more::IntoIterator for &'__deriveMoreLifetime mut Numbers {
type Item = <&'__deriveMoreLifetime mut Vec<i32> as derive_more::IntoIterator>::Item;
type IntoIter = <&'__deriveMoreLifetime mut Vec<i32> as derive_more::IntoIterator>::IntoIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
<&'__deriveMoreLifetime mut Vec<i32> as ::core::iter::IntoIterator>::into_iter(
<&'__deriveMoreLifetime mut Vec<i32> as derive_more::IntoIterator>::into_iter(
&mut self.numbers,
)
}
Expand Down
16 changes: 8 additions & 8 deletions impl/doc/mul.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 42d5251

Please sign in to comment.