11#![ doc = include_str ! ( "../README.md" ) ]
22#![ warn( missing_docs) ]
3+ #![ cfg_attr( not( feature = "std" ) , no_std) ]
4+ #[ cfg( not( feature = "std" ) ) ]
5+ extern crate alloc;
36
47use core:: fmt;
58use core:: ptr:: copy_nonoverlapping;
@@ -17,6 +20,11 @@ mod bindings {
1720 #![ allow( rustdoc:: broken_intra_doc_links) ]
1821 include ! ( concat!( env!( "OUT_DIR" ) , "/bindings.rs" ) ) ;
1922}
23+ #[ cfg( not( feature = "std" ) ) ]
24+ use alloc:: string;
25+ #[ cfg( feature = "std" ) ]
26+ use std:: string;
27+
2028#[ doc( no_inline) ]
2129pub use bindings:: * ;
2230
@@ -92,7 +100,7 @@ impl ngx_str_t {
92100 self . len == 0
93101 }
94102
95- /// Convert the nginx string to a string slice (`&str`).
103+ /// Returns the contents of this `ngx_str_t` as a string slice (`&str`).
96104 ///
97105 /// # Safety
98106 /// This function is marked as unsafe because it involves raw pointer manipulation.
@@ -107,6 +115,99 @@ impl ngx_str_t {
107115 core:: str:: from_utf8 ( self . as_bytes ( ) ) . unwrap ( )
108116 }
109117
118+ // /// Returns the contents of this `ngx_str_t` as a mutable string slice (`&str`).
119+ // ///
120+ // /// # Safety
121+ // /// This function is marked as unsafe because it involves raw pointer manipulation.
122+ // /// It assumes that the underlying `data` pointer is valid and points to a valid UTF-8 encoded string.
123+ // ///
124+ // /// # Panics
125+ // /// This function panics if the `ngx_str_t` is not valid UTF-8.
126+ // ///
127+ // /// # Returns
128+ // /// A mutable string slice (`&mut str`) representing the nginx string.
129+ // pub fn to_str_mut(&mut self) -> &str {
130+ // core::str::from_utf8(self.as_bytes()).unwrap()
131+ // }
132+
133+ // /// Convert `ngx_str_t` to a byte slice.
134+ // ///
135+ // /// The returned slice will **not** contain the optional nul terminator that `ngx_str_t.data`
136+ // /// may have.
137+ // ///
138+ // /// # Safety
139+ // /// This function is marked as unsafe because it involves lifetime generation.
140+ // /// Caller must specify the proper lifetime which original `ngx_str_t` belongs to.
141+ // #[inline]
142+ // pub fn into_bytes<'a>(self) -> &'a [u8] {
143+ // if self.is_empty() {
144+ // &[]
145+ // } else {
146+ // // SAFETY: `ngx_str_t` with non-zero len must contain a valid correctly aligned pointer
147+ // unsafe { slice::from_raw_parts(self.data, self.len) }
148+ // }
149+ // }
150+
151+ // /// Convert `ngx_str_t` to a mutable byte slice.
152+ // ///
153+ // /// The returned slice will **not** contain the optional nul terminator that `ngx_str_t.data`
154+ // /// may have.
155+ // ///
156+ // /// # Safety
157+ // /// This function is marked as unsafe because it involves raw pointer manipulation.
158+ // /// It assumes that the underlying `data` pointer is valid and points to a valid UTF-8 encoded string.
159+ // ///
160+ // /// This function is marked as unsafe because it involves lifetime generation.
161+ // /// Caller must specify the proper lifetime which original `ngx_str_t` belongs to.
162+ // ///
163+ // /// # Panics
164+ // /// This function panics if the `ngx_str_t` is not valid UTF-8.
165+ // ///
166+ // /// # Returns
167+ // /// A mutable string slice (`&mut str`) representing the nginx string.
168+ // #[inline]
169+ // pub fn into_bytes_mut<'a>(self) -> &'a mut [u8] {
170+ // if self.is_empty() {
171+ // &mut []
172+ // } else {
173+ // // SAFETY: `ngx_str_t` with non-zero len must contain a valid correctly aligned pointer
174+ // unsafe { slice::from_raw_parts_mut(self.data, self.len) }
175+ // }
176+ // }
177+
178+ // /// Convert `ngx_str_t` to a string slice (`&str`).
179+ // ///
180+ // /// The returned slice will **not** contain the optional nul terminator that `ngx_str_t.data`
181+ // /// may have.
182+ // ///
183+ // /// # Safety
184+ // /// This function is marked as unsafe because it involves lifetime generation.
185+ // /// Caller must specify the proper lifetime which original `ngx_str_t` belongs to.
186+ // pub fn into_str<'a>(self) -> &'a str {
187+ // core::str::from_utf8(self.into_bytes()).unwrap()
188+ // }
189+
190+ // /// Convert `ngx_str_t` to a mutable byte string slice (`&str`).
191+ // ///
192+ // /// The returned slice will **not** contain the optional nul terminator that `ngx_str_t.data`
193+ // /// may have.
194+ // ///
195+ // /// # Safety
196+ // /// This function is marked as unsafe because it involves raw pointer manipulation.
197+ // /// It assumes that the underlying `data` pointer is valid and points to a valid UTF-8 encoded string.
198+ // ///
199+ // /// This function is marked as unsafe because it involves lifetime generation.
200+ // /// Caller must specify the proper lifetime which original `ngx_str_t` belongs to.
201+ // ///
202+ // /// # Panics
203+ // /// This function panics if the `ngx_str_t` is not valid UTF-8.
204+ // ///
205+ // /// # Returns
206+ // /// A mutable string slice (`&mut str`) representing the nginx string.
207+ // pub fn into_str_mut<'a>(self) -> &'a mut str {
208+ // core::str::from_utf8_mut(self.into_bytes_mut()).unwrap()
209+ // }
210+
110211 /// Create an `ngx_str_t` instance from a byte slice.
111212 ///
112213 /// # Safety
@@ -130,7 +231,7 @@ impl ngx_str_t {
130231 ///
131232 /// # Returns
132233 /// An `ngx_str_t` instance representing the given `String`.
133- pub unsafe fn from_string ( pool : * mut ngx_pool_t , data : String ) -> Self {
234+ pub unsafe fn from_string ( pool : * mut ngx_pool_t , data : string :: String ) -> Self {
134235 ngx_str_t {
135236 data : str_to_uchar ( pool, data. as_str ( ) ) ,
136237 len : data. len ( ) ,
@@ -168,19 +269,18 @@ impl From<ngx_str_t> for &[u8] {
168269 }
169270}
170271
171- #[ cfg( feature = "std" ) ]
172- impl TryFrom < ngx_str_t > for String {
173- type Error = std:: string:: FromUtf8Error ;
272+ impl TryFrom < ngx_str_t > for string:: String {
273+ type Error = string:: FromUtf8Error ;
174274
175275 fn try_from ( s : ngx_str_t ) -> Result < Self , Self :: Error > {
176276 let bytes: & [ u8 ] = s. into ( ) ;
177- String :: from_utf8 ( bytes. into ( ) )
277+ string :: String :: from_utf8 ( bytes. into ( ) )
178278 }
179279}
180280
181281impl fmt:: Display for ngx_str_t {
182282 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
183- write ! ( f, "{}" , String :: from_utf8_lossy( ( * self ) . into( ) ) )
283+ write ! ( f, "{}" , string :: String :: from_utf8_lossy( ( * self ) . into( ) ) )
184284 }
185285}
186286
@@ -234,6 +334,6 @@ pub unsafe fn add_to_ngx_table(
234334 table. key . data = str_to_uchar ( pool, key) ;
235335 table. value . len = value. len ( ) ;
236336 table. value . data = str_to_uchar ( pool, value) ;
237- table. lowcase_key = str_to_uchar ( pool, String :: from ( key) . to_ascii_lowercase ( ) . as_str ( ) ) ;
337+ table. lowcase_key = str_to_uchar ( pool, string :: String :: from ( key) . to_ascii_lowercase ( ) . as_str ( ) ) ;
238338 } )
239339}
0 commit comments