| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 |
|
| 6 | use core::{
|
| 7 | cmp,
|
| 8 | fmt,
|
| 9 | ops::{
|
| 10 | self,
|
| 11 | Deref, |
| 12 | }, |
| 13 | };
|
| 14 |
|
| 15 | use kernel::{
|
| 16 | num::{
|
| 17 | Integer,
|
| 18 | Unsigned, |
| 19 | },
|
| 20 | prelude::*, |
| 21 | };
|
| 22 |
|
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | macro_rules! fits_within {
|
| 29 | ($value:expr, $type:ty, $n:expr) => {{
|
| 30 | let shift: u32 = <$type>::BITS - $n;
|
| 31 |
|
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | ($value << shift) >> shift == $value
|
| 37 | }};
|
| 38 | }
|
| 39 |
|
| 40 | |
| 41 | #[inline(always)]
|
| 42 | fn fits_within<T: Integer>(value: T, num_bits: u32) -> bool {
|
| 43 | fits_within!(value, T, num_bits)
|
| 44 | }
|
| 45 |
|
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | #[repr(transparent)]
|
| 239 | #[derive(Clone, Copy, Debug, Default, Hash)]
|
| 240 | pub struct Bounded<T: Integer, const N: u32>(T);
|
| 241 |
|
| 242 | |
| 243 | |
| 244 | |
| 245 | macro_rules! impl_const_new {
|
| 246 | ($($type:ty)*) => {
|
| 247 | $(
|
| 248 | impl<const N: u32> Bounded<$type, N> {
|
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | #[doc = ::core::concat!(
|
| 261 | "let v = Bounded::<",
|
| 262 | ::core::stringify!($type),
|
| 263 | ", 4>::new::<7>();")]
|
| 264 | |
| 265 | |
| 266 | pub const fn new<const VALUE: $type>() -> Self {
|
| 267 | |
| 268 | const_assert!(fits_within!(VALUE, $type, N));
|
| 269 |
|
| 270 | |
| 271 | |
| 272 | unsafe { Self::__new(VALUE) }
|
| 273 | }
|
| 274 | }
|
| 275 | )*
|
| 276 | };
|
| 277 | }
|
| 278 |
|
| 279 | impl_const_new!(
|
| 280 | u8 u16 u32 u64 usize
|
| 281 | i8 i16 i32 i64 isize
|
| 282 | );
|
| 283 |
|
| 284 | impl<T, const N: u32> Bounded<T, N>
|
| 285 | where
|
| 286 | T: Integer,
|
| 287 | {
|
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | const unsafe fn __new(value: T) -> Self {
|
| 297 | |
| 298 | |
| 299 | const_assert!(N != 0);
|
| 300 | |
| 301 | const_assert!(N <= T::BITS);
|
| 302 |
|
| 303 | |
| 304 | Self(value)
|
| 305 | }
|
| 306 |
|
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | pub fn try_new(value: T) -> Option<Self> {
|
| 338 | fits_within(value, N).then(|| {
|
| 339 | |
| 340 | unsafe { Self::__new(value) }
|
| 341 | })
|
| 342 | }
|
| 343 |
|
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | #[inline(always)]
|
| 376 | pub fn from_expr(expr: T) -> Self {
|
| 377 | crate::build_assert::build_assert!(
|
| 378 | fits_within(expr, N),
|
| 379 | "Requested value larger than maximal representable value."
|
| 380 | );
|
| 381 |
|
| 382 | |
| 383 | unsafe { Self::__new(expr) }
|
| 384 | }
|
| 385 |
|
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | pub const fn get(self) -> T {
|
| 400 | self.0
|
| 401 | }
|
| 402 |
|
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | pub const fn extend<const M: u32>(self) -> Bounded<T, M> {
|
| 418 | const_assert!(
|
| 419 | M >= N,
|
| 420 | "Requested number of bits is less than the current representation."
|
| 421 | );
|
| 422 |
|
| 423 | |
| 424 | |
| 425 | unsafe { Bounded::__new(self.0) }
|
| 426 | }
|
| 427 |
|
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | pub fn try_shrink<const M: u32>(self) -> Option<Bounded<T, M>> {
|
| 447 | Bounded::<T, M>::try_new(self.get())
|
| 448 | }
|
| 449 |
|
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | pub fn cast<U>(self) -> Bounded<U, N>
|
| 470 | where
|
| 471 | U: TryFrom<T> + Integer,
|
| 472 | T: Integer,
|
| 473 | U: Integer<Signedness = T::Signedness>,
|
| 474 | {
|
| 475 | |
| 476 | |
| 477 | let value = unsafe { U::try_from(self.get()).unwrap_unchecked() };
|
| 478 |
|
| 479 | |
| 480 | |
| 481 | unsafe { Bounded::__new(value) }
|
| 482 | }
|
| 483 |
|
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | pub fn shr<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
|
| 498 | const_assert!(SHIFT < T::BITS);
|
| 499 | const_assert!(RES + SHIFT >= N);
|
| 500 |
|
| 501 | |
| 502 | |
| 503 | unsafe { Bounded::__new(self.0 >> SHIFT) }
|
| 504 | }
|
| 505 |
|
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | #[inline]
|
| 528 | pub fn shr_exact<const SHIFT: u32, const RES: u32>(self) -> Option<Bounded<T, RES>> {
|
| 529 | let shifted = self.shr::<SHIFT, RES>();
|
| 530 | if shifted.get() << SHIFT == self.0 {
|
| 531 | Some(shifted)
|
| 532 | } else {
|
| 533 | None
|
| 534 | }
|
| 535 | }
|
| 536 |
|
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | pub fn shl<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
|
| 551 | const_assert!(RES >= N + SHIFT);
|
| 552 |
|
| 553 | |
| 554 | |
| 555 | unsafe { Bounded::__new(self.0 << SHIFT) }
|
| 556 | }
|
| 557 | }
|
| 558 |
|
| 559 | impl<T, const N: u32> Deref for Bounded<T, N>
|
| 560 | where
|
| 561 | T: Integer,
|
| 562 | {
|
| 563 | type Target = T;
|
| 564 |
|
| 565 | fn deref(&self) -> &Self::Target {
|
| 566 | |
| 567 | if !fits_within(self.0, N) {
|
| 568 | |
| 569 | |
| 570 | unsafe { core::hint::unreachable_unchecked() }
|
| 571 | }
|
| 572 |
|
| 573 | &self.0
|
| 574 | }
|
| 575 | }
|
| 576 |
|
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | pub trait TryIntoBounded<T: Integer, const N: u32> {
|
| 593 | |
| 594 | |
| 595 | |
| 596 | fn try_into_bounded(self) -> Option<Bounded<T, N>>;
|
| 597 | }
|
| 598 |
|
| 599 | |
| 600 | impl<T, U, const N: u32> TryIntoBounded<T, N> for U
|
| 601 | where
|
| 602 | T: Integer,
|
| 603 | U: TryInto<T>,
|
| 604 | {
|
| 605 | fn try_into_bounded(self) -> Option<Bounded<T, N>> {
|
| 606 | self.try_into().ok().and_then(Bounded::try_new)
|
| 607 | }
|
| 608 | }
|
| 609 |
|
| 610 | |
| 611 |
|
| 612 | impl<T, U, const N: u32, const M: u32> PartialEq<Bounded<U, M>> for Bounded<T, N>
|
| 613 | where
|
| 614 | T: Integer,
|
| 615 | U: Integer,
|
| 616 | T: PartialEq<U>,
|
| 617 | {
|
| 618 | fn eq(&self, other: &Bounded<U, M>) -> bool {
|
| 619 | self.get() == other.get()
|
| 620 | }
|
| 621 | }
|
| 622 |
|
| 623 | impl<T, const N: u32> Eq for Bounded<T, N> where T: Integer {}
|
| 624 |
|
| 625 | impl<T, U, const N: u32, const M: u32> PartialOrd<Bounded<U, M>> for Bounded<T, N>
|
| 626 | where
|
| 627 | T: Integer,
|
| 628 | U: Integer,
|
| 629 | T: PartialOrd<U>,
|
| 630 | {
|
| 631 | fn partial_cmp(&self, other: &Bounded<U, M>) -> Option<cmp::Ordering> {
|
| 632 | self.get().partial_cmp(&other.get())
|
| 633 | }
|
| 634 | }
|
| 635 |
|
| 636 | impl<T, const N: u32> Ord for Bounded<T, N>
|
| 637 | where
|
| 638 | T: Integer,
|
| 639 | T: Ord,
|
| 640 | {
|
| 641 | fn cmp(&self, other: &Self) -> cmp::Ordering {
|
| 642 | self.get().cmp(&other.get())
|
| 643 | }
|
| 644 | }
|
| 645 |
|
| 646 | |
| 647 |
|
| 648 | impl<T, const N: u32> PartialEq<T> for Bounded<T, N>
|
| 649 | where
|
| 650 | T: Integer,
|
| 651 | T: PartialEq,
|
| 652 | {
|
| 653 | fn eq(&self, other: &T) -> bool {
|
| 654 | self.get() == *other
|
| 655 | }
|
| 656 | }
|
| 657 |
|
| 658 | impl<T, const N: u32> PartialOrd<T> for Bounded<T, N>
|
| 659 | where
|
| 660 | T: Integer,
|
| 661 | T: PartialOrd,
|
| 662 | {
|
| 663 | fn partial_cmp(&self, other: &T) -> Option<cmp::Ordering> {
|
| 664 | self.get().partial_cmp(other)
|
| 665 | }
|
| 666 | }
|
| 667 |
|
| 668 | |
| 669 |
|
| 670 | impl<T, const N: u32, const M: u32> ops::Add<Bounded<T, M>> for Bounded<T, N>
|
| 671 | where
|
| 672 | T: Integer,
|
| 673 | T: ops::Add<Output = T>,
|
| 674 | {
|
| 675 | type Output = T;
|
| 676 |
|
| 677 | fn add(self, rhs: Bounded<T, M>) -> Self::Output {
|
| 678 | self.get() + rhs.get()
|
| 679 | }
|
| 680 | }
|
| 681 |
|
| 682 | impl<T, const N: u32, const M: u32> ops::BitAnd<Bounded<T, M>> for Bounded<T, N>
|
| 683 | where
|
| 684 | T: Integer,
|
| 685 | T: ops::BitAnd<Output = T>,
|
| 686 | {
|
| 687 | type Output = T;
|
| 688 |
|
| 689 | fn bitand(self, rhs: Bounded<T, M>) -> Self::Output {
|
| 690 | self.get() & rhs.get()
|
| 691 | }
|
| 692 | }
|
| 693 |
|
| 694 | impl<T, const N: u32, const M: u32> ops::BitOr<Bounded<T, M>> for Bounded<T, N>
|
| 695 | where
|
| 696 | T: Integer,
|
| 697 | T: ops::BitOr<Output = T>,
|
| 698 | {
|
| 699 | type Output = T;
|
| 700 |
|
| 701 | fn bitor(self, rhs: Bounded<T, M>) -> Self::Output {
|
| 702 | self.get() | rhs.get()
|
| 703 | }
|
| 704 | }
|
| 705 |
|
| 706 | impl<T, const N: u32, const M: u32> ops::BitXor<Bounded<T, M>> for Bounded<T, N>
|
| 707 | where
|
| 708 | T: Integer,
|
| 709 | T: ops::BitXor<Output = T>,
|
| 710 | {
|
| 711 | type Output = T;
|
| 712 |
|
| 713 | fn bitxor(self, rhs: Bounded<T, M>) -> Self::Output {
|
| 714 | self.get() ^ rhs.get()
|
| 715 | }
|
| 716 | }
|
| 717 |
|
| 718 | impl<T, const N: u32, const M: u32> ops::Div<Bounded<T, M>> for Bounded<T, N>
|
| 719 | where
|
| 720 | T: Integer,
|
| 721 | T: ops::Div<Output = T>,
|
| 722 | {
|
| 723 | type Output = T;
|
| 724 |
|
| 725 | fn div(self, rhs: Bounded<T, M>) -> Self::Output {
|
| 726 | self.get() / rhs.get()
|
| 727 | }
|
| 728 | }
|
| 729 |
|
| 730 | impl<T, const N: u32, const M: u32> ops::Mul<Bounded<T, M>> for Bounded<T, N>
|
| 731 | where
|
| 732 | T: Integer,
|
| 733 | T: ops::Mul<Output = T>,
|
| 734 | {
|
| 735 | type Output = T;
|
| 736 |
|
| 737 | fn mul(self, rhs: Bounded<T, M>) -> Self::Output {
|
| 738 | self.get() * rhs.get()
|
| 739 | }
|
| 740 | }
|
| 741 |
|
| 742 | impl<T, const N: u32, const M: u32> ops::Rem<Bounded<T, M>> for Bounded<T, N>
|
| 743 | where
|
| 744 | T: Integer,
|
| 745 | T: ops::Rem<Output = T>,
|
| 746 | {
|
| 747 | type Output = T;
|
| 748 |
|
| 749 | fn rem(self, rhs: Bounded<T, M>) -> Self::Output {
|
| 750 | self.get() % rhs.get()
|
| 751 | }
|
| 752 | }
|
| 753 |
|
| 754 | impl<T, const N: u32, const M: u32> ops::Sub<Bounded<T, M>> for Bounded<T, N>
|
| 755 | where
|
| 756 | T: Integer,
|
| 757 | T: ops::Sub<Output = T>,
|
| 758 | {
|
| 759 | type Output = T;
|
| 760 |
|
| 761 | fn sub(self, rhs: Bounded<T, M>) -> Self::Output {
|
| 762 | self.get() - rhs.get()
|
| 763 | }
|
| 764 | }
|
| 765 |
|
| 766 | |
| 767 |
|
| 768 | impl<T, const N: u32> ops::Add<T> for Bounded<T, N>
|
| 769 | where
|
| 770 | T: Integer,
|
| 771 | T: ops::Add<Output = T>,
|
| 772 | {
|
| 773 | type Output = T;
|
| 774 |
|
| 775 | fn add(self, rhs: T) -> Self::Output {
|
| 776 | self.get() + rhs
|
| 777 | }
|
| 778 | }
|
| 779 |
|
| 780 | impl<T, const N: u32> ops::BitAnd<T> for Bounded<T, N>
|
| 781 | where
|
| 782 | T: Integer,
|
| 783 | T: ops::BitAnd<Output = T>,
|
| 784 | {
|
| 785 | type Output = T;
|
| 786 |
|
| 787 | fn bitand(self, rhs: T) -> Self::Output {
|
| 788 | self.get() & rhs
|
| 789 | }
|
| 790 | }
|
| 791 |
|
| 792 | impl<T, const N: u32> ops::BitOr<T> for Bounded<T, N>
|
| 793 | where
|
| 794 | T: Integer,
|
| 795 | T: ops::BitOr<Output = T>,
|
| 796 | {
|
| 797 | type Output = T;
|
| 798 |
|
| 799 | fn bitor(self, rhs: T) -> Self::Output {
|
| 800 | self.get() | rhs
|
| 801 | }
|
| 802 | }
|
| 803 |
|
| 804 | impl<T, const N: u32> ops::BitXor<T> for Bounded<T, N>
|
| 805 | where
|
| 806 | T: Integer,
|
| 807 | T: ops::BitXor<Output = T>,
|
| 808 | {
|
| 809 | type Output = T;
|
| 810 |
|
| 811 | fn bitxor(self, rhs: T) -> Self::Output {
|
| 812 | self.get() ^ rhs
|
| 813 | }
|
| 814 | }
|
| 815 |
|
| 816 | impl<T, const N: u32> ops::Div<T> for Bounded<T, N>
|
| 817 | where
|
| 818 | T: Integer,
|
| 819 | T: ops::Div<Output = T>,
|
| 820 | {
|
| 821 | type Output = T;
|
| 822 |
|
| 823 | fn div(self, rhs: T) -> Self::Output {
|
| 824 | self.get() / rhs
|
| 825 | }
|
| 826 | }
|
| 827 |
|
| 828 | impl<T, const N: u32> ops::Mul<T> for Bounded<T, N>
|
| 829 | where
|
| 830 | T: Integer,
|
| 831 | T: ops::Mul<Output = T>,
|
| 832 | {
|
| 833 | type Output = T;
|
| 834 |
|
| 835 | fn mul(self, rhs: T) -> Self::Output {
|
| 836 | self.get() * rhs
|
| 837 | }
|
| 838 | }
|
| 839 |
|
| 840 | impl<T, const N: u32> ops::Neg for Bounded<T, N>
|
| 841 | where
|
| 842 | T: Integer,
|
| 843 | T: ops::Neg<Output = T>,
|
| 844 | {
|
| 845 | type Output = T;
|
| 846 |
|
| 847 | fn neg(self) -> Self::Output {
|
| 848 | -self.get()
|
| 849 | }
|
| 850 | }
|
| 851 |
|
| 852 | impl<T, const N: u32> ops::Not for Bounded<T, N>
|
| 853 | where
|
| 854 | T: Integer,
|
| 855 | T: ops::Not<Output = T>,
|
| 856 | {
|
| 857 | type Output = T;
|
| 858 |
|
| 859 | fn not(self) -> Self::Output {
|
| 860 | !self.get()
|
| 861 | }
|
| 862 | }
|
| 863 |
|
| 864 | impl<T, const N: u32> ops::Rem<T> for Bounded<T, N>
|
| 865 | where
|
| 866 | T: Integer,
|
| 867 | T: ops::Rem<Output = T>,
|
| 868 | {
|
| 869 | type Output = T;
|
| 870 |
|
| 871 | fn rem(self, rhs: T) -> Self::Output {
|
| 872 | self.get() % rhs
|
| 873 | }
|
| 874 | }
|
| 875 |
|
| 876 | impl<T, const N: u32> ops::Sub<T> for Bounded<T, N>
|
| 877 | where
|
| 878 | T: Integer,
|
| 879 | T: ops::Sub<Output = T>,
|
| 880 | {
|
| 881 | type Output = T;
|
| 882 |
|
| 883 | fn sub(self, rhs: T) -> Self::Output {
|
| 884 | self.get() - rhs
|
| 885 | }
|
| 886 | }
|
| 887 |
|
| 888 | |
| 889 |
|
| 890 | impl<T, const N: u32> fmt::Display for Bounded<T, N>
|
| 891 | where
|
| 892 | T: Integer,
|
| 893 | T: fmt::Display,
|
| 894 | {
|
| 895 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 896 | self.get().fmt(f)
|
| 897 | }
|
| 898 | }
|
| 899 |
|
| 900 | impl<T, const N: u32> fmt::Binary for Bounded<T, N>
|
| 901 | where
|
| 902 | T: Integer,
|
| 903 | T: fmt::Binary,
|
| 904 | {
|
| 905 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 906 | self.get().fmt(f)
|
| 907 | }
|
| 908 | }
|
| 909 |
|
| 910 | impl<T, const N: u32> fmt::LowerExp for Bounded<T, N>
|
| 911 | where
|
| 912 | T: Integer,
|
| 913 | T: fmt::LowerExp,
|
| 914 | {
|
| 915 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 916 | self.get().fmt(f)
|
| 917 | }
|
| 918 | }
|
| 919 |
|
| 920 | impl<T, const N: u32> fmt::LowerHex for Bounded<T, N>
|
| 921 | where
|
| 922 | T: Integer,
|
| 923 | T: fmt::LowerHex,
|
| 924 | {
|
| 925 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 926 | self.get().fmt(f)
|
| 927 | }
|
| 928 | }
|
| 929 |
|
| 930 | impl<T, const N: u32> fmt::Octal for Bounded<T, N>
|
| 931 | where
|
| 932 | T: Integer,
|
| 933 | T: fmt::Octal,
|
| 934 | {
|
| 935 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 936 | self.get().fmt(f)
|
| 937 | }
|
| 938 | }
|
| 939 |
|
| 940 | impl<T, const N: u32> fmt::UpperExp for Bounded<T, N>
|
| 941 | where
|
| 942 | T: Integer,
|
| 943 | T: fmt::UpperExp,
|
| 944 | {
|
| 945 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 946 | self.get().fmt(f)
|
| 947 | }
|
| 948 | }
|
| 949 |
|
| 950 | impl<T, const N: u32> fmt::UpperHex for Bounded<T, N>
|
| 951 | where
|
| 952 | T: Integer,
|
| 953 | T: fmt::UpperHex,
|
| 954 | {
|
| 955 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 956 | self.get().fmt(f)
|
| 957 | }
|
| 958 | }
|
| 959 |
|
| 960 | |
| 961 | |
| 962 | |
| 963 | macro_rules! impl_size_rule {
|
| 964 | ($trait:ty, $($num_bits:literal)*) => {
|
| 965 | $(
|
| 966 | impl<T> $trait for Bounded<T, $num_bits> where T: Integer {}
|
| 967 | )*
|
| 968 | };
|
| 969 | }
|
| 970 |
|
| 971 | |
| 972 | |
| 973 | trait AtLeastXBits<const N: usize> {}
|
| 974 |
|
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | mod atleast_impls {
|
| 981 | use super::*;
|
| 982 |
|
| 983 | |
| 984 | impl_size_rule!(AtLeastXBits<64>, 64);
|
| 985 |
|
| 986 | |
| 987 | impl<T> AtLeastXBits<32> for T where T: AtLeastXBits<64> {}
|
| 988 | |
| 989 | impl_size_rule!(AtLeastXBits<32>,
|
| 990 | 32 33 34 35 36 37 38 39
|
| 991 | 40 41 42 43 44 45 46 47
|
| 992 | 48 49 50 51 52 53 54 55
|
| 993 | 56 57 58 59 60 61 62 63
|
| 994 | );
|
| 995 |
|
| 996 | |
| 997 | impl<T> AtLeastXBits<16> for T where T: AtLeastXBits<32> {}
|
| 998 | |
| 999 | impl_size_rule!(AtLeastXBits<16>,
|
| 1000 | 16 17 18 19 20 21 22 23
|
| 1001 | 24 25 26 27 28 29 30 31
|
| 1002 | );
|
| 1003 |
|
| 1004 | |
| 1005 | impl<T> AtLeastXBits<8> for T where T: AtLeastXBits<16> {}
|
| 1006 | |
| 1007 | impl_size_rule!(AtLeastXBits<8>, 8 9 10 11 12 13 14 15);
|
| 1008 | }
|
| 1009 |
|
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | macro_rules! impl_from_primitive {
|
| 1018 | ($($type:ty)*) => {
|
| 1019 | $(
|
| 1020 | #[doc = ::core::concat!(
|
| 1021 | "Conversion from a [`",
|
| 1022 | ::core::stringify!($type),
|
| 1023 | "`] into a [`Bounded`] of same signedness with enough bits to store it.")]
|
| 1024 | impl<T, const N: u32> From<$type> for Bounded<T, N>
|
| 1025 | where
|
| 1026 | $type: Integer,
|
| 1027 | T: Integer<Signedness = <$type as Integer>::Signedness> + From<$type>,
|
| 1028 | Self: AtLeastXBits<{ <$type as Integer>::BITS as usize }>,
|
| 1029 | {
|
| 1030 | fn from(value: $type) -> Self {
|
| 1031 | |
| 1032 | |
| 1033 | unsafe { Self::__new(T::from(value)) }
|
| 1034 | }
|
| 1035 | }
|
| 1036 | )*
|
| 1037 | }
|
| 1038 | }
|
| 1039 |
|
| 1040 | impl_from_primitive!(
|
| 1041 | u8 u16 u32 u64 usize
|
| 1042 | i8 i16 i32 i64 isize
|
| 1043 | );
|
| 1044 |
|
| 1045 | |
| 1046 | |
| 1047 | trait FitsInXBits<const N: usize> {}
|
| 1048 |
|
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | mod fits_impls {
|
| 1055 | use super::*;
|
| 1056 |
|
| 1057 | |
| 1058 | impl_size_rule!(FitsInXBits<8>, 1 2 3 4 5 6 7 8);
|
| 1059 |
|
| 1060 | |
| 1061 | impl<T> FitsInXBits<16> for T where T: FitsInXBits<8> {}
|
| 1062 | |
| 1063 | impl_size_rule!(FitsInXBits<16>, 9 10 11 12 13 14 15 16);
|
| 1064 |
|
| 1065 | |
| 1066 | impl<T> FitsInXBits<32> for T where T: FitsInXBits<16> {}
|
| 1067 | |
| 1068 | impl_size_rule!(FitsInXBits<32>,
|
| 1069 | 17 18 19 20 21 22 23 24
|
| 1070 | 25 26 27 28 29 30 31 32
|
| 1071 | );
|
| 1072 |
|
| 1073 | |
| 1074 | impl<T> FitsInXBits<64> for T where T: FitsInXBits<32> {}
|
| 1075 | |
| 1076 | impl_size_rule!(FitsInXBits<64>,
|
| 1077 | 33 34 35 36 37 38 39 40
|
| 1078 | 41 42 43 44 45 46 47 48
|
| 1079 | 49 50 51 52 53 54 55 56
|
| 1080 | 57 58 59 60 61 62 63 64
|
| 1081 | );
|
| 1082 | }
|
| 1083 |
|
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | macro_rules! impl_into_primitive {
|
| 1092 | ($($type:ty)*) => {
|
| 1093 | $(
|
| 1094 | #[doc = ::core::concat!(
|
| 1095 | "Conversion from a [`Bounded`] with no more bits than a [`",
|
| 1096 | ::core::stringify!($type),
|
| 1097 | "`] and of same signedness into [`",
|
| 1098 | ::core::stringify!($type),
|
| 1099 | "`]")]
|
| 1100 | impl<T, const N: u32> From<Bounded<T, N>> for $type
|
| 1101 | where
|
| 1102 | $type: Integer + TryFrom<T>,
|
| 1103 | T: Integer<Signedness = <$type as Integer>::Signedness>,
|
| 1104 | Bounded<T, N>: FitsInXBits<{ <$type as Integer>::BITS as usize }>,
|
| 1105 | {
|
| 1106 | fn from(value: Bounded<T, N>) -> $type {
|
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | unsafe { <$type>::try_from(value.get()).unwrap_unchecked() }
|
| 1111 | }
|
| 1112 | }
|
| 1113 | )*
|
| 1114 | }
|
| 1115 | }
|
| 1116 |
|
| 1117 | impl_into_primitive!(
|
| 1118 | u8 u16 u32 u64 usize
|
| 1119 | i8 i16 i32 i64 isize
|
| 1120 | );
|
| 1121 |
|
| 1122 | |
| 1123 |
|
| 1124 | impl<T> From<Bounded<T, 1>> for bool
|
| 1125 | where
|
| 1126 | T: Integer<Signedness = Unsigned> + Zeroable,
|
| 1127 | {
|
| 1128 | fn from(value: Bounded<T, 1>) -> Self {
|
| 1129 | value.get() != Zeroable::zeroed()
|
| 1130 | }
|
| 1131 | }
|
| 1132 |
|
| 1133 | |
| 1134 |
|
| 1135 | impl<T, const N: u32> From<bool> for Bounded<T, N>
|
| 1136 | where
|
| 1137 | T: Integer<Signedness = Unsigned> + From<bool>,
|
| 1138 | {
|
| 1139 | fn from(value: bool) -> Self {
|
| 1140 | |
| 1141 | |
| 1142 | unsafe { Self::__new(T::from(value)) }
|
| 1143 | }
|
| 1144 | }
|
| 1145 |
|
| 1146 | impl<T> Bounded<T, 1>
|
| 1147 | where
|
| 1148 | T: Integer<Signedness = Unsigned> + Zeroable,
|
| 1149 | {
|
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | pub fn into_bool(self) -> bool {
|
| 1163 | self.into()
|
| 1164 | }
|
| 1165 | }
|