1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::fmt::Debug;
use std::str;

use super::private;
use super::util::MAX_UTF8_LENGTH;

pub trait Encoded {
    fn __as_bytes(&self) -> &[u8] {
        self.__as_str().as_bytes()
    }

    fn __as_str(&self) -> &str;
}

#[derive(Clone, Debug)]
pub struct EncodedChar {
    buffer: [u8; MAX_UTF8_LENGTH],
    length: usize,
}

impl Encoded for EncodedChar {
    fn __as_str(&self) -> &str {
        // SAFETY: This slice was encoded from a character.
        unsafe { str::from_utf8_unchecked(&self.buffer[..self.length]) }
    }
}

impl Encoded for &str {
    fn __as_str(&self) -> &str {
        self
    }
}

/// Allows a type to be used for searching by [`RawOsStr`] and [`RawOsString`].
///
/// This trait is very similar to [`str::pattern::Pattern`], but its methods
/// are private and it is implemented for different types.
///
/// [`RawOsStr`]: super::RawOsStr
/// [`RawOsString`]: super::RawOsString
#[cfg_attr(os_str_bytes_docs_rs, doc(cfg(feature = "raw_os_str")))]
pub trait Pattern: private::Sealed {
    #[doc(hidden)]
    type __Encoded: Clone + Debug + Encoded;

    #[doc(hidden)]
    fn __encode(self) -> Self::__Encoded;
}

impl Pattern for char {
    type __Encoded = EncodedChar;

    fn __encode(self) -> Self::__Encoded {
        let mut encoded = EncodedChar {
            buffer: [0; MAX_UTF8_LENGTH],
            length: 0,
        };
        encoded.length = self.encode_utf8(&mut encoded.buffer).len();
        encoded
    }
}

impl Pattern for &str {
    type __Encoded = Self;

    fn __encode(self) -> Self::__Encoded {
        self
    }
}

impl<'a> Pattern for &'a String {
    type __Encoded = <&'a str as Pattern>::__Encoded;

    fn __encode(self) -> Self::__Encoded {
        (**self).__encode()
    }
}