diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:44 +0000 |
commit | c23a457e72abe608715ac76f076f47dc42af07a5 (patch) | |
tree | 2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /vendor/deranged/src/traits.rs | |
parent | Releasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip |
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/deranged/src/traits.rs')
-rw-r--r-- | vendor/deranged/src/traits.rs | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/vendor/deranged/src/traits.rs b/vendor/deranged/src/traits.rs new file mode 100644 index 000000000..d1b69ac01 --- /dev/null +++ b/vendor/deranged/src/traits.rs @@ -0,0 +1,117 @@ +use crate::{ + RangedI128, RangedI16, RangedI32, RangedI64, RangedI8, RangedIsize, RangedU128, RangedU16, + RangedU32, RangedU64, RangedU8, RangedUsize, +}; + +macro_rules! declare_traits { + ($($trait_name:ident),* $(,)?) => {$( + pub(crate) trait $trait_name { + const ASSERT: (); + } + )*}; +} + +macro_rules! impl_traits_for_all { + ($($ranged_ty:ident $inner_ty:ident),* $(,)?) => {$( + impl<const MIN: $inner_ty, const MAX: $inner_ty> RangeIsValid for $ranged_ty<MIN, MAX> { + const ASSERT: () = assert!(MIN <= MAX); + } + + impl< + const CURRENT_MIN: $inner_ty, + const CURRENT_MAX: $inner_ty, + const NEW_MIN: $inner_ty, + const NEW_MAX: $inner_ty, + > ExpandIsValid for ($ranged_ty<CURRENT_MIN, CURRENT_MAX>, $ranged_ty<NEW_MIN, NEW_MAX>) { + const ASSERT: () = { + assert!(NEW_MIN <= CURRENT_MIN); + assert!(NEW_MAX >= CURRENT_MAX); + }; + } + + impl< + const CURRENT_MIN: $inner_ty, + const CURRENT_MAX: $inner_ty, + const NEW_MIN: $inner_ty, + const NEW_MAX: $inner_ty, + > NarrowIsValid for ($ranged_ty<CURRENT_MIN, CURRENT_MAX>, $ranged_ty<NEW_MIN, NEW_MAX>) { + const ASSERT: () = { + assert!(NEW_MIN >= CURRENT_MIN); + assert!(NEW_MAX <= CURRENT_MAX); + }; + } + + impl< + const VALUE: $inner_ty, + const MIN: $inner_ty, + const MAX: $inner_ty, + > StaticIsValid for ($ranged_ty<MIN, VALUE>, $ranged_ty<VALUE, MAX>) { + const ASSERT: () = { + assert!(VALUE >= MIN); + assert!(VALUE <= MAX); + }; + } + )*}; +} + +macro_rules! impl_traits_for_signed { + ($($ranged_ty:ident $inner_ty:ident),* $(,)?) => {$( + impl<const MIN: $inner_ty, const MAX: $inner_ty> AbsIsSafe for $ranged_ty<MIN, MAX> { + const ASSERT: () = { + assert!(MIN != <$inner_ty>::MIN); + assert!(-MIN <= MAX); + }; + } + + impl<const MIN: $inner_ty, const MAX: $inner_ty> NegIsSafe for $ranged_ty<MIN, MAX> { + const ASSERT: () = { + assert!(MIN != <$inner_ty>::MIN); + assert!(-MIN <= MAX); + assert!(-MAX >= MIN); + }; + } + + impl_traits_for_all!($ranged_ty $inner_ty); + )*}; +} + +macro_rules! impl_traits_for_unsigned { + ($($ranged_ty:ident $inner_ty:ident),* $(,)?) => {$( + impl<const MIN: $inner_ty, const MAX: $inner_ty> AbsIsSafe for $ranged_ty<MIN, MAX> { + const ASSERT: () = (); + } + + impl<const MIN: $inner_ty, const MAX: $inner_ty> NegIsSafe for $ranged_ty<MIN, MAX> { + const ASSERT: () = assert!(MAX == 0); + } + + impl_traits_for_all!($ranged_ty $inner_ty); + )*}; +} + +declare_traits![ + RangeIsValid, + AbsIsSafe, + NegIsSafe, + ExpandIsValid, + NarrowIsValid, + StaticIsValid, +]; + +impl_traits_for_signed! { + RangedI8 i8, + RangedI16 i16, + RangedI32 i32, + RangedI64 i64, + RangedI128 i128, + RangedIsize isize, +} + +impl_traits_for_unsigned! { + RangedU8 u8, + RangedU16 u16, + RangedU32 u32, + RangedU64 u64, + RangedU128 u128, + RangedUsize usize, +} |