Skip to content

Commit 98cb85d

Browse files
committed
fix: replace std to core and add config out
1 parent 0c0e735 commit 98cb85d

File tree

12 files changed

+81
-55
lines changed

12 files changed

+81
-55
lines changed

nginx-sys/build/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ fn generate_binding(nginx_build_dir: PathBuf) {
9797
.header("build/wrapper.h")
9898
.clang_args(clang_args)
9999
.layout_tests(false)
100+
.use_core()
100101
.generate()
101102
.expect("Unable to generate bindings");
102103

nginx-sys/src/lib.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#![doc = include_str!("../README.md")]
22
#![warn(missing_docs)]
3+
#![no_std]
34

4-
use std::fmt;
5-
use std::ptr::copy_nonoverlapping;
6-
use std::slice;
5+
use core::fmt;
6+
use core::ptr::copy_nonoverlapping;
7+
use core::slice;
8+
9+
#[cfg(all(not(feature = "std"), feature = "alloc"))]
10+
use alloc::string::{FromUtf8Error, String};
11+
#[cfg(feature = "std")]
12+
use std::string::{FromUtf8Error, String};
713

814
#[doc(hidden)]
915
mod bindings {
@@ -104,7 +110,7 @@ impl ngx_str_t {
104110
/// # Returns
105111
/// A string slice (`&str`) representing the nginx string.
106112
pub fn to_str(&self) -> &str {
107-
std::str::from_utf8(self.as_bytes()).unwrap()
113+
core::str::from_utf8(self.as_bytes()).unwrap()
108114
}
109115

110116
/// Create an `ngx_str_t` instance from a byte slice.
@@ -130,6 +136,7 @@ impl ngx_str_t {
130136
///
131137
/// # Returns
132138
/// An `ngx_str_t` instance representing the given `String`.
139+
#[cfg(feature = "alloc")]
133140
pub unsafe fn from_string(pool: *mut ngx_pool_t, data: String) -> Self {
134141
ngx_str_t {
135142
data: str_to_uchar(pool, data.as_str()),
@@ -168,26 +175,29 @@ impl From<ngx_str_t> for &[u8] {
168175
}
169176
}
170177

171-
impl TryFrom<ngx_str_t> for String {
172-
type Error = std::string::FromUtf8Error;
173-
174-
fn try_from(s: ngx_str_t) -> Result<Self, Self::Error> {
175-
let bytes: &[u8] = s.into();
176-
String::from_utf8(bytes.into())
177-
}
178-
}
179-
180178
impl fmt::Display for ngx_str_t {
181179
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
182-
write!(f, "{}", String::from_utf8_lossy((*self).into()))
180+
// The implementation is similar to an inlined `String::from_utf8_lossy`, with two
181+
// important differences:
182+
//
183+
// - it writes directly to the Formatter instead of allocating a temporary String
184+
// - invalid sequences are represented as escaped individual bytes
185+
for chunk in self.as_bytes().utf8_chunks() {
186+
f.write_str(chunk.valid())?;
187+
for byte in chunk.invalid() {
188+
f.write_str("\\x")?;
189+
fmt::LowerHex::fmt(byte, f)?;
190+
}
191+
}
192+
Ok(())
183193
}
184194
}
185195

186196
impl TryFrom<ngx_str_t> for &str {
187-
type Error = std::str::Utf8Error;
197+
type Error = core::str::Utf8Error;
188198

189199
fn try_from(s: ngx_str_t) -> Result<Self, Self::Error> {
190-
std::str::from_utf8(s.into())
200+
core::str::from_utf8(s.into())
191201
}
192202
}
193203

@@ -218,6 +228,7 @@ impl TryFrom<ngx_str_t> for &str {
218228
/// let result = add_to_ngx_table(table, pool, key, value);
219229
/// # }
220230
/// ```
231+
#[cfg(feature = "alloc")]
221232
pub unsafe fn add_to_ngx_table(
222233
table: *mut ngx_table_elt_t,
223234
pool: *mut ngx_pool_t,

src/core/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::slice;
1+
use core::slice;
22

33
use crate::ffi::*;
44

src/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ macro_rules! ngx_null_command {
2222
set: None,
2323
conf: 0,
2424
offset: 0,
25-
post: ::std::ptr::null_mut(),
25+
post: ::core::ptr::null_mut(),
2626
}
2727
};
2828
}

src/core/pool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::ffi::c_void;
2-
use std::{mem, ptr};
1+
use core::ffi::c_void;
2+
use core::{mem, ptr};
33

44
use crate::core::buffer::{Buffer, MemoryBuffer, TemporaryBuffer};
55
use crate::ffi::*;

src/core/status.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt;
1+
use core::fmt;
22

33
use crate::ffi::*;
44

src/core/string.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::borrow::Cow;
2-
use std::slice;
3-
use std::str::{self, Utf8Error};
1+
use core::slice;
2+
use core::str::{self, Utf8Error};
43

54
use crate::ffi::*;
65

@@ -27,7 +26,7 @@ macro_rules! ngx_null_string {
2726
() => {
2827
$crate::ffi::ngx_str_t {
2928
len: 0,
30-
data: ::std::ptr::null_mut(),
29+
data: ::core::ptr::null_mut(),
3130
}
3231
};
3332
}
@@ -61,11 +60,14 @@ impl NgxStr {
6160
str::from_utf8(self.as_bytes())
6261
}
6362

64-
/// Converts an [`NgxStr`] into a [`Cow<str>`], replacing invalid UTF-8 sequences.
63+
/// Converts an [`NgxStr`] into a [`std::borrow::Cow<str>`], replacing invalid UTF-8 sequences.
6564
///
6665
/// See [`String::from_utf8_lossy`].
67-
pub fn to_string_lossy(&self) -> Cow<str> {
68-
String::from_utf8_lossy(self.as_bytes())
66+
///
67+
/// This module is temporally available only with `std` feature.
68+
#[cfg(feature = "std")]
69+
pub fn to_string_lossy(&self) -> std::borrow::Cow<str> {
70+
std::string::String::from_utf8_lossy(self.as_bytes())
6971
}
7072

7173
/// Returns `true` if the [`NgxStr`] is empty, otherwise `false`.

src/http/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::c_void;
1+
use core::ffi::c_void;
22

33
use crate::ffi::*;
44

src/http/module.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::ffi::{c_char, c_void};
2-
use std::ptr;
1+
use core::ffi::{c_char, c_void};
2+
use core::fmt;
3+
use core::ptr;
34

45
use crate::core::NGX_CONF_ERROR;
56
use crate::core::*;
@@ -12,10 +13,11 @@ pub enum MergeConfigError {
1213
NoValue,
1314
}
1415

16+
#[cfg(feature = "std")]
1517
impl std::error::Error for MergeConfigError {}
1618

17-
impl std::fmt::Display for MergeConfigError {
18-
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
19+
impl fmt::Display for MergeConfigError {
20+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1921
match self {
2022
MergeConfigError::NoValue => "no value".fmt(fmt),
2123
}

src/http/request.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::error::Error;
2-
use std::ffi::c_void;
3-
use std::fmt;
4-
use std::str::FromStr;
1+
use core::ffi::c_void;
2+
use core::fmt;
3+
use core::str::FromStr;
54

65
use crate::core::*;
76
use crate::ffi::*;
@@ -30,7 +29,7 @@ macro_rules! http_subrequest_handler {
3029
( $name: ident, $handler: expr ) => {
3130
unsafe extern "C" fn $name(
3231
r: *mut $crate::ffi::ngx_http_request_t,
33-
data: *mut ::std::ffi::c_void,
32+
data: *mut ::core::ffi::c_void,
3433
rc: $crate::ffi::ngx_int_t,
3534
) -> $crate::ffi::ngx_int_t {
3635
$handler(r, data, rc)
@@ -116,7 +115,7 @@ impl Request {
116115
/// Is this the main request (as opposed to a subrequest)?
117116
pub fn is_main(&self) -> bool {
118117
let main = self.0.main.cast();
119-
std::ptr::eq(self, main)
118+
core::ptr::eq(self, main)
120119
}
121120

122121
/// Request pool.
@@ -263,6 +262,7 @@ impl Request {
263262
/// Add header to the `headers_in` object.
264263
///
265264
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
265+
#[cfg(feature = "alloc")]
266266
pub fn add_header_in(&mut self, key: &str, value: &str) -> Option<()> {
267267
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_in.headers) as _ };
268268
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
@@ -271,6 +271,7 @@ impl Request {
271271
/// Add header to the `headers_out` object.
272272
///
273273
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
274+
#[cfg(feature = "alloc")]
274275
pub fn add_header_out(&mut self, key: &str, value: &str) -> Option<()> {
275276
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_out.headers) as _ };
276277
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
@@ -337,7 +338,7 @@ impl Request {
337338
ngx_http_internal_redirect(
338339
(self as *const Request as *mut Request).cast(),
339340
uri_ptr,
340-
std::ptr::null_mut(),
341+
core::ptr::null_mut(),
341342
);
342343
}
343344
}
@@ -354,7 +355,7 @@ impl Request {
354355
let uri_ptr = unsafe { &mut ngx_str_t::from_str(self.0.pool, uri) as *mut _ };
355356
// -------------
356357
// allocate memory and set values for ngx_http_post_subrequest_t
357-
let sub_ptr = self.pool().alloc(std::mem::size_of::<ngx_http_post_subrequest_t>());
358+
let sub_ptr = self.pool().alloc(core::mem::size_of::<ngx_http_post_subrequest_t>());
358359

359360
// assert!(sub_ptr.is_null());
360361
let post_subreq = sub_ptr as *const ngx_http_post_subrequest_t as *mut ngx_http_post_subrequest_t;
@@ -364,12 +365,12 @@ impl Request {
364365
}
365366
// -------------
366367

367-
let mut psr: *mut ngx_http_request_t = std::ptr::null_mut();
368+
let mut psr: *mut ngx_http_request_t = core::ptr::null_mut();
368369
let r = unsafe {
369370
ngx_http_subrequest(
370371
(self as *const Request as *mut Request).cast(),
371372
uri_ptr,
372-
std::ptr::null_mut(),
373+
core::ptr::null_mut(),
373374
&mut psr as *mut _,
374375
sub_ptr as *mut _,
375376
NGX_HTTP_SUBREQUEST_WAITED as _,
@@ -383,7 +384,7 @@ impl Request {
383384
* allocate fake request body to avoid attempts to read it and to make
384385
* sure real body file (if already read) won't be closed by upstream
385386
*/
386-
sr.request_body = self.pool().alloc(std::mem::size_of::<ngx_http_request_body_t>()) as *mut _;
387+
sr.request_body = self.pool().alloc(core::mem::size_of::<ngx_http_request_body_t>()) as *mut _;
387388

388389
if sr.request_body.is_null() {
389390
return Status::NGX_ERROR;
@@ -422,7 +423,7 @@ impl fmt::Debug for Request {
422423

423424
/// Iterator for [`ngx_list_t`] types.
424425
///
425-
/// Implementes the std::iter::Iterator trait.
426+
/// Implementes the core::iter::Iterator trait.
426427
pub struct NgxListIterator {
427428
done: bool,
428429
part: *const ngx_list_part_t,
@@ -711,7 +712,8 @@ impl fmt::Display for InvalidMethod {
711712
}
712713
}
713714

714-
impl Error for InvalidMethod {}
715+
#[cfg(feature = "std")]
716+
impl std::error::Error for InvalidMethod {}
715717

716718
#[derive(Clone, PartialEq, Eq, Hash)]
717719
enum MethodInner {

0 commit comments

Comments
 (0)