flex/
derive.rs

1//! Derive implementations for `Flex`.
2//!
3//! This module provides implementations of common traits such as `Clone`,
4//! `Eq`, `PartialEq`, `Ord`, `PartialOrd`, and `Hash` for the `Flex` type.
5//! In normal situations, these traits would be derived. But we need to proxy
6//! all the impls through `.derive()`.
7
8use core::hash::{Hash, Hasher};
9use core::ops::Deref;
10
11use super::Flex;
12
13#[cfg(feature = "alloc")]
14use alloc::boxed::Box;
15
16impl<'a, T: ?Sized> Deref for Flex<'a, T> {
17    type Target = T;
18
19    fn deref(&self) -> &Self::Target {
20        match self {
21            Flex::Lend(r) => r,
22
23            #[cfg(feature = "alloc")]
24            Flex::Give(b) => b.deref(),
25        }
26    }
27}
28
29#[cfg(not(feature = "alloc"))]
30impl<'a, T: ?Sized> Clone for Flex<'a, T> {
31    fn clone(&self) -> Self {
32        match self {
33            Flex::Lend(r) => Flex::Lend(*r),
34        }
35    }
36}
37
38#[cfg(feature = "alloc")]
39impl<'a, T: ?Sized> Clone for Flex<'a, T>
40where
41    Box<T>: Clone,
42{
43    fn clone(&self) -> Self {
44        match self {
45            Flex::Lend(r) => Flex::Lend(*r),
46            Flex::Give(b) => Flex::Give(b.clone()),
47        }
48    }
49}
50
51impl<'a, T: ?Sized + Eq> Eq for Flex<'a, T> {}
52
53impl<'a, T: ?Sized + PartialEq> PartialEq for Flex<'a, T> {
54    fn eq(&self, other: &Self) -> bool {
55        self.deref().eq(other.deref())
56    }
57}
58
59impl<'a, T: ?Sized + PartialEq> PartialEq<T> for Flex<'a, T> {
60    fn eq(&self, other: &T) -> bool {
61        self.deref() == other
62    }
63}
64
65impl<'a, T: ?Sized + PartialEq> PartialEq<&T> for Flex<'a, T> {
66    fn eq(&self, other: &&T) -> bool {
67        self.deref() == *other
68    }
69}
70
71#[cfg(feature = "alloc")]
72#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
73impl<'a, T: ?Sized + PartialEq> PartialEq<Box<T>> for Flex<'a, T> {
74    fn eq(&self, other: &Box<T>) -> bool {
75        self.deref() == other.deref()
76    }
77}
78
79impl<'a, T: ?Sized + Ord> Ord for Flex<'a, T> {
80    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
81        self.deref().cmp(other.deref())
82    }
83}
84
85impl<'a, T: ?Sized + PartialOrd> PartialOrd for Flex<'a, T> {
86    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
87        self.deref().partial_cmp(other.deref())
88    }
89}
90
91impl<'a, T: ?Sized + PartialOrd> PartialOrd<T> for Flex<'a, T> {
92    fn partial_cmp(&self, other: &T) -> Option<core::cmp::Ordering> {
93        self.deref().partial_cmp(other)
94    }
95}
96
97impl<'a, T: ?Sized + PartialOrd> PartialOrd<&T> for Flex<'a, T> {
98    fn partial_cmp(&self, other: &&T) -> Option<core::cmp::Ordering> {
99        self.deref().partial_cmp(*other)
100    }
101}
102
103#[cfg(feature = "alloc")]
104#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
105impl<'a, T: ?Sized + PartialOrd> PartialOrd<Box<T>> for Flex<'a, T> {
106    fn partial_cmp(&self, other: &Box<T>) -> Option<core::cmp::Ordering> {
107        self.deref().partial_cmp(other.deref())
108    }
109}
110
111impl<'a, T: ?Sized + Hash> Hash for Flex<'a, T> {
112    fn hash<H: Hasher>(&self, state: &mut H) {
113        self.deref().hash(state)
114    }
115}