From c058a2f02d2f3cad7deb12d1665ff40693c5883e Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 02:46:03 +0300 Subject: [PATCH 01/11] Bootstrap solution --- README.md | 14 +- impl/src/add_assign_like.rs | 3 +- impl/src/add_like.rs | 3 +- impl/src/as/mod.rs | 2 +- impl/src/error.rs | 24 +- impl/src/fmt/debug.rs | 4 +- impl/src/fmt/display.rs | 2 +- impl/src/from.rs | 15 +- impl/src/not_like.rs | 3 +- impl/src/try_from.rs | 3 +- impl/src/utils.rs | 4 +- src/lib.rs | 478 ++++++++++++++++++------------------ tests/display.rs | 4 +- tests/error/mod.rs | 2 +- 14 files changed, 291 insertions(+), 270 deletions(-) diff --git a/README.md b/README.md index b019bbaa..6a5151b9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/impl/src/add_assign_like.rs b/impl/src/add_assign_like.rs index 7d476732..79d3763a 100644 --- a/impl/src/add_assign_like.rs +++ b/impl/src/add_assign_like.rs @@ -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) { diff --git a/impl/src/add_like.rs b/impl/src/add_like.rs index 097a89c6..0c6673b1 100644 --- a/impl/src/add_like.rs +++ b/impl/src/add_like.rs @@ -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] diff --git a/impl/src/as/mod.rs b/impl/src/as/mod.rs index 5157fe86..c9a1cc8f 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::core::convert::#trait_ident <#return_ty> }; let generics = match &impl_kind { diff --git a/impl/src/error.rs b/impl/src/error.rs index 61c2e9ad..de71c4ee 100644 --- a/impl/src/error.rs +++ b/impl/src/error.rs @@ -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 } @@ -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 ),* }, @@ -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 } @@ -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 @@ -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); } }) } @@ -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); } }) } diff --git a/impl/src/fmt/debug.rs b/impl/src/fmt/debug.rs index 199b9b0f..42e115f1 100644 --- a/impl/src/fmt/debug.rs +++ b/impl/src/fmt/debug.rs @@ -62,7 +62,7 @@ pub fn expand(input: &syn::DeriveInput, _: &str) -> syn::Result { 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<'_> @@ -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) }) diff --git a/impl/src/fmt/display.rs b/impl/src/fmt/display.rs index 41320df5..b1c656b3 100644 --- a/impl/src/fmt/display.rs +++ b/impl/src/fmt/display.rs @@ -64,7 +64,7 @@ pub fn expand(input: &syn::DeriveInput, trait_name: &str) -> syn::Result ) -> derive_more::core::fmt::Result { diff --git a/impl/src/from.rs b/impl/src/from.rs index 10539ee3..46a16bd3 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::core::convert::From<#from_ty>>::from( value #( .#index )* ), } @@ -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 @@ -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 @@ -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 )* ), }; @@ -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()); @@ -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 diff --git a/impl/src/not_like.rs b/impl/src/not_like.rs index 40fae23f..5609da91 100644 --- a/impl/src/not_like.rs +++ b/impl/src/not_like.rs @@ -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] diff --git a/impl/src/try_from.rs b/impl/src/try_from.rs index f50c321f..b53c073f 100644 --- a/impl/src/try_from.rs +++ b/impl/src/try_from.rs @@ -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)] diff --git a/impl/src/utils.rs b/impl/src/utils.rs index b9543f42..28205bd0 100644 --- a/impl/src/utils.rs +++ b/impl/src/utils.rs @@ -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) => { @@ -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)) diff --git a/src/lib.rs b/src/lib.rs index b201da74..db924eea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,17 +66,6 @@ pub mod __private { pub use crate::vendor::thiserror::aserror::AsDynError; } -/// Module containing macro definitions only, without corresponding traits. -/// -/// Use it in your import paths, if you don't want to import traits, but only macros. -pub mod derive { - // This can be unused if no feature is enabled. We already error in that case, but this warning - // distracts from that error. So we suppress the warning. - #[allow(unused_imports)] - #[doc(inline)] - pub use derive_more_impl::*; -} - // The modules containing error types and other helpers. #[cfg(feature = "add")] @@ -119,303 +108,316 @@ mod try_unwrap; #[doc(inline)] pub use crate::try_unwrap::TryUnwrapError; -// When re-exporting traits from std we need to do a pretty crazy trick, because we ONLY want -// to re-export the traits and not derives that are called the same in the std module, -// because those would conflict with our own. The way we do this is by first importing both -// the trait and possible derive into a separate module and re-export them. Then we wildcard import -// all the things from that module into the main module, but we also import our own derive by its -// exact name. Due to the way wildcard imports work in rust, that results in our own derive taking -// precedence over any derive from std. For some reason the named re-export of our own derive -// cannot be in in this (or really any) macro too. It will somehow still consider it a wildcard -// then and will result in this warning ambiguous_glob_reexports, and not actually exporting of our -// derive. -macro_rules! re_export_traits(( - $feature:literal, $new_module_name:ident, $module:path $(, $traits:ident)* $(,)?) => { - #[cfg(all(feature = $feature, any(not(docsrs), ci)))] - mod $new_module_name { +// This can be unused if no feature is enabled. We already error in that case, but this warning +// distracts from that error. So we suppress the warning. +#[allow(unused_imports)] +#[doc(inline)] +pub use derive_more_impl::*; + +/// Module containing macro definitions with their corresponding traits along. +/// +/// Use it in your import paths, if you do want to import macros along with their traits. +pub mod with_trait { + // When re-exporting traits from `std` we need to do a pretty crazy trick, because we ONLY want + // to re-export the traits and not derives that are called the same in the `std` module, because + // those would conflict with our own ones. The way we do this is by first importing both the + // trait and possible derive into a separate module and re-export them. Then, we wildcard-import + // all the things from that module into the main module, but we also import our own derive by + // its exact name. Due to the way wildcard imports work in Rust, that results in our own derive + // taking precedence over any derive from `std`. For some reason the named re-export of our own + // derive cannot be in this (or really any) macro too. It will somehow still consider it a + // wildcard then and will result in this warning `ambiguous_glob_reexports`, and not actually + // exporting of our derive. + macro_rules! re_export_traits(( + $feature:literal, $new_module_name:ident, $module:path $(, $traits:ident)* $(,)?) => { + #[cfg(all(feature = $feature, any(not(docsrs), ci)))] + mod $new_module_name { + #[doc(hidden)] + pub use $module::{$($traits),*}; + } + + #[cfg(all(feature = $feature, any(not(docsrs), ci)))] #[doc(hidden)] - pub use $module::{$($traits),*}; + pub use crate::with_trait::all_traits_and_derives::$new_module_name::*; } + ); - #[cfg(all(feature = $feature, any(not(docsrs), ci)))] - #[doc(hidden)] - pub use crate::all_traits_and_derives::$new_module_name::*; - } -); + mod all_traits_and_derives { + re_export_traits!( + "add", + add_traits, + core::ops, + Add, + BitAnd, + BitOr, + BitXor, + Sub, + ); + re_export_traits!( + "add_assign", + add_assign_traits, + core::ops, + AddAssign, + BitAndAssign, + BitOrAssign, + BitXorAssign, + SubAssign, + ); + re_export_traits!("as_ref", as_ref_traits, core::convert, AsMut, AsRef); + re_export_traits!("debug", debug_traits, core::fmt, Debug); + re_export_traits!("deref", deref_traits, core::ops, Deref); + re_export_traits!("deref_mut", deref_mut_traits, core::ops, DerefMut); + re_export_traits!( + "display", + display_traits, + core::fmt, + Binary, + Display, + LowerExp, + LowerHex, + Octal, + Pointer, + UpperExp, + UpperHex, + ); + + #[cfg(not(feature = "std"))] + re_export_traits!("error", error_traits, core::error, Error); + #[cfg(feature = "std")] + re_export_traits!("error", error_traits, std::error, Error); + + re_export_traits!("from", from_traits, core::convert, From); + + re_export_traits!("from_str", from_str_traits, core::str, FromStr); + + re_export_traits!("index", index_traits, core::ops, Index); + + re_export_traits!("index_mut", index_mut_traits, core::ops, IndexMut); + + re_export_traits!("into", into_traits, core::convert, Into); + + re_export_traits!( + "into_iterator", + into_iterator_traits, + core::iter, + IntoIterator, + ); + + re_export_traits!("mul", mul_traits, core::ops, Div, Mul, Rem, Shl, Shr); + + #[cfg(feature = "mul_assign")] + re_export_traits!( + "mul_assign", + mul_assign_traits, + core::ops, + DivAssign, + MulAssign, + RemAssign, + ShlAssign, + ShrAssign, + ); + + re_export_traits!("not", not_traits, core::ops, Neg, Not); + + re_export_traits!("sum", sum_traits, core::iter, Product, Sum); + + re_export_traits!("try_from", try_from_traits, core::convert, TryFrom); + + re_export_traits!("try_into", try_into_traits, core::convert, TryInto); + + // Now re-export our own derives by their exact name to overwrite any derives that the trait + // re-exporting might inadvertently pull into scope. + #[cfg(feature = "add")] + pub use derive_more_impl::{Add, BitAnd, BitOr, BitXor, Sub}; + + #[cfg(feature = "add_assign")] + pub use derive_more_impl::{ + AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, SubAssign, + }; + + #[cfg(feature = "as_ref")] + pub use derive_more_impl::{AsMut, AsRef}; + + #[cfg(feature = "constructor")] + pub use derive_more_impl::Constructor; + + #[cfg(feature = "debug")] + pub use derive_more_impl::Debug; + + #[cfg(feature = "deref")] + pub use derive_more_impl::Deref; + + #[cfg(feature = "deref_mut")] + pub use derive_more_impl::DerefMut; + + #[cfg(feature = "display")] + pub use derive_more_impl::{ + Binary, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex, + }; -mod all_traits_and_derives { - re_export_traits!( - "add", - add_traits, - core::ops, - Add, - BitAnd, - BitOr, - BitXor, - Sub, - ); - re_export_traits!( - "add_assign", - add_assign_traits, - core::ops, - AddAssign, - BitAndAssign, - BitOrAssign, - BitXorAssign, - SubAssign, - ); - re_export_traits!("as_ref", as_ref_traits, core::convert, AsMut, AsRef); - re_export_traits!("debug", debug_traits, core::fmt, Debug); - re_export_traits!("deref", deref_traits, core::ops, Deref); - re_export_traits!("deref_mut", deref_mut_traits, core::ops, DerefMut); - re_export_traits!( - "display", - display_traits, - core::fmt, - Binary, - Display, - LowerExp, - LowerHex, - Octal, - Pointer, - UpperExp, - UpperHex, - ); + #[cfg(feature = "error")] + pub use derive_more_impl::Error; - #[cfg(not(feature = "std"))] - re_export_traits!("error", error_traits, core::error, Error); - #[cfg(feature = "std")] - re_export_traits!("error", error_traits, std::error, Error); + #[cfg(feature = "from")] + pub use derive_more_impl::From; - re_export_traits!("from", from_traits, core::convert, From); + #[cfg(feature = "from_str")] + pub use derive_more_impl::FromStr; - re_export_traits!("from_str", from_str_traits, core::str, FromStr); + #[cfg(feature = "index")] + pub use derive_more_impl::Index; - re_export_traits!("index", index_traits, core::ops, Index); + #[cfg(feature = "index_mut")] + pub use derive_more_impl::IndexMut; - re_export_traits!("index_mut", index_mut_traits, core::ops, IndexMut); + #[cfg(feature = "into")] + pub use derive_more_impl::Into; - re_export_traits!("into", into_traits, core::convert, Into); + #[cfg(feature = "into_iterator")] + pub use derive_more_impl::IntoIterator; - re_export_traits!( - "into_iterator", - into_iterator_traits, - core::iter, - IntoIterator, - ); + #[cfg(feature = "is_variant")] + pub use derive_more_impl::IsVariant; - re_export_traits!("mul", mul_traits, core::ops, Div, Mul, Rem, Shl, Shr); + #[cfg(feature = "mul")] + pub use derive_more_impl::{Div, Mul, Rem, Shl, Shr}; - #[cfg(feature = "mul_assign")] - re_export_traits!( - "mul_assign", - mul_assign_traits, - core::ops, - DivAssign, - MulAssign, - RemAssign, - ShlAssign, - ShrAssign, - ); + #[cfg(feature = "mul_assign")] + pub use derive_more_impl::{ + DivAssign, MulAssign, RemAssign, ShlAssign, ShrAssign, + }; + + #[cfg(feature = "not")] + pub use derive_more_impl::{Neg, Not}; - re_export_traits!("not", not_traits, core::ops, Neg, Not); + #[cfg(feature = "sum")] + pub use derive_more_impl::{Product, Sum}; - re_export_traits!("sum", sum_traits, core::iter, Product, Sum); + #[cfg(feature = "try_from")] + pub use derive_more_impl::TryFrom; - re_export_traits!("try_from", try_from_traits, core::convert, TryFrom); + #[cfg(feature = "try_into")] + pub use derive_more_impl::TryInto; - re_export_traits!("try_into", try_into_traits, core::convert, TryInto); + #[cfg(feature = "try_unwrap")] + pub use derive_more_impl::TryUnwrap; + + #[cfg(feature = "unwrap")] + pub use derive_more_impl::Unwrap; + } - // Now re-export our own derives by their exact name to overwrite any derives that the trait - // re-exporting might inadvertently pull into scope. + // Now re-export our own derives and the std traits by their exact name to make rust-analyzer + // recognize the #[doc(hidden)] flag. + // See issues: + // 1. https://github.com/rust-lang/rust-analyzer/issues/11698 + // 2. https://github.com/rust-lang/rust-analyzer/issues/14079 #[cfg(feature = "add")] - pub use derive_more_impl::{Add, BitAnd, BitOr, BitXor, Sub}; + #[doc(hidden)] + pub use all_traits_and_derives::{Add, BitAnd, BitOr, BitXor, Sub}; #[cfg(feature = "add_assign")] - pub use derive_more_impl::{ + #[doc(hidden)] + pub use all_traits_and_derives::{ AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, SubAssign, }; #[cfg(feature = "as_ref")] - pub use derive_more_impl::{AsMut, AsRef}; + #[doc(hidden)] + pub use all_traits_and_derives::{AsMut, AsRef}; #[cfg(feature = "constructor")] - pub use derive_more_impl::Constructor; + #[doc(hidden)] + pub use all_traits_and_derives::Constructor; #[cfg(feature = "debug")] - pub use derive_more_impl::Debug; + #[doc(hidden)] + pub use all_traits_and_derives::Debug; #[cfg(feature = "deref")] - pub use derive_more_impl::Deref; + #[doc(hidden)] + pub use all_traits_and_derives::Deref; #[cfg(feature = "deref_mut")] - pub use derive_more_impl::DerefMut; + #[doc(hidden)] + pub use all_traits_and_derives::DerefMut; #[cfg(feature = "display")] - pub use derive_more_impl::{ + #[doc(hidden)] + pub use all_traits_and_derives::{ Binary, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex, }; #[cfg(feature = "error")] - pub use derive_more_impl::Error; + #[doc(hidden)] + pub use all_traits_and_derives::Error; #[cfg(feature = "from")] - pub use derive_more_impl::From; + #[doc(hidden)] + pub use all_traits_and_derives::From; #[cfg(feature = "from_str")] - pub use derive_more_impl::FromStr; + #[doc(hidden)] + pub use all_traits_and_derives::FromStr; #[cfg(feature = "index")] - pub use derive_more_impl::Index; + #[doc(hidden)] + pub use all_traits_and_derives::Index; #[cfg(feature = "index_mut")] - pub use derive_more_impl::IndexMut; + #[doc(hidden)] + pub use all_traits_and_derives::IndexMut; #[cfg(feature = "into")] - pub use derive_more_impl::Into; + #[doc(hidden)] + pub use all_traits_and_derives::Into; #[cfg(feature = "into_iterator")] - pub use derive_more_impl::IntoIterator; + #[doc(hidden)] + pub use all_traits_and_derives::IntoIterator; #[cfg(feature = "is_variant")] - pub use derive_more_impl::IsVariant; + #[doc(hidden)] + pub use all_traits_and_derives::IsVariant; #[cfg(feature = "mul")] - pub use derive_more_impl::{Div, Mul, Rem, Shl, Shr}; + #[doc(hidden)] + pub use all_traits_and_derives::{Div, Mul, Rem, Shl, Shr}; #[cfg(feature = "mul_assign")] - pub use derive_more_impl::{DivAssign, MulAssign, RemAssign, ShlAssign, ShrAssign}; + #[doc(hidden)] + pub use all_traits_and_derives::{ + DivAssign, MulAssign, RemAssign, ShlAssign, ShrAssign, + }; #[cfg(feature = "not")] - pub use derive_more_impl::{Neg, Not}; + #[doc(hidden)] + pub use all_traits_and_derives::{Neg, Not}; #[cfg(feature = "sum")] - pub use derive_more_impl::{Product, Sum}; + #[doc(hidden)] + pub use all_traits_and_derives::{Product, Sum}; #[cfg(feature = "try_from")] - pub use derive_more_impl::TryFrom; + #[doc(hidden)] + pub use all_traits_and_derives::TryFrom; #[cfg(feature = "try_into")] - pub use derive_more_impl::TryInto; + #[doc(hidden)] + pub use all_traits_and_derives::TryInto; #[cfg(feature = "try_unwrap")] - pub use derive_more_impl::TryUnwrap; + #[doc(hidden)] + pub use all_traits_and_derives::TryUnwrap; #[cfg(feature = "unwrap")] - pub use derive_more_impl::Unwrap; -} - -// Now re-export our own derives and the std traits by their exact name to make rust-analyzer -// recognize the #[doc(hidden)] flag. -// See issues: -// 1. https://github.com/rust-lang/rust-analyzer/issues/11698 -// 2. https://github.com/rust-lang/rust-analyzer/issues/14079 -#[cfg(feature = "add")] -#[doc(hidden)] -pub use all_traits_and_derives::{Add, BitAnd, BitOr, BitXor, Sub}; - -#[cfg(feature = "add_assign")] -#[doc(hidden)] -pub use all_traits_and_derives::{ - AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, SubAssign, -}; - -#[cfg(feature = "as_ref")] -#[doc(hidden)] -pub use all_traits_and_derives::{AsMut, AsRef}; - -#[cfg(feature = "constructor")] -#[doc(hidden)] -pub use all_traits_and_derives::Constructor; - -#[cfg(feature = "debug")] -#[doc(hidden)] -pub use all_traits_and_derives::Debug; - -#[cfg(feature = "deref")] -#[doc(hidden)] -pub use all_traits_and_derives::Deref; - -#[cfg(feature = "deref_mut")] -#[doc(hidden)] -pub use all_traits_and_derives::DerefMut; - -#[cfg(feature = "display")] -#[doc(hidden)] -pub use all_traits_and_derives::{ - Binary, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex, -}; + #[doc(hidden)] + pub use all_traits_and_derives::Unwrap; -#[cfg(feature = "error")] -#[doc(hidden)] -pub use all_traits_and_derives::Error; - -#[cfg(feature = "from")] -#[doc(hidden)] -pub use all_traits_and_derives::From; - -#[cfg(feature = "from_str")] -#[doc(hidden)] -pub use all_traits_and_derives::FromStr; - -#[cfg(feature = "index")] -#[doc(hidden)] -pub use all_traits_and_derives::Index; - -#[cfg(feature = "index_mut")] -#[doc(hidden)] -pub use all_traits_and_derives::IndexMut; - -#[cfg(feature = "into")] -#[doc(hidden)] -pub use all_traits_and_derives::Into; - -#[cfg(feature = "into_iterator")] -#[doc(hidden)] -pub use all_traits_and_derives::IntoIterator; - -#[cfg(feature = "is_variant")] -#[doc(hidden)] -pub use all_traits_and_derives::IsVariant; - -#[cfg(feature = "mul")] -#[doc(hidden)] -pub use all_traits_and_derives::{Div, Mul, Rem, Shl, Shr}; - -#[cfg(feature = "mul_assign")] -#[doc(hidden)] -pub use all_traits_and_derives::{ - DivAssign, MulAssign, RemAssign, ShlAssign, ShrAssign, -}; - -#[cfg(feature = "not")] -#[doc(hidden)] -pub use all_traits_and_derives::{Neg, Not}; - -#[cfg(feature = "sum")] -#[doc(hidden)] -pub use all_traits_and_derives::{Product, Sum}; - -#[cfg(feature = "try_from")] -#[doc(hidden)] -pub use all_traits_and_derives::TryFrom; - -#[cfg(feature = "try_into")] -#[doc(hidden)] -pub use all_traits_and_derives::TryInto; - -#[cfg(feature = "try_unwrap")] -#[doc(hidden)] -pub use all_traits_and_derives::TryUnwrap; - -#[cfg(feature = "unwrap")] -#[doc(hidden)] -pub use all_traits_and_derives::Unwrap; - -// Re-export the derive macros again to show docs for our derives (but not for traits). This is -// done using a glob import to not hit E0252. -#[allow(unused_imports)] -pub use derive_more_impl::*; + // Re-export the derive macros again to show docs for our derives (but not for traits). This is + // done using a glob import to not hit E0252. + #[allow(unused_imports)] + pub use derive_more_impl::*; +} // Check if any feature is enabled #[cfg(not(any( diff --git a/tests/display.rs b/tests/display.rs index a7257237..fe899436 100644 --- a/tests/display.rs +++ b/tests/display.rs @@ -8,7 +8,7 @@ extern crate alloc; #[cfg(not(feature = "std"))] use alloc::{format, string::ToString}; -use derive_more::{ +use derive_more::with_trait::{ Binary, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex, }; @@ -2454,7 +2454,7 @@ mod type_variables { #[allow(unused_imports)] use our_alloc::Vec; - use derive_more::Display; + use derive_more::with_trait::Display; #[derive(Display, Debug)] #[display("{inner:?}")] diff --git a/tests/error/mod.rs b/tests/error/mod.rs index 352d40aa..3d4d3d5b 100644 --- a/tests/error/mod.rs +++ b/tests/error/mod.rs @@ -1,4 +1,4 @@ -use derive_more::Error; +use derive_more::with_trait::Error; /// Derives `std::fmt::Display` for structs/enums. /// Derived implementation outputs empty string. From b1f0b567e83101d5914b51f8eea495fa8ca3bbd8 Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 02:52:36 +0300 Subject: [PATCH 02/11] Fix `no_std` tests --- tests/no_std.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/no_std.rs b/tests/no_std.rs index 566fe13a..af148240 100644 --- a/tests/no_std.rs +++ b/tests/no_std.rs @@ -1,7 +1,7 @@ #![no_std] #![allow(dead_code)] // some code is tested for type checking only -use derive_more::{ +use derive_more::with_trait::{ Add, AddAssign, Constructor, Deref, DerefMut, Display, From, FromStr, Index, IndexMut, Into, IntoIterator, Mul, MulAssign, Not, Sum, TryInto, }; From cf4d55597dbeb258b1bcb8de89c459558560f956 Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 02:54:29 +0300 Subject: [PATCH 03/11] Fix `no_std` tests, vol.2 --- tests/no_std.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/no_std.rs b/tests/no_std.rs index af148240..b50089f0 100644 --- a/tests/no_std.rs +++ b/tests/no_std.rs @@ -77,7 +77,8 @@ enum EnumWithUnit { #[rustversion::nightly] mod error { - use derive_more::{Display, Error, From}; + use derive_more::with_trait::{Display, Error, From}; + #[derive(Default, Debug, Display, Error)] struct Simple; From fda192db185027bf0fb0a6aeb385c0e8d88f7b34 Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 03:07:26 +0300 Subject: [PATCH 04/11] Fix doc tests --- impl/doc/add.md | 6 +++--- impl/doc/add_assign.md | 4 ++-- impl/doc/display.md | 4 ++-- impl/doc/sum.md | 2 +- impl/doc/try_into.md | 42 +++++++++++++++++++++--------------------- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/impl/doc/add.md b/impl/doc/add.md index ad709385..5f8e12ad 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 derive_more::Add for MyInts { +impl derive_more::core::ops::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 derive_more::Add for Point2D { +impl derive_more::core::ops::Add for Point2D { type Output = Point2D; fn add(self, rhs: Point2D) -> Point2D { Point2D { @@ -112,7 +112,7 @@ Code like this will be generated: # UnsignedTwo(u32), # Unit, # } -impl derive_more::Add for MixedInts { +impl derive_more::core::ops::Add for MixedInts { type Output = Result; fn add(self, rhs: MixedInts) -> Result { match (self, rhs) { diff --git a/impl/doc/add_assign.md b/impl/doc/add_assign.md index 4eab9aad..d52fe252 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 derive_more::AddAssign for MyInts { +impl derive_more::core::ops::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 derive_more::AddAssign for Point2D { +impl derive_more::core::ops::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/display.md b/impl/doc/display.md index 211ffced..0bdf3425 100644 --- a/impl/doc/display.md +++ b/impl/doc/display.md @@ -33,7 +33,7 @@ once to get the address of the field itself, instead of the address of the reference to the field: ```rust -# use derive_more::Display; +# use derive_more::with_trait::Display; # #[derive(Display)] #[display("{field:p} {:p}", *field)] @@ -107,7 +107,7 @@ Explicitly specified bounds are added to the inferred ones. Note how no `V: Disp because it's inferred already. ```rust -# use derive_more::Display; +# use derive_more::with_trait::Display; # # trait MyTrait { fn my_function(&self) -> i32; } # diff --git a/impl/doc/sum.md b/impl/doc/sum.md index 8219c55f..0a23314a 100644 --- a/impl/doc/sum.md +++ b/impl/doc/sum.md @@ -51,7 +51,7 @@ Code like this will be generated for the `Sum` implementation: # MyInts(self.0.add(rhs.0), self.1.add(rhs.1)) # } # } -impl derive_more::Sum for MyInts { +impl derive_more::core::ops::Sum for MyInts { #[inline] fn sum>(iter: I) -> Self { iter.fold( diff --git a/impl/doc/try_into.md b/impl/doc/try_into.md index 3953fbf7..c2ea7c38 100644 --- a/impl/doc/try_into.md +++ b/impl/doc/try_into.md @@ -95,48 +95,48 @@ Code like this will be generated: # UnsignedOne(u32), # UnsignedTwo(u32), # } -impl derive_more::TryFrom for (i32) { - type Error = &'static str; +impl TryFrom for (i32) { + type Error = derive_more::TryIntoError; fn try_from(value: MixedInts) -> Result { match value { MixedInts::SmallInt(__0) => Ok(__0), - _ => Err("Only SmallInt can be converted to i32"), + _ => Err(derive_more::TryIntoError::new(value, "SmallInt", "i32")), } } } -impl derive_more::TryFrom for (i64) { - type Error = &'static str; +impl TryFrom for (i64) { + type Error = derive_more::TryIntoError; fn try_from(value: MixedInts) -> Result { match value { MixedInts::BigInt(__0) => Ok(__0), - _ => Err("Only BigInt can be converted to i64"), + _ => Err(derive_more::TryIntoError::new(value, "BigInt", "i64")), } } } -impl derive_more::TryFrom for (i32, i32) { - type Error = &'static str; +impl TryFrom for (i32, i32) { + type Error = derive_more::TryIntoError; fn try_from(value: MixedInts) -> Result { match value { MixedInts::TwoSmallInts(__0, __1) => Ok((__0, __1)), - _ => Err("Only TwoSmallInts can be converted to (i32, i32)"), + _ => Err(derive_more::TryIntoError::new(value, "TwoSmallInts", "(i32, i32)")), } } } -impl derive_more::TryFrom for (i64, i64) { - type Error = &'static str; +impl TryFrom for (i64, i64) { + type Error = derive_more::TryIntoError; fn try_from(value: MixedInts) -> Result { match value { MixedInts::NamedSmallInts { x: __0, y: __1 } => Ok((__0, __1)), - _ => Err("Only NamedSmallInts can be converted to (i64, i64)"), + _ => Err(derive_more::TryIntoError::new(value, "NamedSmallInts", "(i64, i64)")), } } } -impl derive_more::TryFrom for (u32) { - type Error = &'static str; +impl TryFrom for (u32) { + type Error = derive_more::TryIntoError; fn try_from(value: MixedInts) -> Result { match value { MixedInts::UnsignedOne(__0) | MixedInts::UnsignedTwo(__0) => Ok(__0), - _ => Err("Only UnsignedOne, UnsignedTwo can be converted to u32"), + _ => Err(derive_more::TryIntoError::new(value, "UnsignedOne", "u32")), } } } @@ -161,21 +161,21 @@ Code like this will be generated: # SmallInt(i32), # Unit, # } -impl derive_more::TryFrom for (i32) { - type Error = &'static str; +impl TryFrom for (i32) { + type Error = derive_more::TryIntoError; fn try_from(value: EnumWithUnit) -> Result { match value { EnumWithUnit::SmallInt(__0) => Ok(__0), - _ => Err("Only SmallInt can be converted to i32"), + _ => Err(derive_more::TryIntoError::new(value, "SmallInt", "i32")), } } } -impl derive_more::TryFrom for () { - type Error = &'static str; +impl TryFrom for () { + type Error = derive_more::TryIntoError; fn try_from(value: EnumWithUnit) -> Result { match value { EnumWithUnit::Unit => Ok(()), - _ => Err("Only Unit can be converted to ()"), + _ => Err(derive_more::TryIntoError::new(value, "Unit", "()")), } } } From 0e96f69bacf33cd8eca1e6579805f35751e1c022 Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 03:14:47 +0300 Subject: [PATCH 05/11] Fix doc tests, vol.2 --- impl/doc/into_iterator.md | 26 +++++++++++++------------- impl/doc/mul.md | 16 ++++++++-------- impl/doc/mul_assign.md | 8 ++++---- impl/doc/not.md | 8 ++++---- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/impl/doc/into_iterator.md b/impl/doc/into_iterator.md index c660a908..e89dcf71 100644 --- a/impl/doc/into_iterator.md +++ b/impl/doc/into_iterator.md @@ -32,7 +32,7 @@ struct Numbers { assert_eq!(Some(5), MyVec(vec![5, 8]).into_iter().next()); -let mut nums = Numbers{numbers: vec![100, 200], useless: false}; +let mut nums = Numbers { numbers: vec![100, 200], useless: false }; assert_eq!(Some(&100), (&nums).into_iter().next()); assert_eq!(Some(&mut 100), (&mut nums).into_iter().next()); assert_eq!(Some(100), nums.into_iter().next()); @@ -63,30 +63,30 @@ Code like this will be generated: # numbers: Vec, # useless: bool, # } -impl derive_more::IntoIterator for Numbers { - type Item = as derive_more::IntoIterator>::Item; - type IntoIter = as derive_more::IntoIterator>::IntoIter; +impl IntoIterator for Numbers { + type Item = as IntoIterator>::Item; + type IntoIter = as IntoIterator>::IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter { - as derive_more::IntoIterator>::into_iter(self.numbers) + as IntoIterator>::into_iter(self.numbers) } } -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; +impl<'__deriveMoreLifetime> IntoIterator for &'__deriveMoreLifetime Numbers { + type Item = <&'__deriveMoreLifetime Vec as IntoIterator>::Item; + type IntoIter = <&'__deriveMoreLifetime Vec as IntoIterator>::IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter { - <&'__deriveMoreLifetime Vec as derive_more::IntoIterator>::into_iter(&self.numbers) + <&'__deriveMoreLifetime Vec as IntoIterator>::into_iter(&self.numbers) } } -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; +impl<'__deriveMoreLifetime> IntoIterator for &'__deriveMoreLifetime mut Numbers { + type Item = <&'__deriveMoreLifetime mut Vec as IntoIterator>::Item; + type IntoIter = <&'__deriveMoreLifetime mut Vec as IntoIterator>::IntoIter; #[inline] fn into_iter(self) -> Self::IntoIter { - <&'__deriveMoreLifetime mut Vec as derive_more::IntoIterator>::into_iter( + <&'__deriveMoreLifetime mut Vec as IntoIterator>::into_iter( &mut self.numbers, ) } diff --git a/impl/doc/mul.md b/impl/doc/mul.md index a9055158..b3a9fbea 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> derive_more::Mul<__RhsT> for MyInt - where i32: derive_more::Mul<__RhsT, Output = i32> +impl<__RhsT> derive_more::core::ops::Mul<__RhsT> for MyInt + where i32: derive_more::core::ops::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: Copy> derive_more::Mul<__RhsT> for MyInts - where i32: derive_more::Mul<__RhsT, Output = i32> +impl<__RhsT: Copy> derive_more::core::ops::Mul<__RhsT> for MyInts + where i32: derive_more::core::ops::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> derive_more::Mul<__RhsT> for Point1D - where i32: derive_more::Mul<__RhsT, Output = i32> +impl<__RhsT> derive_more::core::ops::Mul<__RhsT> for Point1D + where i32: derive_more::core::ops::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: Copy> derive_more::Mul<__RhsT> for Point2D - where i32: derive_more::Mul<__RhsT, Output = i32> +impl<__RhsT: Copy> derive_more::core::ops::Mul<__RhsT> for Point2D + where i32: derive_more::core::ops::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 aedefdfd..ea88b2e3 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: Copy> derive_more::MulAssign<__RhsT> for MyInts - where i32: derive_more::MulAssign<__RhsT> +impl<__RhsT: Copy> derive_more::core::ops::MulAssign<__RhsT> for MyInts + where i32: derive_more::core::ops::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: Copy> derive_more::MulAssign<__RhsT> for Point2D - where i32: derive_more::MulAssign<__RhsT> +impl<__RhsT: Copy> derive_more::core::ops::MulAssign<__RhsT> for Point2D + where i32: derive_more::core::ops::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 98834472..a52e7cc1 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 derive_more::Not for MyInts { +impl derive_more::core::ops::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 derive_more::Not for Point2D { +impl derive_more::core::ops::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 derive_more::Not for MixedInts { +impl derive_more::core::ops::Not for MixedInts { type Output = MixedInts; fn not(self) -> MixedInts { match self { @@ -147,7 +147,7 @@ Code like this will be generated: # SmallInt(i32), # Unit, # } -impl derive_more::Not for EnumWithUnit { +impl derive_more::core::ops::Not for EnumWithUnit { type Output = Result; fn not(self) -> Result { match self { From febdcca26b7681ad67936b3957c694fe4346f847 Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 03:23:53 +0300 Subject: [PATCH 06/11] Fix doc tests, vol.3 --- impl/doc/as_mut.md | 6 +++--- impl/doc/as_ref.md | 6 +++--- impl/doc/deref.md | 8 ++++---- impl/doc/deref_mut.md | 6 +++--- impl/doc/index.md | 8 ++++---- impl/doc/index_mut.md | 6 +++--- impl/doc/sum.md | 4 ++-- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/impl/doc/as_mut.md b/impl/doc/as_mut.md index 180674a7..bee04f19 100644 --- a/impl/doc/as_mut.md +++ b/impl/doc/as_mut.md @@ -24,7 +24,7 @@ Generates: ```rust # struct MyWrapper(String); -impl derive_more::AsMut for MyWrapper { +impl 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 derive_more::AsMut for SingleFieldForward +impl AsMut for SingleFieldForward where - Vec: derive_more::AsMut, + Vec: AsMut, { #[inline] fn as_mut(&mut self) -> &mut T { diff --git a/impl/doc/as_ref.md b/impl/doc/as_ref.md index aedded5a..d6cee1d4 100644 --- a/impl/doc/as_ref.md +++ b/impl/doc/as_ref.md @@ -24,7 +24,7 @@ Generates: ```rust # struct MyWrapper(String); -impl derive_more::AsRef for MyWrapper { +impl AsRef for MyWrapper { fn as_ref(&self) -> &String { &self.0 } @@ -50,9 +50,9 @@ This generates code equivalent to: ```rust # struct SingleFieldForward(Vec); -impl derive_more::AsRef for SingleFieldForward +impl AsRef for SingleFieldForward where - Vec: derive_more::AsRef, + Vec: AsRef, { #[inline] fn as_ref(&self) -> &T { diff --git a/impl/doc/deref.md b/impl/doc/deref.md index 2a48886d..c7dc1047 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 derive_more::Deref for CoolVec { +impl derive_more::core::ops::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 derive_more::Deref for MyBoxedInt { - type Target = as derive_more::Deref>::Target; +impl derive_more::core::ops::Deref for MyBoxedInt { + type Target = as derive_more::core::ops::Deref>::Target; #[inline] fn deref(&self) -> &Self::Target { - as derive_more::Deref>::deref(&self.0) + as derive_more::core::ops::Deref>::deref(&self.0) } } ``` diff --git a/impl/doc/deref_mut.md b/impl/doc/deref_mut.md index 32853379..319a79c8 100644 --- a/impl/doc/deref_mut.md +++ b/impl/doc/deref_mut.md @@ -85,7 +85,7 @@ Code like this will be generated: # &self.vec # } # } -impl derive_more::DerefMut for CoolVec { +impl derive_more::core::ops::DerefMut for CoolVec { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.vec @@ -116,10 +116,10 @@ When deriving a forwarded `DerefMut` for a struct: # as Deref>::deref(&self.0) # } # } -impl derive_more::DerefMut for MyBoxedInt { +impl derive_more::core::ops::DerefMut for MyBoxedInt { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - as derive_more::DerefMut>::deref_mut(&mut self.0) + as derive_more::core::ops::DerefMut>::deref_mut(&mut self.0) } } ``` diff --git a/impl/doc/index.md b/impl/doc/index.md index 47f70921..e075a115 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> derive_more::Index<__IdxT> for Numbers +impl<__IdxT> derive_more::core::ops::Index<__IdxT> for Numbers where - Vec: derive_more::Index<__IdxT>, + Vec: derive_more::core::ops::Index<__IdxT>, { - type Output = as derive_more::Index<__IdxT>>::Output; + type Output = as derive_more::core::ops::Index<__IdxT>>::Output; #[inline] fn index(&self, idx: __IdxT) -> &Self::Output { - as derive_more::Index<__IdxT>>::index(&self.numbers, idx) + as derive_more::core::ops::Index<__IdxT>>::index(&self.numbers, idx) } } ``` diff --git a/impl/doc/index_mut.md b/impl/doc/index_mut.md index c1e86974..57911583 100644 --- a/impl/doc/index_mut.md +++ b/impl/doc/index_mut.md @@ -73,13 +73,13 @@ Code like this will be generated to implement `IndexMut`: # as Index<__IdxT>>::index(&self.numbers, idx) # } # } -impl<__IdxT> derive_more::IndexMut<__IdxT> for Numbers +impl<__IdxT> derive_more::core::ops::IndexMut<__IdxT> for Numbers where - Vec: derive_more::IndexMut<__IdxT>, + Vec: derive_more::core::ops::IndexMut<__IdxT>, { #[inline] fn index_mut(&mut self, idx: __IdxT) -> &mut Self::Output { - as derive_more::IndexMut<__IdxT>>::index_mut(&mut self.numbers, idx) + as derive_more::core::ops::IndexMut<__IdxT>>::index_mut(&mut self.numbers, idx) } } ``` diff --git a/impl/doc/sum.md b/impl/doc/sum.md index 0a23314a..4de50d75 100644 --- a/impl/doc/sum.md +++ b/impl/doc/sum.md @@ -22,7 +22,7 @@ easiest to implement by adding `#[derive(MulSelf)]`. struct MyInts(i32, i64); let int_vec = vec![MyInts(2, 3), MyInts(4, 5), MyInts(6, 7)]; -assert!(MyInts(12, 15) == int_vec.into_iter().sum()) +assert!(MyInts(12, 15) == int_vec.into_iter().sum()); ``` @@ -51,7 +51,7 @@ Code like this will be generated for the `Sum` implementation: # MyInts(self.0.add(rhs.0), self.1.add(rhs.1)) # } # } -impl derive_more::core::ops::Sum for MyInts { +impl derive_more::core::iter::Sum for MyInts { #[inline] fn sum>(iter: I) -> Self { iter.fold( From 0696a13a96306e4546e33eb32b0df958640a40f9 Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 03:25:52 +0300 Subject: [PATCH 07/11] Fix doc tests, vol.4 --- impl/doc/from_str.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/impl/doc/from_str.md b/impl/doc/from_str.md index bf9d1e73..8ffd78d3 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 derive_more::FromStr for MyInt { - type Err = ::Err; +impl derive_more::core::str::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 derive_more::FromStr for Point1D { - type Err = ::Err; +impl derive_more::core::str::FromStr for Point1D { + type Err = ::Err; fn from_str(src: &str) -> Result { return Ok(Point1D { x: i32::from_str(src)?, @@ -121,8 +121,8 @@ Code like this will be generated: # Baz, # } # -impl derive_more::FromStr for EnumNoFields { - type Err = derive_more::FromStrError; +impl derive_more::core::str::FromStr for EnumNoFields { + type Err = derive_more::core::str::FromStrError; fn from_str(src: &str) -> Result { Ok(match src.to_lowercase().as_str() { "foo" => EnumNoFields::Foo, From 1274ca97724bc83f28912ea8344e2f22f777559c Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 03:30:28 +0300 Subject: [PATCH 08/11] Fix doc tests, vol.5 --- impl/doc/from_str.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/impl/doc/from_str.md b/impl/doc/from_str.md index 8ffd78d3..d43e4a5c 100644 --- a/impl/doc/from_str.md +++ b/impl/doc/from_str.md @@ -122,7 +122,7 @@ Code like this will be generated: # } # impl derive_more::core::str::FromStr for EnumNoFields { - type Err = derive_more::core::str::FromStrError; + type Err = derive_more::FromStrError; fn from_str(src: &str) -> Result { Ok(match src.to_lowercase().as_str() { "foo" => EnumNoFields::Foo, From 041d2fba097e271ba79988128e297d09468152d7 Mon Sep 17 00:00:00 2001 From: tyranron Date: Fri, 30 Aug 2024 03:36:56 +0300 Subject: [PATCH 09/11] Mention in CHANGELOG --- CHANGELOG.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5029725c..e110ad3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## 1.0.1 - Unreleased + +## 2.0.0 - Unreleased + +### Breaking changes + +- `use derive_more::SomeTrait` now imports macro only. Importing macro with + its trait along is possible now via `use derive_more::with_trait::SomeTrait`. + ([#406](https://github.com/JelteF/derive_more/pull/406)) ### Fixed From 7340ed09458de657c516215e58f5bec8171a114f Mon Sep 17 00:00:00 2001 From: Kai Ren Date: Mon, 9 Sep 2024 15:01:37 +0200 Subject: [PATCH 10/11] Update src/lib.rs Co-authored-by: Jelte Fennema-Nio --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index db924eea..0832353a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,9 +114,9 @@ pub use crate::try_unwrap::TryUnwrapError; #[doc(inline)] pub use derive_more_impl::*; -/// Module containing macro definitions with their corresponding traits along. +/// Module containing derive definitions with their corresponding traits along. /// -/// Use it in your import paths, if you do want to import macros along with their traits. +/// Use it in your import paths, if you do want to import derives along with their traits. pub mod with_trait { // When re-exporting traits from `std` we need to do a pretty crazy trick, because we ONLY want // to re-export the traits and not derives that are called the same in the `std` module, because From fd1d3f3f73f4d6027a406b1af019e84d5ecdfb98 Mon Sep 17 00:00:00 2001 From: tyranron Date: Tue, 10 Sep 2024 00:37:46 +0300 Subject: [PATCH 11/11] Return `derive` module --- README.md | 7 ++++--- impl/src/try_from.rs | 3 ++- src/lib.rs | 11 +++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6a5151b9..e317d469 100644 --- a/README.md +++ b/README.md @@ -146,10 +146,11 @@ trait will be in scope when you add the following code: use derive_more::with_trait::Display; // also imports `core::fmt::Display` ``` -By default, derive macros only, without the corresponding traits, are import from -the crate's root: +By default, derive macros only, without the corresponding traits, are imported from +the crate's root (or from the `derive` module): ```rust -use derive_more::Display; // imports macro only +use derive_more::Display; // imports macro only +use derive_more::derive::*; // imports all macros only ``` #### Hygiene diff --git a/impl/src/try_from.rs b/impl/src/try_from.rs index a338950a..38654ec0 100644 --- a/impl/src/try_from.rs +++ b/impl/src/try_from.rs @@ -123,7 +123,8 @@ impl ToTokens for Expansion { quote! { #[automatically_derived] - impl #impl_generics derive_more::core::convert::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 = #error; #[allow(non_upper_case_globals)] diff --git a/src/lib.rs b/src/lib.rs index 0832353a..e878f5df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,6 +114,17 @@ pub use crate::try_unwrap::TryUnwrapError; #[doc(inline)] pub use derive_more_impl::*; +/// Module containing derive definitions only, without their corresponding traits. +/// +/// Use it in your import paths, if you don't want to import traits, but only macros. +pub mod derive { + // This can be unused if no feature is enabled. We already error in that case, but this warning + // distracts from that error. So we suppress the warning. + #[allow(unused_imports)] + #[doc(inline)] + pub use derive_more_impl::*; +} + /// Module containing derive definitions with their corresponding traits along. /// /// Use it in your import paths, if you do want to import derives along with their traits.