strpool/
traits.rs

1use super::PoolStr;
2use core::ops::Deref;
3
4impl<const P: usize> core::fmt::Debug for PoolStr<P> {
5    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
6        self.deref().fmt(f)
7    }
8}
9
10impl<const P: usize> core::fmt::Display for PoolStr<P> {
11    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
12        self.deref().fmt(f)
13    }
14}
15
16// It's possible that two copies of the same
17// string exist at the same time in the store
18// even if it's unlikely (see intern_1_127).
19// for this reason, we have to fall back to
20// a traditional comparison if the pointers
21// aren't the same.
22impl<const P: usize> PartialEq for PoolStr<P> {
23    fn eq(&self, other: &Self) -> bool {
24           self.len_ptr == other.len_ptr
25        || self.deref() == other.deref()
26    }
27}
28
29impl<const P: usize> Eq for PoolStr <P>{}
30
31impl<const P: usize> PartialEq<str> for PoolStr<P> {
32    fn eq(&self, other: &str) -> bool {
33        self.deref() == other
34    }
35}
36
37impl<const P: usize> PartialEq<PoolStr<P>> for str {
38    fn eq(&self, other: &PoolStr<P>) -> bool {
39        self == other.deref()
40    }
41}
42
43impl<const P: usize> AsRef<str> for PoolStr<P> {
44    fn as_ref(&self) -> &str {
45        self.deref()
46    }
47}
48
49impl<const P: usize> Default for PoolStr<P> {
50    fn default() -> Self {
51        Self::empty()
52    }
53}
54
55impl<const P: usize> core::hash::Hash for PoolStr<P> {
56    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
57        self.deref().hash(state);
58    }
59}
60
61impl<const P: usize, I> core::ops::Index<I> for PoolStr<P>
62where I: core::slice::SliceIndex<str>,
63{
64    type Output = I::Output;
65
66    #[inline]
67    fn index(&self, index: I) -> &I::Output {
68        &self.deref()[index]
69    }
70}
71
72impl<const P: usize> PartialOrd<str> for PoolStr<P> {
73    #[inline]
74    fn partial_cmp(&self, other: &str) -> Option<core::cmp::Ordering> {
75        self.deref().partial_cmp(other)
76    }
77}
78
79impl<const P: usize> PartialOrd<PoolStr<P>> for PoolStr<P> {
80    #[inline]
81    fn partial_cmp(&self, other: &PoolStr<P>) -> Option<core::cmp::Ordering> {
82        self.deref().partial_cmp(other.deref())
83    }
84}
85
86impl<const P: usize> Ord for PoolStr<P> {
87    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
88        self.deref().cmp(other.deref())
89    }
90}