diff --git a/impl/doc/add.md b/impl/doc/add.md index 5d58996a..23fab973 100644 --- a/impl/doc/add.md +++ b/impl/doc/add.md @@ -113,8 +113,8 @@ Code like this will be generated: # Unit, # } impl ::core::ops::Add for MixedInts { - type Output = Result; - fn add(self, rhs: MixedInts) -> Result { + type Output = Result; + fn add(self, rhs: MixedInts) -> Result { match (self, rhs) { (MixedInts::SmallInt(__l_0), MixedInts::SmallInt(__r_0)) => { Ok(MixedInts::SmallInt(__l_0.add(__r_0))) @@ -138,11 +138,11 @@ impl ::core::ops::Add for MixedInts { (MixedInts::UnsignedTwo(__l_0), MixedInts::UnsignedTwo(__r_0)) => { Ok(MixedInts::UnsignedTwo(__l_0.add(__r_0))) } - (MixedInts::Unit, MixedInts::Unit) => Err(::derive_more::ops::BinaryError::Unit( - ::derive_more::ops::UnitError::new("add"), + (MixedInts::Unit, MixedInts::Unit) => Err(::derive_more::BinaryError::Unit( + ::derive_more::UnitError::new("add"), )), - _ => Err(::derive_more::ops::BinaryError::Mismatch( - ::derive_more::ops::WrongVariantError::new("add"), + _ => Err(::derive_more::BinaryError::Mismatch( + ::derive_more::WrongVariantError::new("add"), )), } } diff --git a/impl/doc/not.md b/impl/doc/not.md index 9997c085..affb7900 100644 --- a/impl/doc/not.md +++ b/impl/doc/not.md @@ -148,11 +148,11 @@ Code like this will be generated: # Unit, # } impl ::core::ops::Not for EnumWithUnit { - type Output = Result; - fn not(self) -> Result { + type Output = Result; + fn not(self) -> Result { match self { EnumWithUnit::SmallInt(__0) => Ok(EnumWithUnit::SmallInt(__0.not())), - EnumWithUnit::Unit => Err(::derive_more::ops::UnitError::new("not")), + EnumWithUnit::Unit => Err(::derive_more::UnitError::new("not")), } } } diff --git a/impl/src/add_like.rs b/impl/src/add_like.rs index 58076461..82beb2a8 100644 --- a/impl/src/add_like.rs +++ b/impl/src/add_like.rs @@ -32,7 +32,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { }, Data::Enum(ref data_enum) => ( quote! { - ::core::result::Result<#input_type #ty_generics, ::derive_more::ops::BinaryError> + ::core::result::Result<#input_type #ty_generics, ::derive_more::BinaryError> }, enum_content(input_type, data_enum, &method_ident), ), @@ -126,8 +126,8 @@ fn enum_content( let operation_name = method_ident.to_string(); matches.push(quote! { (#subtype, #subtype) => ::core::result::Result::Err( - ::derive_more::ops::BinaryError::Unit( - ::derive_more::ops::UnitError::new(#operation_name) + ::derive_more::BinaryError::Unit( + ::derive_more::UnitError::new(#operation_name) ) ) }); @@ -140,8 +140,8 @@ fn enum_content( // match. let operation_name = method_ident.to_string(); matches.push(quote! { - _ => ::core::result::Result::Err(::derive_more::ops::BinaryError::Mismatch( - ::derive_more::ops::WrongVariantError::new(#operation_name) + _ => ::core::result::Result::Err(::derive_more::BinaryError::Mismatch( + ::derive_more::WrongVariantError::new(#operation_name) )) }); } diff --git a/impl/src/not_like.rs b/impl/src/not_like.rs index 6bab7c8e..76fa49e1 100644 --- a/impl/src/not_like.rs +++ b/impl/src/not_like.rs @@ -148,7 +148,7 @@ fn enum_output_type_and_content( let operation_name = method_ident.to_string(); matches.push(quote! { #subtype => ::core::result::Result::Err( - ::derive_more::ops::UnitError::new(#operation_name) + ::derive_more::UnitError::new(#operation_name) ) }); } @@ -162,7 +162,7 @@ fn enum_output_type_and_content( }; let output_type = if has_unit_type { - quote! { ::core::result::Result<#input_type #ty_generics, ::derive_more::ops::UnitError> } + quote! { ::core::result::Result<#input_type #ty_generics, ::derive_more::UnitError> } } else { quote! { #input_type #ty_generics } }; diff --git a/src/add.rs b/src/add.rs new file mode 100644 index 00000000..acb9e4ca --- /dev/null +++ b/src/add.rs @@ -0,0 +1,64 @@ +//! Definitions used in derived implementations of [`core::ops::Add`]-like traits. + +use core::fmt; + +use crate::UnitError; + +/// Error returned by the derived implementations when an arithmetic or logic +/// operation is invoked on mismatched enum variants. +#[derive(Clone, Copy, Debug)] +pub struct WrongVariantError { + operation_name: &'static str, +} + +impl WrongVariantError { + #[doc(hidden)] + #[must_use] + #[inline] + pub const fn new(operation_name: &'static str) -> Self { + Self { operation_name } + } +} + +impl fmt::Display for WrongVariantError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Trying to {}() mismatched enum variants", + self.operation_name, + ) + } +} + +#[cfg(feature = "std")] +impl std::error::Error for WrongVariantError {} + +/// Possible errors returned by the derived implementations of binary +/// arithmetic or logic operations. +#[derive(Clone, Copy, Debug)] +pub enum BinaryError { + /// Operation is attempted between mismatched enum variants. + Mismatch(WrongVariantError), + + /// Operation is attempted on unit-like enum variants. + Unit(UnitError), +} + +impl fmt::Display for BinaryError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Mismatch(e) => write!(f, "{e}"), + Self::Unit(e) => write!(f, "{e}"), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for BinaryError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::Mismatch(e) => e.source(), + Self::Unit(e) => e.source(), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 91201a19..8e02a91d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,9 +57,17 @@ pub mod __private { pub use crate::vendor::thiserror::aserror::AsDynError; } -// The modules containing error types and other helpers +// The modules containing error types and other helpers. + +#[cfg(feature = "add")] +mod add; +#[cfg(feature = "add")] +pub use crate::add::{BinaryError, WrongVariantError}; + +#[cfg(any(feature = "add", feature = "not"))] +mod ops; #[cfg(any(feature = "add", feature = "not"))] -pub mod ops; +pub use crate::ops::UnitError; #[cfg(feature = "debug")] mod fmt; @@ -83,7 +91,7 @@ pub use crate::convert::TryIntoError; mod try_unwrap; #[cfg(feature = "try_unwrap")] #[doc(inline)] -pub use self::try_unwrap::TryUnwrapError; +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, @@ -99,6 +107,7 @@ 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),*}; } diff --git a/src/ops.rs b/src/ops.rs index fbac0e61..8b7b1477 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -26,67 +26,3 @@ impl fmt::Display for UnitError { #[cfg(feature = "std")] impl std::error::Error for UnitError {} - -#[cfg(feature = "add")] -/// Error returned by the derived implementations when an arithmetic or logic -/// operation is invoked on mismatched enum variants. -#[derive(Clone, Copy, Debug)] -pub struct WrongVariantError { - operation_name: &'static str, -} - -#[cfg(feature = "add")] -impl WrongVariantError { - #[doc(hidden)] - #[must_use] - #[inline] - pub const fn new(operation_name: &'static str) -> Self { - Self { operation_name } - } -} - -#[cfg(feature = "add")] -impl fmt::Display for WrongVariantError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "Trying to {}() mismatched enum variants", - self.operation_name - ) - } -} - -#[cfg(all(feature = "add", feature = "std"))] -impl std::error::Error for WrongVariantError {} - -#[cfg(feature = "add")] -/// Possible errors returned by the derived implementations of binary -/// arithmetic or logic operations. -#[derive(Clone, Copy, Debug)] -pub enum BinaryError { - /// Operation is attempted between mismatched enum variants. - Mismatch(WrongVariantError), - - /// Operation is attempted on unit-like enum variants. - Unit(UnitError), -} - -#[cfg(feature = "add")] -impl fmt::Display for BinaryError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Mismatch(e) => write!(f, "{e}"), - Self::Unit(e) => write!(f, "{e}"), - } - } -} - -#[cfg(all(feature = "add", feature = "std"))] -impl std::error::Error for BinaryError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - Self::Mismatch(e) => e.source(), - Self::Unit(e) => e.source(), - } - } -}