From c3c31ec607fd592dd6b53787d0e00e1948fa1173 Mon Sep 17 00:00:00 2001 From: JyJyJcr <82190170+JyJyJcr@users.noreply.github.com> Date: Fri, 3 Jan 2025 02:39:17 +0900 Subject: [PATCH] fix: replace `std` to `core` and add config out --- nginx-sys/build/main.rs | 1 + nginx-sys/src/lib.rs | 25 +++++++++---------------- src/core/buffer.rs | 2 +- src/core/mod.rs | 2 +- src/core/pool.rs | 4 ++-- src/core/status.rs | 2 +- src/core/string.rs | 13 +++++++++---- src/http/conf.rs | 2 +- src/http/module.rs | 10 ++++++---- src/http/request.rs | 24 ++++++++++++------------ src/http/status.rs | 6 +++--- src/lib.rs | 20 ++++++++++++++------ 12 files changed, 60 insertions(+), 51 deletions(-) diff --git a/nginx-sys/build/main.rs b/nginx-sys/build/main.rs index fadf50b..087410b 100644 --- a/nginx-sys/build/main.rs +++ b/nginx-sys/build/main.rs @@ -97,6 +97,7 @@ fn generate_binding(nginx_build_dir: PathBuf) { .header("build/wrapper.h") .clang_args(clang_args) .layout_tests(false) + .use_core() .generate() .expect("Unable to generate bindings"); diff --git a/nginx-sys/src/lib.rs b/nginx-sys/src/lib.rs index d818977..28d4cd6 100644 --- a/nginx-sys/src/lib.rs +++ b/nginx-sys/src/lib.rs @@ -1,9 +1,10 @@ #![doc = include_str!("../README.md")] #![warn(missing_docs)] +#![no_std] -use std::fmt; -use std::ptr::copy_nonoverlapping; -use std::slice; +use core::fmt; +use core::ptr::copy_nonoverlapping; +use core::slice; #[doc(hidden)] mod bindings { @@ -104,7 +105,7 @@ impl ngx_str_t { /// # Returns /// A string slice (`&str`) representing the nginx string. pub fn to_str(&self) -> &str { - std::str::from_utf8(self.as_bytes()).unwrap() + core::str::from_utf8(self.as_bytes()).unwrap() } /// Create an `ngx_str_t` instance from a byte slice. @@ -147,15 +148,6 @@ impl From for &[u8] { } } -impl TryFrom for String { - type Error = std::string::FromUtf8Error; - - fn try_from(s: ngx_str_t) -> Result { - let bytes: &[u8] = s.into(); - String::from_utf8(bytes.into()) - } -} - impl fmt::Display for ngx_str_t { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // The implementation is similar to an inlined `String::from_utf8_lossy`, with two @@ -175,10 +167,10 @@ impl fmt::Display for ngx_str_t { } impl TryFrom for &str { - type Error = std::str::Utf8Error; + type Error = core::str::Utf8Error; fn try_from(s: ngx_str_t) -> Result { - std::str::from_utf8(s.into()) + core::str::from_utf8(s.into()) } } @@ -231,7 +223,8 @@ pub unsafe fn add_to_ngx_table( #[cfg(test)] mod tests { - use std::string::ToString; + extern crate alloc; + use alloc::string::ToString; use super::*; diff --git a/src/core/buffer.rs b/src/core/buffer.rs index 9086238..f3e2100 100644 --- a/src/core/buffer.rs +++ b/src/core/buffer.rs @@ -1,4 +1,4 @@ -use std::slice; +use core::slice; use crate::ffi::*; diff --git a/src/core/mod.rs b/src/core/mod.rs index d38462b..07ef434 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -22,7 +22,7 @@ macro_rules! ngx_null_command { set: None, conf: 0, offset: 0, - post: ::std::ptr::null_mut(), + post: ::core::ptr::null_mut(), } }; } diff --git a/src/core/pool.rs b/src/core/pool.rs index 4099065..bc65ad5 100644 --- a/src/core/pool.rs +++ b/src/core/pool.rs @@ -1,5 +1,5 @@ -use std::ffi::c_void; -use std::{mem, ptr}; +use core::ffi::c_void; +use core::{mem, ptr}; use crate::core::buffer::{Buffer, MemoryBuffer, TemporaryBuffer}; use crate::ffi::*; diff --git a/src/core/status.rs b/src/core/status.rs index 6720849..46bb79f 100644 --- a/src/core/status.rs +++ b/src/core/status.rs @@ -1,4 +1,4 @@ -use std::fmt; +use core::fmt; use crate::ffi::*; diff --git a/src/core/string.rs b/src/core/string.rs index 47d6d6e..15e9a71 100644 --- a/src/core/string.rs +++ b/src/core/string.rs @@ -1,6 +1,10 @@ -use std::borrow::Cow; -use std::slice; -use std::str::{self, Utf8Error}; +use core::slice; +use core::str::{self, Utf8Error}; + +#[cfg(all(not(feature = "std"), feature = "alloc"))] +use alloc::{borrow::Cow, string::String}; +#[cfg(feature = "std")] +use std::{borrow::Cow, string::String}; use crate::ffi::*; @@ -27,7 +31,7 @@ macro_rules! ngx_null_string { () => { $crate::ffi::ngx_str_t { len: 0, - data: ::std::ptr::null_mut(), + data: ::core::ptr::null_mut(), } }; } @@ -64,6 +68,7 @@ impl NgxStr { /// Converts an [`NgxStr`] into a [`Cow`], replacing invalid UTF-8 sequences. /// /// See [`String::from_utf8_lossy`]. + #[cfg(feature = "alloc")] pub fn to_string_lossy(&self) -> Cow { String::from_utf8_lossy(self.as_bytes()) } diff --git a/src/http/conf.rs b/src/http/conf.rs index f5de90d..c9f14ea 100644 --- a/src/http/conf.rs +++ b/src/http/conf.rs @@ -1,4 +1,4 @@ -use std::ffi::c_void; +use core::ffi::c_void; use crate::ffi::*; diff --git a/src/http/module.rs b/src/http/module.rs index 6a0f587..6f25c97 100644 --- a/src/http/module.rs +++ b/src/http/module.rs @@ -1,5 +1,6 @@ -use std::ffi::{c_char, c_void}; -use std::ptr; +use core::ffi::{c_char, c_void}; +use core::fmt; +use core::ptr; use crate::core::NGX_CONF_ERROR; use crate::core::*; @@ -12,10 +13,11 @@ pub enum MergeConfigError { NoValue, } +#[cfg(feature = "std")] impl std::error::Error for MergeConfigError {} -impl std::fmt::Display for MergeConfigError { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { +impl fmt::Display for MergeConfigError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match self { MergeConfigError::NoValue => "no value".fmt(fmt), } diff --git a/src/http/request.rs b/src/http/request.rs index 365b5d3..c9af30c 100644 --- a/src/http/request.rs +++ b/src/http/request.rs @@ -1,8 +1,7 @@ +use core::ffi::c_void; +use core::fmt; use core::slice; -use std::error::Error; -use std::ffi::c_void; -use std::fmt; -use std::str::FromStr; +use core::str::FromStr; use crate::core::*; use crate::ffi::*; @@ -31,7 +30,7 @@ macro_rules! http_subrequest_handler { ( $name: ident, $handler: expr ) => { unsafe extern "C" fn $name( r: *mut $crate::ffi::ngx_http_request_t, - data: *mut ::std::ffi::c_void, + data: *mut ::core::ffi::c_void, rc: $crate::ffi::ngx_int_t, ) -> $crate::ffi::ngx_int_t { $handler(r, data, rc) @@ -117,7 +116,7 @@ impl Request { /// Is this the main request (as opposed to a subrequest)? pub fn is_main(&self) -> bool { let main = self.0.main.cast(); - std::ptr::eq(self, main) + core::ptr::eq(self, main) } /// Request pool. @@ -338,7 +337,7 @@ impl Request { ngx_http_internal_redirect( (self as *const Request as *mut Request).cast(), uri_ptr, - std::ptr::null_mut(), + core::ptr::null_mut(), ); } } @@ -355,7 +354,7 @@ impl Request { let uri_ptr = unsafe { &mut ngx_str_t::from_str(self.0.pool, uri) as *mut _ }; // ------------- // allocate memory and set values for ngx_http_post_subrequest_t - let sub_ptr = self.pool().alloc(std::mem::size_of::()); + let sub_ptr = self.pool().alloc(core::mem::size_of::()); // assert!(sub_ptr.is_null()); 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 { } // ------------- - let mut psr: *mut ngx_http_request_t = std::ptr::null_mut(); + let mut psr: *mut ngx_http_request_t = core::ptr::null_mut(); let r = unsafe { ngx_http_subrequest( (self as *const Request as *mut Request).cast(), uri_ptr, - std::ptr::null_mut(), + core::ptr::null_mut(), &mut psr as *mut _, sub_ptr as *mut _, NGX_HTTP_SUBREQUEST_WAITED as _, @@ -384,7 +383,7 @@ impl Request { * allocate fake request body to avoid attempts to read it and to make * sure real body file (if already read) won't be closed by upstream */ - sr.request_body = self.pool().alloc(std::mem::size_of::()) as *mut _; + sr.request_body = self.pool().alloc(core::mem::size_of::()) as *mut _; if sr.request_body.is_null() { return Status::NGX_ERROR; @@ -710,7 +709,8 @@ impl fmt::Display for InvalidMethod { } } -impl Error for InvalidMethod {} +#[cfg(feature = "std")] +impl std::error::Error for InvalidMethod {} #[derive(Clone, PartialEq, Eq, Hash)] enum MethodInner { diff --git a/src/http/status.rs b/src/http/status.rs index 96e5503..ec77a92 100644 --- a/src/http/status.rs +++ b/src/http/status.rs @@ -1,5 +1,4 @@ -use std::error::Error; -use std::fmt; +use core::fmt; use crate::core::Status; use crate::ffi::*; @@ -29,7 +28,8 @@ impl fmt::Display for InvalidHTTPStatusCode { } } -impl Error for InvalidHTTPStatusCode {} +#[cfg(feature = "std")] +impl std::error::Error for InvalidHTTPStatusCode {} impl From for Status { fn from(val: HTTPStatus) -> Self { diff --git a/src/lib.rs b/src/lib.rs index 641fec8..52f8e88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,12 @@ //! # now you can use dynamic modules with the NGINX //! ``` +// support both std and no_std +#![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] +#[cfg(all(not(feature = "std"), feature = "alloc"))] +extern crate alloc; + /// The core module. /// /// This module provides fundamental utilities needed to interface with many NGINX primitives. @@ -54,6 +59,9 @@ pub mod http; /// The log module. /// /// This module provides an interface into the NGINX logger framework. +/// +/// This module is temporally available only with `std` feature. +#[cfg(feature = "std")] pub mod log; /// Define modules exported by this library. @@ -67,20 +75,20 @@ macro_rules! ngx_modules { #[allow(non_upper_case_globals)] pub static mut ngx_modules: [*const $crate::ffi::ngx_module_t; $crate::count!($( $mod, )+) + 1] = [ $( unsafe { &$mod } as *const $crate::ffi::ngx_module_t, )+ - ::std::ptr::null() + ::core::ptr::null() ]; #[no_mangle] #[allow(non_upper_case_globals)] - pub static mut ngx_module_names: [*const ::std::ffi::c_char; $crate::count!($( $mod, )+) + 1] = [ - $( concat!(stringify!($mod), "\0").as_ptr() as *const ::std::ffi::c_char, )+ - ::std::ptr::null() + pub static mut ngx_module_names: [*const ::core::ffi::c_char; $crate::count!($( $mod, )+) + 1] = [ + $( concat!(stringify!($mod), "\0").as_ptr() as *const ::core::ffi::c_char, )+ + ::core::ptr::null() ]; #[no_mangle] #[allow(non_upper_case_globals)] - pub static mut ngx_module_order: [*const ::std::ffi::c_char; 1] = [ - ::std::ptr::null() + pub static mut ngx_module_order: [*const ::core::ffi::c_char; 1] = [ + ::core::ptr::null() ]; }; }