1 use super::private; 2 3 pub trait Encoded { __get(&self) -> &[u8]4 fn __get(&self) -> &[u8]; 5 } 6 7 #[derive(Clone)] 8 pub struct EncodedChar { 9 buffer: [u8; 4], 10 length: usize, 11 } 12 13 impl Encoded for EncodedChar { 14 #[inline] __get(&self) -> &[u8]15 fn __get(&self) -> &[u8] { 16 &self.buffer[..self.length] 17 } 18 } 19 20 impl Encoded for &str { 21 #[inline] __get(&self) -> &[u8]22 fn __get(&self) -> &[u8] { 23 self.as_bytes() 24 } 25 } 26 27 /// Allows a type to be used for searching by [`RawOsStr`] and [`RawOsString`]. 28 /// 29 /// This trait is very similar to [`str::pattern::Pattern`], but its methods 30 /// are private and it is implemented for different types. 31 /// 32 /// [`RawOsStr`]: super::RawOsStr 33 /// [`RawOsString`]: super::RawOsString 34 /// [`str::pattern::Pattern`]: ::std::str::pattern::Pattern 35 #[cfg_attr(os_str_bytes_docs_rs, doc(cfg(feature = "raw_os_str")))] 36 pub trait Pattern: private::Sealed { 37 #[doc(hidden)] 38 type __Encoded: Clone + Encoded; 39 40 #[doc(hidden)] __encode(self) -> Self::__Encoded41 fn __encode(self) -> Self::__Encoded; 42 } 43 44 impl Pattern for char { 45 #[doc(hidden)] 46 type __Encoded = EncodedChar; 47 48 #[doc(hidden)] __encode(self) -> Self::__Encoded49 fn __encode(self) -> Self::__Encoded { 50 let mut encoded = EncodedChar { 51 buffer: [0; 4], 52 length: 0, 53 }; 54 encoded.length = self.encode_utf8(&mut encoded.buffer).len(); 55 encoded 56 } 57 } 58 59 impl Pattern for &str { 60 #[doc(hidden)] 61 type __Encoded = Self; 62 63 #[doc(hidden)] __encode(self) -> Self::__Encoded64 fn __encode(self) -> Self::__Encoded { 65 self 66 } 67 } 68 69 impl<'a> Pattern for &'a String { 70 #[doc(hidden)] 71 type __Encoded = <&'a str as Pattern>::__Encoded; 72 73 #[doc(hidden)] __encode(self) -> Self::__Encoded74 fn __encode(self) -> Self::__Encoded { 75 (**self).__encode() 76 } 77 } 78