• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::fmt::Debug;
2 
3 use super::private;
4 
5 pub trait Encoded {
__get(&self) -> &[u8]6     fn __get(&self) -> &[u8];
7 }
8 
9 #[derive(Clone, Debug)]
10 pub struct EncodedChar {
11     buffer: [u8; 4],
12     length: usize,
13 }
14 
15 impl Encoded for EncodedChar {
__get(&self) -> &[u8]16     fn __get(&self) -> &[u8] {
17         &self.buffer[..self.length]
18     }
19 }
20 
21 impl Encoded for &str {
__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 + Debug + Encoded;
39 
40     #[doc(hidden)]
__encode(self) -> Self::__Encoded41     fn __encode(self) -> Self::__Encoded;
42 }
43 
44 impl Pattern for char {
45     type __Encoded = EncodedChar;
46 
__encode(self) -> Self::__Encoded47     fn __encode(self) -> Self::__Encoded {
48         let mut encoded = EncodedChar {
49             buffer: [0; 4],
50             length: 0,
51         };
52         encoded.length = self.encode_utf8(&mut encoded.buffer).len();
53         encoded
54     }
55 }
56 
57 impl Pattern for &str {
58     type __Encoded = Self;
59 
__encode(self) -> Self::__Encoded60     fn __encode(self) -> Self::__Encoded {
61         self
62     }
63 }
64 
65 impl<'a> Pattern for &'a String {
66     type __Encoded = <&'a str as Pattern>::__Encoded;
67 
__encode(self) -> Self::__Encoded68     fn __encode(self) -> Self::__Encoded {
69         (**self).__encode()
70     }
71 }
72