1#[cfg(feature = "std")]
4use std::time::{Instant, SystemTime};
5use {
6 crate::{Error, MaybeParens},
7 alloc::format,
8 core::{num::NonZero, time::Duration},
9};
10pub trait Cadd: Sized {
14 #[expect(missing_docs, reason = "no need for doc")]
15 type Other;
16 #[expect(missing_docs, reason = "no need for doc")]
17 type Output;
18 #[expect(missing_docs, reason = "no need for doc")]
19 type Error;
20 fn cadd(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
24 fn cadd_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
28}
29#[doc(alias = "checked_add")]
35#[inline]
36pub fn cadd<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
37where
38 T: Cadd,
39{
40 a.cadd(b)
41}
42impl Cadd for NonZero<u8> {
43 type Other = u8;
44 type Output = NonZero<u8>;
45 type Error = Error;
46 #[inline]
50 fn cadd(self, other: u8) -> Result<NonZero<u8>, Error> {
51 self.checked_add(other).ok_or_else(|| {
52 Error::new(format!(
53 "failed to compute {:?} + {:?}: {} overflow",
54 self,
55 MaybeParens(other),
56 "NonZero<u8>"
57 ))
58 })
59 }
60 #[inline]
61 fn cadd_assign(&mut self, other: u8) -> Result<(), Self::Error> {
65 *self = self.cadd(other)?;
66 Ok(())
67 }
68}
69impl Cadd for NonZero<u16> {
70 type Other = u16;
71 type Output = NonZero<u16>;
72 type Error = Error;
73 #[inline]
77 fn cadd(self, other: u16) -> Result<NonZero<u16>, Error> {
78 self.checked_add(other).ok_or_else(|| {
79 Error::new(format!(
80 "failed to compute {:?} + {:?}: {} overflow",
81 self,
82 MaybeParens(other),
83 "NonZero<u16>"
84 ))
85 })
86 }
87 #[inline]
88 fn cadd_assign(&mut self, other: u16) -> Result<(), Self::Error> {
92 *self = self.cadd(other)?;
93 Ok(())
94 }
95}
96impl Cadd for NonZero<u32> {
97 type Other = u32;
98 type Output = NonZero<u32>;
99 type Error = Error;
100 #[inline]
104 fn cadd(self, other: u32) -> Result<NonZero<u32>, Error> {
105 self.checked_add(other).ok_or_else(|| {
106 Error::new(format!(
107 "failed to compute {:?} + {:?}: {} overflow",
108 self,
109 MaybeParens(other),
110 "NonZero<u32>"
111 ))
112 })
113 }
114 #[inline]
115 fn cadd_assign(&mut self, other: u32) -> Result<(), Self::Error> {
119 *self = self.cadd(other)?;
120 Ok(())
121 }
122}
123impl Cadd for NonZero<u64> {
124 type Other = u64;
125 type Output = NonZero<u64>;
126 type Error = Error;
127 #[inline]
131 fn cadd(self, other: u64) -> Result<NonZero<u64>, Error> {
132 self.checked_add(other).ok_or_else(|| {
133 Error::new(format!(
134 "failed to compute {:?} + {:?}: {} overflow",
135 self,
136 MaybeParens(other),
137 "NonZero<u64>"
138 ))
139 })
140 }
141 #[inline]
142 fn cadd_assign(&mut self, other: u64) -> Result<(), Self::Error> {
146 *self = self.cadd(other)?;
147 Ok(())
148 }
149}
150impl Cadd for NonZero<u128> {
151 type Other = u128;
152 type Output = NonZero<u128>;
153 type Error = Error;
154 #[inline]
158 fn cadd(self, other: u128) -> Result<NonZero<u128>, Error> {
159 self.checked_add(other).ok_or_else(|| {
160 Error::new(format!(
161 "failed to compute {:?} + {:?}: {} overflow",
162 self,
163 MaybeParens(other),
164 "NonZero<u128>"
165 ))
166 })
167 }
168 #[inline]
169 fn cadd_assign(&mut self, other: u128) -> Result<(), Self::Error> {
173 *self = self.cadd(other)?;
174 Ok(())
175 }
176}
177impl Cadd for NonZero<usize> {
178 type Other = usize;
179 type Output = NonZero<usize>;
180 type Error = Error;
181 #[inline]
185 fn cadd(self, other: usize) -> Result<NonZero<usize>, Error> {
186 self.checked_add(other).ok_or_else(|| {
187 Error::new(format!(
188 "failed to compute {:?} + {:?}: {} overflow",
189 self,
190 MaybeParens(other),
191 "NonZero<usize>"
192 ))
193 })
194 }
195 #[inline]
196 fn cadd_assign(&mut self, other: usize) -> Result<(), Self::Error> {
200 *self = self.cadd(other)?;
201 Ok(())
202 }
203}
204impl Cadd for i8 {
205 type Other = i8;
206 type Output = i8;
207 type Error = Error;
208 #[inline]
212 fn cadd(self, other: i8) -> Result<i8, Error> {
213 self.checked_add(other).ok_or_else(|| {
214 Error::new(format!(
215 "failed to compute {:?} + {:?}: {} overflow",
216 self,
217 MaybeParens(other),
218 "i8"
219 ))
220 })
221 }
222 #[inline]
223 fn cadd_assign(&mut self, other: i8) -> Result<(), Self::Error> {
227 *self = self.cadd(other)?;
228 Ok(())
229 }
230}
231impl Cadd for i16 {
232 type Other = i16;
233 type Output = i16;
234 type Error = Error;
235 #[inline]
239 fn cadd(self, other: i16) -> Result<i16, Error> {
240 self.checked_add(other).ok_or_else(|| {
241 Error::new(format!(
242 "failed to compute {:?} + {:?}: {} overflow",
243 self,
244 MaybeParens(other),
245 "i16"
246 ))
247 })
248 }
249 #[inline]
250 fn cadd_assign(&mut self, other: i16) -> Result<(), Self::Error> {
254 *self = self.cadd(other)?;
255 Ok(())
256 }
257}
258impl Cadd for i32 {
259 type Other = i32;
260 type Output = i32;
261 type Error = Error;
262 #[inline]
266 fn cadd(self, other: i32) -> Result<i32, Error> {
267 self.checked_add(other).ok_or_else(|| {
268 Error::new(format!(
269 "failed to compute {:?} + {:?}: {} overflow",
270 self,
271 MaybeParens(other),
272 "i32"
273 ))
274 })
275 }
276 #[inline]
277 fn cadd_assign(&mut self, other: i32) -> Result<(), Self::Error> {
281 *self = self.cadd(other)?;
282 Ok(())
283 }
284}
285impl Cadd for i64 {
286 type Other = i64;
287 type Output = i64;
288 type Error = Error;
289 #[inline]
293 fn cadd(self, other: i64) -> Result<i64, Error> {
294 self.checked_add(other).ok_or_else(|| {
295 Error::new(format!(
296 "failed to compute {:?} + {:?}: {} overflow",
297 self,
298 MaybeParens(other),
299 "i64"
300 ))
301 })
302 }
303 #[inline]
304 fn cadd_assign(&mut self, other: i64) -> Result<(), Self::Error> {
308 *self = self.cadd(other)?;
309 Ok(())
310 }
311}
312impl Cadd for i128 {
313 type Other = i128;
314 type Output = i128;
315 type Error = Error;
316 #[inline]
320 fn cadd(self, other: i128) -> Result<i128, Error> {
321 self.checked_add(other).ok_or_else(|| {
322 Error::new(format!(
323 "failed to compute {:?} + {:?}: {} overflow",
324 self,
325 MaybeParens(other),
326 "i128"
327 ))
328 })
329 }
330 #[inline]
331 fn cadd_assign(&mut self, other: i128) -> Result<(), Self::Error> {
335 *self = self.cadd(other)?;
336 Ok(())
337 }
338}
339impl Cadd for isize {
340 type Other = isize;
341 type Output = isize;
342 type Error = Error;
343 #[inline]
347 fn cadd(self, other: isize) -> Result<isize, Error> {
348 self.checked_add(other).ok_or_else(|| {
349 Error::new(format!(
350 "failed to compute {:?} + {:?}: {} overflow",
351 self,
352 MaybeParens(other),
353 "isize"
354 ))
355 })
356 }
357 #[inline]
358 fn cadd_assign(&mut self, other: isize) -> Result<(), Self::Error> {
362 *self = self.cadd(other)?;
363 Ok(())
364 }
365}
366impl Cadd for u8 {
367 type Other = u8;
368 type Output = u8;
369 type Error = Error;
370 #[inline]
374 fn cadd(self, other: u8) -> Result<u8, Error> {
375 self.checked_add(other).ok_or_else(|| {
376 Error::new(format!(
377 "failed to compute {:?} + {:?}: {} overflow",
378 self,
379 MaybeParens(other),
380 "u8"
381 ))
382 })
383 }
384 #[inline]
385 fn cadd_assign(&mut self, other: u8) -> Result<(), Self::Error> {
389 *self = self.cadd(other)?;
390 Ok(())
391 }
392}
393impl Cadd for u16 {
394 type Other = u16;
395 type Output = u16;
396 type Error = Error;
397 #[inline]
401 fn cadd(self, other: u16) -> Result<u16, Error> {
402 self.checked_add(other).ok_or_else(|| {
403 Error::new(format!(
404 "failed to compute {:?} + {:?}: {} overflow",
405 self,
406 MaybeParens(other),
407 "u16"
408 ))
409 })
410 }
411 #[inline]
412 fn cadd_assign(&mut self, other: u16) -> Result<(), Self::Error> {
416 *self = self.cadd(other)?;
417 Ok(())
418 }
419}
420impl Cadd for u32 {
421 type Other = u32;
422 type Output = u32;
423 type Error = Error;
424 #[inline]
428 fn cadd(self, other: u32) -> Result<u32, Error> {
429 self.checked_add(other).ok_or_else(|| {
430 Error::new(format!(
431 "failed to compute {:?} + {:?}: {} overflow",
432 self,
433 MaybeParens(other),
434 "u32"
435 ))
436 })
437 }
438 #[inline]
439 fn cadd_assign(&mut self, other: u32) -> Result<(), Self::Error> {
443 *self = self.cadd(other)?;
444 Ok(())
445 }
446}
447impl Cadd for u64 {
448 type Other = u64;
449 type Output = u64;
450 type Error = Error;
451 #[inline]
455 fn cadd(self, other: u64) -> Result<u64, Error> {
456 self.checked_add(other).ok_or_else(|| {
457 Error::new(format!(
458 "failed to compute {:?} + {:?}: {} overflow",
459 self,
460 MaybeParens(other),
461 "u64"
462 ))
463 })
464 }
465 #[inline]
466 fn cadd_assign(&mut self, other: u64) -> Result<(), Self::Error> {
470 *self = self.cadd(other)?;
471 Ok(())
472 }
473}
474impl Cadd for u128 {
475 type Other = u128;
476 type Output = u128;
477 type Error = Error;
478 #[inline]
482 fn cadd(self, other: u128) -> Result<u128, Error> {
483 self.checked_add(other).ok_or_else(|| {
484 Error::new(format!(
485 "failed to compute {:?} + {:?}: {} overflow",
486 self,
487 MaybeParens(other),
488 "u128"
489 ))
490 })
491 }
492 #[inline]
493 fn cadd_assign(&mut self, other: u128) -> Result<(), Self::Error> {
497 *self = self.cadd(other)?;
498 Ok(())
499 }
500}
501impl Cadd for usize {
502 type Other = usize;
503 type Output = usize;
504 type Error = Error;
505 #[inline]
509 fn cadd(self, other: usize) -> Result<usize, Error> {
510 self.checked_add(other).ok_or_else(|| {
511 Error::new(format!(
512 "failed to compute {:?} + {:?}: {} overflow",
513 self,
514 MaybeParens(other),
515 "usize"
516 ))
517 })
518 }
519 #[inline]
520 fn cadd_assign(&mut self, other: usize) -> Result<(), Self::Error> {
524 *self = self.cadd(other)?;
525 Ok(())
526 }
527}
528impl Cadd for Duration {
529 type Other = Duration;
530 type Output = Duration;
531 type Error = Error;
532 #[inline]
536 fn cadd(self, other: Duration) -> Result<Duration, Error> {
537 self.checked_add(other).ok_or_else(|| {
538 Error::new(format!(
539 "failed to compute {:?} + {:?}: {} overflow",
540 self,
541 MaybeParens(other),
542 "Duration"
543 ))
544 })
545 }
546 #[inline]
547 fn cadd_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
551 *self = self.cadd(other)?;
552 Ok(())
553 }
554}
555#[cfg(feature = "std")]
556impl Cadd for Instant {
557 type Other = Duration;
558 type Output = Instant;
559 type Error = Error;
560 #[inline]
564 fn cadd(self, other: Duration) -> Result<Instant, Error> {
565 self.checked_add(other).ok_or_else(|| {
566 Error::new(format!(
567 "failed to compute {:?} + {:?}: {} overflow",
568 self,
569 MaybeParens(other),
570 "Instant"
571 ))
572 })
573 }
574 #[inline]
575 fn cadd_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
579 *self = self.cadd(other)?;
580 Ok(())
581 }
582}
583#[cfg(feature = "std")]
584impl Cadd for SystemTime {
585 type Other = Duration;
586 type Output = SystemTime;
587 type Error = Error;
588 #[inline]
592 fn cadd(self, other: Duration) -> Result<SystemTime, Error> {
593 self.checked_add(other).ok_or_else(|| {
594 Error::new(format!(
595 "failed to compute {:?} + {:?}: {} overflow",
596 self,
597 MaybeParens(other),
598 "SystemTime"
599 ))
600 })
601 }
602 #[inline]
603 fn cadd_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
607 *self = self.cadd(other)?;
608 Ok(())
609 }
610}
611pub trait CaddUnsigned: Sized {
615 #[expect(missing_docs, reason = "no need for doc")]
616 type Other;
617 #[expect(missing_docs, reason = "no need for doc")]
618 type Output;
619 #[expect(missing_docs, reason = "no need for doc")]
620 type Error;
621 fn cadd_unsigned(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
625}
626#[doc(alias = "checked_add_unsigned")]
632#[inline]
633pub fn cadd_unsigned<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
634where
635 T: CaddUnsigned,
636{
637 a.cadd_unsigned(b)
638}
639impl CaddUnsigned for i8 {
640 type Other = u8;
641 type Output = i8;
642 type Error = Error;
643 #[inline]
647 fn cadd_unsigned(self, other: u8) -> Result<i8, Error> {
648 self.checked_add_unsigned(other).ok_or_else(|| {
649 Error::new(format!(
650 "failed to compute add_unsigned({:?}, {:?}): {} overflow",
651 self, other, "i8"
652 ))
653 })
654 }
655}
656impl CaddUnsigned for i16 {
657 type Other = u16;
658 type Output = i16;
659 type Error = Error;
660 #[inline]
664 fn cadd_unsigned(self, other: u16) -> Result<i16, Error> {
665 self.checked_add_unsigned(other).ok_or_else(|| {
666 Error::new(format!(
667 "failed to compute add_unsigned({:?}, {:?}): {} overflow",
668 self, other, "i16"
669 ))
670 })
671 }
672}
673impl CaddUnsigned for i32 {
674 type Other = u32;
675 type Output = i32;
676 type Error = Error;
677 #[inline]
681 fn cadd_unsigned(self, other: u32) -> Result<i32, Error> {
682 self.checked_add_unsigned(other).ok_or_else(|| {
683 Error::new(format!(
684 "failed to compute add_unsigned({:?}, {:?}): {} overflow",
685 self, other, "i32"
686 ))
687 })
688 }
689}
690impl CaddUnsigned for i64 {
691 type Other = u64;
692 type Output = i64;
693 type Error = Error;
694 #[inline]
698 fn cadd_unsigned(self, other: u64) -> Result<i64, Error> {
699 self.checked_add_unsigned(other).ok_or_else(|| {
700 Error::new(format!(
701 "failed to compute add_unsigned({:?}, {:?}): {} overflow",
702 self, other, "i64"
703 ))
704 })
705 }
706}
707impl CaddUnsigned for i128 {
708 type Other = u128;
709 type Output = i128;
710 type Error = Error;
711 #[inline]
715 fn cadd_unsigned(self, other: u128) -> Result<i128, Error> {
716 self.checked_add_unsigned(other).ok_or_else(|| {
717 Error::new(format!(
718 "failed to compute add_unsigned({:?}, {:?}): {} overflow",
719 self, other, "i128"
720 ))
721 })
722 }
723}
724impl CaddUnsigned for isize {
725 type Other = usize;
726 type Output = isize;
727 type Error = Error;
728 #[inline]
732 fn cadd_unsigned(self, other: usize) -> Result<isize, Error> {
733 self.checked_add_unsigned(other).ok_or_else(|| {
734 Error::new(format!(
735 "failed to compute add_unsigned({:?}, {:?}): {} overflow",
736 self, other, "isize"
737 ))
738 })
739 }
740}
741pub trait CaddSigned: Sized {
745 #[expect(missing_docs, reason = "no need for doc")]
746 type Other;
747 #[expect(missing_docs, reason = "no need for doc")]
748 type Output;
749 #[expect(missing_docs, reason = "no need for doc")]
750 type Error;
751 fn cadd_signed(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
755}
756#[doc(alias = "checked_add_signed")]
762#[inline]
763pub fn cadd_signed<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
764where
765 T: CaddSigned,
766{
767 a.cadd_signed(b)
768}
769impl CaddSigned for u8 {
770 type Other = i8;
771 type Output = u8;
772 type Error = Error;
773 #[inline]
777 fn cadd_signed(self, other: i8) -> Result<u8, Error> {
778 self.checked_add_signed(other).ok_or_else(|| {
779 Error::new(format!(
780 "failed to compute add_signed({:?}, {:?}): {} overflow",
781 self, other, "u8"
782 ))
783 })
784 }
785}
786impl CaddSigned for u16 {
787 type Other = i16;
788 type Output = u16;
789 type Error = Error;
790 #[inline]
794 fn cadd_signed(self, other: i16) -> Result<u16, Error> {
795 self.checked_add_signed(other).ok_or_else(|| {
796 Error::new(format!(
797 "failed to compute add_signed({:?}, {:?}): {} overflow",
798 self, other, "u16"
799 ))
800 })
801 }
802}
803impl CaddSigned for u32 {
804 type Other = i32;
805 type Output = u32;
806 type Error = Error;
807 #[inline]
811 fn cadd_signed(self, other: i32) -> Result<u32, Error> {
812 self.checked_add_signed(other).ok_or_else(|| {
813 Error::new(format!(
814 "failed to compute add_signed({:?}, {:?}): {} overflow",
815 self, other, "u32"
816 ))
817 })
818 }
819}
820impl CaddSigned for u64 {
821 type Other = i64;
822 type Output = u64;
823 type Error = Error;
824 #[inline]
828 fn cadd_signed(self, other: i64) -> Result<u64, Error> {
829 self.checked_add_signed(other).ok_or_else(|| {
830 Error::new(format!(
831 "failed to compute add_signed({:?}, {:?}): {} overflow",
832 self, other, "u64"
833 ))
834 })
835 }
836}
837impl CaddSigned for u128 {
838 type Other = i128;
839 type Output = u128;
840 type Error = Error;
841 #[inline]
845 fn cadd_signed(self, other: i128) -> Result<u128, Error> {
846 self.checked_add_signed(other).ok_or_else(|| {
847 Error::new(format!(
848 "failed to compute add_signed({:?}, {:?}): {} overflow",
849 self, other, "u128"
850 ))
851 })
852 }
853}
854impl CaddSigned for usize {
855 type Other = isize;
856 type Output = usize;
857 type Error = Error;
858 #[inline]
862 fn cadd_signed(self, other: isize) -> Result<usize, Error> {
863 self.checked_add_signed(other).ok_or_else(|| {
864 Error::new(format!(
865 "failed to compute add_signed({:?}, {:?}): {} overflow",
866 self, other, "usize"
867 ))
868 })
869 }
870}
871pub trait Csub: Sized {
875 #[expect(missing_docs, reason = "no need for doc")]
876 type Other;
877 #[expect(missing_docs, reason = "no need for doc")]
878 type Output;
879 #[expect(missing_docs, reason = "no need for doc")]
880 type Error;
881 fn csub(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
885 fn csub_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
889}
890#[doc(alias = "checked_sub")]
896#[inline]
897pub fn csub<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
898where
899 T: Csub,
900{
901 a.csub(b)
902}
903impl Csub for i8 {
904 type Other = i8;
905 type Output = i8;
906 type Error = Error;
907 #[inline]
911 fn csub(self, other: i8) -> Result<i8, Error> {
912 self.checked_sub(other).ok_or_else(|| {
913 Error::new(format!(
914 "failed to compute {:?} - {:?}: {} overflow",
915 self,
916 MaybeParens(other),
917 "i8"
918 ))
919 })
920 }
921 #[inline]
922 fn csub_assign(&mut self, other: i8) -> Result<(), Self::Error> {
926 *self = self.csub(other)?;
927 Ok(())
928 }
929}
930impl Csub for i16 {
931 type Other = i16;
932 type Output = i16;
933 type Error = Error;
934 #[inline]
938 fn csub(self, other: i16) -> Result<i16, Error> {
939 self.checked_sub(other).ok_or_else(|| {
940 Error::new(format!(
941 "failed to compute {:?} - {:?}: {} overflow",
942 self,
943 MaybeParens(other),
944 "i16"
945 ))
946 })
947 }
948 #[inline]
949 fn csub_assign(&mut self, other: i16) -> Result<(), Self::Error> {
953 *self = self.csub(other)?;
954 Ok(())
955 }
956}
957impl Csub for i32 {
958 type Other = i32;
959 type Output = i32;
960 type Error = Error;
961 #[inline]
965 fn csub(self, other: i32) -> Result<i32, Error> {
966 self.checked_sub(other).ok_or_else(|| {
967 Error::new(format!(
968 "failed to compute {:?} - {:?}: {} overflow",
969 self,
970 MaybeParens(other),
971 "i32"
972 ))
973 })
974 }
975 #[inline]
976 fn csub_assign(&mut self, other: i32) -> Result<(), Self::Error> {
980 *self = self.csub(other)?;
981 Ok(())
982 }
983}
984impl Csub for i64 {
985 type Other = i64;
986 type Output = i64;
987 type Error = Error;
988 #[inline]
992 fn csub(self, other: i64) -> Result<i64, Error> {
993 self.checked_sub(other).ok_or_else(|| {
994 Error::new(format!(
995 "failed to compute {:?} - {:?}: {} overflow",
996 self,
997 MaybeParens(other),
998 "i64"
999 ))
1000 })
1001 }
1002 #[inline]
1003 fn csub_assign(&mut self, other: i64) -> Result<(), Self::Error> {
1007 *self = self.csub(other)?;
1008 Ok(())
1009 }
1010}
1011impl Csub for i128 {
1012 type Other = i128;
1013 type Output = i128;
1014 type Error = Error;
1015 #[inline]
1019 fn csub(self, other: i128) -> Result<i128, Error> {
1020 self.checked_sub(other).ok_or_else(|| {
1021 Error::new(format!(
1022 "failed to compute {:?} - {:?}: {} overflow",
1023 self,
1024 MaybeParens(other),
1025 "i128"
1026 ))
1027 })
1028 }
1029 #[inline]
1030 fn csub_assign(&mut self, other: i128) -> Result<(), Self::Error> {
1034 *self = self.csub(other)?;
1035 Ok(())
1036 }
1037}
1038impl Csub for isize {
1039 type Other = isize;
1040 type Output = isize;
1041 type Error = Error;
1042 #[inline]
1046 fn csub(self, other: isize) -> Result<isize, Error> {
1047 self.checked_sub(other).ok_or_else(|| {
1048 Error::new(format!(
1049 "failed to compute {:?} - {:?}: {} overflow",
1050 self,
1051 MaybeParens(other),
1052 "isize"
1053 ))
1054 })
1055 }
1056 #[inline]
1057 fn csub_assign(&mut self, other: isize) -> Result<(), Self::Error> {
1061 *self = self.csub(other)?;
1062 Ok(())
1063 }
1064}
1065impl Csub for u8 {
1066 type Other = u8;
1067 type Output = u8;
1068 type Error = Error;
1069 #[inline]
1073 fn csub(self, other: u8) -> Result<u8, Error> {
1074 self.checked_sub(other).ok_or_else(|| {
1075 Error::new(format!(
1076 "failed to compute {:?} - {:?}: {} overflow",
1077 self,
1078 MaybeParens(other),
1079 "u8"
1080 ))
1081 })
1082 }
1083 #[inline]
1084 fn csub_assign(&mut self, other: u8) -> Result<(), Self::Error> {
1088 *self = self.csub(other)?;
1089 Ok(())
1090 }
1091}
1092impl Csub for u16 {
1093 type Other = u16;
1094 type Output = u16;
1095 type Error = Error;
1096 #[inline]
1100 fn csub(self, other: u16) -> Result<u16, Error> {
1101 self.checked_sub(other).ok_or_else(|| {
1102 Error::new(format!(
1103 "failed to compute {:?} - {:?}: {} overflow",
1104 self,
1105 MaybeParens(other),
1106 "u16"
1107 ))
1108 })
1109 }
1110 #[inline]
1111 fn csub_assign(&mut self, other: u16) -> Result<(), Self::Error> {
1115 *self = self.csub(other)?;
1116 Ok(())
1117 }
1118}
1119impl Csub for u32 {
1120 type Other = u32;
1121 type Output = u32;
1122 type Error = Error;
1123 #[inline]
1127 fn csub(self, other: u32) -> Result<u32, Error> {
1128 self.checked_sub(other).ok_or_else(|| {
1129 Error::new(format!(
1130 "failed to compute {:?} - {:?}: {} overflow",
1131 self,
1132 MaybeParens(other),
1133 "u32"
1134 ))
1135 })
1136 }
1137 #[inline]
1138 fn csub_assign(&mut self, other: u32) -> Result<(), Self::Error> {
1142 *self = self.csub(other)?;
1143 Ok(())
1144 }
1145}
1146impl Csub for u64 {
1147 type Other = u64;
1148 type Output = u64;
1149 type Error = Error;
1150 #[inline]
1154 fn csub(self, other: u64) -> Result<u64, Error> {
1155 self.checked_sub(other).ok_or_else(|| {
1156 Error::new(format!(
1157 "failed to compute {:?} - {:?}: {} overflow",
1158 self,
1159 MaybeParens(other),
1160 "u64"
1161 ))
1162 })
1163 }
1164 #[inline]
1165 fn csub_assign(&mut self, other: u64) -> Result<(), Self::Error> {
1169 *self = self.csub(other)?;
1170 Ok(())
1171 }
1172}
1173impl Csub for u128 {
1174 type Other = u128;
1175 type Output = u128;
1176 type Error = Error;
1177 #[inline]
1181 fn csub(self, other: u128) -> Result<u128, Error> {
1182 self.checked_sub(other).ok_or_else(|| {
1183 Error::new(format!(
1184 "failed to compute {:?} - {:?}: {} overflow",
1185 self,
1186 MaybeParens(other),
1187 "u128"
1188 ))
1189 })
1190 }
1191 #[inline]
1192 fn csub_assign(&mut self, other: u128) -> Result<(), Self::Error> {
1196 *self = self.csub(other)?;
1197 Ok(())
1198 }
1199}
1200impl Csub for usize {
1201 type Other = usize;
1202 type Output = usize;
1203 type Error = Error;
1204 #[inline]
1208 fn csub(self, other: usize) -> Result<usize, Error> {
1209 self.checked_sub(other).ok_or_else(|| {
1210 Error::new(format!(
1211 "failed to compute {:?} - {:?}: {} overflow",
1212 self,
1213 MaybeParens(other),
1214 "usize"
1215 ))
1216 })
1217 }
1218 #[inline]
1219 fn csub_assign(&mut self, other: usize) -> Result<(), Self::Error> {
1223 *self = self.csub(other)?;
1224 Ok(())
1225 }
1226}
1227impl Csub for Duration {
1228 type Other = Duration;
1229 type Output = Duration;
1230 type Error = Error;
1231 #[inline]
1235 fn csub(self, other: Duration) -> Result<Duration, Error> {
1236 self.checked_sub(other).ok_or_else(|| {
1237 Error::new(format!(
1238 "failed to compute {:?} - {:?}: {} overflow",
1239 self,
1240 MaybeParens(other),
1241 "Duration"
1242 ))
1243 })
1244 }
1245 #[inline]
1246 fn csub_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
1250 *self = self.csub(other)?;
1251 Ok(())
1252 }
1253}
1254#[cfg(feature = "std")]
1255impl Csub for Instant {
1256 type Other = Duration;
1257 type Output = Instant;
1258 type Error = Error;
1259 #[inline]
1263 fn csub(self, other: Duration) -> Result<Instant, Error> {
1264 self.checked_sub(other).ok_or_else(|| {
1265 Error::new(format!(
1266 "failed to compute {:?} - {:?}: {} overflow",
1267 self,
1268 MaybeParens(other),
1269 "Instant"
1270 ))
1271 })
1272 }
1273 #[inline]
1274 fn csub_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
1278 *self = self.csub(other)?;
1279 Ok(())
1280 }
1281}
1282#[cfg(feature = "std")]
1283impl Csub for SystemTime {
1284 type Other = Duration;
1285 type Output = SystemTime;
1286 type Error = Error;
1287 #[inline]
1291 fn csub(self, other: Duration) -> Result<SystemTime, Error> {
1292 self.checked_sub(other).ok_or_else(|| {
1293 Error::new(format!(
1294 "failed to compute {:?} - {:?}: {} overflow",
1295 self,
1296 MaybeParens(other),
1297 "SystemTime"
1298 ))
1299 })
1300 }
1301 #[inline]
1302 fn csub_assign(&mut self, other: Duration) -> Result<(), Self::Error> {
1306 *self = self.csub(other)?;
1307 Ok(())
1308 }
1309}
1310pub trait CsubUnsigned: Sized {
1314 #[expect(missing_docs, reason = "no need for doc")]
1315 type Other;
1316 #[expect(missing_docs, reason = "no need for doc")]
1317 type Output;
1318 #[expect(missing_docs, reason = "no need for doc")]
1319 type Error;
1320 fn csub_unsigned(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
1324}
1325#[doc(alias = "checked_sub_unsigned")]
1331#[inline]
1332pub fn csub_unsigned<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
1333where
1334 T: CsubUnsigned,
1335{
1336 a.csub_unsigned(b)
1337}
1338impl CsubUnsigned for i8 {
1339 type Other = u8;
1340 type Output = i8;
1341 type Error = Error;
1342 #[inline]
1346 fn csub_unsigned(self, other: u8) -> Result<i8, Error> {
1347 self.checked_sub_unsigned(other).ok_or_else(|| {
1348 Error::new(format!(
1349 "failed to compute sub_unsigned({:?}, {:?}): {} overflow",
1350 self, other, "i8"
1351 ))
1352 })
1353 }
1354}
1355impl CsubUnsigned for i16 {
1356 type Other = u16;
1357 type Output = i16;
1358 type Error = Error;
1359 #[inline]
1363 fn csub_unsigned(self, other: u16) -> Result<i16, Error> {
1364 self.checked_sub_unsigned(other).ok_or_else(|| {
1365 Error::new(format!(
1366 "failed to compute sub_unsigned({:?}, {:?}): {} overflow",
1367 self, other, "i16"
1368 ))
1369 })
1370 }
1371}
1372impl CsubUnsigned for i32 {
1373 type Other = u32;
1374 type Output = i32;
1375 type Error = Error;
1376 #[inline]
1380 fn csub_unsigned(self, other: u32) -> Result<i32, Error> {
1381 self.checked_sub_unsigned(other).ok_or_else(|| {
1382 Error::new(format!(
1383 "failed to compute sub_unsigned({:?}, {:?}): {} overflow",
1384 self, other, "i32"
1385 ))
1386 })
1387 }
1388}
1389impl CsubUnsigned for i64 {
1390 type Other = u64;
1391 type Output = i64;
1392 type Error = Error;
1393 #[inline]
1397 fn csub_unsigned(self, other: u64) -> Result<i64, Error> {
1398 self.checked_sub_unsigned(other).ok_or_else(|| {
1399 Error::new(format!(
1400 "failed to compute sub_unsigned({:?}, {:?}): {} overflow",
1401 self, other, "i64"
1402 ))
1403 })
1404 }
1405}
1406impl CsubUnsigned for i128 {
1407 type Other = u128;
1408 type Output = i128;
1409 type Error = Error;
1410 #[inline]
1414 fn csub_unsigned(self, other: u128) -> Result<i128, Error> {
1415 self.checked_sub_unsigned(other).ok_or_else(|| {
1416 Error::new(format!(
1417 "failed to compute sub_unsigned({:?}, {:?}): {} overflow",
1418 self, other, "i128"
1419 ))
1420 })
1421 }
1422}
1423impl CsubUnsigned for isize {
1424 type Other = usize;
1425 type Output = isize;
1426 type Error = Error;
1427 #[inline]
1431 fn csub_unsigned(self, other: usize) -> Result<isize, Error> {
1432 self.checked_sub_unsigned(other).ok_or_else(|| {
1433 Error::new(format!(
1434 "failed to compute sub_unsigned({:?}, {:?}): {} overflow",
1435 self, other, "isize"
1436 ))
1437 })
1438 }
1439}
1440pub trait CsubSigned: Sized {
1444 #[expect(missing_docs, reason = "no need for doc")]
1445 type Other;
1446 #[expect(missing_docs, reason = "no need for doc")]
1447 type Output;
1448 #[expect(missing_docs, reason = "no need for doc")]
1449 type Error;
1450 fn csub_signed(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
1454}
1455#[doc(alias = "checked_sub_signed")]
1461#[inline]
1462pub fn csub_signed<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
1463where
1464 T: CsubSigned,
1465{
1466 a.csub_signed(b)
1467}
1468impl CsubSigned for u8 {
1469 type Other = i8;
1470 type Output = u8;
1471 type Error = Error;
1472 #[inline]
1476 fn csub_signed(self, other: i8) -> Result<u8, Error> {
1477 self.checked_sub_signed(other).ok_or_else(|| {
1478 Error::new(format!(
1479 "failed to compute sub_signed({:?}, {:?}): {} overflow",
1480 self, other, "u8"
1481 ))
1482 })
1483 }
1484}
1485impl CsubSigned for u16 {
1486 type Other = i16;
1487 type Output = u16;
1488 type Error = Error;
1489 #[inline]
1493 fn csub_signed(self, other: i16) -> Result<u16, Error> {
1494 self.checked_sub_signed(other).ok_or_else(|| {
1495 Error::new(format!(
1496 "failed to compute sub_signed({:?}, {:?}): {} overflow",
1497 self, other, "u16"
1498 ))
1499 })
1500 }
1501}
1502impl CsubSigned for u32 {
1503 type Other = i32;
1504 type Output = u32;
1505 type Error = Error;
1506 #[inline]
1510 fn csub_signed(self, other: i32) -> Result<u32, Error> {
1511 self.checked_sub_signed(other).ok_or_else(|| {
1512 Error::new(format!(
1513 "failed to compute sub_signed({:?}, {:?}): {} overflow",
1514 self, other, "u32"
1515 ))
1516 })
1517 }
1518}
1519impl CsubSigned for u64 {
1520 type Other = i64;
1521 type Output = u64;
1522 type Error = Error;
1523 #[inline]
1527 fn csub_signed(self, other: i64) -> Result<u64, Error> {
1528 self.checked_sub_signed(other).ok_or_else(|| {
1529 Error::new(format!(
1530 "failed to compute sub_signed({:?}, {:?}): {} overflow",
1531 self, other, "u64"
1532 ))
1533 })
1534 }
1535}
1536impl CsubSigned for u128 {
1537 type Other = i128;
1538 type Output = u128;
1539 type Error = Error;
1540 #[inline]
1544 fn csub_signed(self, other: i128) -> Result<u128, Error> {
1545 self.checked_sub_signed(other).ok_or_else(|| {
1546 Error::new(format!(
1547 "failed to compute sub_signed({:?}, {:?}): {} overflow",
1548 self, other, "u128"
1549 ))
1550 })
1551 }
1552}
1553impl CsubSigned for usize {
1554 type Other = isize;
1555 type Output = usize;
1556 type Error = Error;
1557 #[inline]
1561 fn csub_signed(self, other: isize) -> Result<usize, Error> {
1562 self.checked_sub_signed(other).ok_or_else(|| {
1563 Error::new(format!(
1564 "failed to compute sub_signed({:?}, {:?}): {} overflow",
1565 self, other, "usize"
1566 ))
1567 })
1568 }
1569}
1570pub trait CsignedDiff: Sized {
1574 #[expect(missing_docs, reason = "no need for doc")]
1575 type Other;
1576 #[expect(missing_docs, reason = "no need for doc")]
1577 type Output;
1578 #[expect(missing_docs, reason = "no need for doc")]
1579 type Error;
1580 fn csigned_diff(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
1584}
1585#[doc(alias = "checked_signed_diff")]
1591#[inline]
1592pub fn csigned_diff<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
1593where
1594 T: CsignedDiff,
1595{
1596 a.csigned_diff(b)
1597}
1598impl CsignedDiff for u8 {
1599 type Other = u8;
1600 type Output = i8;
1601 type Error = Error;
1602 #[inline]
1606 fn csigned_diff(self, other: u8) -> Result<i8, Error> {
1607 self.checked_signed_diff(other).ok_or_else(|| {
1608 Error::new(format!(
1609 "failed to compute signed_diff({:?}, {:?}): {} overflow",
1610 self, other, "i8"
1611 ))
1612 })
1613 }
1614}
1615impl CsignedDiff for u16 {
1616 type Other = u16;
1617 type Output = i16;
1618 type Error = Error;
1619 #[inline]
1623 fn csigned_diff(self, other: u16) -> Result<i16, Error> {
1624 self.checked_signed_diff(other).ok_or_else(|| {
1625 Error::new(format!(
1626 "failed to compute signed_diff({:?}, {:?}): {} overflow",
1627 self, other, "i16"
1628 ))
1629 })
1630 }
1631}
1632impl CsignedDiff for u32 {
1633 type Other = u32;
1634 type Output = i32;
1635 type Error = Error;
1636 #[inline]
1640 fn csigned_diff(self, other: u32) -> Result<i32, Error> {
1641 self.checked_signed_diff(other).ok_or_else(|| {
1642 Error::new(format!(
1643 "failed to compute signed_diff({:?}, {:?}): {} overflow",
1644 self, other, "i32"
1645 ))
1646 })
1647 }
1648}
1649impl CsignedDiff for u64 {
1650 type Other = u64;
1651 type Output = i64;
1652 type Error = Error;
1653 #[inline]
1657 fn csigned_diff(self, other: u64) -> Result<i64, Error> {
1658 self.checked_signed_diff(other).ok_or_else(|| {
1659 Error::new(format!(
1660 "failed to compute signed_diff({:?}, {:?}): {} overflow",
1661 self, other, "i64"
1662 ))
1663 })
1664 }
1665}
1666impl CsignedDiff for u128 {
1667 type Other = u128;
1668 type Output = i128;
1669 type Error = Error;
1670 #[inline]
1674 fn csigned_diff(self, other: u128) -> Result<i128, Error> {
1675 self.checked_signed_diff(other).ok_or_else(|| {
1676 Error::new(format!(
1677 "failed to compute signed_diff({:?}, {:?}): {} overflow",
1678 self, other, "i128"
1679 ))
1680 })
1681 }
1682}
1683impl CsignedDiff for usize {
1684 type Other = usize;
1685 type Output = isize;
1686 type Error = Error;
1687 #[inline]
1691 fn csigned_diff(self, other: usize) -> Result<isize, Error> {
1692 self.checked_signed_diff(other).ok_or_else(|| {
1693 Error::new(format!(
1694 "failed to compute signed_diff({:?}, {:?}): {} overflow",
1695 self, other, "isize"
1696 ))
1697 })
1698 }
1699}
1700pub trait Cneg: Sized {
1704 #[expect(missing_docs, reason = "no need for doc")]
1705 type Output;
1706 #[expect(missing_docs, reason = "no need for doc")]
1707 type Error;
1708 fn cneg(self) -> Result<Self::Output, Self::Error>;
1712}
1713#[doc(alias = "checked_neg")]
1717#[inline]
1718pub fn cneg<T>(value: T) -> Result<T::Output, T::Error>
1719where
1720 T: Cneg,
1721{
1722 Cneg::cneg(value)
1723}
1724impl Cneg for NonZero<i8> {
1725 type Output = NonZero<i8>;
1726 type Error = Error;
1727 #[inline]
1731 fn cneg(self) -> Result<NonZero<i8>, Error> {
1732 self.checked_neg().ok_or_else(|| {
1733 Error::new(format!(
1734 "failed to compute -({:?}): {} overflow",
1735 self, "NonZero<i8>"
1736 ))
1737 })
1738 }
1739}
1740impl Cneg for NonZero<i16> {
1741 type Output = NonZero<i16>;
1742 type Error = Error;
1743 #[inline]
1747 fn cneg(self) -> Result<NonZero<i16>, Error> {
1748 self.checked_neg().ok_or_else(|| {
1749 Error::new(format!(
1750 "failed to compute -({:?}): {} overflow",
1751 self, "NonZero<i16>"
1752 ))
1753 })
1754 }
1755}
1756impl Cneg for NonZero<i32> {
1757 type Output = NonZero<i32>;
1758 type Error = Error;
1759 #[inline]
1763 fn cneg(self) -> Result<NonZero<i32>, Error> {
1764 self.checked_neg().ok_or_else(|| {
1765 Error::new(format!(
1766 "failed to compute -({:?}): {} overflow",
1767 self, "NonZero<i32>"
1768 ))
1769 })
1770 }
1771}
1772impl Cneg for NonZero<i64> {
1773 type Output = NonZero<i64>;
1774 type Error = Error;
1775 #[inline]
1779 fn cneg(self) -> Result<NonZero<i64>, Error> {
1780 self.checked_neg().ok_or_else(|| {
1781 Error::new(format!(
1782 "failed to compute -({:?}): {} overflow",
1783 self, "NonZero<i64>"
1784 ))
1785 })
1786 }
1787}
1788impl Cneg for NonZero<i128> {
1789 type Output = NonZero<i128>;
1790 type Error = Error;
1791 #[inline]
1795 fn cneg(self) -> Result<NonZero<i128>, Error> {
1796 self.checked_neg().ok_or_else(|| {
1797 Error::new(format!(
1798 "failed to compute -({:?}): {} overflow",
1799 self, "NonZero<i128>"
1800 ))
1801 })
1802 }
1803}
1804impl Cneg for NonZero<isize> {
1805 type Output = NonZero<isize>;
1806 type Error = Error;
1807 #[inline]
1811 fn cneg(self) -> Result<NonZero<isize>, Error> {
1812 self.checked_neg().ok_or_else(|| {
1813 Error::new(format!(
1814 "failed to compute -({:?}): {} overflow",
1815 self, "NonZero<isize>"
1816 ))
1817 })
1818 }
1819}
1820impl Cneg for i8 {
1821 type Output = i8;
1822 type Error = Error;
1823 #[inline]
1827 fn cneg(self) -> Result<i8, Error> {
1828 self.checked_neg().ok_or_else(|| {
1829 Error::new(format!(
1830 "failed to compute -({:?}): {} overflow",
1831 self, "i8"
1832 ))
1833 })
1834 }
1835}
1836impl Cneg for i16 {
1837 type Output = i16;
1838 type Error = Error;
1839 #[inline]
1843 fn cneg(self) -> Result<i16, Error> {
1844 self.checked_neg().ok_or_else(|| {
1845 Error::new(format!(
1846 "failed to compute -({:?}): {} overflow",
1847 self, "i16"
1848 ))
1849 })
1850 }
1851}
1852impl Cneg for i32 {
1853 type Output = i32;
1854 type Error = Error;
1855 #[inline]
1859 fn cneg(self) -> Result<i32, Error> {
1860 self.checked_neg().ok_or_else(|| {
1861 Error::new(format!(
1862 "failed to compute -({:?}): {} overflow",
1863 self, "i32"
1864 ))
1865 })
1866 }
1867}
1868impl Cneg for i64 {
1869 type Output = i64;
1870 type Error = Error;
1871 #[inline]
1875 fn cneg(self) -> Result<i64, Error> {
1876 self.checked_neg().ok_or_else(|| {
1877 Error::new(format!(
1878 "failed to compute -({:?}): {} overflow",
1879 self, "i64"
1880 ))
1881 })
1882 }
1883}
1884impl Cneg for i128 {
1885 type Output = i128;
1886 type Error = Error;
1887 #[inline]
1891 fn cneg(self) -> Result<i128, Error> {
1892 self.checked_neg().ok_or_else(|| {
1893 Error::new(format!(
1894 "failed to compute -({:?}): {} overflow",
1895 self, "i128"
1896 ))
1897 })
1898 }
1899}
1900impl Cneg for isize {
1901 type Output = isize;
1902 type Error = Error;
1903 #[inline]
1907 fn cneg(self) -> Result<isize, Error> {
1908 self.checked_neg().ok_or_else(|| {
1909 Error::new(format!(
1910 "failed to compute -({:?}): {} overflow",
1911 self, "isize"
1912 ))
1913 })
1914 }
1915}
1916impl Cneg for u8 {
1917 type Output = u8;
1918 type Error = Error;
1919 #[inline]
1923 fn cneg(self) -> Result<u8, Error> {
1924 self.checked_neg().ok_or_else(|| {
1925 Error::new(format!(
1926 "failed to compute -({:?}): {} overflow",
1927 self, "u8"
1928 ))
1929 })
1930 }
1931}
1932impl Cneg for u16 {
1933 type Output = u16;
1934 type Error = Error;
1935 #[inline]
1939 fn cneg(self) -> Result<u16, Error> {
1940 self.checked_neg().ok_or_else(|| {
1941 Error::new(format!(
1942 "failed to compute -({:?}): {} overflow",
1943 self, "u16"
1944 ))
1945 })
1946 }
1947}
1948impl Cneg for u32 {
1949 type Output = u32;
1950 type Error = Error;
1951 #[inline]
1955 fn cneg(self) -> Result<u32, Error> {
1956 self.checked_neg().ok_or_else(|| {
1957 Error::new(format!(
1958 "failed to compute -({:?}): {} overflow",
1959 self, "u32"
1960 ))
1961 })
1962 }
1963}
1964impl Cneg for u64 {
1965 type Output = u64;
1966 type Error = Error;
1967 #[inline]
1971 fn cneg(self) -> Result<u64, Error> {
1972 self.checked_neg().ok_or_else(|| {
1973 Error::new(format!(
1974 "failed to compute -({:?}): {} overflow",
1975 self, "u64"
1976 ))
1977 })
1978 }
1979}
1980impl Cneg for u128 {
1981 type Output = u128;
1982 type Error = Error;
1983 #[inline]
1987 fn cneg(self) -> Result<u128, Error> {
1988 self.checked_neg().ok_or_else(|| {
1989 Error::new(format!(
1990 "failed to compute -({:?}): {} overflow",
1991 self, "u128"
1992 ))
1993 })
1994 }
1995}
1996impl Cneg for usize {
1997 type Output = usize;
1998 type Error = Error;
1999 #[inline]
2003 fn cneg(self) -> Result<usize, Error> {
2004 self.checked_neg().ok_or_else(|| {
2005 Error::new(format!(
2006 "failed to compute -({:?}): {} overflow",
2007 self, "usize"
2008 ))
2009 })
2010 }
2011}
2012pub trait Cmul: Sized {
2016 #[expect(missing_docs, reason = "no need for doc")]
2017 type Other;
2018 #[expect(missing_docs, reason = "no need for doc")]
2019 type Output;
2020 #[expect(missing_docs, reason = "no need for doc")]
2021 type Error;
2022 fn cmul(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
2026 fn cmul_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
2030}
2031#[doc(alias = "checked_mul")]
2037#[inline]
2038pub fn cmul<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
2039where
2040 T: Cmul,
2041{
2042 a.cmul(b)
2043}
2044impl Cmul for NonZero<u8> {
2045 type Other = NonZero<u8>;
2046 type Output = NonZero<u8>;
2047 type Error = Error;
2048 #[inline]
2052 fn cmul(self, other: NonZero<u8>) -> Result<NonZero<u8>, Error> {
2053 self.checked_mul(other).ok_or_else(|| {
2054 Error::new(format!(
2055 "failed to compute {:?} * {:?}: {} overflow",
2056 self,
2057 MaybeParens(other),
2058 "NonZero<u8>"
2059 ))
2060 })
2061 }
2062 #[inline]
2063 fn cmul_assign(&mut self, other: NonZero<u8>) -> Result<(), Self::Error> {
2067 *self = self.cmul(other)?;
2068 Ok(())
2069 }
2070}
2071impl Cmul for NonZero<u16> {
2072 type Other = NonZero<u16>;
2073 type Output = NonZero<u16>;
2074 type Error = Error;
2075 #[inline]
2079 fn cmul(self, other: NonZero<u16>) -> Result<NonZero<u16>, Error> {
2080 self.checked_mul(other).ok_or_else(|| {
2081 Error::new(format!(
2082 "failed to compute {:?} * {:?}: {} overflow",
2083 self,
2084 MaybeParens(other),
2085 "NonZero<u16>"
2086 ))
2087 })
2088 }
2089 #[inline]
2090 fn cmul_assign(&mut self, other: NonZero<u16>) -> Result<(), Self::Error> {
2094 *self = self.cmul(other)?;
2095 Ok(())
2096 }
2097}
2098impl Cmul for NonZero<u32> {
2099 type Other = NonZero<u32>;
2100 type Output = NonZero<u32>;
2101 type Error = Error;
2102 #[inline]
2106 fn cmul(self, other: NonZero<u32>) -> Result<NonZero<u32>, Error> {
2107 self.checked_mul(other).ok_or_else(|| {
2108 Error::new(format!(
2109 "failed to compute {:?} * {:?}: {} overflow",
2110 self,
2111 MaybeParens(other),
2112 "NonZero<u32>"
2113 ))
2114 })
2115 }
2116 #[inline]
2117 fn cmul_assign(&mut self, other: NonZero<u32>) -> Result<(), Self::Error> {
2121 *self = self.cmul(other)?;
2122 Ok(())
2123 }
2124}
2125impl Cmul for NonZero<u64> {
2126 type Other = NonZero<u64>;
2127 type Output = NonZero<u64>;
2128 type Error = Error;
2129 #[inline]
2133 fn cmul(self, other: NonZero<u64>) -> Result<NonZero<u64>, Error> {
2134 self.checked_mul(other).ok_or_else(|| {
2135 Error::new(format!(
2136 "failed to compute {:?} * {:?}: {} overflow",
2137 self,
2138 MaybeParens(other),
2139 "NonZero<u64>"
2140 ))
2141 })
2142 }
2143 #[inline]
2144 fn cmul_assign(&mut self, other: NonZero<u64>) -> Result<(), Self::Error> {
2148 *self = self.cmul(other)?;
2149 Ok(())
2150 }
2151}
2152impl Cmul for NonZero<u128> {
2153 type Other = NonZero<u128>;
2154 type Output = NonZero<u128>;
2155 type Error = Error;
2156 #[inline]
2160 fn cmul(self, other: NonZero<u128>) -> Result<NonZero<u128>, Error> {
2161 self.checked_mul(other).ok_or_else(|| {
2162 Error::new(format!(
2163 "failed to compute {:?} * {:?}: {} overflow",
2164 self,
2165 MaybeParens(other),
2166 "NonZero<u128>"
2167 ))
2168 })
2169 }
2170 #[inline]
2171 fn cmul_assign(&mut self, other: NonZero<u128>) -> Result<(), Self::Error> {
2175 *self = self.cmul(other)?;
2176 Ok(())
2177 }
2178}
2179impl Cmul for NonZero<usize> {
2180 type Other = NonZero<usize>;
2181 type Output = NonZero<usize>;
2182 type Error = Error;
2183 #[inline]
2187 fn cmul(self, other: NonZero<usize>) -> Result<NonZero<usize>, Error> {
2188 self.checked_mul(other).ok_or_else(|| {
2189 Error::new(format!(
2190 "failed to compute {:?} * {:?}: {} overflow",
2191 self,
2192 MaybeParens(other),
2193 "NonZero<usize>"
2194 ))
2195 })
2196 }
2197 #[inline]
2198 fn cmul_assign(&mut self, other: NonZero<usize>) -> Result<(), Self::Error> {
2202 *self = self.cmul(other)?;
2203 Ok(())
2204 }
2205}
2206impl Cmul for NonZero<i8> {
2207 type Other = NonZero<i8>;
2208 type Output = NonZero<i8>;
2209 type Error = Error;
2210 #[inline]
2214 fn cmul(self, other: NonZero<i8>) -> Result<NonZero<i8>, Error> {
2215 self.checked_mul(other).ok_or_else(|| {
2216 Error::new(format!(
2217 "failed to compute {:?} * {:?}: {} overflow",
2218 self,
2219 MaybeParens(other),
2220 "NonZero<i8>"
2221 ))
2222 })
2223 }
2224 #[inline]
2225 fn cmul_assign(&mut self, other: NonZero<i8>) -> Result<(), Self::Error> {
2229 *self = self.cmul(other)?;
2230 Ok(())
2231 }
2232}
2233impl Cmul for NonZero<i16> {
2234 type Other = NonZero<i16>;
2235 type Output = NonZero<i16>;
2236 type Error = Error;
2237 #[inline]
2241 fn cmul(self, other: NonZero<i16>) -> Result<NonZero<i16>, Error> {
2242 self.checked_mul(other).ok_or_else(|| {
2243 Error::new(format!(
2244 "failed to compute {:?} * {:?}: {} overflow",
2245 self,
2246 MaybeParens(other),
2247 "NonZero<i16>"
2248 ))
2249 })
2250 }
2251 #[inline]
2252 fn cmul_assign(&mut self, other: NonZero<i16>) -> Result<(), Self::Error> {
2256 *self = self.cmul(other)?;
2257 Ok(())
2258 }
2259}
2260impl Cmul for NonZero<i32> {
2261 type Other = NonZero<i32>;
2262 type Output = NonZero<i32>;
2263 type Error = Error;
2264 #[inline]
2268 fn cmul(self, other: NonZero<i32>) -> Result<NonZero<i32>, Error> {
2269 self.checked_mul(other).ok_or_else(|| {
2270 Error::new(format!(
2271 "failed to compute {:?} * {:?}: {} overflow",
2272 self,
2273 MaybeParens(other),
2274 "NonZero<i32>"
2275 ))
2276 })
2277 }
2278 #[inline]
2279 fn cmul_assign(&mut self, other: NonZero<i32>) -> Result<(), Self::Error> {
2283 *self = self.cmul(other)?;
2284 Ok(())
2285 }
2286}
2287impl Cmul for NonZero<i64> {
2288 type Other = NonZero<i64>;
2289 type Output = NonZero<i64>;
2290 type Error = Error;
2291 #[inline]
2295 fn cmul(self, other: NonZero<i64>) -> Result<NonZero<i64>, Error> {
2296 self.checked_mul(other).ok_or_else(|| {
2297 Error::new(format!(
2298 "failed to compute {:?} * {:?}: {} overflow",
2299 self,
2300 MaybeParens(other),
2301 "NonZero<i64>"
2302 ))
2303 })
2304 }
2305 #[inline]
2306 fn cmul_assign(&mut self, other: NonZero<i64>) -> Result<(), Self::Error> {
2310 *self = self.cmul(other)?;
2311 Ok(())
2312 }
2313}
2314impl Cmul for NonZero<i128> {
2315 type Other = NonZero<i128>;
2316 type Output = NonZero<i128>;
2317 type Error = Error;
2318 #[inline]
2322 fn cmul(self, other: NonZero<i128>) -> Result<NonZero<i128>, Error> {
2323 self.checked_mul(other).ok_or_else(|| {
2324 Error::new(format!(
2325 "failed to compute {:?} * {:?}: {} overflow",
2326 self,
2327 MaybeParens(other),
2328 "NonZero<i128>"
2329 ))
2330 })
2331 }
2332 #[inline]
2333 fn cmul_assign(&mut self, other: NonZero<i128>) -> Result<(), Self::Error> {
2337 *self = self.cmul(other)?;
2338 Ok(())
2339 }
2340}
2341impl Cmul for NonZero<isize> {
2342 type Other = NonZero<isize>;
2343 type Output = NonZero<isize>;
2344 type Error = Error;
2345 #[inline]
2349 fn cmul(self, other: NonZero<isize>) -> Result<NonZero<isize>, Error> {
2350 self.checked_mul(other).ok_or_else(|| {
2351 Error::new(format!(
2352 "failed to compute {:?} * {:?}: {} overflow",
2353 self,
2354 MaybeParens(other),
2355 "NonZero<isize>"
2356 ))
2357 })
2358 }
2359 #[inline]
2360 fn cmul_assign(&mut self, other: NonZero<isize>) -> Result<(), Self::Error> {
2364 *self = self.cmul(other)?;
2365 Ok(())
2366 }
2367}
2368impl Cmul for i8 {
2369 type Other = i8;
2370 type Output = i8;
2371 type Error = Error;
2372 #[inline]
2376 fn cmul(self, other: i8) -> Result<i8, Error> {
2377 self.checked_mul(other).ok_or_else(|| {
2378 Error::new(format!(
2379 "failed to compute {:?} * {:?}: {} overflow",
2380 self,
2381 MaybeParens(other),
2382 "i8"
2383 ))
2384 })
2385 }
2386 #[inline]
2387 fn cmul_assign(&mut self, other: i8) -> Result<(), Self::Error> {
2391 *self = self.cmul(other)?;
2392 Ok(())
2393 }
2394}
2395impl Cmul for i16 {
2396 type Other = i16;
2397 type Output = i16;
2398 type Error = Error;
2399 #[inline]
2403 fn cmul(self, other: i16) -> Result<i16, Error> {
2404 self.checked_mul(other).ok_or_else(|| {
2405 Error::new(format!(
2406 "failed to compute {:?} * {:?}: {} overflow",
2407 self,
2408 MaybeParens(other),
2409 "i16"
2410 ))
2411 })
2412 }
2413 #[inline]
2414 fn cmul_assign(&mut self, other: i16) -> Result<(), Self::Error> {
2418 *self = self.cmul(other)?;
2419 Ok(())
2420 }
2421}
2422impl Cmul for i32 {
2423 type Other = i32;
2424 type Output = i32;
2425 type Error = Error;
2426 #[inline]
2430 fn cmul(self, other: i32) -> Result<i32, Error> {
2431 self.checked_mul(other).ok_or_else(|| {
2432 Error::new(format!(
2433 "failed to compute {:?} * {:?}: {} overflow",
2434 self,
2435 MaybeParens(other),
2436 "i32"
2437 ))
2438 })
2439 }
2440 #[inline]
2441 fn cmul_assign(&mut self, other: i32) -> Result<(), Self::Error> {
2445 *self = self.cmul(other)?;
2446 Ok(())
2447 }
2448}
2449impl Cmul for i64 {
2450 type Other = i64;
2451 type Output = i64;
2452 type Error = Error;
2453 #[inline]
2457 fn cmul(self, other: i64) -> Result<i64, Error> {
2458 self.checked_mul(other).ok_or_else(|| {
2459 Error::new(format!(
2460 "failed to compute {:?} * {:?}: {} overflow",
2461 self,
2462 MaybeParens(other),
2463 "i64"
2464 ))
2465 })
2466 }
2467 #[inline]
2468 fn cmul_assign(&mut self, other: i64) -> Result<(), Self::Error> {
2472 *self = self.cmul(other)?;
2473 Ok(())
2474 }
2475}
2476impl Cmul for i128 {
2477 type Other = i128;
2478 type Output = i128;
2479 type Error = Error;
2480 #[inline]
2484 fn cmul(self, other: i128) -> Result<i128, Error> {
2485 self.checked_mul(other).ok_or_else(|| {
2486 Error::new(format!(
2487 "failed to compute {:?} * {:?}: {} overflow",
2488 self,
2489 MaybeParens(other),
2490 "i128"
2491 ))
2492 })
2493 }
2494 #[inline]
2495 fn cmul_assign(&mut self, other: i128) -> Result<(), Self::Error> {
2499 *self = self.cmul(other)?;
2500 Ok(())
2501 }
2502}
2503impl Cmul for isize {
2504 type Other = isize;
2505 type Output = isize;
2506 type Error = Error;
2507 #[inline]
2511 fn cmul(self, other: isize) -> Result<isize, Error> {
2512 self.checked_mul(other).ok_or_else(|| {
2513 Error::new(format!(
2514 "failed to compute {:?} * {:?}: {} overflow",
2515 self,
2516 MaybeParens(other),
2517 "isize"
2518 ))
2519 })
2520 }
2521 #[inline]
2522 fn cmul_assign(&mut self, other: isize) -> Result<(), Self::Error> {
2526 *self = self.cmul(other)?;
2527 Ok(())
2528 }
2529}
2530impl Cmul for u8 {
2531 type Other = u8;
2532 type Output = u8;
2533 type Error = Error;
2534 #[inline]
2538 fn cmul(self, other: u8) -> Result<u8, Error> {
2539 self.checked_mul(other).ok_or_else(|| {
2540 Error::new(format!(
2541 "failed to compute {:?} * {:?}: {} overflow",
2542 self,
2543 MaybeParens(other),
2544 "u8"
2545 ))
2546 })
2547 }
2548 #[inline]
2549 fn cmul_assign(&mut self, other: u8) -> Result<(), Self::Error> {
2553 *self = self.cmul(other)?;
2554 Ok(())
2555 }
2556}
2557impl Cmul for u16 {
2558 type Other = u16;
2559 type Output = u16;
2560 type Error = Error;
2561 #[inline]
2565 fn cmul(self, other: u16) -> Result<u16, Error> {
2566 self.checked_mul(other).ok_or_else(|| {
2567 Error::new(format!(
2568 "failed to compute {:?} * {:?}: {} overflow",
2569 self,
2570 MaybeParens(other),
2571 "u16"
2572 ))
2573 })
2574 }
2575 #[inline]
2576 fn cmul_assign(&mut self, other: u16) -> Result<(), Self::Error> {
2580 *self = self.cmul(other)?;
2581 Ok(())
2582 }
2583}
2584impl Cmul for u32 {
2585 type Other = u32;
2586 type Output = u32;
2587 type Error = Error;
2588 #[inline]
2592 fn cmul(self, other: u32) -> Result<u32, Error> {
2593 self.checked_mul(other).ok_or_else(|| {
2594 Error::new(format!(
2595 "failed to compute {:?} * {:?}: {} overflow",
2596 self,
2597 MaybeParens(other),
2598 "u32"
2599 ))
2600 })
2601 }
2602 #[inline]
2603 fn cmul_assign(&mut self, other: u32) -> Result<(), Self::Error> {
2607 *self = self.cmul(other)?;
2608 Ok(())
2609 }
2610}
2611impl Cmul for u64 {
2612 type Other = u64;
2613 type Output = u64;
2614 type Error = Error;
2615 #[inline]
2619 fn cmul(self, other: u64) -> Result<u64, Error> {
2620 self.checked_mul(other).ok_or_else(|| {
2621 Error::new(format!(
2622 "failed to compute {:?} * {:?}: {} overflow",
2623 self,
2624 MaybeParens(other),
2625 "u64"
2626 ))
2627 })
2628 }
2629 #[inline]
2630 fn cmul_assign(&mut self, other: u64) -> Result<(), Self::Error> {
2634 *self = self.cmul(other)?;
2635 Ok(())
2636 }
2637}
2638impl Cmul for u128 {
2639 type Other = u128;
2640 type Output = u128;
2641 type Error = Error;
2642 #[inline]
2646 fn cmul(self, other: u128) -> Result<u128, Error> {
2647 self.checked_mul(other).ok_or_else(|| {
2648 Error::new(format!(
2649 "failed to compute {:?} * {:?}: {} overflow",
2650 self,
2651 MaybeParens(other),
2652 "u128"
2653 ))
2654 })
2655 }
2656 #[inline]
2657 fn cmul_assign(&mut self, other: u128) -> Result<(), Self::Error> {
2661 *self = self.cmul(other)?;
2662 Ok(())
2663 }
2664}
2665impl Cmul for usize {
2666 type Other = usize;
2667 type Output = usize;
2668 type Error = Error;
2669 #[inline]
2673 fn cmul(self, other: usize) -> Result<usize, Error> {
2674 self.checked_mul(other).ok_or_else(|| {
2675 Error::new(format!(
2676 "failed to compute {:?} * {:?}: {} overflow",
2677 self,
2678 MaybeParens(other),
2679 "usize"
2680 ))
2681 })
2682 }
2683 #[inline]
2684 fn cmul_assign(&mut self, other: usize) -> Result<(), Self::Error> {
2688 *self = self.cmul(other)?;
2689 Ok(())
2690 }
2691}
2692impl Cmul for Duration {
2693 type Other = u32;
2694 type Output = Duration;
2695 type Error = Error;
2696 #[inline]
2700 fn cmul(self, other: u32) -> Result<Duration, Error> {
2701 self.checked_mul(other).ok_or_else(|| {
2702 Error::new(format!(
2703 "failed to compute {:?} * {:?}: {} overflow",
2704 self,
2705 MaybeParens(other),
2706 "Duration"
2707 ))
2708 })
2709 }
2710 #[inline]
2711 fn cmul_assign(&mut self, other: u32) -> Result<(), Self::Error> {
2715 *self = self.cmul(other)?;
2716 Ok(())
2717 }
2718}
2719pub trait Cdiv: Sized {
2723 #[expect(missing_docs, reason = "no need for doc")]
2724 type Divisor;
2725 #[expect(missing_docs, reason = "no need for doc")]
2726 type Output;
2727 #[expect(missing_docs, reason = "no need for doc")]
2728 type Error;
2729 fn cdiv(self, divisor: Self::Divisor) -> Result<Self::Output, Self::Error>;
2733 fn cdiv_assign(&mut self, divisor: Self::Divisor) -> Result<(), Self::Error>;
2737}
2738#[doc(alias = "checked_div")]
2744#[inline]
2745pub fn cdiv<T>(value: T, divisor: T::Divisor) -> Result<T::Output, T::Error>
2746where
2747 T: Cdiv,
2748{
2749 value.cdiv(divisor)
2750}
2751impl Cdiv for i8 {
2752 type Divisor = i8;
2753 type Output = i8;
2754 type Error = Error;
2755 #[inline]
2759 fn cdiv(self, divisor: i8) -> Result<i8, Error> {
2760 self.checked_div(divisor).ok_or_else(|| {
2761 Error::new({
2762 if divisor == 0 {
2763 format!(
2764 "failed to compute {:?} / {:?}: division by zero",
2765 self,
2766 MaybeParens(divisor)
2767 )
2768 } else {
2769 format!(
2770 "failed to compute {:?} / {:?}: {} overflow",
2771 self,
2772 MaybeParens(divisor),
2773 "i8"
2774 )
2775 }
2776 })
2777 })
2778 }
2779 #[inline]
2780 fn cdiv_assign(&mut self, divisor: i8) -> Result<(), Self::Error> {
2784 *self = self.cdiv(divisor)?;
2785 Ok(())
2786 }
2787}
2788impl Cdiv for i16 {
2789 type Divisor = i16;
2790 type Output = i16;
2791 type Error = Error;
2792 #[inline]
2796 fn cdiv(self, divisor: i16) -> Result<i16, Error> {
2797 self.checked_div(divisor).ok_or_else(|| {
2798 Error::new({
2799 if divisor == 0 {
2800 format!(
2801 "failed to compute {:?} / {:?}: division by zero",
2802 self,
2803 MaybeParens(divisor)
2804 )
2805 } else {
2806 format!(
2807 "failed to compute {:?} / {:?}: {} overflow",
2808 self,
2809 MaybeParens(divisor),
2810 "i16"
2811 )
2812 }
2813 })
2814 })
2815 }
2816 #[inline]
2817 fn cdiv_assign(&mut self, divisor: i16) -> Result<(), Self::Error> {
2821 *self = self.cdiv(divisor)?;
2822 Ok(())
2823 }
2824}
2825impl Cdiv for i32 {
2826 type Divisor = i32;
2827 type Output = i32;
2828 type Error = Error;
2829 #[inline]
2833 fn cdiv(self, divisor: i32) -> Result<i32, Error> {
2834 self.checked_div(divisor).ok_or_else(|| {
2835 Error::new({
2836 if divisor == 0 {
2837 format!(
2838 "failed to compute {:?} / {:?}: division by zero",
2839 self,
2840 MaybeParens(divisor)
2841 )
2842 } else {
2843 format!(
2844 "failed to compute {:?} / {:?}: {} overflow",
2845 self,
2846 MaybeParens(divisor),
2847 "i32"
2848 )
2849 }
2850 })
2851 })
2852 }
2853 #[inline]
2854 fn cdiv_assign(&mut self, divisor: i32) -> Result<(), Self::Error> {
2858 *self = self.cdiv(divisor)?;
2859 Ok(())
2860 }
2861}
2862impl Cdiv for i64 {
2863 type Divisor = i64;
2864 type Output = i64;
2865 type Error = Error;
2866 #[inline]
2870 fn cdiv(self, divisor: i64) -> Result<i64, Error> {
2871 self.checked_div(divisor).ok_or_else(|| {
2872 Error::new({
2873 if divisor == 0 {
2874 format!(
2875 "failed to compute {:?} / {:?}: division by zero",
2876 self,
2877 MaybeParens(divisor)
2878 )
2879 } else {
2880 format!(
2881 "failed to compute {:?} / {:?}: {} overflow",
2882 self,
2883 MaybeParens(divisor),
2884 "i64"
2885 )
2886 }
2887 })
2888 })
2889 }
2890 #[inline]
2891 fn cdiv_assign(&mut self, divisor: i64) -> Result<(), Self::Error> {
2895 *self = self.cdiv(divisor)?;
2896 Ok(())
2897 }
2898}
2899impl Cdiv for i128 {
2900 type Divisor = i128;
2901 type Output = i128;
2902 type Error = Error;
2903 #[inline]
2907 fn cdiv(self, divisor: i128) -> Result<i128, Error> {
2908 self.checked_div(divisor).ok_or_else(|| {
2909 Error::new({
2910 if divisor == 0 {
2911 format!(
2912 "failed to compute {:?} / {:?}: division by zero",
2913 self,
2914 MaybeParens(divisor)
2915 )
2916 } else {
2917 format!(
2918 "failed to compute {:?} / {:?}: {} overflow",
2919 self,
2920 MaybeParens(divisor),
2921 "i128"
2922 )
2923 }
2924 })
2925 })
2926 }
2927 #[inline]
2928 fn cdiv_assign(&mut self, divisor: i128) -> Result<(), Self::Error> {
2932 *self = self.cdiv(divisor)?;
2933 Ok(())
2934 }
2935}
2936impl Cdiv for isize {
2937 type Divisor = isize;
2938 type Output = isize;
2939 type Error = Error;
2940 #[inline]
2944 fn cdiv(self, divisor: isize) -> Result<isize, Error> {
2945 self.checked_div(divisor).ok_or_else(|| {
2946 Error::new({
2947 if divisor == 0 {
2948 format!(
2949 "failed to compute {:?} / {:?}: division by zero",
2950 self,
2951 MaybeParens(divisor)
2952 )
2953 } else {
2954 format!(
2955 "failed to compute {:?} / {:?}: {} overflow",
2956 self,
2957 MaybeParens(divisor),
2958 "isize"
2959 )
2960 }
2961 })
2962 })
2963 }
2964 #[inline]
2965 fn cdiv_assign(&mut self, divisor: isize) -> Result<(), Self::Error> {
2969 *self = self.cdiv(divisor)?;
2970 Ok(())
2971 }
2972}
2973impl Cdiv for u8 {
2974 type Divisor = u8;
2975 type Output = u8;
2976 type Error = Error;
2977 #[inline]
2981 fn cdiv(self, divisor: u8) -> Result<u8, Error> {
2982 self.checked_div(divisor).ok_or_else(|| {
2983 Error::new({
2984 if divisor == 0 {
2985 format!(
2986 "failed to compute {:?} / {:?}: division by zero",
2987 self,
2988 MaybeParens(divisor)
2989 )
2990 } else {
2991 format!(
2992 "failed to compute {:?} / {:?}: {} overflow",
2993 self,
2994 MaybeParens(divisor),
2995 "u8"
2996 )
2997 }
2998 })
2999 })
3000 }
3001 #[inline]
3002 fn cdiv_assign(&mut self, divisor: u8) -> Result<(), Self::Error> {
3006 *self = self.cdiv(divisor)?;
3007 Ok(())
3008 }
3009}
3010impl Cdiv for u16 {
3011 type Divisor = u16;
3012 type Output = u16;
3013 type Error = Error;
3014 #[inline]
3018 fn cdiv(self, divisor: u16) -> Result<u16, Error> {
3019 self.checked_div(divisor).ok_or_else(|| {
3020 Error::new({
3021 if divisor == 0 {
3022 format!(
3023 "failed to compute {:?} / {:?}: division by zero",
3024 self,
3025 MaybeParens(divisor)
3026 )
3027 } else {
3028 format!(
3029 "failed to compute {:?} / {:?}: {} overflow",
3030 self,
3031 MaybeParens(divisor),
3032 "u16"
3033 )
3034 }
3035 })
3036 })
3037 }
3038 #[inline]
3039 fn cdiv_assign(&mut self, divisor: u16) -> Result<(), Self::Error> {
3043 *self = self.cdiv(divisor)?;
3044 Ok(())
3045 }
3046}
3047impl Cdiv for u32 {
3048 type Divisor = u32;
3049 type Output = u32;
3050 type Error = Error;
3051 #[inline]
3055 fn cdiv(self, divisor: u32) -> Result<u32, Error> {
3056 self.checked_div(divisor).ok_or_else(|| {
3057 Error::new({
3058 if divisor == 0 {
3059 format!(
3060 "failed to compute {:?} / {:?}: division by zero",
3061 self,
3062 MaybeParens(divisor)
3063 )
3064 } else {
3065 format!(
3066 "failed to compute {:?} / {:?}: {} overflow",
3067 self,
3068 MaybeParens(divisor),
3069 "u32"
3070 )
3071 }
3072 })
3073 })
3074 }
3075 #[inline]
3076 fn cdiv_assign(&mut self, divisor: u32) -> Result<(), Self::Error> {
3080 *self = self.cdiv(divisor)?;
3081 Ok(())
3082 }
3083}
3084impl Cdiv for u64 {
3085 type Divisor = u64;
3086 type Output = u64;
3087 type Error = Error;
3088 #[inline]
3092 fn cdiv(self, divisor: u64) -> Result<u64, Error> {
3093 self.checked_div(divisor).ok_or_else(|| {
3094 Error::new({
3095 if divisor == 0 {
3096 format!(
3097 "failed to compute {:?} / {:?}: division by zero",
3098 self,
3099 MaybeParens(divisor)
3100 )
3101 } else {
3102 format!(
3103 "failed to compute {:?} / {:?}: {} overflow",
3104 self,
3105 MaybeParens(divisor),
3106 "u64"
3107 )
3108 }
3109 })
3110 })
3111 }
3112 #[inline]
3113 fn cdiv_assign(&mut self, divisor: u64) -> Result<(), Self::Error> {
3117 *self = self.cdiv(divisor)?;
3118 Ok(())
3119 }
3120}
3121impl Cdiv for u128 {
3122 type Divisor = u128;
3123 type Output = u128;
3124 type Error = Error;
3125 #[inline]
3129 fn cdiv(self, divisor: u128) -> Result<u128, Error> {
3130 self.checked_div(divisor).ok_or_else(|| {
3131 Error::new({
3132 if divisor == 0 {
3133 format!(
3134 "failed to compute {:?} / {:?}: division by zero",
3135 self,
3136 MaybeParens(divisor)
3137 )
3138 } else {
3139 format!(
3140 "failed to compute {:?} / {:?}: {} overflow",
3141 self,
3142 MaybeParens(divisor),
3143 "u128"
3144 )
3145 }
3146 })
3147 })
3148 }
3149 #[inline]
3150 fn cdiv_assign(&mut self, divisor: u128) -> Result<(), Self::Error> {
3154 *self = self.cdiv(divisor)?;
3155 Ok(())
3156 }
3157}
3158impl Cdiv for usize {
3159 type Divisor = usize;
3160 type Output = usize;
3161 type Error = Error;
3162 #[inline]
3166 fn cdiv(self, divisor: usize) -> Result<usize, Error> {
3167 self.checked_div(divisor).ok_or_else(|| {
3168 Error::new({
3169 if divisor == 0 {
3170 format!(
3171 "failed to compute {:?} / {:?}: division by zero",
3172 self,
3173 MaybeParens(divisor)
3174 )
3175 } else {
3176 format!(
3177 "failed to compute {:?} / {:?}: {} overflow",
3178 self,
3179 MaybeParens(divisor),
3180 "usize"
3181 )
3182 }
3183 })
3184 })
3185 }
3186 #[inline]
3187 fn cdiv_assign(&mut self, divisor: usize) -> Result<(), Self::Error> {
3191 *self = self.cdiv(divisor)?;
3192 Ok(())
3193 }
3194}
3195impl Cdiv for Duration {
3196 type Divisor = u32;
3197 type Output = Duration;
3198 type Error = Error;
3199 #[inline]
3203 fn cdiv(self, divisor: u32) -> Result<Duration, Error> {
3204 self.checked_div(divisor).ok_or_else(|| {
3205 Error::new({
3206 if divisor == 0 {
3207 format!(
3208 "failed to compute {:?} / {:?}: division by zero",
3209 self,
3210 MaybeParens(divisor)
3211 )
3212 } else {
3213 format!(
3214 "failed to compute {:?} / {:?}: {} overflow",
3215 self,
3216 MaybeParens(divisor),
3217 "Duration"
3218 )
3219 }
3220 })
3221 })
3222 }
3223 #[inline]
3224 fn cdiv_assign(&mut self, divisor: u32) -> Result<(), Self::Error> {
3228 *self = self.cdiv(divisor)?;
3229 Ok(())
3230 }
3231}
3232pub trait CdivEuclid: Sized {
3236 #[expect(missing_docs, reason = "no need for doc")]
3237 type Divisor;
3238 #[expect(missing_docs, reason = "no need for doc")]
3239 type Output;
3240 #[expect(missing_docs, reason = "no need for doc")]
3241 type Error;
3242 fn cdiv_euclid(self, divisor: Self::Divisor) -> Result<Self::Output, Self::Error>;
3246}
3247#[doc(alias = "checked_div_euclid")]
3253#[inline]
3254pub fn cdiv_euclid<T>(value: T, divisor: T::Divisor) -> Result<T::Output, T::Error>
3255where
3256 T: CdivEuclid,
3257{
3258 value.cdiv_euclid(divisor)
3259}
3260impl CdivEuclid for i8 {
3261 type Divisor = i8;
3262 type Output = i8;
3263 type Error = Error;
3264 #[inline]
3268 fn cdiv_euclid(self, divisor: i8) -> Result<i8, Error> {
3269 self.checked_div_euclid(divisor).ok_or_else(|| {
3270 Error::new({
3271 if divisor == 0 {
3272 format!(
3273 "failed to compute div_euclid({:?}, {:?}): division by zero",
3274 self, divisor
3275 )
3276 } else {
3277 format!(
3278 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3279 self, divisor, "i8"
3280 )
3281 }
3282 })
3283 })
3284 }
3285}
3286impl CdivEuclid for i16 {
3287 type Divisor = i16;
3288 type Output = i16;
3289 type Error = Error;
3290 #[inline]
3294 fn cdiv_euclid(self, divisor: i16) -> Result<i16, Error> {
3295 self.checked_div_euclid(divisor).ok_or_else(|| {
3296 Error::new({
3297 if divisor == 0 {
3298 format!(
3299 "failed to compute div_euclid({:?}, {:?}): division by zero",
3300 self, divisor
3301 )
3302 } else {
3303 format!(
3304 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3305 self, divisor, "i16"
3306 )
3307 }
3308 })
3309 })
3310 }
3311}
3312impl CdivEuclid for i32 {
3313 type Divisor = i32;
3314 type Output = i32;
3315 type Error = Error;
3316 #[inline]
3320 fn cdiv_euclid(self, divisor: i32) -> Result<i32, Error> {
3321 self.checked_div_euclid(divisor).ok_or_else(|| {
3322 Error::new({
3323 if divisor == 0 {
3324 format!(
3325 "failed to compute div_euclid({:?}, {:?}): division by zero",
3326 self, divisor
3327 )
3328 } else {
3329 format!(
3330 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3331 self, divisor, "i32"
3332 )
3333 }
3334 })
3335 })
3336 }
3337}
3338impl CdivEuclid for i64 {
3339 type Divisor = i64;
3340 type Output = i64;
3341 type Error = Error;
3342 #[inline]
3346 fn cdiv_euclid(self, divisor: i64) -> Result<i64, Error> {
3347 self.checked_div_euclid(divisor).ok_or_else(|| {
3348 Error::new({
3349 if divisor == 0 {
3350 format!(
3351 "failed to compute div_euclid({:?}, {:?}): division by zero",
3352 self, divisor
3353 )
3354 } else {
3355 format!(
3356 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3357 self, divisor, "i64"
3358 )
3359 }
3360 })
3361 })
3362 }
3363}
3364impl CdivEuclid for i128 {
3365 type Divisor = i128;
3366 type Output = i128;
3367 type Error = Error;
3368 #[inline]
3372 fn cdiv_euclid(self, divisor: i128) -> Result<i128, Error> {
3373 self.checked_div_euclid(divisor).ok_or_else(|| {
3374 Error::new({
3375 if divisor == 0 {
3376 format!(
3377 "failed to compute div_euclid({:?}, {:?}): division by zero",
3378 self, divisor
3379 )
3380 } else {
3381 format!(
3382 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3383 self, divisor, "i128"
3384 )
3385 }
3386 })
3387 })
3388 }
3389}
3390impl CdivEuclid for isize {
3391 type Divisor = isize;
3392 type Output = isize;
3393 type Error = Error;
3394 #[inline]
3398 fn cdiv_euclid(self, divisor: isize) -> Result<isize, Error> {
3399 self.checked_div_euclid(divisor).ok_or_else(|| {
3400 Error::new({
3401 if divisor == 0 {
3402 format!(
3403 "failed to compute div_euclid({:?}, {:?}): division by zero",
3404 self, divisor
3405 )
3406 } else {
3407 format!(
3408 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3409 self, divisor, "isize"
3410 )
3411 }
3412 })
3413 })
3414 }
3415}
3416impl CdivEuclid for u8 {
3417 type Divisor = u8;
3418 type Output = u8;
3419 type Error = Error;
3420 #[inline]
3424 fn cdiv_euclid(self, divisor: u8) -> Result<u8, Error> {
3425 self.checked_div_euclid(divisor).ok_or_else(|| {
3426 Error::new({
3427 if divisor == 0 {
3428 format!(
3429 "failed to compute div_euclid({:?}, {:?}): division by zero",
3430 self, divisor
3431 )
3432 } else {
3433 format!(
3434 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3435 self, divisor, "u8"
3436 )
3437 }
3438 })
3439 })
3440 }
3441}
3442impl CdivEuclid for u16 {
3443 type Divisor = u16;
3444 type Output = u16;
3445 type Error = Error;
3446 #[inline]
3450 fn cdiv_euclid(self, divisor: u16) -> Result<u16, Error> {
3451 self.checked_div_euclid(divisor).ok_or_else(|| {
3452 Error::new({
3453 if divisor == 0 {
3454 format!(
3455 "failed to compute div_euclid({:?}, {:?}): division by zero",
3456 self, divisor
3457 )
3458 } else {
3459 format!(
3460 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3461 self, divisor, "u16"
3462 )
3463 }
3464 })
3465 })
3466 }
3467}
3468impl CdivEuclid for u32 {
3469 type Divisor = u32;
3470 type Output = u32;
3471 type Error = Error;
3472 #[inline]
3476 fn cdiv_euclid(self, divisor: u32) -> Result<u32, Error> {
3477 self.checked_div_euclid(divisor).ok_or_else(|| {
3478 Error::new({
3479 if divisor == 0 {
3480 format!(
3481 "failed to compute div_euclid({:?}, {:?}): division by zero",
3482 self, divisor
3483 )
3484 } else {
3485 format!(
3486 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3487 self, divisor, "u32"
3488 )
3489 }
3490 })
3491 })
3492 }
3493}
3494impl CdivEuclid for u64 {
3495 type Divisor = u64;
3496 type Output = u64;
3497 type Error = Error;
3498 #[inline]
3502 fn cdiv_euclid(self, divisor: u64) -> Result<u64, Error> {
3503 self.checked_div_euclid(divisor).ok_or_else(|| {
3504 Error::new({
3505 if divisor == 0 {
3506 format!(
3507 "failed to compute div_euclid({:?}, {:?}): division by zero",
3508 self, divisor
3509 )
3510 } else {
3511 format!(
3512 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3513 self, divisor, "u64"
3514 )
3515 }
3516 })
3517 })
3518 }
3519}
3520impl CdivEuclid for u128 {
3521 type Divisor = u128;
3522 type Output = u128;
3523 type Error = Error;
3524 #[inline]
3528 fn cdiv_euclid(self, divisor: u128) -> Result<u128, Error> {
3529 self.checked_div_euclid(divisor).ok_or_else(|| {
3530 Error::new({
3531 if divisor == 0 {
3532 format!(
3533 "failed to compute div_euclid({:?}, {:?}): division by zero",
3534 self, divisor
3535 )
3536 } else {
3537 format!(
3538 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3539 self, divisor, "u128"
3540 )
3541 }
3542 })
3543 })
3544 }
3545}
3546impl CdivEuclid for usize {
3547 type Divisor = usize;
3548 type Output = usize;
3549 type Error = Error;
3550 #[inline]
3554 fn cdiv_euclid(self, divisor: usize) -> Result<usize, Error> {
3555 self.checked_div_euclid(divisor).ok_or_else(|| {
3556 Error::new({
3557 if divisor == 0 {
3558 format!(
3559 "failed to compute div_euclid({:?}, {:?}): division by zero",
3560 self, divisor
3561 )
3562 } else {
3563 format!(
3564 "failed to compute div_euclid({:?}, {:?}): {} overflow",
3565 self, divisor, "usize"
3566 )
3567 }
3568 })
3569 })
3570 }
3571}
3572pub trait Crem: Sized {
3576 #[expect(missing_docs, reason = "no need for doc")]
3577 type Divisor;
3578 #[expect(missing_docs, reason = "no need for doc")]
3579 type Output;
3580 #[expect(missing_docs, reason = "no need for doc")]
3581 type Error;
3582 fn crem(self, divisor: Self::Divisor) -> Result<Self::Output, Self::Error>;
3586 fn crem_assign(&mut self, divisor: Self::Divisor) -> Result<(), Self::Error>;
3590}
3591#[doc(alias = "checked_rem")]
3597#[inline]
3598pub fn crem<T>(value: T, divisor: T::Divisor) -> Result<T::Output, T::Error>
3599where
3600 T: Crem,
3601{
3602 value.crem(divisor)
3603}
3604impl Crem for i8 {
3605 type Divisor = i8;
3606 type Output = i8;
3607 type Error = Error;
3608 #[inline]
3612 fn crem(self, divisor: i8) -> Result<i8, Error> {
3613 self.checked_rem(divisor).ok_or_else(|| {
3614 Error::new({
3615 if divisor == 0 {
3616 format!(
3617 "failed to compute {:?} % {:?}: division by zero",
3618 self,
3619 MaybeParens(divisor)
3620 )
3621 } else {
3622 format!(
3623 "failed to compute {:?} % {:?}: {} overflow",
3624 self,
3625 MaybeParens(divisor),
3626 "i8"
3627 )
3628 }
3629 })
3630 })
3631 }
3632 #[inline]
3633 fn crem_assign(&mut self, divisor: i8) -> Result<(), Self::Error> {
3637 *self = self.crem(divisor)?;
3638 Ok(())
3639 }
3640}
3641impl Crem for i16 {
3642 type Divisor = i16;
3643 type Output = i16;
3644 type Error = Error;
3645 #[inline]
3649 fn crem(self, divisor: i16) -> Result<i16, Error> {
3650 self.checked_rem(divisor).ok_or_else(|| {
3651 Error::new({
3652 if divisor == 0 {
3653 format!(
3654 "failed to compute {:?} % {:?}: division by zero",
3655 self,
3656 MaybeParens(divisor)
3657 )
3658 } else {
3659 format!(
3660 "failed to compute {:?} % {:?}: {} overflow",
3661 self,
3662 MaybeParens(divisor),
3663 "i16"
3664 )
3665 }
3666 })
3667 })
3668 }
3669 #[inline]
3670 fn crem_assign(&mut self, divisor: i16) -> Result<(), Self::Error> {
3674 *self = self.crem(divisor)?;
3675 Ok(())
3676 }
3677}
3678impl Crem for i32 {
3679 type Divisor = i32;
3680 type Output = i32;
3681 type Error = Error;
3682 #[inline]
3686 fn crem(self, divisor: i32) -> Result<i32, Error> {
3687 self.checked_rem(divisor).ok_or_else(|| {
3688 Error::new({
3689 if divisor == 0 {
3690 format!(
3691 "failed to compute {:?} % {:?}: division by zero",
3692 self,
3693 MaybeParens(divisor)
3694 )
3695 } else {
3696 format!(
3697 "failed to compute {:?} % {:?}: {} overflow",
3698 self,
3699 MaybeParens(divisor),
3700 "i32"
3701 )
3702 }
3703 })
3704 })
3705 }
3706 #[inline]
3707 fn crem_assign(&mut self, divisor: i32) -> Result<(), Self::Error> {
3711 *self = self.crem(divisor)?;
3712 Ok(())
3713 }
3714}
3715impl Crem for i64 {
3716 type Divisor = i64;
3717 type Output = i64;
3718 type Error = Error;
3719 #[inline]
3723 fn crem(self, divisor: i64) -> Result<i64, Error> {
3724 self.checked_rem(divisor).ok_or_else(|| {
3725 Error::new({
3726 if divisor == 0 {
3727 format!(
3728 "failed to compute {:?} % {:?}: division by zero",
3729 self,
3730 MaybeParens(divisor)
3731 )
3732 } else {
3733 format!(
3734 "failed to compute {:?} % {:?}: {} overflow",
3735 self,
3736 MaybeParens(divisor),
3737 "i64"
3738 )
3739 }
3740 })
3741 })
3742 }
3743 #[inline]
3744 fn crem_assign(&mut self, divisor: i64) -> Result<(), Self::Error> {
3748 *self = self.crem(divisor)?;
3749 Ok(())
3750 }
3751}
3752impl Crem for i128 {
3753 type Divisor = i128;
3754 type Output = i128;
3755 type Error = Error;
3756 #[inline]
3760 fn crem(self, divisor: i128) -> Result<i128, Error> {
3761 self.checked_rem(divisor).ok_or_else(|| {
3762 Error::new({
3763 if divisor == 0 {
3764 format!(
3765 "failed to compute {:?} % {:?}: division by zero",
3766 self,
3767 MaybeParens(divisor)
3768 )
3769 } else {
3770 format!(
3771 "failed to compute {:?} % {:?}: {} overflow",
3772 self,
3773 MaybeParens(divisor),
3774 "i128"
3775 )
3776 }
3777 })
3778 })
3779 }
3780 #[inline]
3781 fn crem_assign(&mut self, divisor: i128) -> Result<(), Self::Error> {
3785 *self = self.crem(divisor)?;
3786 Ok(())
3787 }
3788}
3789impl Crem for isize {
3790 type Divisor = isize;
3791 type Output = isize;
3792 type Error = Error;
3793 #[inline]
3797 fn crem(self, divisor: isize) -> Result<isize, Error> {
3798 self.checked_rem(divisor).ok_or_else(|| {
3799 Error::new({
3800 if divisor == 0 {
3801 format!(
3802 "failed to compute {:?} % {:?}: division by zero",
3803 self,
3804 MaybeParens(divisor)
3805 )
3806 } else {
3807 format!(
3808 "failed to compute {:?} % {:?}: {} overflow",
3809 self,
3810 MaybeParens(divisor),
3811 "isize"
3812 )
3813 }
3814 })
3815 })
3816 }
3817 #[inline]
3818 fn crem_assign(&mut self, divisor: isize) -> Result<(), Self::Error> {
3822 *self = self.crem(divisor)?;
3823 Ok(())
3824 }
3825}
3826impl Crem for u8 {
3827 type Divisor = u8;
3828 type Output = u8;
3829 type Error = Error;
3830 #[inline]
3834 fn crem(self, divisor: u8) -> Result<u8, Error> {
3835 self.checked_rem(divisor).ok_or_else(|| {
3836 Error::new({
3837 if divisor == 0 {
3838 format!(
3839 "failed to compute {:?} % {:?}: division by zero",
3840 self,
3841 MaybeParens(divisor)
3842 )
3843 } else {
3844 format!(
3845 "failed to compute {:?} % {:?}: {} overflow",
3846 self,
3847 MaybeParens(divisor),
3848 "u8"
3849 )
3850 }
3851 })
3852 })
3853 }
3854 #[inline]
3855 fn crem_assign(&mut self, divisor: u8) -> Result<(), Self::Error> {
3859 *self = self.crem(divisor)?;
3860 Ok(())
3861 }
3862}
3863impl Crem for u16 {
3864 type Divisor = u16;
3865 type Output = u16;
3866 type Error = Error;
3867 #[inline]
3871 fn crem(self, divisor: u16) -> Result<u16, Error> {
3872 self.checked_rem(divisor).ok_or_else(|| {
3873 Error::new({
3874 if divisor == 0 {
3875 format!(
3876 "failed to compute {:?} % {:?}: division by zero",
3877 self,
3878 MaybeParens(divisor)
3879 )
3880 } else {
3881 format!(
3882 "failed to compute {:?} % {:?}: {} overflow",
3883 self,
3884 MaybeParens(divisor),
3885 "u16"
3886 )
3887 }
3888 })
3889 })
3890 }
3891 #[inline]
3892 fn crem_assign(&mut self, divisor: u16) -> Result<(), Self::Error> {
3896 *self = self.crem(divisor)?;
3897 Ok(())
3898 }
3899}
3900impl Crem for u32 {
3901 type Divisor = u32;
3902 type Output = u32;
3903 type Error = Error;
3904 #[inline]
3908 fn crem(self, divisor: u32) -> Result<u32, Error> {
3909 self.checked_rem(divisor).ok_or_else(|| {
3910 Error::new({
3911 if divisor == 0 {
3912 format!(
3913 "failed to compute {:?} % {:?}: division by zero",
3914 self,
3915 MaybeParens(divisor)
3916 )
3917 } else {
3918 format!(
3919 "failed to compute {:?} % {:?}: {} overflow",
3920 self,
3921 MaybeParens(divisor),
3922 "u32"
3923 )
3924 }
3925 })
3926 })
3927 }
3928 #[inline]
3929 fn crem_assign(&mut self, divisor: u32) -> Result<(), Self::Error> {
3933 *self = self.crem(divisor)?;
3934 Ok(())
3935 }
3936}
3937impl Crem for u64 {
3938 type Divisor = u64;
3939 type Output = u64;
3940 type Error = Error;
3941 #[inline]
3945 fn crem(self, divisor: u64) -> Result<u64, Error> {
3946 self.checked_rem(divisor).ok_or_else(|| {
3947 Error::new({
3948 if divisor == 0 {
3949 format!(
3950 "failed to compute {:?} % {:?}: division by zero",
3951 self,
3952 MaybeParens(divisor)
3953 )
3954 } else {
3955 format!(
3956 "failed to compute {:?} % {:?}: {} overflow",
3957 self,
3958 MaybeParens(divisor),
3959 "u64"
3960 )
3961 }
3962 })
3963 })
3964 }
3965 #[inline]
3966 fn crem_assign(&mut self, divisor: u64) -> Result<(), Self::Error> {
3970 *self = self.crem(divisor)?;
3971 Ok(())
3972 }
3973}
3974impl Crem for u128 {
3975 type Divisor = u128;
3976 type Output = u128;
3977 type Error = Error;
3978 #[inline]
3982 fn crem(self, divisor: u128) -> Result<u128, Error> {
3983 self.checked_rem(divisor).ok_or_else(|| {
3984 Error::new({
3985 if divisor == 0 {
3986 format!(
3987 "failed to compute {:?} % {:?}: division by zero",
3988 self,
3989 MaybeParens(divisor)
3990 )
3991 } else {
3992 format!(
3993 "failed to compute {:?} % {:?}: {} overflow",
3994 self,
3995 MaybeParens(divisor),
3996 "u128"
3997 )
3998 }
3999 })
4000 })
4001 }
4002 #[inline]
4003 fn crem_assign(&mut self, divisor: u128) -> Result<(), Self::Error> {
4007 *self = self.crem(divisor)?;
4008 Ok(())
4009 }
4010}
4011impl Crem for usize {
4012 type Divisor = usize;
4013 type Output = usize;
4014 type Error = Error;
4015 #[inline]
4019 fn crem(self, divisor: usize) -> Result<usize, Error> {
4020 self.checked_rem(divisor).ok_or_else(|| {
4021 Error::new({
4022 if divisor == 0 {
4023 format!(
4024 "failed to compute {:?} % {:?}: division by zero",
4025 self,
4026 MaybeParens(divisor)
4027 )
4028 } else {
4029 format!(
4030 "failed to compute {:?} % {:?}: {} overflow",
4031 self,
4032 MaybeParens(divisor),
4033 "usize"
4034 )
4035 }
4036 })
4037 })
4038 }
4039 #[inline]
4040 fn crem_assign(&mut self, divisor: usize) -> Result<(), Self::Error> {
4044 *self = self.crem(divisor)?;
4045 Ok(())
4046 }
4047}
4048pub trait CremEuclid: Sized {
4052 #[expect(missing_docs, reason = "no need for doc")]
4053 type Divisor;
4054 #[expect(missing_docs, reason = "no need for doc")]
4055 type Output;
4056 #[expect(missing_docs, reason = "no need for doc")]
4057 type Error;
4058 fn crem_euclid(self, divisor: Self::Divisor) -> Result<Self::Output, Self::Error>;
4062}
4063#[doc(alias = "checked_rem_euclid")]
4069#[inline]
4070pub fn crem_euclid<T>(value: T, divisor: T::Divisor) -> Result<T::Output, T::Error>
4071where
4072 T: CremEuclid,
4073{
4074 value.crem_euclid(divisor)
4075}
4076impl CremEuclid for i8 {
4077 type Divisor = i8;
4078 type Output = i8;
4079 type Error = Error;
4080 #[inline]
4084 fn crem_euclid(self, divisor: i8) -> Result<i8, Error> {
4085 self.checked_rem_euclid(divisor).ok_or_else(|| {
4086 Error::new({
4087 if divisor == 0 {
4088 format!(
4089 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4090 self, divisor
4091 )
4092 } else {
4093 format!(
4094 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4095 self, divisor, "i8"
4096 )
4097 }
4098 })
4099 })
4100 }
4101}
4102impl CremEuclid for i16 {
4103 type Divisor = i16;
4104 type Output = i16;
4105 type Error = Error;
4106 #[inline]
4110 fn crem_euclid(self, divisor: i16) -> Result<i16, Error> {
4111 self.checked_rem_euclid(divisor).ok_or_else(|| {
4112 Error::new({
4113 if divisor == 0 {
4114 format!(
4115 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4116 self, divisor
4117 )
4118 } else {
4119 format!(
4120 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4121 self, divisor, "i16"
4122 )
4123 }
4124 })
4125 })
4126 }
4127}
4128impl CremEuclid for i32 {
4129 type Divisor = i32;
4130 type Output = i32;
4131 type Error = Error;
4132 #[inline]
4136 fn crem_euclid(self, divisor: i32) -> Result<i32, Error> {
4137 self.checked_rem_euclid(divisor).ok_or_else(|| {
4138 Error::new({
4139 if divisor == 0 {
4140 format!(
4141 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4142 self, divisor
4143 )
4144 } else {
4145 format!(
4146 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4147 self, divisor, "i32"
4148 )
4149 }
4150 })
4151 })
4152 }
4153}
4154impl CremEuclid for i64 {
4155 type Divisor = i64;
4156 type Output = i64;
4157 type Error = Error;
4158 #[inline]
4162 fn crem_euclid(self, divisor: i64) -> Result<i64, Error> {
4163 self.checked_rem_euclid(divisor).ok_or_else(|| {
4164 Error::new({
4165 if divisor == 0 {
4166 format!(
4167 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4168 self, divisor
4169 )
4170 } else {
4171 format!(
4172 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4173 self, divisor, "i64"
4174 )
4175 }
4176 })
4177 })
4178 }
4179}
4180impl CremEuclid for i128 {
4181 type Divisor = i128;
4182 type Output = i128;
4183 type Error = Error;
4184 #[inline]
4188 fn crem_euclid(self, divisor: i128) -> Result<i128, Error> {
4189 self.checked_rem_euclid(divisor).ok_or_else(|| {
4190 Error::new({
4191 if divisor == 0 {
4192 format!(
4193 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4194 self, divisor
4195 )
4196 } else {
4197 format!(
4198 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4199 self, divisor, "i128"
4200 )
4201 }
4202 })
4203 })
4204 }
4205}
4206impl CremEuclid for isize {
4207 type Divisor = isize;
4208 type Output = isize;
4209 type Error = Error;
4210 #[inline]
4214 fn crem_euclid(self, divisor: isize) -> Result<isize, Error> {
4215 self.checked_rem_euclid(divisor).ok_or_else(|| {
4216 Error::new({
4217 if divisor == 0 {
4218 format!(
4219 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4220 self, divisor
4221 )
4222 } else {
4223 format!(
4224 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4225 self, divisor, "isize"
4226 )
4227 }
4228 })
4229 })
4230 }
4231}
4232impl CremEuclid for u8 {
4233 type Divisor = u8;
4234 type Output = u8;
4235 type Error = Error;
4236 #[inline]
4240 fn crem_euclid(self, divisor: u8) -> Result<u8, Error> {
4241 self.checked_rem_euclid(divisor).ok_or_else(|| {
4242 Error::new({
4243 if divisor == 0 {
4244 format!(
4245 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4246 self, divisor
4247 )
4248 } else {
4249 format!(
4250 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4251 self, divisor, "u8"
4252 )
4253 }
4254 })
4255 })
4256 }
4257}
4258impl CremEuclid for u16 {
4259 type Divisor = u16;
4260 type Output = u16;
4261 type Error = Error;
4262 #[inline]
4266 fn crem_euclid(self, divisor: u16) -> Result<u16, Error> {
4267 self.checked_rem_euclid(divisor).ok_or_else(|| {
4268 Error::new({
4269 if divisor == 0 {
4270 format!(
4271 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4272 self, divisor
4273 )
4274 } else {
4275 format!(
4276 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4277 self, divisor, "u16"
4278 )
4279 }
4280 })
4281 })
4282 }
4283}
4284impl CremEuclid for u32 {
4285 type Divisor = u32;
4286 type Output = u32;
4287 type Error = Error;
4288 #[inline]
4292 fn crem_euclid(self, divisor: u32) -> Result<u32, Error> {
4293 self.checked_rem_euclid(divisor).ok_or_else(|| {
4294 Error::new({
4295 if divisor == 0 {
4296 format!(
4297 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4298 self, divisor
4299 )
4300 } else {
4301 format!(
4302 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4303 self, divisor, "u32"
4304 )
4305 }
4306 })
4307 })
4308 }
4309}
4310impl CremEuclid for u64 {
4311 type Divisor = u64;
4312 type Output = u64;
4313 type Error = Error;
4314 #[inline]
4318 fn crem_euclid(self, divisor: u64) -> Result<u64, Error> {
4319 self.checked_rem_euclid(divisor).ok_or_else(|| {
4320 Error::new({
4321 if divisor == 0 {
4322 format!(
4323 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4324 self, divisor
4325 )
4326 } else {
4327 format!(
4328 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4329 self, divisor, "u64"
4330 )
4331 }
4332 })
4333 })
4334 }
4335}
4336impl CremEuclid for u128 {
4337 type Divisor = u128;
4338 type Output = u128;
4339 type Error = Error;
4340 #[inline]
4344 fn crem_euclid(self, divisor: u128) -> Result<u128, Error> {
4345 self.checked_rem_euclid(divisor).ok_or_else(|| {
4346 Error::new({
4347 if divisor == 0 {
4348 format!(
4349 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4350 self, divisor
4351 )
4352 } else {
4353 format!(
4354 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4355 self, divisor, "u128"
4356 )
4357 }
4358 })
4359 })
4360 }
4361}
4362impl CremEuclid for usize {
4363 type Divisor = usize;
4364 type Output = usize;
4365 type Error = Error;
4366 #[inline]
4370 fn crem_euclid(self, divisor: usize) -> Result<usize, Error> {
4371 self.checked_rem_euclid(divisor).ok_or_else(|| {
4372 Error::new({
4373 if divisor == 0 {
4374 format!(
4375 "failed to compute rem_euclid({:?}, {:?}): division by zero",
4376 self, divisor
4377 )
4378 } else {
4379 format!(
4380 "failed to compute rem_euclid({:?}, {:?}): {} overflow",
4381 self, divisor, "usize"
4382 )
4383 }
4384 })
4385 })
4386 }
4387}
4388pub trait Cilog: Sized {
4392 #[expect(missing_docs, reason = "no need for doc")]
4393 type Base;
4394 #[expect(missing_docs, reason = "no need for doc")]
4395 type Output;
4396 #[expect(missing_docs, reason = "no need for doc")]
4397 type Error;
4398 fn cilog(self, base: Self::Base) -> Result<Self::Output, Self::Error>;
4402}
4403#[doc(alias = "checked_ilog")]
4409#[inline]
4410pub fn cilog<T>(value: T, base: T::Base) -> Result<T::Output, T::Error>
4411where
4412 T: Cilog,
4413{
4414 value.cilog(base)
4415}
4416impl Cilog for i8 {
4417 type Base = i8;
4418 type Output = u32;
4419 type Error = Error;
4420 #[inline]
4424 fn cilog(self, base: i8) -> Result<u32, Error> {
4425 self.checked_ilog(base).ok_or_else(|| {
4426 Error::new({
4427 if base < 2 {
4428 format!(
4429 "failed to compute ilog({:?}, {:?}): base is less than 2",
4430 self, base
4431 )
4432 } else {
4433 format!(
4434 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4435 self, base
4436 )
4437 }
4438 })
4439 })
4440 }
4441}
4442impl Cilog for i16 {
4443 type Base = i16;
4444 type Output = u32;
4445 type Error = Error;
4446 #[inline]
4450 fn cilog(self, base: i16) -> Result<u32, Error> {
4451 self.checked_ilog(base).ok_or_else(|| {
4452 Error::new({
4453 if base < 2 {
4454 format!(
4455 "failed to compute ilog({:?}, {:?}): base is less than 2",
4456 self, base
4457 )
4458 } else {
4459 format!(
4460 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4461 self, base
4462 )
4463 }
4464 })
4465 })
4466 }
4467}
4468impl Cilog for i32 {
4469 type Base = i32;
4470 type Output = u32;
4471 type Error = Error;
4472 #[inline]
4476 fn cilog(self, base: i32) -> Result<u32, Error> {
4477 self.checked_ilog(base).ok_or_else(|| {
4478 Error::new({
4479 if base < 2 {
4480 format!(
4481 "failed to compute ilog({:?}, {:?}): base is less than 2",
4482 self, base
4483 )
4484 } else {
4485 format!(
4486 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4487 self, base
4488 )
4489 }
4490 })
4491 })
4492 }
4493}
4494impl Cilog for i64 {
4495 type Base = i64;
4496 type Output = u32;
4497 type Error = Error;
4498 #[inline]
4502 fn cilog(self, base: i64) -> Result<u32, Error> {
4503 self.checked_ilog(base).ok_or_else(|| {
4504 Error::new({
4505 if base < 2 {
4506 format!(
4507 "failed to compute ilog({:?}, {:?}): base is less than 2",
4508 self, base
4509 )
4510 } else {
4511 format!(
4512 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4513 self, base
4514 )
4515 }
4516 })
4517 })
4518 }
4519}
4520impl Cilog for i128 {
4521 type Base = i128;
4522 type Output = u32;
4523 type Error = Error;
4524 #[inline]
4528 fn cilog(self, base: i128) -> Result<u32, Error> {
4529 self.checked_ilog(base).ok_or_else(|| {
4530 Error::new({
4531 if base < 2 {
4532 format!(
4533 "failed to compute ilog({:?}, {:?}): base is less than 2",
4534 self, base
4535 )
4536 } else {
4537 format!(
4538 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4539 self, base
4540 )
4541 }
4542 })
4543 })
4544 }
4545}
4546impl Cilog for isize {
4547 type Base = isize;
4548 type Output = u32;
4549 type Error = Error;
4550 #[inline]
4554 fn cilog(self, base: isize) -> Result<u32, Error> {
4555 self.checked_ilog(base).ok_or_else(|| {
4556 Error::new({
4557 if base < 2 {
4558 format!(
4559 "failed to compute ilog({:?}, {:?}): base is less than 2",
4560 self, base
4561 )
4562 } else {
4563 format!(
4564 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4565 self, base
4566 )
4567 }
4568 })
4569 })
4570 }
4571}
4572impl Cilog for u8 {
4573 type Base = u8;
4574 type Output = u32;
4575 type Error = Error;
4576 #[inline]
4580 fn cilog(self, base: u8) -> Result<u32, Error> {
4581 self.checked_ilog(base).ok_or_else(|| {
4582 Error::new({
4583 if base < 2 {
4584 format!(
4585 "failed to compute ilog({:?}, {:?}): base is less than 2",
4586 self, base
4587 )
4588 } else {
4589 format!(
4590 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4591 self, base
4592 )
4593 }
4594 })
4595 })
4596 }
4597}
4598impl Cilog for u16 {
4599 type Base = u16;
4600 type Output = u32;
4601 type Error = Error;
4602 #[inline]
4606 fn cilog(self, base: u16) -> Result<u32, Error> {
4607 self.checked_ilog(base).ok_or_else(|| {
4608 Error::new({
4609 if base < 2 {
4610 format!(
4611 "failed to compute ilog({:?}, {:?}): base is less than 2",
4612 self, base
4613 )
4614 } else {
4615 format!(
4616 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4617 self, base
4618 )
4619 }
4620 })
4621 })
4622 }
4623}
4624impl Cilog for u32 {
4625 type Base = u32;
4626 type Output = u32;
4627 type Error = Error;
4628 #[inline]
4632 fn cilog(self, base: u32) -> Result<u32, Error> {
4633 self.checked_ilog(base).ok_or_else(|| {
4634 Error::new({
4635 if base < 2 {
4636 format!(
4637 "failed to compute ilog({:?}, {:?}): base is less than 2",
4638 self, base
4639 )
4640 } else {
4641 format!(
4642 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4643 self, base
4644 )
4645 }
4646 })
4647 })
4648 }
4649}
4650impl Cilog for u64 {
4651 type Base = u64;
4652 type Output = u32;
4653 type Error = Error;
4654 #[inline]
4658 fn cilog(self, base: u64) -> Result<u32, Error> {
4659 self.checked_ilog(base).ok_or_else(|| {
4660 Error::new({
4661 if base < 2 {
4662 format!(
4663 "failed to compute ilog({:?}, {:?}): base is less than 2",
4664 self, base
4665 )
4666 } else {
4667 format!(
4668 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4669 self, base
4670 )
4671 }
4672 })
4673 })
4674 }
4675}
4676impl Cilog for u128 {
4677 type Base = u128;
4678 type Output = u32;
4679 type Error = Error;
4680 #[inline]
4684 fn cilog(self, base: u128) -> Result<u32, Error> {
4685 self.checked_ilog(base).ok_or_else(|| {
4686 Error::new({
4687 if base < 2 {
4688 format!(
4689 "failed to compute ilog({:?}, {:?}): base is less than 2",
4690 self, base
4691 )
4692 } else {
4693 format!(
4694 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4695 self, base
4696 )
4697 }
4698 })
4699 })
4700 }
4701}
4702impl Cilog for usize {
4703 type Base = usize;
4704 type Output = u32;
4705 type Error = Error;
4706 #[inline]
4710 fn cilog(self, base: usize) -> Result<u32, Error> {
4711 self.checked_ilog(base).ok_or_else(|| {
4712 Error::new({
4713 if base < 2 {
4714 format!(
4715 "failed to compute ilog({:?}, {:?}): base is less than 2",
4716 self, base
4717 )
4718 } else {
4719 format!(
4720 "failed to compute ilog({:?}, {:?}): first argument is not positive",
4721 self, base
4722 )
4723 }
4724 })
4725 })
4726 }
4727}
4728pub trait Cilog2: Sized {
4732 #[expect(missing_docs, reason = "no need for doc")]
4733 type Output;
4734 #[expect(missing_docs, reason = "no need for doc")]
4735 type Error;
4736 fn cilog2(self) -> Result<Self::Output, Self::Error>;
4740}
4741#[doc(alias = "checked_ilog2")]
4745#[inline]
4746pub fn cilog2<T>(value: T) -> Result<T::Output, T::Error>
4747where
4748 T: Cilog2,
4749{
4750 Cilog2::cilog2(value)
4751}
4752impl Cilog2 for i8 {
4753 type Output = u32;
4754 type Error = Error;
4755 #[inline]
4759 fn cilog2(self) -> Result<u32, Error> {
4760 self.checked_ilog2().ok_or_else(|| {
4761 Error::new(format!(
4762 "failed to compute ilog2({:?}): argument is not positive",
4763 self
4764 ))
4765 })
4766 }
4767}
4768impl Cilog2 for i16 {
4769 type Output = u32;
4770 type Error = Error;
4771 #[inline]
4775 fn cilog2(self) -> Result<u32, Error> {
4776 self.checked_ilog2().ok_or_else(|| {
4777 Error::new(format!(
4778 "failed to compute ilog2({:?}): argument is not positive",
4779 self
4780 ))
4781 })
4782 }
4783}
4784impl Cilog2 for i32 {
4785 type Output = u32;
4786 type Error = Error;
4787 #[inline]
4791 fn cilog2(self) -> Result<u32, Error> {
4792 self.checked_ilog2().ok_or_else(|| {
4793 Error::new(format!(
4794 "failed to compute ilog2({:?}): argument is not positive",
4795 self
4796 ))
4797 })
4798 }
4799}
4800impl Cilog2 for i64 {
4801 type Output = u32;
4802 type Error = Error;
4803 #[inline]
4807 fn cilog2(self) -> Result<u32, Error> {
4808 self.checked_ilog2().ok_or_else(|| {
4809 Error::new(format!(
4810 "failed to compute ilog2({:?}): argument is not positive",
4811 self
4812 ))
4813 })
4814 }
4815}
4816impl Cilog2 for i128 {
4817 type Output = u32;
4818 type Error = Error;
4819 #[inline]
4823 fn cilog2(self) -> Result<u32, Error> {
4824 self.checked_ilog2().ok_or_else(|| {
4825 Error::new(format!(
4826 "failed to compute ilog2({:?}): argument is not positive",
4827 self
4828 ))
4829 })
4830 }
4831}
4832impl Cilog2 for isize {
4833 type Output = u32;
4834 type Error = Error;
4835 #[inline]
4839 fn cilog2(self) -> Result<u32, Error> {
4840 self.checked_ilog2().ok_or_else(|| {
4841 Error::new(format!(
4842 "failed to compute ilog2({:?}): argument is not positive",
4843 self
4844 ))
4845 })
4846 }
4847}
4848impl Cilog2 for u8 {
4849 type Output = u32;
4850 type Error = Error;
4851 #[inline]
4855 fn cilog2(self) -> Result<u32, Error> {
4856 self.checked_ilog2().ok_or_else(|| {
4857 Error::new(format!(
4858 "failed to compute ilog2({:?}): argument is not positive",
4859 self
4860 ))
4861 })
4862 }
4863}
4864impl Cilog2 for u16 {
4865 type Output = u32;
4866 type Error = Error;
4867 #[inline]
4871 fn cilog2(self) -> Result<u32, Error> {
4872 self.checked_ilog2().ok_or_else(|| {
4873 Error::new(format!(
4874 "failed to compute ilog2({:?}): argument is not positive",
4875 self
4876 ))
4877 })
4878 }
4879}
4880impl Cilog2 for u32 {
4881 type Output = u32;
4882 type Error = Error;
4883 #[inline]
4887 fn cilog2(self) -> Result<u32, Error> {
4888 self.checked_ilog2().ok_or_else(|| {
4889 Error::new(format!(
4890 "failed to compute ilog2({:?}): argument is not positive",
4891 self
4892 ))
4893 })
4894 }
4895}
4896impl Cilog2 for u64 {
4897 type Output = u32;
4898 type Error = Error;
4899 #[inline]
4903 fn cilog2(self) -> Result<u32, Error> {
4904 self.checked_ilog2().ok_or_else(|| {
4905 Error::new(format!(
4906 "failed to compute ilog2({:?}): argument is not positive",
4907 self
4908 ))
4909 })
4910 }
4911}
4912impl Cilog2 for u128 {
4913 type Output = u32;
4914 type Error = Error;
4915 #[inline]
4919 fn cilog2(self) -> Result<u32, Error> {
4920 self.checked_ilog2().ok_or_else(|| {
4921 Error::new(format!(
4922 "failed to compute ilog2({:?}): argument is not positive",
4923 self
4924 ))
4925 })
4926 }
4927}
4928impl Cilog2 for usize {
4929 type Output = u32;
4930 type Error = Error;
4931 #[inline]
4935 fn cilog2(self) -> Result<u32, Error> {
4936 self.checked_ilog2().ok_or_else(|| {
4937 Error::new(format!(
4938 "failed to compute ilog2({:?}): argument is not positive",
4939 self
4940 ))
4941 })
4942 }
4943}
4944pub trait Cilog10: Sized {
4948 #[expect(missing_docs, reason = "no need for doc")]
4949 type Output;
4950 #[expect(missing_docs, reason = "no need for doc")]
4951 type Error;
4952 fn cilog10(self) -> Result<Self::Output, Self::Error>;
4956}
4957#[doc(alias = "checked_ilog10")]
4961#[inline]
4962pub fn cilog10<T>(value: T) -> Result<T::Output, T::Error>
4963where
4964 T: Cilog10,
4965{
4966 Cilog10::cilog10(value)
4967}
4968impl Cilog10 for i8 {
4969 type Output = u32;
4970 type Error = Error;
4971 #[inline]
4975 fn cilog10(self) -> Result<u32, Error> {
4976 self.checked_ilog10().ok_or_else(|| {
4977 Error::new(format!(
4978 "failed to compute ilog10({:?}): argument is not positive",
4979 self
4980 ))
4981 })
4982 }
4983}
4984impl Cilog10 for i16 {
4985 type Output = u32;
4986 type Error = Error;
4987 #[inline]
4991 fn cilog10(self) -> Result<u32, Error> {
4992 self.checked_ilog10().ok_or_else(|| {
4993 Error::new(format!(
4994 "failed to compute ilog10({:?}): argument is not positive",
4995 self
4996 ))
4997 })
4998 }
4999}
5000impl Cilog10 for i32 {
5001 type Output = u32;
5002 type Error = Error;
5003 #[inline]
5007 fn cilog10(self) -> Result<u32, Error> {
5008 self.checked_ilog10().ok_or_else(|| {
5009 Error::new(format!(
5010 "failed to compute ilog10({:?}): argument is not positive",
5011 self
5012 ))
5013 })
5014 }
5015}
5016impl Cilog10 for i64 {
5017 type Output = u32;
5018 type Error = Error;
5019 #[inline]
5023 fn cilog10(self) -> Result<u32, Error> {
5024 self.checked_ilog10().ok_or_else(|| {
5025 Error::new(format!(
5026 "failed to compute ilog10({:?}): argument is not positive",
5027 self
5028 ))
5029 })
5030 }
5031}
5032impl Cilog10 for i128 {
5033 type Output = u32;
5034 type Error = Error;
5035 #[inline]
5039 fn cilog10(self) -> Result<u32, Error> {
5040 self.checked_ilog10().ok_or_else(|| {
5041 Error::new(format!(
5042 "failed to compute ilog10({:?}): argument is not positive",
5043 self
5044 ))
5045 })
5046 }
5047}
5048impl Cilog10 for isize {
5049 type Output = u32;
5050 type Error = Error;
5051 #[inline]
5055 fn cilog10(self) -> Result<u32, Error> {
5056 self.checked_ilog10().ok_or_else(|| {
5057 Error::new(format!(
5058 "failed to compute ilog10({:?}): argument is not positive",
5059 self
5060 ))
5061 })
5062 }
5063}
5064impl Cilog10 for u8 {
5065 type Output = u32;
5066 type Error = Error;
5067 #[inline]
5071 fn cilog10(self) -> Result<u32, Error> {
5072 self.checked_ilog10().ok_or_else(|| {
5073 Error::new(format!(
5074 "failed to compute ilog10({:?}): argument is not positive",
5075 self
5076 ))
5077 })
5078 }
5079}
5080impl Cilog10 for u16 {
5081 type Output = u32;
5082 type Error = Error;
5083 #[inline]
5087 fn cilog10(self) -> Result<u32, Error> {
5088 self.checked_ilog10().ok_or_else(|| {
5089 Error::new(format!(
5090 "failed to compute ilog10({:?}): argument is not positive",
5091 self
5092 ))
5093 })
5094 }
5095}
5096impl Cilog10 for u32 {
5097 type Output = u32;
5098 type Error = Error;
5099 #[inline]
5103 fn cilog10(self) -> Result<u32, Error> {
5104 self.checked_ilog10().ok_or_else(|| {
5105 Error::new(format!(
5106 "failed to compute ilog10({:?}): argument is not positive",
5107 self
5108 ))
5109 })
5110 }
5111}
5112impl Cilog10 for u64 {
5113 type Output = u32;
5114 type Error = Error;
5115 #[inline]
5119 fn cilog10(self) -> Result<u32, Error> {
5120 self.checked_ilog10().ok_or_else(|| {
5121 Error::new(format!(
5122 "failed to compute ilog10({:?}): argument is not positive",
5123 self
5124 ))
5125 })
5126 }
5127}
5128impl Cilog10 for u128 {
5129 type Output = u32;
5130 type Error = Error;
5131 #[inline]
5135 fn cilog10(self) -> Result<u32, Error> {
5136 self.checked_ilog10().ok_or_else(|| {
5137 Error::new(format!(
5138 "failed to compute ilog10({:?}): argument is not positive",
5139 self
5140 ))
5141 })
5142 }
5143}
5144impl Cilog10 for usize {
5145 type Output = u32;
5146 type Error = Error;
5147 #[inline]
5151 fn cilog10(self) -> Result<u32, Error> {
5152 self.checked_ilog10().ok_or_else(|| {
5153 Error::new(format!(
5154 "failed to compute ilog10({:?}): argument is not positive",
5155 self
5156 ))
5157 })
5158 }
5159}
5160pub trait Cshl: Sized {
5164 #[expect(missing_docs, reason = "no need for doc")]
5165 type Other;
5166 #[expect(missing_docs, reason = "no need for doc")]
5167 type Output;
5168 #[expect(missing_docs, reason = "no need for doc")]
5169 type Error;
5170 fn cshl(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
5174 fn cshl_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
5178}
5179#[doc(alias = "checked_shl")]
5185#[inline]
5186pub fn cshl<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
5187where
5188 T: Cshl,
5189{
5190 a.cshl(b)
5191}
5192impl Cshl for i8 {
5193 type Other = u32;
5194 type Output = i8;
5195 type Error = Error;
5196 #[inline]
5200 fn cshl(self, other: u32) -> Result<i8, Error> {
5201 self.checked_shl(other).ok_or_else(|| {
5202 Error::new(format!(
5203 "failed to compute {:?} << {:?}: shift amount is too large",
5204 MaybeParens(self),
5205 MaybeParens(other)
5206 ))
5207 })
5208 }
5209 #[inline]
5210 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5214 *self = self.cshl(other)?;
5215 Ok(())
5216 }
5217}
5218impl Cshl for i16 {
5219 type Other = u32;
5220 type Output = i16;
5221 type Error = Error;
5222 #[inline]
5226 fn cshl(self, other: u32) -> Result<i16, Error> {
5227 self.checked_shl(other).ok_or_else(|| {
5228 Error::new(format!(
5229 "failed to compute {:?} << {:?}: shift amount is too large",
5230 MaybeParens(self),
5231 MaybeParens(other)
5232 ))
5233 })
5234 }
5235 #[inline]
5236 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5240 *self = self.cshl(other)?;
5241 Ok(())
5242 }
5243}
5244impl Cshl for i32 {
5245 type Other = u32;
5246 type Output = i32;
5247 type Error = Error;
5248 #[inline]
5252 fn cshl(self, other: u32) -> Result<i32, Error> {
5253 self.checked_shl(other).ok_or_else(|| {
5254 Error::new(format!(
5255 "failed to compute {:?} << {:?}: shift amount is too large",
5256 MaybeParens(self),
5257 MaybeParens(other)
5258 ))
5259 })
5260 }
5261 #[inline]
5262 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5266 *self = self.cshl(other)?;
5267 Ok(())
5268 }
5269}
5270impl Cshl for i64 {
5271 type Other = u32;
5272 type Output = i64;
5273 type Error = Error;
5274 #[inline]
5278 fn cshl(self, other: u32) -> Result<i64, Error> {
5279 self.checked_shl(other).ok_or_else(|| {
5280 Error::new(format!(
5281 "failed to compute {:?} << {:?}: shift amount is too large",
5282 MaybeParens(self),
5283 MaybeParens(other)
5284 ))
5285 })
5286 }
5287 #[inline]
5288 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5292 *self = self.cshl(other)?;
5293 Ok(())
5294 }
5295}
5296impl Cshl for i128 {
5297 type Other = u32;
5298 type Output = i128;
5299 type Error = Error;
5300 #[inline]
5304 fn cshl(self, other: u32) -> Result<i128, Error> {
5305 self.checked_shl(other).ok_or_else(|| {
5306 Error::new(format!(
5307 "failed to compute {:?} << {:?}: shift amount is too large",
5308 MaybeParens(self),
5309 MaybeParens(other)
5310 ))
5311 })
5312 }
5313 #[inline]
5314 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5318 *self = self.cshl(other)?;
5319 Ok(())
5320 }
5321}
5322impl Cshl for isize {
5323 type Other = u32;
5324 type Output = isize;
5325 type Error = Error;
5326 #[inline]
5330 fn cshl(self, other: u32) -> Result<isize, Error> {
5331 self.checked_shl(other).ok_or_else(|| {
5332 Error::new(format!(
5333 "failed to compute {:?} << {:?}: shift amount is too large",
5334 MaybeParens(self),
5335 MaybeParens(other)
5336 ))
5337 })
5338 }
5339 #[inline]
5340 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5344 *self = self.cshl(other)?;
5345 Ok(())
5346 }
5347}
5348impl Cshl for u8 {
5349 type Other = u32;
5350 type Output = u8;
5351 type Error = Error;
5352 #[inline]
5356 fn cshl(self, other: u32) -> Result<u8, Error> {
5357 self.checked_shl(other).ok_or_else(|| {
5358 Error::new(format!(
5359 "failed to compute {:?} << {:?}: shift amount is too large",
5360 MaybeParens(self),
5361 MaybeParens(other)
5362 ))
5363 })
5364 }
5365 #[inline]
5366 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5370 *self = self.cshl(other)?;
5371 Ok(())
5372 }
5373}
5374impl Cshl for u16 {
5375 type Other = u32;
5376 type Output = u16;
5377 type Error = Error;
5378 #[inline]
5382 fn cshl(self, other: u32) -> Result<u16, Error> {
5383 self.checked_shl(other).ok_or_else(|| {
5384 Error::new(format!(
5385 "failed to compute {:?} << {:?}: shift amount is too large",
5386 MaybeParens(self),
5387 MaybeParens(other)
5388 ))
5389 })
5390 }
5391 #[inline]
5392 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5396 *self = self.cshl(other)?;
5397 Ok(())
5398 }
5399}
5400impl Cshl for u32 {
5401 type Other = u32;
5402 type Output = u32;
5403 type Error = Error;
5404 #[inline]
5408 fn cshl(self, other: u32) -> Result<u32, Error> {
5409 self.checked_shl(other).ok_or_else(|| {
5410 Error::new(format!(
5411 "failed to compute {:?} << {:?}: shift amount is too large",
5412 MaybeParens(self),
5413 MaybeParens(other)
5414 ))
5415 })
5416 }
5417 #[inline]
5418 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5422 *self = self.cshl(other)?;
5423 Ok(())
5424 }
5425}
5426impl Cshl for u64 {
5427 type Other = u32;
5428 type Output = u64;
5429 type Error = Error;
5430 #[inline]
5434 fn cshl(self, other: u32) -> Result<u64, Error> {
5435 self.checked_shl(other).ok_or_else(|| {
5436 Error::new(format!(
5437 "failed to compute {:?} << {:?}: shift amount is too large",
5438 MaybeParens(self),
5439 MaybeParens(other)
5440 ))
5441 })
5442 }
5443 #[inline]
5444 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5448 *self = self.cshl(other)?;
5449 Ok(())
5450 }
5451}
5452impl Cshl for u128 {
5453 type Other = u32;
5454 type Output = u128;
5455 type Error = Error;
5456 #[inline]
5460 fn cshl(self, other: u32) -> Result<u128, Error> {
5461 self.checked_shl(other).ok_or_else(|| {
5462 Error::new(format!(
5463 "failed to compute {:?} << {:?}: shift amount is too large",
5464 MaybeParens(self),
5465 MaybeParens(other)
5466 ))
5467 })
5468 }
5469 #[inline]
5470 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5474 *self = self.cshl(other)?;
5475 Ok(())
5476 }
5477}
5478impl Cshl for usize {
5479 type Other = u32;
5480 type Output = usize;
5481 type Error = Error;
5482 #[inline]
5486 fn cshl(self, other: u32) -> Result<usize, Error> {
5487 self.checked_shl(other).ok_or_else(|| {
5488 Error::new(format!(
5489 "failed to compute {:?} << {:?}: shift amount is too large",
5490 MaybeParens(self),
5491 MaybeParens(other)
5492 ))
5493 })
5494 }
5495 #[inline]
5496 fn cshl_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5500 *self = self.cshl(other)?;
5501 Ok(())
5502 }
5503}
5504pub trait Cshr: Sized {
5508 #[expect(missing_docs, reason = "no need for doc")]
5509 type Other;
5510 #[expect(missing_docs, reason = "no need for doc")]
5511 type Output;
5512 #[expect(missing_docs, reason = "no need for doc")]
5513 type Error;
5514 fn cshr(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
5518 fn cshr_assign(&mut self, other: Self::Other) -> Result<(), Self::Error>;
5522}
5523#[doc(alias = "checked_shr")]
5529#[inline]
5530pub fn cshr<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
5531where
5532 T: Cshr,
5533{
5534 a.cshr(b)
5535}
5536impl Cshr for i8 {
5537 type Other = u32;
5538 type Output = i8;
5539 type Error = Error;
5540 #[inline]
5544 fn cshr(self, other: u32) -> Result<i8, Error> {
5545 self.checked_shr(other).ok_or_else(|| {
5546 Error::new(format!(
5547 "failed to compute {:?} >> {:?}: shift amount is too large",
5548 MaybeParens(self),
5549 MaybeParens(other)
5550 ))
5551 })
5552 }
5553 #[inline]
5554 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5558 *self = self.cshr(other)?;
5559 Ok(())
5560 }
5561}
5562impl Cshr for i16 {
5563 type Other = u32;
5564 type Output = i16;
5565 type Error = Error;
5566 #[inline]
5570 fn cshr(self, other: u32) -> Result<i16, Error> {
5571 self.checked_shr(other).ok_or_else(|| {
5572 Error::new(format!(
5573 "failed to compute {:?} >> {:?}: shift amount is too large",
5574 MaybeParens(self),
5575 MaybeParens(other)
5576 ))
5577 })
5578 }
5579 #[inline]
5580 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5584 *self = self.cshr(other)?;
5585 Ok(())
5586 }
5587}
5588impl Cshr for i32 {
5589 type Other = u32;
5590 type Output = i32;
5591 type Error = Error;
5592 #[inline]
5596 fn cshr(self, other: u32) -> Result<i32, Error> {
5597 self.checked_shr(other).ok_or_else(|| {
5598 Error::new(format!(
5599 "failed to compute {:?} >> {:?}: shift amount is too large",
5600 MaybeParens(self),
5601 MaybeParens(other)
5602 ))
5603 })
5604 }
5605 #[inline]
5606 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5610 *self = self.cshr(other)?;
5611 Ok(())
5612 }
5613}
5614impl Cshr for i64 {
5615 type Other = u32;
5616 type Output = i64;
5617 type Error = Error;
5618 #[inline]
5622 fn cshr(self, other: u32) -> Result<i64, Error> {
5623 self.checked_shr(other).ok_or_else(|| {
5624 Error::new(format!(
5625 "failed to compute {:?} >> {:?}: shift amount is too large",
5626 MaybeParens(self),
5627 MaybeParens(other)
5628 ))
5629 })
5630 }
5631 #[inline]
5632 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5636 *self = self.cshr(other)?;
5637 Ok(())
5638 }
5639}
5640impl Cshr for i128 {
5641 type Other = u32;
5642 type Output = i128;
5643 type Error = Error;
5644 #[inline]
5648 fn cshr(self, other: u32) -> Result<i128, Error> {
5649 self.checked_shr(other).ok_or_else(|| {
5650 Error::new(format!(
5651 "failed to compute {:?} >> {:?}: shift amount is too large",
5652 MaybeParens(self),
5653 MaybeParens(other)
5654 ))
5655 })
5656 }
5657 #[inline]
5658 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5662 *self = self.cshr(other)?;
5663 Ok(())
5664 }
5665}
5666impl Cshr for isize {
5667 type Other = u32;
5668 type Output = isize;
5669 type Error = Error;
5670 #[inline]
5674 fn cshr(self, other: u32) -> Result<isize, Error> {
5675 self.checked_shr(other).ok_or_else(|| {
5676 Error::new(format!(
5677 "failed to compute {:?} >> {:?}: shift amount is too large",
5678 MaybeParens(self),
5679 MaybeParens(other)
5680 ))
5681 })
5682 }
5683 #[inline]
5684 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5688 *self = self.cshr(other)?;
5689 Ok(())
5690 }
5691}
5692impl Cshr for u8 {
5693 type Other = u32;
5694 type Output = u8;
5695 type Error = Error;
5696 #[inline]
5700 fn cshr(self, other: u32) -> Result<u8, Error> {
5701 self.checked_shr(other).ok_or_else(|| {
5702 Error::new(format!(
5703 "failed to compute {:?} >> {:?}: shift amount is too large",
5704 MaybeParens(self),
5705 MaybeParens(other)
5706 ))
5707 })
5708 }
5709 #[inline]
5710 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5714 *self = self.cshr(other)?;
5715 Ok(())
5716 }
5717}
5718impl Cshr for u16 {
5719 type Other = u32;
5720 type Output = u16;
5721 type Error = Error;
5722 #[inline]
5726 fn cshr(self, other: u32) -> Result<u16, Error> {
5727 self.checked_shr(other).ok_or_else(|| {
5728 Error::new(format!(
5729 "failed to compute {:?} >> {:?}: shift amount is too large",
5730 MaybeParens(self),
5731 MaybeParens(other)
5732 ))
5733 })
5734 }
5735 #[inline]
5736 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5740 *self = self.cshr(other)?;
5741 Ok(())
5742 }
5743}
5744impl Cshr for u32 {
5745 type Other = u32;
5746 type Output = u32;
5747 type Error = Error;
5748 #[inline]
5752 fn cshr(self, other: u32) -> Result<u32, Error> {
5753 self.checked_shr(other).ok_or_else(|| {
5754 Error::new(format!(
5755 "failed to compute {:?} >> {:?}: shift amount is too large",
5756 MaybeParens(self),
5757 MaybeParens(other)
5758 ))
5759 })
5760 }
5761 #[inline]
5762 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5766 *self = self.cshr(other)?;
5767 Ok(())
5768 }
5769}
5770impl Cshr for u64 {
5771 type Other = u32;
5772 type Output = u64;
5773 type Error = Error;
5774 #[inline]
5778 fn cshr(self, other: u32) -> Result<u64, Error> {
5779 self.checked_shr(other).ok_or_else(|| {
5780 Error::new(format!(
5781 "failed to compute {:?} >> {:?}: shift amount is too large",
5782 MaybeParens(self),
5783 MaybeParens(other)
5784 ))
5785 })
5786 }
5787 #[inline]
5788 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5792 *self = self.cshr(other)?;
5793 Ok(())
5794 }
5795}
5796impl Cshr for u128 {
5797 type Other = u32;
5798 type Output = u128;
5799 type Error = Error;
5800 #[inline]
5804 fn cshr(self, other: u32) -> Result<u128, Error> {
5805 self.checked_shr(other).ok_or_else(|| {
5806 Error::new(format!(
5807 "failed to compute {:?} >> {:?}: shift amount is too large",
5808 MaybeParens(self),
5809 MaybeParens(other)
5810 ))
5811 })
5812 }
5813 #[inline]
5814 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5818 *self = self.cshr(other)?;
5819 Ok(())
5820 }
5821}
5822impl Cshr for usize {
5823 type Other = u32;
5824 type Output = usize;
5825 type Error = Error;
5826 #[inline]
5830 fn cshr(self, other: u32) -> Result<usize, Error> {
5831 self.checked_shr(other).ok_or_else(|| {
5832 Error::new(format!(
5833 "failed to compute {:?} >> {:?}: shift amount is too large",
5834 MaybeParens(self),
5835 MaybeParens(other)
5836 ))
5837 })
5838 }
5839 #[inline]
5840 fn cshr_assign(&mut self, other: u32) -> Result<(), Self::Error> {
5844 *self = self.cshr(other)?;
5845 Ok(())
5846 }
5847}
5848pub trait Cpow: Sized {
5852 #[expect(missing_docs, reason = "no need for doc")]
5853 type Power;
5854 #[expect(missing_docs, reason = "no need for doc")]
5855 type Output;
5856 #[expect(missing_docs, reason = "no need for doc")]
5857 type Error;
5858 fn cpow(self, power: Self::Power) -> Result<Self::Output, Self::Error>;
5862}
5863#[doc(alias = "checked_pow")]
5869#[inline]
5870pub fn cpow<T>(value: T, power: T::Power) -> Result<T::Output, T::Error>
5871where
5872 T: Cpow,
5873{
5874 value.cpow(power)
5875}
5876impl Cpow for NonZero<u8> {
5877 type Power = u32;
5878 type Output = NonZero<u8>;
5879 type Error = Error;
5880 #[inline]
5884 fn cpow(self, power: u32) -> Result<NonZero<u8>, Error> {
5885 self.checked_pow(power).ok_or_else(|| {
5886 Error::new(format!(
5887 "failed to compute pow({:?}, {:?}): {} overflow",
5888 self, power, "NonZero<u8>"
5889 ))
5890 })
5891 }
5892}
5893impl Cpow for NonZero<u16> {
5894 type Power = u32;
5895 type Output = NonZero<u16>;
5896 type Error = Error;
5897 #[inline]
5901 fn cpow(self, power: u32) -> Result<NonZero<u16>, Error> {
5902 self.checked_pow(power).ok_or_else(|| {
5903 Error::new(format!(
5904 "failed to compute pow({:?}, {:?}): {} overflow",
5905 self, power, "NonZero<u16>"
5906 ))
5907 })
5908 }
5909}
5910impl Cpow for NonZero<u32> {
5911 type Power = u32;
5912 type Output = NonZero<u32>;
5913 type Error = Error;
5914 #[inline]
5918 fn cpow(self, power: u32) -> Result<NonZero<u32>, Error> {
5919 self.checked_pow(power).ok_or_else(|| {
5920 Error::new(format!(
5921 "failed to compute pow({:?}, {:?}): {} overflow",
5922 self, power, "NonZero<u32>"
5923 ))
5924 })
5925 }
5926}
5927impl Cpow for NonZero<u64> {
5928 type Power = u32;
5929 type Output = NonZero<u64>;
5930 type Error = Error;
5931 #[inline]
5935 fn cpow(self, power: u32) -> Result<NonZero<u64>, Error> {
5936 self.checked_pow(power).ok_or_else(|| {
5937 Error::new(format!(
5938 "failed to compute pow({:?}, {:?}): {} overflow",
5939 self, power, "NonZero<u64>"
5940 ))
5941 })
5942 }
5943}
5944impl Cpow for NonZero<u128> {
5945 type Power = u32;
5946 type Output = NonZero<u128>;
5947 type Error = Error;
5948 #[inline]
5952 fn cpow(self, power: u32) -> Result<NonZero<u128>, Error> {
5953 self.checked_pow(power).ok_or_else(|| {
5954 Error::new(format!(
5955 "failed to compute pow({:?}, {:?}): {} overflow",
5956 self, power, "NonZero<u128>"
5957 ))
5958 })
5959 }
5960}
5961impl Cpow for NonZero<usize> {
5962 type Power = u32;
5963 type Output = NonZero<usize>;
5964 type Error = Error;
5965 #[inline]
5969 fn cpow(self, power: u32) -> Result<NonZero<usize>, Error> {
5970 self.checked_pow(power).ok_or_else(|| {
5971 Error::new(format!(
5972 "failed to compute pow({:?}, {:?}): {} overflow",
5973 self, power, "NonZero<usize>"
5974 ))
5975 })
5976 }
5977}
5978impl Cpow for NonZero<i8> {
5979 type Power = u32;
5980 type Output = NonZero<i8>;
5981 type Error = Error;
5982 #[inline]
5986 fn cpow(self, power: u32) -> Result<NonZero<i8>, Error> {
5987 self.checked_pow(power).ok_or_else(|| {
5988 Error::new(format!(
5989 "failed to compute pow({:?}, {:?}): {} overflow",
5990 self, power, "NonZero<i8>"
5991 ))
5992 })
5993 }
5994}
5995impl Cpow for NonZero<i16> {
5996 type Power = u32;
5997 type Output = NonZero<i16>;
5998 type Error = Error;
5999 #[inline]
6003 fn cpow(self, power: u32) -> Result<NonZero<i16>, Error> {
6004 self.checked_pow(power).ok_or_else(|| {
6005 Error::new(format!(
6006 "failed to compute pow({:?}, {:?}): {} overflow",
6007 self, power, "NonZero<i16>"
6008 ))
6009 })
6010 }
6011}
6012impl Cpow for NonZero<i32> {
6013 type Power = u32;
6014 type Output = NonZero<i32>;
6015 type Error = Error;
6016 #[inline]
6020 fn cpow(self, power: u32) -> Result<NonZero<i32>, Error> {
6021 self.checked_pow(power).ok_or_else(|| {
6022 Error::new(format!(
6023 "failed to compute pow({:?}, {:?}): {} overflow",
6024 self, power, "NonZero<i32>"
6025 ))
6026 })
6027 }
6028}
6029impl Cpow for NonZero<i64> {
6030 type Power = u32;
6031 type Output = NonZero<i64>;
6032 type Error = Error;
6033 #[inline]
6037 fn cpow(self, power: u32) -> Result<NonZero<i64>, Error> {
6038 self.checked_pow(power).ok_or_else(|| {
6039 Error::new(format!(
6040 "failed to compute pow({:?}, {:?}): {} overflow",
6041 self, power, "NonZero<i64>"
6042 ))
6043 })
6044 }
6045}
6046impl Cpow for NonZero<i128> {
6047 type Power = u32;
6048 type Output = NonZero<i128>;
6049 type Error = Error;
6050 #[inline]
6054 fn cpow(self, power: u32) -> Result<NonZero<i128>, Error> {
6055 self.checked_pow(power).ok_or_else(|| {
6056 Error::new(format!(
6057 "failed to compute pow({:?}, {:?}): {} overflow",
6058 self, power, "NonZero<i128>"
6059 ))
6060 })
6061 }
6062}
6063impl Cpow for NonZero<isize> {
6064 type Power = u32;
6065 type Output = NonZero<isize>;
6066 type Error = Error;
6067 #[inline]
6071 fn cpow(self, power: u32) -> Result<NonZero<isize>, Error> {
6072 self.checked_pow(power).ok_or_else(|| {
6073 Error::new(format!(
6074 "failed to compute pow({:?}, {:?}): {} overflow",
6075 self, power, "NonZero<isize>"
6076 ))
6077 })
6078 }
6079}
6080impl Cpow for i8 {
6081 type Power = u32;
6082 type Output = i8;
6083 type Error = Error;
6084 #[inline]
6088 fn cpow(self, power: u32) -> Result<i8, Error> {
6089 self.checked_pow(power).ok_or_else(|| {
6090 Error::new(format!(
6091 "failed to compute pow({:?}, {:?}): {} overflow",
6092 self, power, "i8"
6093 ))
6094 })
6095 }
6096}
6097impl Cpow for i16 {
6098 type Power = u32;
6099 type Output = i16;
6100 type Error = Error;
6101 #[inline]
6105 fn cpow(self, power: u32) -> Result<i16, Error> {
6106 self.checked_pow(power).ok_or_else(|| {
6107 Error::new(format!(
6108 "failed to compute pow({:?}, {:?}): {} overflow",
6109 self, power, "i16"
6110 ))
6111 })
6112 }
6113}
6114impl Cpow for i32 {
6115 type Power = u32;
6116 type Output = i32;
6117 type Error = Error;
6118 #[inline]
6122 fn cpow(self, power: u32) -> Result<i32, Error> {
6123 self.checked_pow(power).ok_or_else(|| {
6124 Error::new(format!(
6125 "failed to compute pow({:?}, {:?}): {} overflow",
6126 self, power, "i32"
6127 ))
6128 })
6129 }
6130}
6131impl Cpow for i64 {
6132 type Power = u32;
6133 type Output = i64;
6134 type Error = Error;
6135 #[inline]
6139 fn cpow(self, power: u32) -> Result<i64, Error> {
6140 self.checked_pow(power).ok_or_else(|| {
6141 Error::new(format!(
6142 "failed to compute pow({:?}, {:?}): {} overflow",
6143 self, power, "i64"
6144 ))
6145 })
6146 }
6147}
6148impl Cpow for i128 {
6149 type Power = u32;
6150 type Output = i128;
6151 type Error = Error;
6152 #[inline]
6156 fn cpow(self, power: u32) -> Result<i128, Error> {
6157 self.checked_pow(power).ok_or_else(|| {
6158 Error::new(format!(
6159 "failed to compute pow({:?}, {:?}): {} overflow",
6160 self, power, "i128"
6161 ))
6162 })
6163 }
6164}
6165impl Cpow for isize {
6166 type Power = u32;
6167 type Output = isize;
6168 type Error = Error;
6169 #[inline]
6173 fn cpow(self, power: u32) -> Result<isize, Error> {
6174 self.checked_pow(power).ok_or_else(|| {
6175 Error::new(format!(
6176 "failed to compute pow({:?}, {:?}): {} overflow",
6177 self, power, "isize"
6178 ))
6179 })
6180 }
6181}
6182impl Cpow for u8 {
6183 type Power = u32;
6184 type Output = u8;
6185 type Error = Error;
6186 #[inline]
6190 fn cpow(self, power: u32) -> Result<u8, Error> {
6191 self.checked_pow(power).ok_or_else(|| {
6192 Error::new(format!(
6193 "failed to compute pow({:?}, {:?}): {} overflow",
6194 self, power, "u8"
6195 ))
6196 })
6197 }
6198}
6199impl Cpow for u16 {
6200 type Power = u32;
6201 type Output = u16;
6202 type Error = Error;
6203 #[inline]
6207 fn cpow(self, power: u32) -> Result<u16, Error> {
6208 self.checked_pow(power).ok_or_else(|| {
6209 Error::new(format!(
6210 "failed to compute pow({:?}, {:?}): {} overflow",
6211 self, power, "u16"
6212 ))
6213 })
6214 }
6215}
6216impl Cpow for u32 {
6217 type Power = u32;
6218 type Output = u32;
6219 type Error = Error;
6220 #[inline]
6224 fn cpow(self, power: u32) -> Result<u32, Error> {
6225 self.checked_pow(power).ok_or_else(|| {
6226 Error::new(format!(
6227 "failed to compute pow({:?}, {:?}): {} overflow",
6228 self, power, "u32"
6229 ))
6230 })
6231 }
6232}
6233impl Cpow for u64 {
6234 type Power = u32;
6235 type Output = u64;
6236 type Error = Error;
6237 #[inline]
6241 fn cpow(self, power: u32) -> Result<u64, Error> {
6242 self.checked_pow(power).ok_or_else(|| {
6243 Error::new(format!(
6244 "failed to compute pow({:?}, {:?}): {} overflow",
6245 self, power, "u64"
6246 ))
6247 })
6248 }
6249}
6250impl Cpow for u128 {
6251 type Power = u32;
6252 type Output = u128;
6253 type Error = Error;
6254 #[inline]
6258 fn cpow(self, power: u32) -> Result<u128, Error> {
6259 self.checked_pow(power).ok_or_else(|| {
6260 Error::new(format!(
6261 "failed to compute pow({:?}, {:?}): {} overflow",
6262 self, power, "u128"
6263 ))
6264 })
6265 }
6266}
6267impl Cpow for usize {
6268 type Power = u32;
6269 type Output = usize;
6270 type Error = Error;
6271 #[inline]
6275 fn cpow(self, power: u32) -> Result<usize, Error> {
6276 self.checked_pow(power).ok_or_else(|| {
6277 Error::new(format!(
6278 "failed to compute pow({:?}, {:?}): {} overflow",
6279 self, power, "usize"
6280 ))
6281 })
6282 }
6283}
6284pub trait Cabs: Sized {
6288 #[expect(missing_docs, reason = "no need for doc")]
6289 type Output;
6290 #[expect(missing_docs, reason = "no need for doc")]
6291 type Error;
6292 fn cabs(self) -> Result<Self::Output, Self::Error>;
6296}
6297#[doc(alias = "checked_abs")]
6301#[inline]
6302pub fn cabs<T>(value: T) -> Result<T::Output, T::Error>
6303where
6304 T: Cabs,
6305{
6306 Cabs::cabs(value)
6307}
6308impl Cabs for NonZero<i8> {
6309 type Output = NonZero<i8>;
6310 type Error = Error;
6311 #[inline]
6315 fn cabs(self) -> Result<NonZero<i8>, Error> {
6316 self.checked_abs().ok_or_else(|| {
6317 Error::new(format!(
6318 "failed to compute abs({:?}): {} overflow",
6319 self, "NonZero<i8>"
6320 ))
6321 })
6322 }
6323}
6324impl Cabs for NonZero<i16> {
6325 type Output = NonZero<i16>;
6326 type Error = Error;
6327 #[inline]
6331 fn cabs(self) -> Result<NonZero<i16>, Error> {
6332 self.checked_abs().ok_or_else(|| {
6333 Error::new(format!(
6334 "failed to compute abs({:?}): {} overflow",
6335 self, "NonZero<i16>"
6336 ))
6337 })
6338 }
6339}
6340impl Cabs for NonZero<i32> {
6341 type Output = NonZero<i32>;
6342 type Error = Error;
6343 #[inline]
6347 fn cabs(self) -> Result<NonZero<i32>, Error> {
6348 self.checked_abs().ok_or_else(|| {
6349 Error::new(format!(
6350 "failed to compute abs({:?}): {} overflow",
6351 self, "NonZero<i32>"
6352 ))
6353 })
6354 }
6355}
6356impl Cabs for NonZero<i64> {
6357 type Output = NonZero<i64>;
6358 type Error = Error;
6359 #[inline]
6363 fn cabs(self) -> Result<NonZero<i64>, Error> {
6364 self.checked_abs().ok_or_else(|| {
6365 Error::new(format!(
6366 "failed to compute abs({:?}): {} overflow",
6367 self, "NonZero<i64>"
6368 ))
6369 })
6370 }
6371}
6372impl Cabs for NonZero<i128> {
6373 type Output = NonZero<i128>;
6374 type Error = Error;
6375 #[inline]
6379 fn cabs(self) -> Result<NonZero<i128>, Error> {
6380 self.checked_abs().ok_or_else(|| {
6381 Error::new(format!(
6382 "failed to compute abs({:?}): {} overflow",
6383 self, "NonZero<i128>"
6384 ))
6385 })
6386 }
6387}
6388impl Cabs for NonZero<isize> {
6389 type Output = NonZero<isize>;
6390 type Error = Error;
6391 #[inline]
6395 fn cabs(self) -> Result<NonZero<isize>, Error> {
6396 self.checked_abs().ok_or_else(|| {
6397 Error::new(format!(
6398 "failed to compute abs({:?}): {} overflow",
6399 self, "NonZero<isize>"
6400 ))
6401 })
6402 }
6403}
6404impl Cabs for i8 {
6405 type Output = i8;
6406 type Error = Error;
6407 #[inline]
6411 fn cabs(self) -> Result<i8, Error> {
6412 self.checked_abs().ok_or_else(|| {
6413 Error::new(format!(
6414 "failed to compute abs({:?}): {} overflow",
6415 self, "i8"
6416 ))
6417 })
6418 }
6419}
6420impl Cabs for i16 {
6421 type Output = i16;
6422 type Error = Error;
6423 #[inline]
6427 fn cabs(self) -> Result<i16, Error> {
6428 self.checked_abs().ok_or_else(|| {
6429 Error::new(format!(
6430 "failed to compute abs({:?}): {} overflow",
6431 self, "i16"
6432 ))
6433 })
6434 }
6435}
6436impl Cabs for i32 {
6437 type Output = i32;
6438 type Error = Error;
6439 #[inline]
6443 fn cabs(self) -> Result<i32, Error> {
6444 self.checked_abs().ok_or_else(|| {
6445 Error::new(format!(
6446 "failed to compute abs({:?}): {} overflow",
6447 self, "i32"
6448 ))
6449 })
6450 }
6451}
6452impl Cabs for i64 {
6453 type Output = i64;
6454 type Error = Error;
6455 #[inline]
6459 fn cabs(self) -> Result<i64, Error> {
6460 self.checked_abs().ok_or_else(|| {
6461 Error::new(format!(
6462 "failed to compute abs({:?}): {} overflow",
6463 self, "i64"
6464 ))
6465 })
6466 }
6467}
6468impl Cabs for i128 {
6469 type Output = i128;
6470 type Error = Error;
6471 #[inline]
6475 fn cabs(self) -> Result<i128, Error> {
6476 self.checked_abs().ok_or_else(|| {
6477 Error::new(format!(
6478 "failed to compute abs({:?}): {} overflow",
6479 self, "i128"
6480 ))
6481 })
6482 }
6483}
6484impl Cabs for isize {
6485 type Output = isize;
6486 type Error = Error;
6487 #[inline]
6491 fn cabs(self) -> Result<isize, Error> {
6492 self.checked_abs().ok_or_else(|| {
6493 Error::new(format!(
6494 "failed to compute abs({:?}): {} overflow",
6495 self, "isize"
6496 ))
6497 })
6498 }
6499}
6500pub trait Cisqrt: Sized {
6504 #[expect(missing_docs, reason = "no need for doc")]
6505 type Output;
6506 #[expect(missing_docs, reason = "no need for doc")]
6507 type Error;
6508 fn cisqrt(self) -> Result<Self::Output, Self::Error>;
6512}
6513#[doc(alias = "checked_isqrt")]
6517#[inline]
6518pub fn cisqrt<T>(value: T) -> Result<T::Output, T::Error>
6519where
6520 T: Cisqrt,
6521{
6522 Cisqrt::cisqrt(value)
6523}
6524impl Cisqrt for i8 {
6525 type Output = i8;
6526 type Error = Error;
6527 #[inline]
6531 fn cisqrt(self) -> Result<i8, Error> {
6532 self.checked_isqrt().ok_or_else(|| {
6533 Error::new(format!(
6534 "failed to compute isqrt({:?}): argument is negative",
6535 self
6536 ))
6537 })
6538 }
6539}
6540impl Cisqrt for i16 {
6541 type Output = i16;
6542 type Error = Error;
6543 #[inline]
6547 fn cisqrt(self) -> Result<i16, Error> {
6548 self.checked_isqrt().ok_or_else(|| {
6549 Error::new(format!(
6550 "failed to compute isqrt({:?}): argument is negative",
6551 self
6552 ))
6553 })
6554 }
6555}
6556impl Cisqrt for i32 {
6557 type Output = i32;
6558 type Error = Error;
6559 #[inline]
6563 fn cisqrt(self) -> Result<i32, Error> {
6564 self.checked_isqrt().ok_or_else(|| {
6565 Error::new(format!(
6566 "failed to compute isqrt({:?}): argument is negative",
6567 self
6568 ))
6569 })
6570 }
6571}
6572impl Cisqrt for i64 {
6573 type Output = i64;
6574 type Error = Error;
6575 #[inline]
6579 fn cisqrt(self) -> Result<i64, Error> {
6580 self.checked_isqrt().ok_or_else(|| {
6581 Error::new(format!(
6582 "failed to compute isqrt({:?}): argument is negative",
6583 self
6584 ))
6585 })
6586 }
6587}
6588impl Cisqrt for i128 {
6589 type Output = i128;
6590 type Error = Error;
6591 #[inline]
6595 fn cisqrt(self) -> Result<i128, Error> {
6596 self.checked_isqrt().ok_or_else(|| {
6597 Error::new(format!(
6598 "failed to compute isqrt({:?}): argument is negative",
6599 self
6600 ))
6601 })
6602 }
6603}
6604impl Cisqrt for isize {
6605 type Output = isize;
6606 type Error = Error;
6607 #[inline]
6611 fn cisqrt(self) -> Result<isize, Error> {
6612 self.checked_isqrt().ok_or_else(|| {
6613 Error::new(format!(
6614 "failed to compute isqrt({:?}): argument is negative",
6615 self
6616 ))
6617 })
6618 }
6619}
6620pub trait CnextMultipleOf: Sized {
6624 #[expect(missing_docs, reason = "no need for doc")]
6625 type Other;
6626 #[expect(missing_docs, reason = "no need for doc")]
6627 type Output;
6628 #[expect(missing_docs, reason = "no need for doc")]
6629 type Error;
6630 fn cnext_multiple_of(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
6634}
6635#[doc(alias = "checked_next_multiple_of")]
6641#[inline]
6642pub fn cnext_multiple_of<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
6643where
6644 T: CnextMultipleOf,
6645{
6646 a.cnext_multiple_of(b)
6647}
6648impl CnextMultipleOf for u8 {
6649 type Other = u8;
6650 type Output = u8;
6651 type Error = Error;
6652 #[inline]
6656 fn cnext_multiple_of(self, other: u8) -> Result<u8, Error> {
6657 self.checked_next_multiple_of(other).ok_or_else(|| {
6658 Error::new({
6659 if other < 2 {
6660 format!(
6661 "failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
6662 self, other
6663 )
6664 } else {
6665 format!(
6666 "failed to compute next_multiple_of({:?}, {:?}): {} overflow",
6667 self, other, "u8"
6668 )
6669 }
6670 })
6671 })
6672 }
6673}
6674impl CnextMultipleOf for u16 {
6675 type Other = u16;
6676 type Output = u16;
6677 type Error = Error;
6678 #[inline]
6682 fn cnext_multiple_of(self, other: u16) -> Result<u16, Error> {
6683 self.checked_next_multiple_of(other).ok_or_else(|| {
6684 Error::new({
6685 if other < 2 {
6686 format!(
6687 "failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
6688 self, other
6689 )
6690 } else {
6691 format!(
6692 "failed to compute next_multiple_of({:?}, {:?}): {} overflow",
6693 self, other, "u16"
6694 )
6695 }
6696 })
6697 })
6698 }
6699}
6700impl CnextMultipleOf for u32 {
6701 type Other = u32;
6702 type Output = u32;
6703 type Error = Error;
6704 #[inline]
6708 fn cnext_multiple_of(self, other: u32) -> Result<u32, Error> {
6709 self.checked_next_multiple_of(other).ok_or_else(|| {
6710 Error::new({
6711 if other < 2 {
6712 format!(
6713 "failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
6714 self, other
6715 )
6716 } else {
6717 format!(
6718 "failed to compute next_multiple_of({:?}, {:?}): {} overflow",
6719 self, other, "u32"
6720 )
6721 }
6722 })
6723 })
6724 }
6725}
6726impl CnextMultipleOf for u64 {
6727 type Other = u64;
6728 type Output = u64;
6729 type Error = Error;
6730 #[inline]
6734 fn cnext_multiple_of(self, other: u64) -> Result<u64, Error> {
6735 self.checked_next_multiple_of(other).ok_or_else(|| {
6736 Error::new({
6737 if other < 2 {
6738 format!(
6739 "failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
6740 self, other
6741 )
6742 } else {
6743 format!(
6744 "failed to compute next_multiple_of({:?}, {:?}): {} overflow",
6745 self, other, "u64"
6746 )
6747 }
6748 })
6749 })
6750 }
6751}
6752impl CnextMultipleOf for u128 {
6753 type Other = u128;
6754 type Output = u128;
6755 type Error = Error;
6756 #[inline]
6760 fn cnext_multiple_of(self, other: u128) -> Result<u128, Error> {
6761 self.checked_next_multiple_of(other).ok_or_else(|| {
6762 Error::new({
6763 if other < 2 {
6764 format!(
6765 "failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
6766 self, other
6767 )
6768 } else {
6769 format!(
6770 "failed to compute next_multiple_of({:?}, {:?}): {} overflow",
6771 self, other, "u128"
6772 )
6773 }
6774 })
6775 })
6776 }
6777}
6778impl CnextMultipleOf for usize {
6779 type Other = usize;
6780 type Output = usize;
6781 type Error = Error;
6782 #[inline]
6786 fn cnext_multiple_of(self, other: usize) -> Result<usize, Error> {
6787 self.checked_next_multiple_of(other).ok_or_else(|| {
6788 Error::new({
6789 if other < 2 {
6790 format!(
6791 "failed to compute next_multiple_of({:?}, {:?}): multiplier is zero",
6792 self, other
6793 )
6794 } else {
6795 format!(
6796 "failed to compute next_multiple_of({:?}, {:?}): {} overflow",
6797 self, other, "usize"
6798 )
6799 }
6800 })
6801 })
6802 }
6803}
6804pub trait CnextPowerOfTwo: Sized {
6808 #[expect(missing_docs, reason = "no need for doc")]
6809 type Output;
6810 #[expect(missing_docs, reason = "no need for doc")]
6811 type Error;
6812 fn cnext_power_of_two(self) -> Result<Self::Output, Self::Error>;
6816}
6817#[doc(alias = "checked_next_power_of_two")]
6821#[inline]
6822pub fn cnext_power_of_two<T>(value: T) -> Result<T::Output, T::Error>
6823where
6824 T: CnextPowerOfTwo,
6825{
6826 CnextPowerOfTwo::cnext_power_of_two(value)
6827}
6828impl CnextPowerOfTwo for NonZero<u8> {
6829 type Output = NonZero<u8>;
6830 type Error = Error;
6831 #[inline]
6835 fn cnext_power_of_two(self) -> Result<NonZero<u8>, Error> {
6836 self.checked_next_power_of_two().ok_or_else(|| {
6837 Error::new(format!(
6838 "failed to compute next_power_of_two({:?}): {} overflow",
6839 self, "NonZero<u8>"
6840 ))
6841 })
6842 }
6843}
6844impl CnextPowerOfTwo for NonZero<u16> {
6845 type Output = NonZero<u16>;
6846 type Error = Error;
6847 #[inline]
6851 fn cnext_power_of_two(self) -> Result<NonZero<u16>, Error> {
6852 self.checked_next_power_of_two().ok_or_else(|| {
6853 Error::new(format!(
6854 "failed to compute next_power_of_two({:?}): {} overflow",
6855 self, "NonZero<u16>"
6856 ))
6857 })
6858 }
6859}
6860impl CnextPowerOfTwo for NonZero<u32> {
6861 type Output = NonZero<u32>;
6862 type Error = Error;
6863 #[inline]
6867 fn cnext_power_of_two(self) -> Result<NonZero<u32>, Error> {
6868 self.checked_next_power_of_two().ok_or_else(|| {
6869 Error::new(format!(
6870 "failed to compute next_power_of_two({:?}): {} overflow",
6871 self, "NonZero<u32>"
6872 ))
6873 })
6874 }
6875}
6876impl CnextPowerOfTwo for NonZero<u64> {
6877 type Output = NonZero<u64>;
6878 type Error = Error;
6879 #[inline]
6883 fn cnext_power_of_two(self) -> Result<NonZero<u64>, Error> {
6884 self.checked_next_power_of_two().ok_or_else(|| {
6885 Error::new(format!(
6886 "failed to compute next_power_of_two({:?}): {} overflow",
6887 self, "NonZero<u64>"
6888 ))
6889 })
6890 }
6891}
6892impl CnextPowerOfTwo for NonZero<u128> {
6893 type Output = NonZero<u128>;
6894 type Error = Error;
6895 #[inline]
6899 fn cnext_power_of_two(self) -> Result<NonZero<u128>, Error> {
6900 self.checked_next_power_of_two().ok_or_else(|| {
6901 Error::new(format!(
6902 "failed to compute next_power_of_two({:?}): {} overflow",
6903 self, "NonZero<u128>"
6904 ))
6905 })
6906 }
6907}
6908impl CnextPowerOfTwo for NonZero<usize> {
6909 type Output = NonZero<usize>;
6910 type Error = Error;
6911 #[inline]
6915 fn cnext_power_of_two(self) -> Result<NonZero<usize>, Error> {
6916 self.checked_next_power_of_two().ok_or_else(|| {
6917 Error::new(format!(
6918 "failed to compute next_power_of_two({:?}): {} overflow",
6919 self, "NonZero<usize>"
6920 ))
6921 })
6922 }
6923}
6924impl CnextPowerOfTwo for u8 {
6925 type Output = u8;
6926 type Error = Error;
6927 #[inline]
6931 fn cnext_power_of_two(self) -> Result<u8, Error> {
6932 self.checked_next_power_of_two().ok_or_else(|| {
6933 Error::new(format!(
6934 "failed to compute next_power_of_two({:?}): {} overflow",
6935 self, "u8"
6936 ))
6937 })
6938 }
6939}
6940impl CnextPowerOfTwo for u16 {
6941 type Output = u16;
6942 type Error = Error;
6943 #[inline]
6947 fn cnext_power_of_two(self) -> Result<u16, Error> {
6948 self.checked_next_power_of_two().ok_or_else(|| {
6949 Error::new(format!(
6950 "failed to compute next_power_of_two({:?}): {} overflow",
6951 self, "u16"
6952 ))
6953 })
6954 }
6955}
6956impl CnextPowerOfTwo for u32 {
6957 type Output = u32;
6958 type Error = Error;
6959 #[inline]
6963 fn cnext_power_of_two(self) -> Result<u32, Error> {
6964 self.checked_next_power_of_two().ok_or_else(|| {
6965 Error::new(format!(
6966 "failed to compute next_power_of_two({:?}): {} overflow",
6967 self, "u32"
6968 ))
6969 })
6970 }
6971}
6972impl CnextPowerOfTwo for u64 {
6973 type Output = u64;
6974 type Error = Error;
6975 #[inline]
6979 fn cnext_power_of_two(self) -> Result<u64, Error> {
6980 self.checked_next_power_of_two().ok_or_else(|| {
6981 Error::new(format!(
6982 "failed to compute next_power_of_two({:?}): {} overflow",
6983 self, "u64"
6984 ))
6985 })
6986 }
6987}
6988impl CnextPowerOfTwo for u128 {
6989 type Output = u128;
6990 type Error = Error;
6991 #[inline]
6995 fn cnext_power_of_two(self) -> Result<u128, Error> {
6996 self.checked_next_power_of_two().ok_or_else(|| {
6997 Error::new(format!(
6998 "failed to compute next_power_of_two({:?}): {} overflow",
6999 self, "u128"
7000 ))
7001 })
7002 }
7003}
7004impl CnextPowerOfTwo for usize {
7005 type Output = usize;
7006 type Error = Error;
7007 #[inline]
7011 fn cnext_power_of_two(self) -> Result<usize, Error> {
7012 self.checked_next_power_of_two().ok_or_else(|| {
7013 Error::new(format!(
7014 "failed to compute next_power_of_two({:?}): {} overflow",
7015 self, "usize"
7016 ))
7017 })
7018 }
7019}
7020pub trait CdurationSince: Sized {
7024 #[expect(missing_docs, reason = "no need for doc")]
7025 type Other;
7026 #[expect(missing_docs, reason = "no need for doc")]
7027 type Output;
7028 #[expect(missing_docs, reason = "no need for doc")]
7029 type Error;
7030 fn cduration_since(self, other: Self::Other) -> Result<Self::Output, Self::Error>;
7034}
7035#[doc(alias = "checked_duration_since")]
7041#[inline]
7042pub fn cduration_since<T>(a: T, b: T::Other) -> Result<T::Output, T::Error>
7043where
7044 T: CdurationSince,
7045{
7046 a.cduration_since(b)
7047}
7048#[cfg(feature = "std")]
7049impl CdurationSince for Instant {
7050 type Other = Instant;
7051 type Output = Duration;
7052 type Error = Error;
7053 #[inline]
7057 fn cduration_since(self, other: Instant) -> Result<Duration, Error> {
7058 self.checked_duration_since(other).ok_or_else(|| {
7059 Error::new(format!(
7060 "failed to compute {:?}.duration_since({:?}): {:?} is earlier than {:?}",
7061 self, other, other, self
7062 ))
7063 })
7064 }
7065}