Skip to content

Commit

Permalink
Bootstrap solution
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranron committed Aug 29, 2024
1 parent 06acaad commit c058a2f
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 270 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ These don't derive traits, but derive static methods instead.

### Re-exports

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:
This crate also re-exports all the standard library traits, that it adds derives
for, in the `with_trait` module. So, both the `Display` derive and the `Display`
trait will be in scope when you add the following code:
```rust
use derive_more::Display; // also imports `core::fmt::Display`
use derive_more::with_trait::Display; // also imports `core::fmt::Display`
```

For derive macros only, without the corresponding traits, do import them from
the `derive` module:
By default, derive macros only, without the corresponding traits, are import from
the crate's root:
```rust
use derive_more::derive::Display; // imports macro only
use derive_more::Display; // imports macro only
```

#### Hygiene
Expand Down
3 changes: 2 additions & 1 deletion impl/src/add_assign_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ 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::core::ops::#trait_ident
for #input_type #ty_generics #where_clause {
#[inline]
#[track_caller]
fn #method_ident(&mut self, rhs: #input_type #ty_generics) {
Expand Down
3 changes: 2 additions & 1 deletion impl/src/add_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ 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::core::ops::#trait_ident
for #input_type #ty_generics #where_clause {
type Output = #output_type;

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion impl/src/as/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<'a> ToTokens for Expansion<'a> {
};

let trait_ty = quote! {
derive_more::#trait_ident <#return_ty>
derive_more::core::convert::#trait_ident <#return_ty>
};

