1#![cfg_attr(docsrs, feature(doc_cfg))]
4#![no_std]
5#![doc(test(attr(deny(warnings))))]
6
7#[cfg(all(feature = "alloc", any(feature = "serde", feature = "quickcheck")))]
8extern crate alloc;
9
10#[cfg(test)]
11mod tests;
12mod unsafe_wrapper;
13
14use core::borrow::Borrow;
15use core::cmp::Ordering;
16use core::error::Error;
17use core::fmt;
18use core::hint::assert_unchecked;
19use core::num::{IntErrorKind, NonZero};
20use core::str::FromStr;
21
22#[cfg(all(docsrs, feature = "macros"))]
39#[macro_export]
40macro_rules! int {
41 ($min:literal, $max:literal) => {};
42}
43
44#[cfg(all(docsrs, feature = "macros"))]
61#[macro_export]
62macro_rules! opt_int {
63 ($min:literal, $max:literal) => {};
64}
65
66#[cfg(all(not(docsrs), feature = "macros"))]
67pub use deranged_macros::int;
68#[cfg(all(not(docsrs), feature = "macros"))]
69pub use deranged_macros::opt_int;
70#[cfg(feature = "powerfmt")]
71use powerfmt::smart_display;
72
73use crate::unsafe_wrapper::Unsafe;
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub struct TryFromIntError;
78
79impl fmt::Display for TryFromIntError {
80 #[inline]
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 f.write_str("out of range integral type conversion attempted")
83 }
84}
85impl Error for TryFromIntError {}
86
87#[allow(missing_copy_implementations)] #[derive(Debug, Clone, PartialEq, Eq)]
108pub struct ParseIntError {
109 #[allow(clippy::missing_docs_in_private_items)]
110 kind: IntErrorKind,
111}
112
113impl ParseIntError {
114 #[allow(clippy::missing_const_for_fn)]
117 #[inline(always)]
118 pub fn kind(&self) -> &IntErrorKind {
119 &self.kind
120 }
121}
122
123impl fmt::Display for ParseIntError {
124 #[inline]
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126 match self.kind {
127 IntErrorKind::Empty => "cannot parse integer from empty string",
128 IntErrorKind::InvalidDigit => "invalid digit found in string",
129 IntErrorKind::PosOverflow => "number too large to fit in target type",
130 IntErrorKind::NegOverflow => "number too small to fit in target type",
131 IntErrorKind::Zero => "number would be zero for non-zero type",
132 _ => "Unknown Int error kind",
133 }
134 .fmt(f)
135 }
136}
137
138impl Error for ParseIntError {}
139
140macro_rules! const_try_opt {
142 ($e:expr) => {
143 match $e {
144 Some(value) => value,
145 None => return None,
146 }
147 };
148}
149
150macro_rules! if_signed {
152 (true $($x:tt)*) => { $($x)*};
153 (false $($x:tt)*) => {};
154}
155
156macro_rules! if_unsigned {
158 (true $($x:tt)*) => {};
159 (false $($x:tt)*) => { $($x)* };
160}
161
162macro_rules! article {
164 (true) => {
165 "An"
166 };
167 (false) => {
168 "A"
169 };
170}
171
172macro_rules! unsafe_unwrap_unchecked {
174 ($e:expr) => {{
175 let opt = $e;
176 debug_assert!(opt.is_some());
177 match $e {
178 Some(value) => value,
179 None => core::hint::unreachable_unchecked(),
180 }
181 }};
182}
183
184#[allow(unused_macro_rules)]
186macro_rules! if_not_manual_rand_09 {
187 ([rand_09 $($rest:ident)*] $($output:tt)*) => {};
188 ([] $($output:tt)*) => {
189 $($output)*
190 };
191 ([$first:ident $($rest:ident)*] $($output:tt)*) => {
192 if_not_manual_rand_09!([$($rest)*] $($output)*);
193 };
194}
195
196macro_rules! impl_ranged {
198 ($(
199 $type:ident {
200 mod_name: $mod_name:ident
201 internal: $internal:ident
202 signed: $is_signed:ident
203 unsigned: $unsigned_type:ident
204 optional: $optional_type:ident
205 from: [$($from:ident($from_internal:ident))+]
206 $(manual: [$($skips:ident)+])?
207 }
208 )*) => {$(
209 #[doc = concat!(
210 article!($is_signed),
211 " `",
212 stringify!($internal),
213 "` that is known to be in the range `MIN..=MAX`.",
214 )]
215 #[repr(transparent)]
216 #[derive(Clone, Copy, Eq, Ord, Hash)]
217 pub struct $type<const MIN: $internal, const MAX: $internal>(
218 Unsafe<$internal>,
219 );
220
221 #[doc = concat!(
222 "An optional `",
223 stringify!($type),
224 "`; similar to `Option<",
225 stringify!($type),
226 ">` with better optimization.",
227 )]
228 #[doc = concat!(
230 "If `MIN` is [`",
231 stringify!($internal),
232 "::MIN`] _and_ `MAX` is [`",
233 stringify!($internal)
234 ,"::MAX`] then compilation will fail. This is because there is no way to represent \
235 the niche value.",
236 )]
237 #[doc = concat!("obtained with [`", stringify!($optional_type), "::get`].")]
243 #[repr(transparent)]
244 #[derive(Clone, Copy, Eq, Hash)]
245 pub struct $optional_type<const MIN: $internal, const MAX: $internal>(
246 $internal,
247 );
248
249 impl $type<0, 0> {
250 #[doc = concat!("A ", stringify!($type), " that is always `VALUE`.")]
251 #[inline(always)]
252 pub const fn exact<const VALUE: $internal>() -> $type<VALUE, VALUE> {
253 unsafe { $type::new_unchecked(VALUE) }
255 }
256 }
257
258 if_unsigned! { $is_signed
259 impl $type<1, { $internal::MAX }> {
260 #[inline(always)]
262 pub const fn from_nonzero(value: NonZero<$internal>) -> Self {
263 unsafe { Self::new_unchecked(value.get()) }
265 }
266
267 #[inline(always)]
269 pub const fn to_nonzero(self) -> NonZero<$internal> {
270 unsafe { NonZero::new_unchecked(self.get()) }
272 }
273 }}
274
275 impl<const MIN: $internal, const MAX: $internal> $type<MIN, MAX> {
276 pub const MIN: Self = Self::new_static::<MIN>();
279
280 pub const MAX: Self = Self::new_static::<MAX>();
283
284 #[track_caller]
290 #[inline(always)]
291 pub const unsafe fn new_unchecked(value: $internal) -> Self {
292 const { assert!(MIN <= MAX); }
293 unsafe {
295 assert_unchecked(MIN <= value && value <= MAX);
296 Self(Unsafe::new(value))
297 }
298 }
299
300 #[doc = concat!("use [`", stringify!($type), "::get_without_hint`].")]
306 #[track_caller]
307 #[inline(always)]
308 pub const fn get(self) -> $internal {
309 const { assert!(MIN <= MAX); }
310 unsafe { assert_unchecked(MIN <= *self.0.get() && *self.0.get() <= MAX) };
312 *self.0.get()
313 }
314
315 #[doc = concat!("The returned value is identical to [`", stringify!($type), "::get`].")]
318 #[inline(always)]
322 pub const fn get_without_hint(self) -> $internal {
323 const { assert!(MIN <= MAX); }
324 *self.0.get()
325 }
326
327 #[track_caller]
328 #[inline(always)]
329 pub(crate) const fn get_ref(&self) -> &$internal {
330 const { assert!(MIN <= MAX); }
331 let value = self.0.get();
332 unsafe { assert_unchecked(MIN <= *value && *value <= MAX) };
334 value
335 }
336
337 #[inline(always)]
339 pub const fn new(value: $internal) -> Option<Self> {
340 const { assert!(MIN <= MAX); }
341 if value < MIN || value > MAX {
342 None
343 } else {
344 Some(unsafe { Self::new_unchecked(value) })
346 }
347 }
348
349 #[inline(always)]
352 pub const fn new_static<const VALUE: $internal>() -> Self {
353 const {
354 assert!(MIN <= VALUE);
355 assert!(VALUE <= MAX);
356 }
357 unsafe { Self::new_unchecked(VALUE) }
359 }
360
361 #[inline]
363 pub const fn new_saturating(value: $internal) -> Self {
364 const { assert!(MIN <= MAX); }
365 if value < MIN {
366 Self::MIN
367 } else if value > MAX {
368 Self::MAX
369 } else {
370 unsafe { Self::new_unchecked(value) }
372 }
373 }
374
375 #[inline(always)]
386 pub const fn emit_range_hint(self) {
387 const { assert!(MIN <= MAX); }
388 let value = self.0.get();
389 unsafe { assert_unchecked(MIN <= *value && *value <= MAX) };
391 }
392
393 #[inline(always)]
396 pub const fn expand<const NEW_MIN: $internal, const NEW_MAX: $internal>(
397 self,
398 ) -> $type<NEW_MIN, NEW_MAX> {
399 const {
400 assert!(MIN <= MAX);
401 assert!(NEW_MIN <= NEW_MAX);
402 assert!(NEW_MIN <= MIN);
403 assert!(NEW_MAX >= MAX);
404 }
405 unsafe { $type::new_unchecked(self.get()) }
407 }
408
409 #[inline(always)]
413 pub const fn narrow<
414 const NEW_MIN: $internal,
415 const NEW_MAX: $internal,
416 >(self) -> Option<$type<NEW_MIN, NEW_MAX>> {
417 const {
418 assert!(MIN <= MAX);
419 assert!(NEW_MIN <= NEW_MAX);
420 assert!(NEW_MIN >= MIN);
421 assert!(NEW_MAX <= MAX);
422 }
423 $type::<NEW_MIN, NEW_MAX>::new(self.get())
424 }
425
426 #[doc = concat!("# use deranged::", stringify!($type), ";")]
446 #[doc = concat!(
447 "assert_eq!(",
448 stringify!($type),
449 "::<5, 10>::from_str_radix(\"A\", 16), Ok(",
450 stringify!($type),
451 "::new_static::<10>()));",
452 )]
453 #[inline]
455 pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
456 const { assert!(MIN <= MAX); }
457 match $internal::from_str_radix(src, radix) {
458 Ok(value) if value > MAX => {
459 Err(ParseIntError { kind: IntErrorKind::PosOverflow })
460 }
461 Ok(value) if value < MIN => {
462 Err(ParseIntError { kind: IntErrorKind::NegOverflow })
463 }
464 Ok(value) => Ok(unsafe { Self::new_unchecked(value) }),
467 Err(e) => Err(ParseIntError { kind: e.kind().clone() }),
468 }
469 }
470
471 #[must_use = "this returns the result of the operation, without modifying the original"]
474 #[inline]
475 pub const fn checked_add(self, rhs: $internal) -> Option<Self> {
476 const { assert!(MIN <= MAX); }
477 Self::new(const_try_opt!(self.get().checked_add(rhs)))
478 }
479
480 #[must_use = "this returns the result of the operation, without modifying the original"]
487 #[track_caller]
488 #[inline(always)]
489 pub const unsafe fn unchecked_add(self, rhs: $internal) -> Self {
490 const { assert!(MIN <= MAX); }
491 unsafe {
493 Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_add(rhs)))
494 }
495 }
496
497 #[must_use = "this returns the result of the operation, without modifying the original"]
500 #[inline]
501 pub const fn checked_sub(self, rhs: $internal) -> Option<Self> {
502 const { assert!(MIN <= MAX); }
503 Self::new(const_try_opt!(self.get().checked_sub(rhs)))
504 }
505
506 #[must_use = "this returns the result of the operation, without modifying the original"]
513 #[track_caller]
514 #[inline(always)]
515 pub const unsafe fn unchecked_sub(self, rhs: $internal) -> Self {
516 const { assert!(MIN <= MAX); }
517 unsafe {
519 Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_sub(rhs)))
520 }
521 }
522
523 #[must_use = "this returns the result of the operation, without modifying the original"]
526 #[inline]
527 pub const fn checked_mul(self, rhs: $internal) -> Option<Self> {
528 const { assert!(MIN <= MAX); }
529 Self::new(const_try_opt!(self.get().checked_mul(rhs)))
530 }
531
532 #[must_use = "this returns the result of the operation, without modifying the original"]
539 #[track_caller]
540 #[inline(always)]
541 pub const unsafe fn unchecked_mul(self, rhs: $internal) -> Self {
542 const { assert!(MIN <= MAX); }
543 unsafe {
545 Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_mul(rhs)))
546 }
547 }
548
549 #[must_use = "this returns the result of the operation, without modifying the original"]
552 #[inline]
553 pub const fn checked_div(self, rhs: $internal) -> Option<Self> {
554 const { assert!(MIN <= MAX); }
555 Self::new(const_try_opt!(self.get().checked_div(rhs)))
556 }
557
558 #[must_use = "this returns the result of the operation, without modifying the original"]
566 #[track_caller]
567 #[inline(always)]
568 pub const unsafe fn unchecked_div(self, rhs: $internal) -> Self {
569 const { assert!(MIN <= MAX); }
570 unsafe {
573 Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_div(rhs)))
574 }
575 }
576
577 #[must_use = "this returns the result of the operation, without modifying the original"]
580 #[inline]
581 pub const fn checked_div_euclid(self, rhs: $internal) -> Option<Self> {
582 const { assert!(MIN <= MAX); }
583 Self::new(const_try_opt!(self.get().checked_div_euclid(rhs)))
584 }
585
586 #[must_use = "this returns the result of the operation, without modifying the original"]
594 #[track_caller]
595 #[inline(always)]
596 pub const unsafe fn unchecked_div_euclid(self, rhs: $internal) -> Self {
597 const { assert!(MIN <= MAX); }
598 unsafe {
601 Self::new_unchecked(
602 unsafe_unwrap_unchecked!(self.get().checked_div_euclid(rhs))
603 )
604 }
605 }
606
607 if_unsigned!($is_signed
608 #[must_use = "this returns the result of the operation, without modifying the original"]
611 #[track_caller]
612 #[inline]
613 pub const fn rem<const RHS_VALUE: $internal>(
614 self,
615 rhs: $type<RHS_VALUE, RHS_VALUE>,
616 ) -> $type<0, RHS_VALUE> {
617 const { assert!(MIN <= MAX); }
618 unsafe { $type::new_unchecked(self.get() % rhs.get()) }
621 });
622
623 #[must_use = "this returns the result of the operation, without modifying the original"]
626 #[inline]
627 pub const fn checked_rem(self, rhs: $internal) -> Option<Self> {
628 const { assert!(MIN <= MAX); }
629 Self::new(const_try_opt!(self.get().checked_rem(rhs)))
630 }
631
632 #[must_use = "this returns the result of the operation, without modifying the original"]
640 #[track_caller]
641 #[inline(always)]
642 pub const unsafe fn unchecked_rem(self, rhs: $internal) -> Self {
643 const { assert!(MIN <= MAX); }
644 unsafe {
647 Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_rem(rhs)))
648 }
649 }
650
651 #[must_use = "this returns the result of the operation, without modifying the original"]
654 #[inline]
655 pub const fn checked_rem_euclid(self, rhs: $internal) -> Option<Self> {
656 const { assert!(MIN <= MAX); }
657 Self::new(const_try_opt!(self.get().checked_rem_euclid(rhs)))
658 }
659
660 #[must_use = "this returns the result of the operation, without modifying the original"]
668 #[track_caller]
669 #[inline(always)]
670 pub const unsafe fn unchecked_rem_euclid(self, rhs: $internal) -> Self {
671 const { assert!(MIN <= MAX); }
672 unsafe {
675 Self::new_unchecked(
676 unsafe_unwrap_unchecked!(self.get().checked_rem_euclid(rhs))
677 )
678 }
679 }
680
681 #[must_use = "this returns the result of the operation, without modifying the original"]
684 #[inline]
685 pub const fn checked_neg(self) -> Option<Self> {
686 const { assert!(MIN <= MAX); }
687 Self::new(const_try_opt!(self.get().checked_neg()))
688 }
689
690 #[must_use = "this returns the result of the operation, without modifying the original"]
696 #[track_caller]
697 #[inline(always)]
698 pub const unsafe fn unchecked_neg(self) -> Self {
699 const { assert!(MIN <= MAX); }
700 unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_neg())) }
702 }
703
704 #[must_use = "this returns the result of the operation, without modifying the original"]
707 #[inline(always)]
708 pub const fn neg(self) -> Self {
709 const {
710 assert!(MIN <= MAX);
711 if_signed! { $is_signed
712 assert!(MIN != $internal::MIN);
713 assert!(-MIN <= MAX);
714 assert!(-MAX >= MIN);
715 }
716 if_unsigned! { $is_signed
717 assert!(MAX == 0);
718 }
719 }
720 unsafe { self.unchecked_neg() }
722 }
723
724 #[must_use = "this returns the result of the operation, without modifying the original"]
727 #[inline]
728 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
729 const { assert!(MIN <= MAX); }
730 Self::new(const_try_opt!(self.get().checked_shl(rhs)))
731 }
732
733 #[must_use = "this returns the result of the operation, without modifying the original"]
739 #[track_caller]
740 #[inline(always)]
741 pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
742 const { assert!(MIN <= MAX); }
743 unsafe {
745 Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_shl(rhs)))
746 }
747 }
748
749 #[must_use = "this returns the result of the operation, without modifying the original"]
752 #[inline]
753 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
754 const { assert!(MIN <= MAX); }
755 Self::new(const_try_opt!(self.get().checked_shr(rhs)))
756 }
757
758 #[must_use = "this returns the result of the operation, without modifying the original"]
764 #[track_caller]
765 #[inline(always)]
766 pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
767 const { assert!(MIN <= MAX); }
768 unsafe {
770 Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_shr(rhs)))
771 }
772 }
773
774 if_signed!($is_signed
775 #[must_use = "this returns the result of the operation, without modifying the original"]
778 #[inline]
779 pub const fn checked_abs(self) -> Option<Self> {
780 const { assert!(MIN <= MAX); }
781 Self::new(const_try_opt!(self.get().checked_abs()))
782 }
783
784 #[must_use = "this returns the result of the operation, without modifying the original"]
791 #[track_caller]
792 #[inline(always)]
793 pub const unsafe fn unchecked_abs(self) -> Self {
794 const { assert!(MIN <= MAX); }
795 unsafe { Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_abs())) }
797 }
798
799 #[must_use = "this returns the result of the operation, without modifying the original"]
802 #[inline(always)]
803 pub const fn abs(self) -> Self {
804 const {
805 assert!(MIN <= MAX);
806 assert!(MIN != $internal::MIN);
807 assert!(-MIN <= MAX);
808 }
809 unsafe { self.unchecked_abs() }
812 });
813
814 #[must_use = "this returns the result of the operation, without modifying the original"]
817 #[inline]
818 pub const fn checked_pow(self, exp: u32) -> Option<Self> {
819 const { assert!(MIN <= MAX); }
820 Self::new(const_try_opt!(self.get().checked_pow(exp)))
821 }
822
823 #[must_use = "this returns the result of the operation, without modifying the original"]
830 #[track_caller]
831 #[inline(always)]
832 pub const unsafe fn unchecked_pow(self, exp: u32) -> Self {
833 const { assert!(MIN <= MAX); }
834 unsafe {
836 Self::new_unchecked(unsafe_unwrap_unchecked!(self.get().checked_pow(exp)))
837 }
838 }
839
840 #[must_use = "this returns the result of the operation, without modifying the original"]
843 #[inline]
844 pub const fn saturating_add(self, rhs: $internal) -> Self {
845 const { assert!(MIN <= MAX); }
846 Self::new_saturating(self.get().saturating_add(rhs))
847 }
848
849 #[must_use = "this returns the result of the operation, without modifying the original"]
852 #[inline]
853 pub const fn saturating_sub(self, rhs: $internal) -> Self {
854 const { assert!(MIN <= MAX); }
855 Self::new_saturating(self.get().saturating_sub(rhs))
856 }
857
858 if_signed!($is_signed
859 #[must_use = "this returns the result of the operation, without modifying the original"]
862 #[inline]
863 pub const fn saturating_neg(self) -> Self {
864 const { assert!(MIN <= MAX); }
865 Self::new_saturating(self.get().saturating_neg())
866 });
867
868 if_signed!($is_signed
869 #[must_use = "this returns the result of the operation, without modifying the original"]
871 #[inline]
872 pub const fn saturating_abs(self) -> Self {
873 const { assert!(MIN <= MAX); }
874 Self::new_saturating(self.get().saturating_abs())
875 });
876
877 #[must_use = "this returns the result of the operation, without modifying the original"]
880 #[inline]
881 pub const fn saturating_mul(self, rhs: $internal) -> Self {
882 const { assert!(MIN <= MAX); }
883 Self::new_saturating(self.get().saturating_mul(rhs))
884 }
885
886 #[must_use = "this returns the result of the operation, without modifying the original"]
889 #[inline]
890 pub const fn saturating_pow(self, exp: u32) -> Self {
891 const { assert!(MIN <= MAX); }
892 Self::new_saturating(self.get().saturating_pow(exp))
893 }
894
895 if_signed! { $is_signed
896 #[inline]
899 pub const fn is_positive(self) -> bool {
900 const { assert!(MIN <= MAX); }
901 self.get().is_positive()
902 }
903
904 #[inline]
907 pub const fn is_negative(self) -> bool {
908 const { assert!(MIN <= MAX); }
909 self.get().is_negative()
910 }
911 }
912
913 #[must_use = "this returns the result of the operation, without modifying the original"]
917 #[track_caller]
918 #[inline]
919 #[allow(trivial_numeric_casts)] const fn rem_euclid_unsigned(
921 rhs: $internal,
922 range_len: $unsigned_type
923 ) -> $unsigned_type {
924 #[allow(unused_comparisons)]
925 if rhs >= 0 {
926 (rhs as $unsigned_type) % range_len
927 } else {
928 let rhs_abs = ($internal::wrapping_sub(0, rhs)) as $unsigned_type;
932 ((($unsigned_type::MAX / range_len) * range_len) - (rhs_abs)) % range_len
939 }
940 }
941
942 #[must_use = "this returns the result of the operation, without modifying the original"]
945 #[inline]
946 #[allow(trivial_numeric_casts)] pub const fn wrapping_add(self, rhs: $internal) -> Self {
948 const { assert!(MIN <= MAX); }
949 if MIN == $internal::MIN && MAX == $internal::MAX {
951 return unsafe { Self::new_unchecked(self.get().wrapping_add(rhs)) }
953 }
954
955 let inner = self.get();
956
957 let range_len = MAX.abs_diff(MIN) + 1;
959
960 let offset = Self::rem_euclid_unsigned(rhs, range_len);
962
963 let greater_vals = MAX.abs_diff(inner);
964 if offset <= greater_vals {
966 unsafe { Self::new_unchecked(
974 ((inner as $unsigned_type).wrapping_add(offset)) as $internal
975 ) }
976 }
977 else {
979 unsafe { Self::new_unchecked(
985 ((MIN as $unsigned_type).wrapping_add(
986 offset - (greater_vals + 1)
987 )) as $internal
988 ) }
989 }
990 }
991
992 #[must_use = "this returns the result of the operation, without modifying the original"]
995 #[inline]
996 #[allow(trivial_numeric_casts)] pub const fn wrapping_sub(self, rhs: $internal) -> Self {
998 const { assert!(MIN <= MAX); }
999 if MIN == $internal::MIN && MAX == $internal::MAX {
1001 return unsafe { Self::new_unchecked(self.get().wrapping_sub(rhs)) }
1003 }
1004
1005 let inner = self.get();
1006
1007 let range_len = MAX.abs_diff(MIN) + 1;
1009
1010 let offset = Self::rem_euclid_unsigned(rhs, range_len);
1012
1013 let lesser_vals = MIN.abs_diff(inner);
1014 if offset <= lesser_vals {
1016 unsafe { Self::new_unchecked(
1024 ((inner as $unsigned_type).wrapping_sub(offset)) as $internal
1025 ) }
1026 }
1027 else {
1029 unsafe { Self::new_unchecked(
1035 ((MAX as $unsigned_type).wrapping_sub(
1036 offset - (lesser_vals + 1)
1037 )) as $internal
1038 ) }
1039 }
1040 }
1041 }
1042
1043 impl<const MIN: $internal, const MAX: $internal> $optional_type<MIN, MAX> {
1044 const NICHE: $internal = match (MIN, MAX) {
1046 ($internal::MIN, $internal::MAX) => panic!("type has no niche"),
1047 ($internal::MIN, _) => $internal::MAX,
1048 (_, _) => $internal::MIN,
1049 };
1050
1051 #[allow(non_upper_case_globals)]
1053 pub const None: Self = Self(Self::NICHE);
1054
1055 #[allow(non_snake_case)]
1057 #[inline(always)]
1058 pub const fn Some(value: $type<MIN, MAX>) -> Self {
1059 const { assert!(MIN <= MAX); }
1060 Self(value.get())
1061 }
1062
1063 #[inline(always)]
1065 pub const fn get(self) -> Option<$type<MIN, MAX>> {
1066 const { assert!(MIN <= MAX); }
1067 if self.0 == Self::NICHE {
1068 None
1069 } else {
1070 Some(unsafe { $type::new_unchecked(self.0) })
1072 }
1073 }
1074
1075 #[inline(always)]
1083 #[track_caller]
1084 pub const unsafe fn some_unchecked(value: $internal) -> Self {
1085 const { assert!(MIN <= MAX); }
1086 unsafe { assert_unchecked(MIN <= value && value <= MAX) };
1088 Self(value)
1089 }
1090
1091 #[inline(always)]
1093 pub(crate) const fn inner(self) -> $internal {
1094 const { assert!(MIN <= MAX); }
1095 self.0
1096 }
1097
1098 #[doc = concat!(
1104 "use [`", stringify!($optional_type), "::get_primitive_without_hint`]."
1105 )]
1106 #[inline(always)]
1107 pub const fn get_primitive(self) -> Option<$internal> {
1108 const { assert!(MIN <= MAX); }
1109 Some(const_try_opt!(self.get()).get())
1110 }
1111
1112 #[doc = concat!(
1115 "The returned value is identical to [`", stringify!($optional_type), "::",
1116 "get_primitive`]."
1117 )]
1118 #[inline(always)]
1122 pub const fn get_primitive_without_hint(self) -> Option<$internal> {
1123 const { assert!(MIN <= MAX); }
1124 Some(const_try_opt!(self.get()).get_without_hint())
1125 }
1126
1127 #[inline(always)]
1129 pub const fn is_none(&self) -> bool {
1130 const { assert!(MIN <= MAX); }
1131 self.get().is_none()
1132 }
1133
1134 #[inline(always)]
1136 pub const fn is_some(&self) -> bool {
1137 const { assert!(MIN <= MAX); }
1138 self.get().is_some()
1139 }
1140 }
1141
1142 impl<const MIN: $internal, const MAX: $internal> fmt::Debug for $type<MIN, MAX> {
1143 #[inline(always)]
1144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1145 const { assert!(MIN <= MAX); }
1146 self.get().fmt(f)
1147 }
1148 }
1149
1150 impl<const MIN: $internal, const MAX: $internal> fmt::Debug for $optional_type<MIN, MAX> {
1151 #[inline(always)]
1152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1153 const { assert!(MIN <= MAX); }
1154 self.get().fmt(f)
1155 }
1156 }
1157
1158 impl<const MIN: $internal, const MAX: $internal> fmt::Display for $type<MIN, MAX> {
1159 #[inline(always)]
1160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1161 const { assert!(MIN <= MAX); }
1162 self.get().fmt(f)
1163 }
1164 }
1165
1166 #[cfg(feature = "powerfmt")]
1167 impl<
1168 const MIN: $internal,
1169 const MAX: $internal,
1170 > smart_display::SmartDisplay for $type<MIN, MAX> {
1171 type Metadata = <$internal as smart_display::SmartDisplay>::Metadata;
1172
1173 #[inline(always)]
1174 fn metadata(
1175 &self,
1176 f: smart_display::FormatterOptions,
1177 ) -> smart_display::Metadata<'_, Self> {
1178 const { assert!(MIN <= MAX); }
1179 self.get_ref().metadata(f).reuse()
1180 }
1181
1182 #[inline(always)]
1183 fn fmt_with_metadata(
1184 &self,
1185 f: &mut fmt::Formatter<'_>,
1186 metadata: smart_display::Metadata<'_, Self>,
1187 ) -> fmt::Result {
1188 const { assert!(MIN <= MAX); }
1189 self.get().fmt_with_metadata(f, metadata.reuse())
1190 }
1191 }
1192
1193 impl<const MIN: $internal, const MAX: $internal> Default for $optional_type<MIN, MAX> {
1194 #[inline(always)]
1195 fn default() -> Self {
1196 const { assert!(MIN <= MAX); }
1197 Self::None
1198 }
1199 }
1200
1201 impl<const MIN: $internal, const MAX: $internal> AsRef<$internal> for $type<MIN, MAX> {
1202 #[inline(always)]
1203 fn as_ref(&self) -> &$internal {
1204 const { assert!(MIN <= MAX); }
1205 &self.get_ref()
1206 }
1207 }
1208
1209 impl<const MIN: $internal, const MAX: $internal> Borrow<$internal> for $type<MIN, MAX> {
1210 #[inline(always)]
1211 fn borrow(&self) -> &$internal {
1212 const { assert!(MIN <= MAX); }
1213 &self.get_ref()
1214 }
1215 }
1216
1217 impl<
1218 const MIN_A: $internal,
1219 const MAX_A: $internal,
1220 const MIN_B: $internal,
1221 const MAX_B: $internal,
1222 > PartialEq<$type<MIN_B, MAX_B>> for $type<MIN_A, MAX_A> {
1223 #[inline(always)]
1224 fn eq(&self, other: &$type<MIN_B, MAX_B>) -> bool {
1225 const {
1226 assert!(MIN_A <= MAX_A);
1227 assert!(MIN_B <= MAX_B);
1228 }
1229 self.get() == other.get()
1230 }
1231 }
1232
1233 impl<
1234 const MIN_A: $internal,
1235 const MAX_A: $internal,
1236 const MIN_B: $internal,
1237 const MAX_B: $internal,
1238 > PartialEq<$optional_type<MIN_B, MAX_B>> for $optional_type<MIN_A, MAX_A> {
1239 #[inline(always)]
1240 fn eq(&self, other: &$optional_type<MIN_B, MAX_B>) -> bool {
1241 const {
1242 assert!(MIN_A <= MAX_A);
1243 assert!(MIN_B <= MAX_B);
1244 }
1245 self.inner() == other.inner()
1246 }
1247 }
1248
1249 impl<
1250 const MIN_A: $internal,
1251 const MAX_A: $internal,
1252 const MIN_B: $internal,
1253 const MAX_B: $internal,
1254 > PartialOrd<$type<MIN_B, MAX_B>> for $type<MIN_A, MAX_A> {
1255 #[inline(always)]
1256 fn partial_cmp(&self, other: &$type<MIN_B, MAX_B>) -> Option<Ordering> {
1257 const {
1258 assert!(MIN_A <= MAX_A);
1259 assert!(MIN_B <= MAX_B);
1260 }
1261 self.get().partial_cmp(&other.get())
1262 }
1263 }
1264
1265 impl<
1266 const MIN_A: $internal,
1267 const MAX_A: $internal,
1268 const MIN_B: $internal,
1269 const MAX_B: $internal,
1270 > PartialOrd<$optional_type<MIN_B, MAX_B>> for $optional_type<MIN_A, MAX_A> {
1271 #[inline]
1272 fn partial_cmp(&self, other: &$optional_type<MIN_B, MAX_B>) -> Option<Ordering> {
1273 const {
1274 assert!(MIN_A <= MAX_A);
1275 assert!(MIN_B <= MAX_B);
1276 }
1277 if self.is_none() && other.is_none() {
1278 Some(Ordering::Equal)
1279 } else if self.is_none() {
1280 Some(Ordering::Less)
1281 } else if other.is_none() {
1282 Some(Ordering::Greater)
1283 } else {
1284 self.inner().partial_cmp(&other.inner())
1285 }
1286 }
1287 }
1288
1289 impl<
1290 const MIN: $internal,
1291 const MAX: $internal,
1292 > Ord for $optional_type<MIN, MAX> {
1293 #[inline]
1294 fn cmp(&self, other: &Self) -> Ordering {
1295 const { assert!(MIN <= MAX); }
1296 if self.is_none() && other.is_none() {
1297 Ordering::Equal
1298 } else if self.is_none() {
1299 Ordering::Less
1300 } else if other.is_none() {
1301 Ordering::Greater
1302 } else {
1303 self.inner().cmp(&other.inner())
1304 }
1305 }
1306 }
1307
1308 impl<const MIN: $internal, const MAX: $internal> fmt::Binary for $type<MIN, MAX> {
1309 #[inline(always)]
1310 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1311 const { assert!(MIN <= MAX); }
1312 self.get().fmt(f)
1313 }
1314 }
1315
1316 impl<const MIN: $internal, const MAX: $internal> fmt::LowerHex for $type<MIN, MAX> {
1317 #[inline(always)]
1318 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1319 const { assert!(MIN <= MAX); }
1320 self.get().fmt(f)
1321 }
1322 }
1323
1324 impl<const MIN: $internal, const MAX: $internal> fmt::UpperHex for $type<MIN, MAX> {
1325 #[inline(always)]
1326 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1327 const { assert!(MIN <= MAX); }
1328 self.get().fmt(f)
1329 }
1330 }
1331
1332 impl<const MIN: $internal, const MAX: $internal> fmt::LowerExp for $type<MIN, MAX> {
1333 #[inline(always)]
1334 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1335 const { assert!(MIN <= MAX); }
1336 self.get().fmt(f)
1337 }
1338 }
1339
1340 impl<const MIN: $internal, const MAX: $internal> fmt::UpperExp for $type<MIN, MAX> {
1341 #[inline(always)]
1342 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1343 const { assert!(MIN <= MAX); }
1344 self.get().fmt(f)
1345 }
1346 }
1347
1348 impl<const MIN: $internal, const MAX: $internal> fmt::Octal for $type<MIN, MAX> {
1349 #[inline(always)]
1350 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1351 const { assert!(MIN <= MAX); }
1352 self.get().fmt(f)
1353 }
1354 }
1355
1356 if_unsigned! { $is_signed
1357 impl From<NonZero<$internal>> for $type<1, { $internal::MAX }> {
1358 #[inline(always)]
1359 fn from(value: NonZero<$internal>) -> Self {
1360 Self::from_nonzero(value)
1361 }
1362 }
1363
1364 impl From<$type<1, { $internal::MAX }>> for NonZero<$internal> {
1365 #[inline(always)]
1366 fn from(value: $type<1, { $internal::MAX }>) -> Self {
1367 value.to_nonzero()
1368 }
1369 }
1370 }
1371
1372 impl<const MIN: $internal, const MAX: $internal> From<$type<MIN, MAX>> for $internal {
1373 #[inline(always)]
1374 fn from(value: $type<MIN, MAX>) -> Self {
1375 const { assert!(MIN <= MAX); }
1376 value.get()
1377 }
1378 }
1379
1380 impl<
1381 const MIN: $internal,
1382 const MAX: $internal,
1383 > From<$type<MIN, MAX>> for $optional_type<MIN, MAX> {
1384 #[inline(always)]
1385 fn from(value: $type<MIN, MAX>) -> Self {
1386 const { assert!(MIN <= MAX); }
1387 Self::Some(value)
1388 }
1389 }
1390
1391 impl<
1392 const MIN: $internal,
1393 const MAX: $internal,
1394 > From<Option<$type<MIN, MAX>>> for $optional_type<MIN, MAX> {
1395 #[inline(always)]
1396 fn from(value: Option<$type<MIN, MAX>>) -> Self {
1397 const { assert!(MIN <= MAX); }
1398 match value {
1399 Some(value) => Self::Some(value),
1400 None => Self::None,
1401 }
1402 }
1403 }
1404
1405 impl<
1406 const MIN: $internal,
1407 const MAX: $internal,
1408 > From<$optional_type<MIN, MAX>> for Option<$type<MIN, MAX>> {
1409 #[inline(always)]
1410 fn from(value: $optional_type<MIN, MAX>) -> Self {
1411 const { assert!(MIN <= MAX); }
1412 value.get()
1413 }
1414 }
1415
1416 impl<const MIN: $internal, const MAX: $internal> TryFrom<$internal> for $type<MIN, MAX> {
1417 type Error = TryFromIntError;
1418
1419 #[inline]
1420 fn try_from(value: $internal) -> Result<Self, Self::Error> {
1421 const { assert!(MIN <= MAX); }
1422 Self::new(value).ok_or(TryFromIntError)
1423 }
1424 }
1425
1426 impl<const MIN: $internal, const MAX: $internal> FromStr for $type<MIN, MAX> {
1427 type Err = ParseIntError;
1428
1429 #[inline]
1430 fn from_str(s: &str) -> Result<Self, Self::Err> {
1431 const { assert!(MIN <= MAX); }
1432 let value = s.parse::<$internal>().map_err(|e| ParseIntError {
1433 kind: e.kind().clone()
1434 })?;
1435 if value < MIN {
1436 Err(ParseIntError { kind: IntErrorKind::NegOverflow })
1437 } else if value > MAX {
1438 Err(ParseIntError { kind: IntErrorKind::PosOverflow })
1439 } else {
1440 Ok(unsafe { Self::new_unchecked(value) })
1442 }
1443 }
1444 }
1445
1446 $(impl<
1447 const MIN_SRC: $from_internal,
1448 const MAX_SRC: $from_internal,
1449 const MIN_DST: $internal,
1450 const MAX_DST: $internal,
1451 > From<$from<MIN_SRC, MAX_SRC>> for $type<MIN_DST, MAX_DST>
1452 {
1453 #[inline(always)]
1454 #[allow(trivial_numeric_casts, unused_comparisons)]
1455 fn from(value: $from<MIN_SRC, MAX_SRC>) -> Self {
1456 const {
1457 assert!(MIN_SRC <= MAX_SRC, "source range is invalid");
1458 assert!(MIN_DST <= MAX_DST, "target range is invalid");
1459
1460 match ($from_internal::MIN == 0, $internal::MIN == 0) {
1461 (true, true) => {
1463 assert!(
1464 MIN_SRC as u128 >= MIN_DST as u128,
1465 "minimum value cannot be represented in the target range"
1466 );
1467 assert!(
1468 MAX_SRC as u128 <= MAX_DST as u128,
1469 "maximum value cannot be represented in the target range"
1470 );
1471 }
1472 (false, false) => {
1474 assert!(
1475 MIN_SRC as i128 >= MIN_DST as i128,
1476 "minimum value cannot be represented in the target range"
1477 );
1478 assert!(
1479 MAX_SRC as i128 <= MAX_DST as i128,
1480 "maximum value cannot be represented in the target range"
1481 );
1482 }
1483 (true, false) => {
1485 assert!(
1486 MIN_DST < 0 || MIN_SRC as u128 >= MIN_DST as u128,
1487 "minimum value cannot be represented in the target range"
1488 );
1489 assert!(
1490 MAX_DST >= 0
1491 && MAX_SRC as u128 <= i128::MAX as u128
1492 && MAX_SRC as i128 <= MAX_DST as i128,
1493 "maximum value cannot be represented in the target range"
1494 );
1495 }
1496 (false, true) => {
1498 assert!(
1499 MIN_SRC >= 0 && MIN_SRC as u128 >= MIN_DST as u128,
1500 "minimum value cannot be represented in the target range"
1501 );
1502 assert!(
1503 MAX_SRC >= 0 && MAX_SRC as u128 <= MAX_DST as u128,
1504 "maximum value cannot be represented in the target range"
1505 );
1506 }
1507 }
1508 }
1509
1510 unsafe { $type::new_unchecked(value.get() as $internal) }
1512 }
1513 })+
1514
1515 #[cfg(feature = "serde")]
1516 impl<const MIN: $internal, const MAX: $internal> serde_core::Serialize for $type<MIN, MAX> {
1517 #[inline(always)]
1518 fn serialize<S: serde_core::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>
1519 {
1520 const { assert!(MIN <= MAX); }
1521 self.get().serialize(serializer)
1522 }
1523 }
1524
1525 #[cfg(feature = "serde")]
1526 impl<
1527 const MIN: $internal,
1528 const MAX: $internal,
1529 > serde_core::Serialize for $optional_type<MIN, MAX> {
1530 #[inline(always)]
1531 fn serialize<S: serde_core::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>
1532 {
1533 const { assert!(MIN <= MAX); }
1534 self.get().serialize(serializer)
1535 }
1536 }
1537
1538 #[cfg(feature = "serde")]
1539 impl<
1540 'de,
1541 const MIN: $internal,
1542 const MAX: $internal,
1543 > serde_core::Deserialize<'de> for $type<MIN, MAX> {
1544 #[inline]
1545 fn deserialize<D: serde_core::Deserializer<'de>>(deserializer: D)
1546 -> Result<Self, D::Error>
1547 {
1548 const { assert!(MIN <= MAX); }
1549 let internal = <$internal>::deserialize(deserializer)?;
1550 Self::new(internal).ok_or_else(||
1551 <D::Error as serde_core::de::Error>::invalid_value(
1552 serde_core::de::Unexpected::Other("integer"),
1553 #[cfg(feature = "alloc")] {
1554 &alloc::format!("an integer in the range {}..={}", MIN, MAX).as_ref()
1555 },
1556 #[cfg(not(feature = "alloc"))] {
1557 &"an integer in the valid range"
1558 }
1559 )
1560 )
1561 }
1562 }
1563
1564 #[cfg(feature = "serde")]
1565 impl<
1566 'de,
1567 const MIN: $internal,
1568 const MAX: $internal,
1569 > serde_core::Deserialize<'de> for $optional_type<MIN, MAX> {
1570 #[inline]
1571 fn deserialize<D: serde_core::Deserializer<'de>>(deserializer: D)
1572 -> Result<Self, D::Error>
1573 {
1574 const { assert!(MIN <= MAX); }
1575 Ok(Self::Some($type::<MIN, MAX>::deserialize(deserializer)?))
1576 }
1577 }
1578
1579 #[cfg(feature = "rand08")]
1580 impl<
1581 const MIN: $internal,
1582 const MAX: $internal,
1583 > rand08::distributions::Distribution<$type<MIN, MAX>> for rand08::distributions::Standard {
1584 #[inline]
1585 fn sample<R: rand08::Rng + ?Sized>(&self, rng: &mut R) -> $type<MIN, MAX> {
1586 const { assert!(MIN <= MAX); }
1587 $type::new(rng.gen_range(MIN..=MAX)).expect("rand failed to generate a valid value")
1588 }
1589 }
1590
1591 if_not_manual_rand_09! {
1592 [$($($skips)+)?]
1593 #[cfg(feature = "rand09")]
1594 impl<
1595 const MIN: $internal,
1596 const MAX: $internal,
1597 > rand09::distr::Distribution<$type<MIN, MAX>> for rand09::distr::StandardUniform {
1598 #[inline]
1599 fn sample<R: rand09::Rng + ?Sized>(&self, rng: &mut R) -> $type<MIN, MAX> {
1600 const { assert!(MIN <= MAX); }
1601 $type::new(rng.random_range(MIN..=MAX)).expect("rand failed to generate a valid value")
1602 }
1603 }
1604 }
1605
1606 #[cfg(feature = "rand08")]
1607 impl<
1608 const MIN: $internal,
1609 const MAX: $internal,
1610 > rand08::distributions::Distribution<$optional_type<MIN, MAX>>
1611 for rand08::distributions::Standard {
1612 #[inline]
1613 fn sample<R: rand08::Rng + ?Sized>(&self, rng: &mut R) -> $optional_type<MIN, MAX> {
1614 const { assert!(MIN <= MAX); }
1615 rng.r#gen::<Option<$type<MIN, MAX>>>().into()
1616 }
1617 }
1618
1619 #[cfg(feature = "rand09")]
1620 impl<
1621 const MIN: $internal,
1622 const MAX: $internal,
1623 > rand09::distr::Distribution<$optional_type<MIN, MAX>>
1624 for rand09::distr::StandardUniform {
1625 #[inline]
1626 fn sample<R: rand09::Rng + ?Sized>(&self, rng: &mut R) -> $optional_type<MIN, MAX> {
1627 const { assert!(MIN <= MAX); }
1628 if rng.random() {
1629 $optional_type::None
1630 } else {
1631 $optional_type::Some(rng.random::<$type<MIN, MAX>>())
1632 }
1633 }
1634 }
1635
1636 #[cfg(feature = "num")]
1637 impl<const MIN: $internal, const MAX: $internal> num_traits::Bounded for $type<MIN, MAX> {
1638 #[inline(always)]
1639 fn min_value() -> Self {
1640 const { assert!(MIN <= MAX); }
1641 Self::MIN
1642 }
1643
1644 #[inline(always)]
1645 fn max_value() -> Self {
1646 const { assert!(MIN <= MAX); }
1647 Self::MAX
1648 }
1649 }
1650
1651 #[cfg(feature = "quickcheck")]
1652 impl<const MIN: $internal, const MAX: $internal> quickcheck::Arbitrary for $type<MIN, MAX> {
1653 #[inline]
1654 fn arbitrary(g: &mut quickcheck::Gen) -> Self {
1655 const { assert!(MIN <= MAX); }
1656 unsafe {
1658 Self::new_unchecked($internal::arbitrary(g).rem_euclid(MAX - MIN + 1) + MIN)
1659 }
1660 }
1661
1662 #[inline]
1663 fn shrink(&self) -> ::alloc::boxed::Box<dyn Iterator<Item = Self>> {
1664 ::alloc::boxed::Box::new(
1665 self.get()
1666 .shrink()
1667 .filter_map(Self::new)
1668 )
1669 }
1670 }
1671
1672 #[cfg(feature = "quickcheck")]
1673 impl<
1674 const MIN: $internal,
1675 const MAX: $internal,
1676 > quickcheck::Arbitrary for $optional_type<MIN, MAX> {
1677 #[inline]
1678 fn arbitrary(g: &mut quickcheck::Gen) -> Self {
1679 const { assert!(MIN <= MAX); }
1680 Option::<$type<MIN, MAX>>::arbitrary(g).into()
1681 }
1682
1683 #[inline]
1684 fn shrink(&self) -> ::alloc::boxed::Box<dyn Iterator<Item = Self>> {
1685 ::alloc::boxed::Box::new(self.get().shrink().map(Self::from))
1686 }
1687 }
1688 )*};
1689}
1690
1691impl_ranged! {
1692 RangedU8 {
1693 mod_name: ranged_u8
1694 internal: u8
1695 signed: false
1696 unsigned: u8
1697 optional: OptionRangedU8
1698 from: [
1699 RangedU16(u16)
1700 RangedU32(u32)
1701 RangedU64(u64)
1702 RangedU128(u128)
1703 RangedUsize(usize)
1704 RangedI8(i8)
1705 RangedI16(i16)
1706 RangedI32(i32)
1707 RangedI64(i64)
1708 RangedI128(i128)
1709 RangedIsize(isize)
1710 ]
1711 }
1712 RangedU16 {
1713 mod_name: ranged_u16
1714 internal: u16
1715 signed: false
1716 unsigned: u16
1717 optional: OptionRangedU16
1718 from: [
1719 RangedU8(u8)
1720 RangedU32(u32)
1721 RangedU64(u64)
1722 RangedU128(u128)
1723 RangedUsize(usize)
1724 RangedI8(i8)
1725 RangedI16(i16)
1726 RangedI32(i32)
1727 RangedI64(i64)
1728 RangedI128(i128)
1729 RangedIsize(isize)
1730 ]
1731 }
1732 RangedU32 {
1733 mod_name: ranged_u32
1734 internal: u32
1735 signed: false
1736 unsigned: u32
1737 optional: OptionRangedU32
1738 from: [
1739 RangedU8(u8)
1740 RangedU16(u16)
1741 RangedU64(u64)
1742 RangedU128(u128)
1743 RangedUsize(usize)
1744 RangedI8(i8)
1745 RangedI16(i16)
1746 RangedI32(i32)
1747 RangedI64(i64)
1748 RangedI128(i128)
1749 RangedIsize(isize)
1750 ]
1751 }
1752 RangedU64 {
1753 mod_name: ranged_u64
1754 internal: u64
1755 signed: false
1756 unsigned: u64
1757 optional: OptionRangedU64
1758 from: [
1759 RangedU8(u8)
1760 RangedU16(u16)
1761 RangedU32(u32)
1762 RangedU128(u128)
1763 RangedUsize(usize)
1764 RangedI8(i8)
1765 RangedI16(i16)
1766 RangedI32(i32)
1767 RangedI64(i64)
1768 RangedI128(i128)
1769 RangedIsize(isize)
1770 ]
1771 }
1772 RangedU128 {
1773 mod_name: ranged_u128
1774 internal: u128
1775 signed: false
1776 unsigned: u128
1777 optional: OptionRangedU128
1778 from: [
1779 RangedU8(u8)
1780 RangedU16(u16)
1781 RangedU32(u32)
1782 RangedU64(u64)
1783 RangedUsize(usize)
1784 RangedI8(i8)
1785 RangedI16(i16)
1786 RangedI32(i32)
1787 RangedI64(i64)
1788 RangedI128(i128)
1789 RangedIsize(isize)
1790 ]
1791 }
1792 RangedUsize {
1793 mod_name: ranged_usize
1794 internal: usize
1795 signed: false
1796 unsigned: usize
1797 optional: OptionRangedUsize
1798 from: [
1799 RangedU8(u8)
1800 RangedU16(u16)
1801 RangedU32(u32)
1802 RangedU64(u64)
1803 RangedU128(u128)
1804 RangedI8(i8)
1805 RangedI16(i16)
1806 RangedI32(i32)
1807 RangedI64(i64)
1808 RangedI128(i128)
1809 RangedIsize(isize)
1810 ]
1811 manual: [rand_09]
1812 }
1813 RangedI8 {
1814 mod_name: ranged_i8
1815 internal: i8
1816 signed: true
1817 unsigned: u8
1818 optional: OptionRangedI8
1819 from: [
1820 RangedU8(u8)
1821 RangedU16(u16)
1822 RangedU32(u32)
1823 RangedU64(u64)
1824 RangedU128(u128)
1825 RangedUsize(usize)
1826 RangedI16(i16)
1827 RangedI32(i32)
1828 RangedI64(i64)
1829 RangedI128(i128)
1830 RangedIsize(isize)
1831 ]
1832 }
1833 RangedI16 {
1834 mod_name: ranged_i16
1835 internal: i16
1836 signed: true
1837 unsigned: u16
1838 optional: OptionRangedI16
1839 from: [
1840 RangedU8(u8)
1841 RangedU16(u16)
1842 RangedU32(u32)
1843 RangedU64(u64)
1844 RangedU128(u128)
1845 RangedUsize(usize)
1846 RangedI8(i8)
1847 RangedI32(i32)
1848 RangedI64(i64)
1849 RangedI128(i128)
1850 RangedIsize(isize)
1851 ]
1852 }
1853 RangedI32 {
1854 mod_name: ranged_i32
1855 internal: i32
1856 signed: true
1857 unsigned: u32
1858 optional: OptionRangedI32
1859 from: [
1860 RangedU8(u8)
1861 RangedU16(u16)
1862 RangedU32(u32)
1863 RangedU64(u64)
1864 RangedU128(u128)
1865 RangedUsize(usize)
1866 RangedI8(i8)
1867 RangedI16(i16)
1868 RangedI64(i64)
1869 RangedI128(i128)
1870 RangedIsize(isize)
1871 ]
1872 }
1873 RangedI64 {
1874 mod_name: ranged_i64
1875 internal: i64
1876 signed: true
1877 unsigned: u64
1878 optional: OptionRangedI64
1879 from: [
1880 RangedU8(u8)
1881 RangedU16(u16)
1882 RangedU32(u32)
1883 RangedU64(u64)
1884 RangedU128(u128)
1885 RangedUsize(usize)
1886 RangedI8(i8)
1887 RangedI16(i16)
1888 RangedI32(i32)
1889 RangedI128(i128)
1890 RangedIsize(isize)
1891 ]
1892 }
1893 RangedI128 {
1894 mod_name: ranged_i128
1895 internal: i128
1896 signed: true
1897 unsigned: u128
1898 optional: OptionRangedI128
1899 from: [
1900 RangedU8(u8)
1901 RangedU16(u16)
1902 RangedU32(u32)
1903 RangedU64(u64)
1904 RangedU128(u128)
1905 RangedUsize(usize)
1906 RangedI8(i8)
1907 RangedI16(i16)
1908 RangedI32(i32)
1909 RangedI64(i64)
1910 RangedIsize(isize)
1911 ]
1912 }
1913 RangedIsize {
1914 mod_name: ranged_isize
1915 internal: isize
1916 signed: true
1917 unsigned: usize
1918 optional: OptionRangedIsize
1919 from: [
1920 RangedU8(u8)
1921 RangedU16(u16)
1922 RangedU32(u32)
1923 RangedU64(u64)
1924 RangedU128(u128)
1925 RangedUsize(usize)
1926 RangedI8(i8)
1927 RangedI16(i16)
1928 RangedI32(i32)
1929 RangedI64(i64)
1930 RangedI128(i128)
1931 ]
1932 manual: [rand_09]
1933 }
1934}
1935
1936#[cfg(feature = "rand09")]
1937impl<const MIN: usize, const MAX: usize> rand09::distr::Distribution<RangedUsize<MIN, MAX>>
1938 for rand09::distr::StandardUniform
1939{
1940 #[inline]
1941 fn sample<R: rand09::Rng + ?Sized>(&self, rng: &mut R) -> RangedUsize<MIN, MAX> {
1942 const {
1943 assert!(MIN <= MAX);
1944 }
1945
1946 #[cfg(target_pointer_width = "16")]
1947 let value = rng.random_range(MIN as u16..=MAX as u16) as usize;
1948 #[cfg(target_pointer_width = "32")]
1949 let value = rng.random_range(MIN as u32..=MAX as u32) as usize;
1950 #[cfg(target_pointer_width = "64")]
1951 let value = rng.random_range(MIN as u64..=MAX as u64) as usize;
1952 #[cfg(not(any(
1953 target_pointer_width = "16",
1954 target_pointer_width = "32",
1955 target_pointer_width = "64"
1956 )))]
1957 compile_error("platform has unusual (and unsupported) pointer width");
1958
1959 RangedUsize::new(value).expect("rand failed to generate a valid value")
1960 }
1961}
1962
1963#[cfg(feature = "rand09")]
1964impl<const MIN: isize, const MAX: isize> rand09::distr::Distribution<RangedIsize<MIN, MAX>>
1965 for rand09::distr::StandardUniform
1966{
1967 #[inline]
1968 fn sample<R: rand09::Rng + ?Sized>(&self, rng: &mut R) -> RangedIsize<MIN, MAX> {
1969 const {
1970 assert!(MIN <= MAX);
1971 }
1972
1973 #[cfg(target_pointer_width = "16")]
1974 let value = rng.random_range(MIN as i16..=MAX as i16) as isize;
1975 #[cfg(target_pointer_width = "32")]
1976 let value = rng.random_range(MIN as i32..=MAX as i32) as isize;
1977 #[cfg(target_pointer_width = "64")]
1978 let value = rng.random_range(MIN as i64..=MAX as i64) as isize;
1979 #[cfg(not(any(
1980 target_pointer_width = "16",
1981 target_pointer_width = "32",
1982 target_pointer_width = "64"
1983 )))]
1984 compile_error("platform has unusual (and unsupported) pointer width");
1985
1986 RangedIsize::new(value).expect("rand failed to generate a valid value")
1987 }
1988}