yoke/
either.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5//! Types to enable polymorphic carts.
6
7use crate::CloneableCart;
8
9use core::ops::Deref;
10use stable_deref_trait::StableDeref;
11
12/// A cart that can be one type or the other. Enables ergonomic polymorphic carts.
13///
14/// `EitherCart` enables yokes originating from different data sources and therefore
15/// having different cart types to be merged into the same yoke type, but still being
16/// able to recover the original cart type if necessary.
17///
18/// All relevant Cart traits are implemented for `EitherCart`, and carts can be
19/// safely wrapped in an `EitherCart`.
20///
21/// Also see [`Yoke::erase_box_cart()`](crate::Yoke::erase_box_cart).
22///
23/// # Examples
24///
25/// ```
26/// use std::rc::Rc;
27/// use yoke::either::EitherCart;
28/// use yoke::Yoke;
29///
30/// let y1: Yoke<&'static str, Rc<str>> =
31///     Yoke::attach_to_zero_copy_cart("reference counted hello world".into());
32///
33/// let y2: Yoke<&'static str, &str> = Yoke::attach_to_zero_copy_cart("borrowed hello world");
34///
35/// type CombinedYoke<'a> = Yoke<&'static str, EitherCart<Rc<str>, &'a str>>;
36///
37/// // Both yokes can be combined into a single yoke type despite different carts
38/// let y3: CombinedYoke = y1.wrap_cart_in_either_a();
39/// let y4: CombinedYoke = y2.wrap_cart_in_either_b();
40///
41/// assert_eq!(*y3.get(), "reference counted hello world");
42/// assert_eq!(*y4.get(), "borrowed hello world");
43///
44/// // The resulting yoke is cloneable if both cart types implement CloneableCart
45/// let y5 = y4.clone();
46/// assert_eq!(*y5.get(), "borrowed hello world");
47/// ```
48#[derive(#[automatically_derived]
#[allow(clippy::exhaustive_enums)]
impl<C0: ::core::clone::Clone, C1: ::core::clone::Clone> ::core::clone::Clone
    for EitherCart<C0, C1> {
    #[inline]
    fn clone(&self) -> EitherCart<C0, C1> {
        match self {
            EitherCart::A(__self_0) =>
                EitherCart::A(::core::clone::Clone::clone(__self_0)),
            EitherCart::B(__self_0) =>
                EitherCart::B(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone, #[automatically_derived]
#[allow(clippy::exhaustive_enums)]
impl<C0: ::core::cmp::PartialEq, C1: ::core::cmp::PartialEq>
    ::core::cmp::PartialEq for EitherCart<C0, C1> {
    #[inline]
    fn eq(&self, other: &EitherCart<C0, C1>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (EitherCart::A(__self_0), EitherCart::A(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (EitherCart::B(__self_0), EitherCart::B(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
#[allow(clippy::exhaustive_enums)]
impl<C0: ::core::cmp::Eq, C1: ::core::cmp::Eq> ::core::cmp::Eq for
    EitherCart<C0, C1> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) -> () {
        let _: ::core::cmp::AssertParamIsEq<C0>;
        let _: ::core::cmp::AssertParamIsEq<C1>;
    }
}Eq, #[automatically_derived]
#[allow(clippy::exhaustive_enums)]
impl<C0: ::core::fmt::Debug, C1: ::core::fmt::Debug> ::core::fmt::Debug for
    EitherCart<C0, C1> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            EitherCart::A(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "A",
                    &__self_0),
            EitherCart::B(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "B",
                    &__self_0),
        }
    }
}Debug)]
49#[allow(clippy::exhaustive_enums)] // stable
50pub enum EitherCart<C0, C1> {
51    A(C0),
52    B(C1),
53}
54
55impl<C0, C1, T> Deref for EitherCart<C0, C1>
56where
57    C0: Deref<Target = T>,
58    C1: Deref<Target = T>,
59    T: ?Sized,
60{
61    type Target = T;
62    fn deref(&self) -> &T {
63        use EitherCart::*;
64        match self {
65            A(a) => a.deref(),
66            B(b) => b.deref(),
67        }
68    }
69}
70
71// Safety: Safe because both sub-types implement the trait.
72unsafe impl<C0, C1, T> StableDeref for EitherCart<C0, C1>
73where
74    C0: StableDeref,
75    C1: StableDeref,
76    C0: Deref<Target = T>,
77    C1: Deref<Target = T>,
78    T: ?Sized,
79{
80}
81
82// Safety: Safe because both sub-types implement the trait.
83unsafe impl<C0, C1> CloneableCart for EitherCart<C0, C1>
84where
85    C0: CloneableCart,
86    C1: CloneableCart,
87{
88}