Debian Codegraph @

src/common/string.rs

raw symbols SCIP
1 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/;
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
3 ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#},
4 opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/,
5 };
6
7 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::commonrust-analyzer cargo sudo-rs 0.2.14 common/::Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#;
8
9 const NULL_BYTErust-analyzer cargo sudo-rs 0.2.14 common/string/NULL_BYTE.: char = '\0';
10 const NULL_BYTE_UTF8_LENrust-analyzer cargo sudo-rs 0.2.14 common/string/NULL_BYTE_UTF8_LEN.: usize = NULL_BYTErust-analyzer cargo sudo-rs 0.2.14 common/string/NULL_BYTE..len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().();
11
12 /// A UTF-8 encoded string with no interior null bytes
13 ///
14 /// This type can be converted into a C (null-terminated) string at no cost
15 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
16 pub struct SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
17 innerrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#inner.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
18 }
19
20 impl SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
21 pub fn newrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]new().(mut string: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString], Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> {
22 if string.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().().containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&0) {
23 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::StringValidationrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#StringValidation#(string));
24 }
25
26 string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(NULL_BYTErust-analyzer cargo sudo-rs 0.2.14 common/string/NULL_BYTE.);
27
28 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString] { innerrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#inner.: string })
29 }
30
31 pub fn from_cli_stringrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]from_cli_string().(cli_string: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString] {
32 Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]::newrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]new().(cli_string.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().())
33 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("strings that come in from CLI should not have interior null bytes")
34 }
35
36 pub fn as_cstrrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]as_cstr().(&self) -> &CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr# {
37 CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_bytes_with_nulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_bytes_with_nul().(self.innerrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#inner..as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
38 }
39
40 pub fn as_strrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]as_str().(&self) -> &str {
41 self
42 }
43 }
44
45 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
46 fn defaultrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Default]default().() -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Default] {
47 Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Default] {
48 innerrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#inner.: NULL_BYTErust-analyzer cargo sudo-rs 0.2.14 common/string/NULL_BYTE..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
49 }
50 }
51 }
52
53 #[cfg(test)]
54 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'_ str> for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
55 fn fromrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`From<&str>`]from().(value: &'_ str) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`From<&str>`] {
56 SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#::try_fromrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`TryFrom<String>`]try_from().(value.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
57 }
58 }
59
60 impl TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
61 type Errorrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`TryFrom<String>`]Error# = Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#;
62
63 fn try_fromrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`TryFrom<String>`]try_from().(value: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`TryFrom<String>`], Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`TryFrom<String>`]::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#> {
64 Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`TryFrom<String>`]::newrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]new().(value)
65 }
66 }
67
68 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#> for Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
69 fn fromrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[String][`From<SudoString>`]from().(value: SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[String][`From<SudoString>`] {
70 let mut s = value.innerrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#inner.;
71 s.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]pop().();
72 s
73 }
74 }
75
76 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#> for OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString# {
77 fn fromrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[OsString][`From<SudoString>`]from().(value: SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[OsString][`From<SudoString>`] {
78 let mut s = value.innerrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#inner.;
79 s.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]pop().();
80 OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`From<String>`]from().(s)
81 }
82 }
83
84 impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<str> for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
85 fn as_refrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`AsRef<str>`]as_ref().(&self) -> &str {
86 self.as_strrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]as_str().()
87 }
88 }
89
90 impl opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
91 type Targetrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Deref]Target# = str;
92
93 fn derefrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Deref]deref().(&self) -> &Selfrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# {
94 let num_bytes = self.innerrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#inner..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().();
95 &self.innerrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#inner.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#num_bytes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().NULL_BYTE_UTF8_LENrust-analyzer cargo sudo-rs 0.2.14 common/string/NULL_BYTE_UTF8_LEN.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().
96 }
97 }
98
99 impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
100 fn fmtrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
101 let s: &str = self;
102 fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[str][Debug]fmt().(s, f)
103 }
104 }
105
106 impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
107 fn fmtrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
108 f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(self)
109 }
110 }
111
112 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<str> for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
113 fn eqrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`PartialEq<str>`]eq().(&self, other: &str) -> bool {
114 let s: &str = self;
115 s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().other
116 }
117 }
118
119 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<&'_ str> for SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString# {
120 fn eqrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][`PartialEq<&str>`]eq().(&self, other: &&str) -> bool {
121 let s: &str = self;
122 s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().other
123 }
124 }
125
126 #[cfg(test)]
127 mod testsrust-analyzer cargo sudo-rs 0.2.14 common/string/tests/ {
128 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#;
129
130 use superrust-analyzer cargo sudo-rs 0.2.14 common/string/::*;
131
132 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
133 fn null_byte_is_utf8_encoded_as_a_single_byterust-analyzer cargo sudo-rs 0.2.14 common/string/tests/null_byte_is_utf8_encoded_as_a_single_byte().() {
134 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(1, NULL_BYTE_UTF8_LENrust-analyzer cargo sudo-rs 0.2.14 common/string/NULL_BYTE_UTF8_LEN.)
135 }
136
137 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
138 fn sanity_checkrust-analyzer cargo sudo-rs 0.2.14 common/string/tests/sanity_check().() {
139 let expected = "hello";
140 let s = SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#::newrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]new().("hello".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
141 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, &*rust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Deref]deref().s);
142 }
143
144 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
145 fn cstr_conversionrust-analyzer cargo sudo-rs 0.2.14 common/string/tests/cstr_conversion().() {
146 let expected = "hello";
147 let cstr = CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::from_vec_with_nulrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]from_vec_with_nul().((expected.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add().+rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add()."\0").into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
148 let s = SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#::newrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]new().(expected.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
149 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString][Deref]deref().cstr, s.as_cstrrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]as_cstr().());
150 }
151
152 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
153 fn rejects_string_that_contains_interior_nullrust-analyzer cargo sudo-rs 0.2.14 common/string/tests/rejects_string_that_contains_interior_null().() {
154 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#::newrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]new().("he\0llo".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
155 }
156 }