From 0840c075f89d311fdfe3c7682f180b14fca665f2 Mon Sep 17 00:00:00 2001 From: Daniel Lehmann Date: Sun, 25 Aug 2024 00:46:19 -0700 Subject: [PATCH] Fix compile errors --- src/lib.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 601a66d..70d6d0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,10 +40,8 @@ impl Display for TryNewError { } #[cfg_attr(feature = "const_convert_and_const_trait_impl", const_trait)] -pub trait Number: Sized { - type UnderlyingType: Copy - + Clone - + Number +pub trait Number: Sized + Copy + Clone { + type UnderlyingType: Number + Debug + From + TryFrom @@ -68,8 +66,16 @@ pub trait Number: Sized { fn value(self) -> Self::UnderlyingType; + #[cfg(feature = "const_convert_and_const_trait_impl")] + fn new_(value: T) -> Self; + #[cfg(not(feature = "const_convert_and_const_trait_impl"))] fn new_(value: T) -> Self; + /// Creates an instance from the given `value`. Unlike the various `new...` functions, this + /// will never fail as the value is masked to the result size. + #[cfg(feature = "const_convert_and_const_trait_impl")] + fn masked_new(value: T) -> Self; + #[cfg(not(feature = "const_convert_and_const_trait_impl"))] fn masked_new(value: T) -> Self; fn as_u8(&self) -> u8; @@ -82,6 +88,13 @@ pub trait Number: Sized { fn as_u128(&self) -> u128; + #[cfg(feature = "const_convert_and_const_trait_impl")] + #[inline] + fn as_(self) -> T { + T::masked_new(self) + } + + #[cfg(not(feature = "const_convert_and_const_trait_impl"))] #[inline] fn as_(self) -> T { T::masked_new(self) @@ -108,7 +121,7 @@ macro_rules! impl_number_native { fn value(self) -> Self::UnderlyingType { self } #[inline] - fn new_(value: T) -> Self { + fn new_(value: T) -> Self { match Self::BITS { 8 => value.as_u8() as Self, 16 => value.as_u16() as Self, @@ -120,7 +133,7 @@ macro_rules! impl_number_native { } #[inline] - fn masked_new(value: T) -> Self { + fn masked_new(value: T) -> Self { // Primitive types don't need masking Self::new_(value) }