Skip to content

Commit

Permalink
Re-export traits (#273)
Browse files Browse the repository at this point in the history
Re-export traits from `std` for all our derives. This way people don't
need to import traits manually when they want to reference them
somewhere, such as bounds.

Fixes #271

---------

Co-authored-by: tyranron <[email protected]>
  • Loading branch information
JelteF and tyranron authored Jul 23, 2023
1 parent fdf7fe1 commit e10b96b
Show file tree
Hide file tree
Showing 28 changed files with 319 additions and 203 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
and ignores field type itself.
- The `Into` derive now uses `#[into(<types>)]` instead of `#[into(types(<types>))]`
and ignores field type itself.
- Importing a derive macro now also import its corresponding trait.

### Added

Expand Down
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ index = ["derive_more-impl/index"]
index_mut = ["derive_more-impl/index_mut"]
into = ["derive_more-impl/into"]
into_iterator = ["derive_more-impl/into_iterator"]
iterator = ["derive_more-impl/iterator"]
mul_assign = ["derive_more-impl/mul_assign"]
mul = ["derive_more-impl/mul"]
not = ["derive_more-impl/not"]
Expand All @@ -73,8 +72,8 @@ try_unwrap = ["derive_more-impl/try_unwrap"]

std = []
full = [
"add_assign",
"add",
"add_assign",
"as_mut",
"as_ref",
"constructor",
Expand All @@ -90,14 +89,13 @@ full = [
"into",
"into_iterator",
"is_variant",
"iterator",
"mul_assign",
"mul",
"mul_assign",
"not",
"sum",
"try_into",
"unwrap",
"try_unwrap",
"unwrap",
]

testing-helpers = ["derive_more-impl/testing-helpers", "dep:rustc_version"]
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ extern crate derive_more;
# fn main() {} // omit wrapping statements above into `main()` in tests
```

## Re-exports

This crate also re-exports all of 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;
```

[`cargo-expand`]: https://github.com/dtolnay/cargo-expand
[`derive-new`]: https://github.com/nrc/derive-new

Expand Down
11 changes: 5 additions & 6 deletions impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ unicode-xid = { version = "0.2.2", optional = true }
rustc_version = { version = "0.4", optional = true }

[dev-dependencies]
derive_more = { path = "..", features = ["add", "debug", "error", "from_str", "not", "std", "try_into", "try_unwrap"] }
derive_more = { path = "..", features = ["full"] }
itertools = "0.11.0"

[badges]
Expand All @@ -45,8 +45,8 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = []

add_assign = []
add = []
add_assign = []
as_mut = []
as_ref = []
constructor = []
Expand All @@ -61,14 +61,13 @@ index = []
index_mut = []
into = ["syn/extra-traits"]
into_iterator = []
iterator = []
mul_assign = ["syn/extra-traits"]
is_variant = ["dep:convert_case"]
mul = ["syn/extra-traits"]
mul_assign = ["syn/extra-traits"]
not = ["syn/extra-traits"]
sum = []
try_into = ["syn/extra-traits"]
is_variant = ["dep:convert_case"]
unwrap = ["dep:convert_case"]
try_unwrap = ["dep:convert_case"]
unwrap = ["dep:convert_case"]

testing-helpers = ["dep:rustc_version"]
2 changes: 0 additions & 2 deletions impl/doc/display.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ Note how we have to bound `U` and `V` by `Display` in the following example, as
Not even `Display`.

```rust
# use std::fmt::Display;
#
# use derive_more::Display;
#
# trait MyTrait { fn my_function(&self) -> i32; }
Expand Down
2 changes: 1 addition & 1 deletion impl/src/add_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {

quote! {
#[automatically_derived]
impl #impl_generics ::core::ops::#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]
Expand Down
1 change: 0 additions & 1 deletion impl/src/as_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let state = State::with_type_bound(
input,
trait_name,
quote! { ::core::convert },
"as_mut".into(),
AttrParams::ignore_and_forward(),
false,
Expand Down
1 change: 0 additions & 1 deletion impl/src/as_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let state = State::with_type_bound(
input,
trait_name,
quote! { ::core::convert },
"as_ref".into(),
AttrParams::ignore_and_forward(),
false,
Expand Down
1 change: 0 additions & 1 deletion impl/src/deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let state = State::with_field_ignore_and_forward(
input,
trait_name,
quote! { ::core::ops },
trait_name.to_lowercase(),
)?;
let SingleFieldData {
Expand Down
8 changes: 2 additions & 6 deletions impl/src/deref_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ use syn::{parse::Result, DeriveInput};

/// Provides the hook to expand `#[derive(DerefMut)]` into an implementation of `DerefMut`
pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> {
let state = State::with_field_ignore_and_forward(
input,
trait_name,
quote! { ::core::ops },
"deref_mut".into(),
)?;
let state =
State::with_field_ignore_and_forward(input, trait_name, "deref_mut".into())?;
let SingleFieldData {
input_type,
trait_path,
Expand Down
14 changes: 6 additions & 8 deletions impl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub fn expand(
let state = State::with_attr_params(
input,
trait_name,
quote! { ::derive_more::__private::Error },
trait_name.to_lowercase(),
allowed_attr_params(),
)?;
Expand All @@ -39,7 +38,7 @@ pub fn expand(

let source = source.map(|source| {
quote! {
fn source(&self) -> Option<&(dyn ::derive_more::__private::Error + 'static)> {
fn source(&self) -> Option<&(dyn ::derive_more::Error + 'static)> {
use ::derive_more::__private::AsDynError;
#source
}
Expand Down Expand Up @@ -73,7 +72,7 @@ pub fn expand(
&generics,
quote! {
where
#(#bounds: ::core::fmt::Debug + ::core::fmt::Display + ::derive_more::__private::Error + 'static),*
#(#bounds: ::core::fmt::Debug + ::core::fmt::Display + ::derive_more::Error + 'static),*
},
);
}
Expand All @@ -82,7 +81,7 @@ pub fn expand(

let render = quote! {
#[automatically_derived]
impl #impl_generics ::derive_more::__private::Error for #ident #ty_generics #where_clause {
impl #impl_generics ::derive_more::Error for #ident #ty_generics #where_clause {
#source
#provide
}
Expand Down Expand Up @@ -120,7 +119,6 @@ fn render_enum(
let state = State::from_variant(
state.input,
state.trait_name,
state.trait_module.clone(),
state.trait_attr.clone(),
allowed_attr_params(),
variant,
Expand Down Expand Up @@ -207,7 +205,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
let source_provider = self.source.map(|source| {
let source_expr = &self.data.members[source];
quote! {
::derive_more::__private::Error::provide(&#source_expr, demand);
::derive_more::Error::provide(&#source_expr, demand);
}
});
let backtrace_provider = self
Expand Down Expand Up @@ -237,7 +235,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
let pattern = self.data.matcher(&[source], &[quote! { source }]);
Some(quote! {
#pattern => {
::derive_more::__private::Error::provide(source, demand);
::derive_more::Error::provide(source, demand);
}
})
}
Expand All @@ -249,7 +247,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
Some(quote! {
#pattern => {
demand.provide_ref::<::std::backtrace::Backtrace>(backtrace);
::derive_more::__private::Error::provide(source, demand);
::derive_more::Error::provide(source, demand);
}
})
}
Expand Down
7 changes: 1 addition & 6 deletions impl/src/from_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ use syn::{parse::Result, DeriveInput};

/// Provides the hook to expand `#[derive(FromStr)]` into an implementation of `FromStr`
pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> {
let state = State::new(
input,
trait_name,
quote! { ::core::str },
trait_name.to_lowercase(),
)?;
let state = State::new(input, trait_name, trait_name.to_lowercase())?;

if state.derive_type == DeriveType::Enum {
Ok(enum_from(input, state, trait_name))
Expand Down
8 changes: 2 additions & 6 deletions impl/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ use syn::{parse::Result, DeriveInput};
/// Provides the hook to expand `#[derive(Index)]` into an implementation of `Index`
pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> {
let index_type = format_ident!("__IdxT");
let mut state = State::with_field_ignore(
input,
trait_name,
quote! { ::core::ops },
trait_name.to_lowercase(),
)?;
let mut state =
State::with_field_ignore(input, trait_name, trait_name.to_lowercase())?;
state.add_trait_path_type_param(quote! { #index_type });
let SingleFieldData {
field,
Expand Down
7 changes: 1 addition & 6 deletions impl/src/index_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ use syn::{parse::Result, DeriveInput};
/// Provides the hook to expand `#[derive(IndexMut)]` into an implementation of `IndexMut`
pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> {
let index_type = format_ident!("__IdxT");
let mut state = State::with_field_ignore(
input,
trait_name,
quote! { ::core::ops },
"index_mut".into(),
)?;
let mut state = State::with_field_ignore(input, trait_name, "index_mut".into())?;
state.add_trait_path_type_param(quote! { #index_type });
let SingleFieldData {
field,
Expand Down
8 changes: 2 additions & 6 deletions impl/src/into_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ use syn::{parse::Result, DeriveInput};

/// Provides the hook to expand `#[derive(IntoIterator)]` into an implementation of `IntoIterator`
pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> {
let state = State::with_field_ignore_and_refs(
input,
trait_name,
quote! { ::core::iter },
"into_iterator".into(),
)?;
let state =
State::with_field_ignore_and_refs(input, trait_name, "into_iterator".into())?;
let SingleFieldData {
input_type,
info,
Expand Down
1 change: 0 additions & 1 deletion impl/src/is_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let state = State::with_attr_params(
input,
trait_name,
quote! {},
"is_variant".into(),
AttrParams {
enum_: vec!["ignore"],
Expand Down
Loading

0 comments on commit e10b96b

Please sign in to comment.