1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 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 338 339 340 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
use std::cmp::Ordering;
#[cfg(feature = "time")]
use tea_time::{DateTime, Time, TimeDelta, TimeUnitTrait};
use super::cast::Cast;
use super::number::Number;
/// A trait for types that can represent a "none" or null-like state.
///
/// This trait is implemented by types that can be in a "none" state,
/// similar to Rust's `Option` type. It provides methods for working
/// with such types in a generic way.
///
/// # Type Parameters
///
/// * `Inner`: The inner type. For regular types `T`, `Inner` is `T` itself.
/// For `Option<T>`, `Inner` is `T`. This allows `Option<T::Inner>` to be
/// the same type for both `T` and `Option<T>`.
///
/// * `Cast<U>`: A type that represents the result of casting to another `IsNone` type.
/// For regular types, `Cast<U>` is typically `U`. For `Option<T>`, `Cast<U>`
/// is `Option<U>`. This allows operations like `Option<T>::cast<f64>()` to result
/// in `Option<f64>`, while `T::cast<f64>()` results in `f64`.
///
/// # Required Methods
///
/// Implementors must define:
/// - `is_none`: Check if the value is in the "none" state.
/// - `none`: Create a new instance in the "none" state.
/// - `to_opt`: Convert to an `Option<Inner>`.
/// - `as_opt`: Get a reference as an `Option<&Inner>`.
/// - `from_inner`: Create an instance from an `Inner` value.
/// - `inner_cast`: Cast between different `IsNone` types.
///
/// # Provided Methods
///
/// The trait also provides default implementations for:
/// - `from_opt`: Create an instance from an `Option<Inner>`.
/// - `unwrap`: Get the inner value, panicking if none.
/// - `not_none`: Check if the value is not in the "none" state.
/// - `map`: Apply a function to the inner value if not none.
pub trait IsNone: Clone
where
Self: Sized,
{
type Inner: IsNone<Inner = Self::Inner>;
type Cast<U: IsNone<Inner = U>>: IsNone<Inner = U>;
/// Checks if the value is in the "none" state.
///
/// # Returns
///
/// `true` if the value is in the "none" state, `false` otherwise.
fn is_none(&self) -> bool;
/// Creates a new instance of `Self` in the "none" state.
///
/// This method is used to generate a value that represents the absence of a valid value,
/// similar to `None` in Rust's `Option` type.
///
/// # Returns
///
/// Returns a new instance of `Self` that is considered to be in the "none" state.
// TODO: some type doesn't have none value, so it should return a `TResult<Self>`
fn none() -> Self;
/// Converts the value to an `Option<Self::Inner>`.
///
/// This method transforms the current value into an `Option` type, where:
/// - If the current value is in the "none" state, it returns `None`.
/// - Otherwise, it returns `Some(inner)`, where `inner` is the wrapped value.
///
/// # Returns
///
/// An `Option<Self::Inner>` representing the current value.
fn to_opt(self) -> Option<Self::Inner>;
/// Converts the value to an `Option<&Self::Inner>`.
///
/// This method returns a reference to the inner value as an `Option` type, where:
/// - If the current value is in the "none" state, it returns `None`.
/// - Otherwise, it returns `Some(&inner)`, where `inner` is a reference to the wrapped value.
///
/// # Returns
///
/// An `Option<&Self::Inner>` representing a reference to the current value.
fn as_opt(&self) -> Option<&Self::Inner>;
/// Creates a new instance of `Self` from the given inner value.
///
/// This method converts a value of type `Self::Inner` into `Self`.
/// For example, if `Self` is `f64`, `Self::Inner` is also `f64`, so this method
/// would essentially be an identity function. However, if `Self` is `Option<f64>`,
/// `Self::Inner` would be `f64`, so this method would wrap the `f64` value in `Some`.
///
/// # Arguments
///
/// * `inner` - The inner value to be converted.
///
/// # Returns
///
/// Returns a new instance of `Self` created from the provided inner value.
fn from_inner(inner: Self::Inner) -> Self;
/// Casts the inner type of `Self` to a new type `U`.
///
/// This method allows for casting between different `IsNone` types, preserving the "none" state
/// if applicable. It uses the `Cast` trait to perform the actual type conversion.
///
/// # Type Parameters
///
/// * `U`: The target type, which must implement `IsNone` and have `Inner = U`.
///
/// # Arguments
///
/// * `inner`: The value of type `U` to be cast.
///
/// # Returns
///
/// Returns `Self::Cast<U>`, which is the result of casting `Self` to a type that can hold `U`.
fn inner_cast<U: IsNone<Inner = U>>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>;
#[inline]
/// Creates a new instance of `Self` from an `Option<Self::Inner>`.
fn from_opt(opt: Option<Self::Inner>) -> Self {
opt.map_or_else(Self::none, Self::from_inner)
}
#[inline]
fn unwrap(self) -> Self::Inner {
self.to_opt().unwrap()
}
#[inline]
fn not_none(&self) -> bool {
!self.is_none()
}
/// Maps a function over the inner value of `Self`, if it exists.
///
/// This method applies a given function to the inner value of `Self` if it's not none,
/// and wraps the result in a new `IsNone` type `U`. If `Self` is none, it returns
/// the none value of type `U`.
///
/// # Type Parameters
///
/// * `F`: The type of the mapping function.
/// * `U`: The target `IsNone` type.
///
/// # Arguments
///
/// * `self`: The `IsNone` value to map over.
/// * `f`: A function that takes `Self::Inner` and returns `U::Inner`.
///
/// # Returns
///
/// Returns a new `IsNone` value of type `U`, which is either:
/// - The result of applying `f` to the inner value, wrapped in `U`, if `self` is not none.
/// - The none value of `U`, if `self` is none.
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
self.to_opt()
.map(|v| U::from_inner(f(v)))
.unwrap_or_else(|| U::none())
}
/// Computes the absolute value of the inner value, if it exists.
///
/// This method applies the absolute value function to the inner value of `Self` if it's not none.
/// If `Self` is none, it returns the none value.
///
/// # Type Constraints
///
/// * `Self::Inner`: Must implement the `Number` trait, which provides the `abs()` method.
///
/// # Returns
///
/// Returns a new `Self` instance containing:
/// - The absolute value of the inner value, if `self` is not none.
/// - The none value of `Self`, if `self` is none.
#[inline]
fn vabs(self) -> Self
where
Self::Inner: Number,
{
self.map(|v| v.abs())
}
#[inline]
/// Compares two values for sorting, treating `None` as the largest value.
///
/// This method is designed for sorting `Some` values from smallest to largest,
/// with `None` values considered larger than any non-`None` value.
///
/// # Arguments
///
/// * `self` - The first `IsNone` value to compare.
/// * `other` - The second `IsNone` value to compare.
///
/// # Returns
///
/// Returns an `Ordering` that can be used for sorting:
/// - `Ordering::Less` if `self` is less than `other`.
/// - `Ordering::Equal` if `self` is equal to `other`.
/// - `Ordering::Greater` if `self` is greater than `other` or if `self` is `None`.
///
/// # Type Constraints
///
/// * `Self::Inner`: Must implement the `PartialOrd` trait.
///
/// # Notes
///
/// - If both values are `Some`, their inner values are compared using `partial_cmp`.
/// - If the inner values can't be compared (e.g., NaN for floats), `None` is considered greater.
/// - If both values are `None`, they are considered equal.
/// - A `None` value is always considered greater than any `Some` value.
fn sort_cmp(&self, other: &Self) -> Ordering
where
Self::Inner: PartialOrd,
{
match (self.as_opt(), other.as_opt()) {
(Some(va), Some(vb)) => va.partial_cmp(vb).unwrap_or_else(|| {
if va.is_none() {
Ordering::Greater
} else {
Ordering::Less
}
}),
(None, None) => Ordering::Equal,
(None, _) => Ordering::Greater,
(_, None) => Ordering::Less,
}
}
#[inline]
/// Compares two values for reverse sorting, treating `None` as the largest value.
///
/// This method is designed for sorting `Some` values from largest to smallest,
/// with `None` values considered larger than any non-`None` value.
///
/// # Arguments
///
/// * `self` - The first `IsNone` value to compare.
/// * `other` - The second `IsNone` value to compare.
///
/// # Returns
///
/// Returns an `Ordering` that can be used for reverse sorting:
/// - `Ordering::Less` if `self` is greater than `other`.
/// - `Ordering::Equal` if `self` is equal to `other`.
/// - `Ordering::Greater` if `self` is less than `other` or if `self` is `None`.
///
/// # Type Constraints
///
/// * `Self::Inner`: Must implement the `PartialOrd` trait.
///
/// # Notes
///
/// - If both values are `Some`, their inner values are compared using `partial_cmp` and then reversed.
/// - If the inner values can't be compared (e.g., NaN for floats), `None` is considered greater.
/// - If both values are `None`, they are considered equal.
/// - A `None` value is always considered greater than any `Some` value.
fn sort_cmp_rev(&self, other: &Self) -> Ordering
where
Self::Inner: PartialOrd,
{
match (self.as_opt(), other.as_opt()) {
(Some(va), Some(vb)) => va
.partial_cmp(vb)
.unwrap_or_else(|| {
if va.is_none() {
Ordering::Less
} else {
Ordering::Greater
}
})
.reverse(),
(None, None) => Ordering::Equal,
(None, _) => Ordering::Greater,
(_, None) => Ordering::Less,
}
}
}
pub trait IntoCast: IsNone<Inner = Self> + Clone + Sized {
#[inline]
fn into_cast<T: IsNone>(self) -> T::Cast<Self>
where
T::Inner: Cast<Self::Inner>,
{
T::inner_cast(self)
}
}
impl<U: IsNone<Inner = U> + Clone> IntoCast for U {}
impl IsNone for f32 {
type Inner = f32;
type Cast<U: IsNone<Inner = U> + Clone> = U;
#[inline]
#[allow(clippy::eq_op)]
fn is_none(&self) -> bool {
self != self
}
#[inline]
fn none() -> Self {
f32::NAN
}
#[inline]
fn to_opt(self) -> Option<Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
#[allow(clippy::eq_op)]
fn not_none(&self) -> bool {
self == self
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
U::from_inner(f(self))
}
}
impl IsNone for f64 {
type Inner = f64;
type Cast<U: IsNone<Inner = U> + Clone> = U;
#[inline]
#[allow(clippy::eq_op)]
fn is_none(&self) -> bool {
self != self
}
#[inline]
fn none() -> Self {
f64::NAN
}
#[inline]
fn to_opt(self) -> Option<Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
#[allow(clippy::eq_op)]
fn not_none(&self) -> bool {
self == self
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
U::from_inner(f(self))
}
}
impl<T: IsNone<Inner = T>> IsNone for Option<T> {
type Inner = T;
type Cast<U: IsNone<Inner = U> + Clone> = Option<U>;
#[inline]
fn is_none(&self) -> bool {
self.is_none()
}
#[inline]
fn none() -> Self {
None
}
#[inline(always)]
fn to_opt(self) -> Option<Self::Inner> {
self
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
self.as_ref()
}
#[inline]
fn from_inner(inner: Self::Inner) -> Self {
if inner.is_none() {
None
} else {
Some(inner)
}
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
if inner.is_none() {
None
} else {
Some(Cast::<U>::cast(inner))
}
}
#[inline]
fn not_none(&self) -> bool {
self.is_some()
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
self.map(|v| U::from_inner(f(v)))
.unwrap_or_else(|| U::none())
}
}
macro_rules! impl_not_none {
($($type: ty),*) => {
$(
impl IsNone for $type {
type Inner = $type;
type Cast<U: IsNone<Inner=U> + Clone> = U;
#[inline]
#[allow(clippy::eq_op)]
fn is_none(&self) -> bool {
false
}
fn none() -> Self {
panic!("Cannot call none() on a non-float type");
}
#[inline(always)]
fn to_opt(self) -> Option<Self::Inner> {
Some(self)
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
Some(self)
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner=U> + Clone>(inner: U) -> Self::Cast<U>
where Self::Inner: Cast<U::Inner>
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
#[allow(clippy::eq_op)]
fn not_none(&self) -> bool {
true
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner
{
U::from_inner(f(self))
}
#[inline]
/// only for sorting(from smallest to largest)
fn sort_cmp(&self, other: &Self) -> Ordering
where
Self: PartialOrd,
{
self.partial_cmp(&other).unwrap()
}
}
)*
};
}
impl_not_none!(bool, u8, i32, i64, isize, u64, usize);
impl IsNone for String {
type Inner = String;
type Cast<U: IsNone<Inner = U> + Clone> = U;
#[inline]
fn is_none(&self) -> bool {
self == "None"
}
#[inline]
fn none() -> Self {
"None".to_string()
}
#[inline]
fn to_opt(self) -> Option<Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
U::from_inner(f(self))
}
}
impl<'a> IsNone for &'a str {
type Inner = &'a str;
type Cast<U: IsNone<Inner = U> + Clone> = U;
#[inline]
fn is_none(&self) -> bool {
*self == "None"
}
#[inline]
fn none() -> Self {
"None"
}
#[inline]
fn to_opt(self) -> Option<Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
U::from_inner(f(self))
}
}
#[cfg(feature = "time")]
impl<Unit: TimeUnitTrait> IsNone for DateTime<Unit> {
type Inner = DateTime<Unit>;
type Cast<U: IsNone<Inner = U> + Clone> = U;
#[inline]
fn is_none(&self) -> bool {
self.is_nat()
}
#[inline]
fn none() -> Self {
DateTime::nat()
}
#[inline]
fn to_opt(self) -> Option<Self::Inner> {
if self.is_nat() {
None
} else {
Some(self)
}
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
U::from_inner(f(self))
}
}
#[cfg(feature = "time")]
impl IsNone for TimeDelta {
type Inner = TimeDelta;
type Cast<U: IsNone<Inner = U> + Clone> = U;
#[inline]
fn is_none(&self) -> bool {
self.is_nat()
}
#[inline]
fn none() -> Self {
Self::nat()
}
#[inline]
fn to_opt(self) -> Option<Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
U::from_inner(f(self))
}
}
#[cfg(feature = "time")]
impl IsNone for Time {
type Inner = Time;
type Cast<U: IsNone<Inner = U> + Clone> = U;
#[inline]
fn is_none(&self) -> bool {
self.is_nat()
}
#[inline]
fn none() -> Self {
Self::nat()
}
#[inline]
fn to_opt(self) -> Option<Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
U::from_inner(f(self))
}
}
impl<T: Clone> IsNone for Vec<T> {
type Inner = Vec<T>;
type Cast<U: IsNone<Inner = U>> = U;
#[inline]
fn is_none(&self) -> bool {
self.is_empty()
}
#[inline]
fn none() -> Self {
Vec::new()
}
#[inline]
fn to_opt(self) -> Option<Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline]
fn as_opt(&self) -> Option<&Self::Inner> {
if self.is_none() {
None
} else {
Some(self)
}
}
#[inline(always)]
fn from_inner(inner: Self::Inner) -> Self {
inner
}
#[inline]
fn inner_cast<U: IsNone<Inner = U> + Clone>(inner: U) -> Self::Cast<U>
where
Self::Inner: Cast<U::Inner>,
{
Cast::<U>::cast(inner)
}
#[inline(always)]
fn unwrap(self) -> Self::Inner {
self
}
#[inline]
fn map<F, U: IsNone>(self, f: F) -> U
where
F: Fn(Self::Inner) -> U::Inner,
{
U::from_inner(f(self))
}
}
#[cfg(test)]
mod tests {
use crate::{Cast, IsNone};
#[test]
fn test_str() {
let a = "dsf";
assert_eq!(a.unwrap(), "dsf");
assert_eq!(a.to_opt(), Some("dsf"));
let a = Some("dsf");
assert_eq!(a.to_opt(), Some("dsf"));
}
#[test]
fn test_type_cast() {
fn test1<T: IsNone>(_v: T) -> f64
where
T::Inner: Cast<f64>,
{
let out = T::inner_cast(0.);
out.unwrap()
}
assert_eq!(0., test1(2_i32));
assert_eq!(0., test1(Some(3_usize)));
fn test2<T: IsNone>(_v: T) -> T::Cast<f64>
where
T::Inner: Cast<f64>,
{
T::inner_cast(0.)
}
assert_eq!(0., test2(2_i32));
assert_eq!(Some(0.), test2(Some(3_usize)));
fn test3<T: IsNone>(_v: T) -> f64
where
T::Inner: Cast<f64>,
{
let out = T::inner_cast(0.);
let v = out.unwrap();
if v > 1. {
v + 1.
} else {
v + 2.
}
}
assert_eq!(2., test3(1_i32));
}
#[test]
fn test_unwrap() {
let v = Some(f64::NAN);
assert!(v.not_none());
assert!(IsNone::unwrap(v).is_nan());
let v: Option<i32> = None;
assert!(!v.not_none());
let v = f64::NAN;
assert!(v.is_none());
let v = 1;
assert!(!v.is_none());
}
}