os_str_bytes/
pattern.rs

1use std::fmt::Debug;
2
3use super::private;
4
5pub trait Encoded {
6    fn __get(&self) -> &[u8];
7}
8
9#[derive(Clone, Debug)]
10pub struct EncodedChar {
11    buffer: [u8; 4],
12    length: usize,
13}
14
15impl Encoded for EncodedChar {
16    fn __get(&self) -> &[u8] {
17        &self.buffer[..self.length]
18    }
19}
20
21impl Encoded for &str {
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")))]
36pub trait Pattern: private::Sealed {
37    #[doc(hidden)]
38    type __Encoded: Clone + Debug + Encoded;
39
40    #[doc(hidden)]
41    fn __encode(self) -> Self::__Encoded;
42}
43
44impl Pattern for char {
45    type __Encoded = EncodedChar;
46
47    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
57impl Pattern for &str {
58    type __Encoded = Self;
59
60    fn __encode(self) -> Self::__Encoded {
61        self
62    }
63}
64
65impl<'a> Pattern for &'a String {
66    type __Encoded = <&'a str as Pattern>::__Encoded;
67
68    fn __encode(self) -> Self::__Encoded {
69        (**self).__encode()
70    }
71}