let generics = match &impl_kind {
Expand Down
24 changes: 18 additions & 6 deletions impl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ 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)> {
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust feature is
// stabilized.
fn source(&self) -> Option<&(dyn derive_more::with_trait::Error + 'static)> {
use derive_more::__private::AsDynError;
#source
}
Expand Down Expand Up @@ -82,7 +84,9 @@ pub fn expand(
where #(
#bounds: derive_more::core::fmt::Debug
+ derive_more::core::fmt::Display
+ derive_more::Error
// TODO: Use `derive_more::core::error::Error` once `error_in_core`
// Rust feature is stabilized.
+ derive_more::with_trait::Error
+ 'static
),*
},
Expand All @@ -93,7 +97,9 @@ pub fn expand(

let render = quote! {
#[automatically_derived]
impl #impl_generics derive_more::Error for #ident #ty_generics #where_clause {
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust feature is
// stabilized.
impl #impl_generics derive_more::with_trait::Error for #ident #ty_generics #where_clause {
#source
#provide
}
Expand Down Expand Up @@ -217,7 +223,9 @@ 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);
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust feature is
// stabilized.
derive_more::with_trait::Error::provide(&#source_expr, request);
}
});
let backtrace_provider = self
Expand Down Expand Up @@ -247,7 +255,9 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
let pattern = self.data.matcher(&[source], &[quote! { source }]);
Some(quote! {
#pattern => {
derive_more::Error::provide(source, request);
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust
// feature is stabilized.
derive_more::with_trait::Error::provide(source, request);
}
})
}
Expand All @@ -259,7 +269,9 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
Some(quote! {
#pattern => {
request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
derive_more::Error::provide(source, request);
// TODO: Use `derive_more::core::error::Error` once `error_in_core` Rust
// feature is stabilized.
derive_more::with_trait::Error::provide(source, request);
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn expand(input: &syn::DeriveInput, _: &str) -> syn::Result<TokenStream> {
Ok(quote! {
#[allow(unreachable_code)] // omit warnings for `!` and other unreachable types
#[automatically_derived]
impl #impl_gens derive_more::Debug for #ident #ty_gens #where_clause {
impl #impl_gens derive_more::core::fmt::Debug for #ident #ty_gens #where_clause {
#[inline]
fn fmt(
&self, __derive_more_f: &mut derive_more::core::fmt::Formatter<'_>
Expand Down Expand Up @@ -411,7 +411,7 @@ impl<'a> Expansion<'a> {
));
}
Some(FieldAttribute::Left(_skip)) => {}
None => out.extend([parse_quote! { #ty: derive_more::Debug }]),
None => out.extend([parse_quote! { #ty: derive_more::core::fmt::Debug }]),
}
Ok(out)
})
Expand Down
2 changes: 1 addition & 1 deletion impl/src/fmt/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn expand(input: &syn::DeriveInput, trait_name: &str) -> syn::Result<TokenSt
Ok(quote! {
#[allow(unreachable_code)] // omit warnings for `!` and other unreachable types
#[automatically_derived]
impl #impl_gens derive_more::#trait_ident for #ident #ty_gens #where_clause {
impl #impl_gens derive_more::core::fmt::#trait_ident for #ident #ty_gens #where_clause {
fn fmt(
&self, __derive_more_f: &mut derive_more::core::fmt::Formatter<'_>
) -> derive_more::core::fmt::Result {
Expand Down
15 changes: 9 additions & 6 deletions impl/src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::core::convert::From<#from_ty>>::from(
value #( .#index )*
),
}
Expand All @@ -174,7 +174,8 @@ impl<'a> Expansion<'a> {
Ok(quote! {
#[allow(unreachable_code)] // omit warnings for `!` and unreachable types
#[automatically_derived]
impl #impl_gens derive_more::From<#ty> for #ident #ty_gens #where_clause {
impl #impl_gens derive_more::core::convert::From<#ty>
for #ident #ty_gens #where_clause {
#[inline]
fn from(value: #ty) -> Self {
#ident #( :: #variant )* #init
Expand All @@ -195,7 +196,8 @@ impl<'a> Expansion<'a> {
Ok(quote! {
#[allow(unreachable_code)] // omit warnings for `!` and other unreachable types
#[automatically_derived]
impl #impl_gens derive_more::From<(#( #field_tys ),*)> for #ident #ty_gens #where_clause {
impl #impl_gens derive_more::core::convert::From<(#( #field_tys ),*)>
for #ident #ty_gens #where_clause {
#[inline]
fn from(value: (#( #field_tys ),*)) -> Self {
#ident #( :: #variant )* #init
Expand All @@ -211,7 +213,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::core::convert::From<#gen_ident>>::from(
value #( .#index )*
),
};
Expand All @@ -227,7 +229,7 @@ impl<'a> Expansion<'a> {
generics
.make_where_clause()
.predicates
.push(parse_quote! { #ty: derive_more::From<#ident> });
.push(parse_quote! { #ty: derive_more::core::convert::From<#ident> });
generics
.params
.push(syn::TypeParam::from(ident.clone()).into());
Expand All @@ -239,7 +241,8 @@ impl<'a> Expansion<'a> {
Ok(quote! {
#[allow(unreachable_code)] // omit warnings for `!` and other unreachable types
#[automatically_derived]
impl #impl_gens derive_more::From<(#( #gen_idents ),*)> for #ident #ty_gens #where_clause {
impl #impl_gens derive_more::core::convert::From<(#( #gen_idents ),*)>
for #ident #ty_gens #where_clause {
#[inline]
fn from(value: (#( #gen_idents ),*)) -> Self {
#ident #(:: #variant)* #init
Expand Down
3 changes: 2 additions & 1 deletion impl/src/not_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {
quote! {
#[allow(unreachable_code)] // omit warnings for `!` and other unreachable types
#[automatically_derived]
impl #impl_generics derive_more::#trait_ident for #input_type #ty_generics #where_clause {
impl #impl_generics derive_more::core::ops::#trait_ident
for #input_type #ty_generics #where_clause {
type Output = #output_type;

#[inline]
Expand Down
3 changes: 2 additions & 1 deletion impl/src/try_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ impl ToTokens for Expansion {

quote! {
#[automatically_derived]
impl #impl_generics derive_more::TryFrom<#repr_ty #ty_generics> for #ident #where_clause {
impl #impl_generics derive_more::core::convert::TryFrom<#repr_ty #ty_generics>
for #ident #where_clause {
type Error = derive_more::TryFromReprError<#repr_ty>;

#[allow(non_upper_case_globals)]
Expand Down
4 changes: 2 additions & 2 deletions impl/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,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::with_trait::#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) => {
Expand Down Expand Up @@ -513,7 +513,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::with_trait::#trait_ident };
let (derive_type, fields): (_, Vec<_>) = match variant.fields {
Fields::Unnamed(ref fields) => {
(DeriveType::Unnamed, unnamed_to_vec(fields))
Expand Down
Loading

0 comments on commit c058a2f

Please sign in to comment.