1use std::slice::SliceIndex;
2
3use super::RawStr;
4
5pub trait RawStrIndex {
30 type Output: ?Sized;
32 fn get(self, s: &RawStr) -> Option<&Self::Output>;
34 fn get_mut(self, s: &mut RawStr) -> Option<&mut Self::Output>;
36 unsafe fn get_unchecked(self, s: &RawStr) -> &Self::Output;
38 unsafe fn get_unchecked_mut(self, s: &mut RawStr) -> &mut Self::Output;
40 fn index(self, s: &RawStr) -> &Self::Output;
42 fn index_mut(self, s: &mut RawStr) -> &mut Self::Output;
44}
45
46#[doc(hidden)]
47pub trait RawStrIndexOutput {
48 type Output: ?Sized;
49 fn into(&self) -> &Self::Output;
50 fn into_mut(&mut self) -> &mut Self::Output;
51}
52
53impl RawStrIndexOutput for [u8] {
54 type Output = RawStr;
55 #[inline]
56 fn into(&self) -> &RawStr {
57 RawStr::from_bytes(self)
58 }
59 #[inline]
60 fn into_mut(&mut self) -> &mut RawStr {
61 RawStr::from_bytes_mut(self)
62 }
63}
64
65impl RawStrIndexOutput for u8 {
66 type Output = u8;
67 #[inline]
68 fn into(&self) -> &u8 {
69 self
70 }
71 #[inline]
72 fn into_mut(&mut self) -> &mut u8 {
73 self
74 }
75}
76
77impl<I> RawStrIndex for I
78where
79 I: SliceIndex<[u8]>,
80 I::Output: RawStrIndexOutput + 'static,
81{
82 type Output = <<I as SliceIndex<[u8]>>::Output as RawStrIndexOutput>::Output;
83 #[inline]
84 fn get(self, s: &RawStr) -> Option<&Self::Output> {
85 s.as_bytes().get(self).map(RawStrIndexOutput::into)
86 }
87 #[inline]
88 fn get_mut(self, s: &mut RawStr) -> Option<&mut Self::Output> {
89 s.as_bytes_mut()
90 .get_mut(self)
91 .map(RawStrIndexOutput::into_mut)
92 }
93 #[inline]
94 unsafe fn get_unchecked(self, s: &RawStr) -> &Self::Output {
95 RawStrIndexOutput::into(s.as_bytes().get_unchecked(self))
96 }
97 #[inline]
98 unsafe fn get_unchecked_mut(self, s: &mut RawStr) -> &mut Self::Output {
99 RawStrIndexOutput::into_mut(s.as_bytes_mut().get_unchecked_mut(self))
100 }
101 #[inline]
102 fn index(self, s: &RawStr) -> &Self::Output {
103 RawStrIndexOutput::into(&s.as_bytes()[self])
104 }
105 #[inline]
106 fn index_mut(self, s: &mut RawStr) -> &mut Self::Output {
107 RawStrIndexOutput::into_mut(&mut s.as_bytes_mut()[self])
108 }
109}