Skip to content

Commit c3c31ec

Browse files
JyJyJcrbavshin-f5
authored andcommitted
fix: replace std to core and add config out
1 parent 89cf0e0 commit c3c31ec

File tree

12 files changed

+60
-51
lines changed

12 files changed

+60
-51
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: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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;
78

89
#[doc(hidden)]
910
mod bindings {
@@ -104,7 +105,7 @@ impl ngx_str_t {
104105
/// # Returns
105106
/// A string slice (`&str`) representing the nginx string.
106107
pub fn to_str(&self) -> &str {
107-
std::str::from_utf8(self.as_bytes()).unwrap()
108+
core::str::from_utf8(self.as_bytes()).unwrap()
108109
}
109110

110111
/// Create an `ngx_str_t` instance from a byte slice.
@@ -147,15 +148,6 @@ impl From<ngx_str_t> for &[u8] {
147148
}
148149
}
149150

150-
impl TryFrom<ngx_str_t> for String {
151-
type Error = std::string::FromUtf8Error;
152-
153-
fn try_from(s: ngx_str_t) -> Result<Self, Self::Error> {
154-
let bytes: &[u8] = s.into();
155-
String::from_utf8(bytes.into())
156-
}
157-
}
158-
159151
impl fmt::Display for ngx_str_t {
160152
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
161153
// The implementation is similar to an inlined `String::from_utf8_lossy`, with two
@@ -175,10 +167,10 @@ impl fmt::Display for ngx_str_t {
175167
}
176168

177169
impl TryFrom<ngx_str_t> for &str {
178-
type Error = std::str::Utf8Error;
170+
type Error = core::str::Utf8Error;
179171

180172
fn try_from(s: ngx_str_t) -> Result<Self, Self::Error> {
181-
std::str::from_utf8(s.into())
173+
core::str::from_utf8(s.into())
182174
}
183175
}
184176

@@ -231,7 +223,8 @@ pub unsafe fn add_to_ngx_table(
231223

232224
#[cfg(test)]
233225
mod tests {
234-
use std::string::ToString;
226+
extern crate alloc;
227+
use alloc::string::ToString;
235228

236229
use super::*;
237230

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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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};
3+
4+
#[cfg(all(not(feature = "std"), feature = "alloc"))]
5+
use alloc::{borrow::Cow, string::String};
6+
#[cfg(feature = "std")]
7+
use std::{borrow::Cow, string::String};
48

59
use crate::ffi::*;
610

@@ -27,7 +31,7 @@ macro_rules! ngx_null_string {
2731
() => {
2832
$crate::ffi::ngx_str_t {
2933
len: 0,
30-
data: ::std::ptr::null_mut(),
34+
data: ::core::ptr::null_mut(),
3135
}
3236
};
3337
}
@@ -64,6 +68,7 @@ impl NgxStr {
6468
/// Converts an [`NgxStr`] into a [`Cow<str>`], replacing invalid UTF-8 sequences.
6569
///
6670
/// See [`String::from_utf8_lossy`].
71+
#[cfg(feature = "alloc")]
6772
pub fn to_string_lossy(&self) -> Cow<str> {
6873
String::from_utf8_lossy(self.as_bytes())
6974
}

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: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use core::ffi::c_void;
2+
use core::fmt;
13
use core::slice;
2-
use std::error::Error;
3-
use std::ffi::c_void;
4-
use std::fmt;
5-
use std::str::FromStr;
4+
use core::str::FromStr;
65

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

123122
/// Request pool.
@@ -338,7 +337,7 @@ impl Request {
338337
ngx_http_internal_redirect(
339338
(self as *const Request as *mut Request).cast(),
340339
uri_ptr,
341-
std::ptr::null_mut(),
340+
core::ptr::null_mut(),
342341
);
343342
}
344343
}
@@ -355,7 +354,7 @@ impl Request {
355354
let uri_ptr = unsafe { &mut ngx_str_t::from_str(self.0.pool, uri) as *mut _ };
356355
// -------------
357356
// allocate memory and set values for ngx_http_post_subrequest_t
358-
let sub_ptr = self.pool().alloc(std::mem::size_of::<ngx_http_post_subrequest_t>());
357+
let sub_ptr = self.pool().alloc(core::mem::size_of::<ngx_http_post_subrequest_t>());
359358

360359
// assert!(sub_ptr.is_null());
361360
let post_subreq = sub_ptr as *const ngx_http_post_subrequest_t as *mut ngx_http_post_subrequest_t;
@@ -365,12 +364,12 @@ impl Request {
365364
}
366365
// -------------
367366

368-
let mut psr: *mut ngx_http_request_t = std::ptr::null_mut();
367+
let mut psr: *mut ngx_http_request_t = core::ptr::null_mut();
369368
let r = unsafe {
370369
ngx_http_subrequest(
371370
(self as *const Request as *mut Request).cast(),
372371
uri_ptr,
373-
std::ptr::null_mut(),
372+
core::ptr::null_mut(),
374373
&mut psr as *mut _,
375374
sub_ptr as *mut _,
376375
NGX_HTTP_SUBREQUEST_WAITED as _,
@@ -384,7 +383,7 @@ impl Request {
384383
* allocate fake request body to avoid attempts to read it and to make
385384
* sure real body file (if already read) won't be closed by upstream
386385
*/
387-
sr.request_body = self.pool().alloc(std::mem::size_of::<ngx_http_request_body_t>()) as *mut _;
386+
sr.request_body = self.pool().alloc(core::mem::size_of::<ngx_http_request_body_t>()) as *mut _;
388387

389388
if sr.request_body.is_null() {
390389
return Status::NGX_ERROR;
@@ -710,7 +709,8 @@ impl fmt::Display for InvalidMethod {
710709
}
711710
}
712711

713-
impl Error for InvalidMethod {}
712+
#[cfg(feature = "std")]
713+
impl std::error::Error for InvalidMethod {}
714714

715715
#[derive(Clone, PartialEq, Eq, Hash)]
716716
enum MethodInner {

0 commit comments

Comments
 (0)