Skip to content

Commit 5b0430e

Browse files
committed
refactor!: replace ngx_null_string! with ngx_str_t::empty()
1 parent a538120 commit 5b0430e

File tree

5 files changed

+27
-34
lines changed

5 files changed

+27
-34
lines changed

examples/httporigdst.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,16 @@ use ngx::ffi::{
88
sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE,
99
};
1010
use ngx::http::{self, HTTPModule};
11-
use ngx::{http_variable_get, ngx_log_debug_http, ngx_null_string, ngx_string};
11+
use ngx::{http_variable_get, ngx_log_debug_http, ngx_string};
1212

1313
const IPV4_STRLEN: usize = INET_ADDRSTRLEN as usize;
1414

15-
#[derive(Debug)]
15+
#[derive(Debug, Default)]
1616
struct NgxHttpOrigDstCtx {
1717
orig_dst_addr: ngx_str_t,
1818
orig_dst_port: ngx_str_t,
1919
}
2020

21-
impl Default for NgxHttpOrigDstCtx {
22-
fn default() -> NgxHttpOrigDstCtx {
23-
NgxHttpOrigDstCtx {
24-
orig_dst_addr: ngx_null_string!(),
25-
orig_dst_port: ngx_null_string!(),
26-
}
27-
}
28-
}
29-
3021
impl NgxHttpOrigDstCtx {
3122
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &mut core::Pool) -> core::Status {
3223
let addr_data = pool.alloc_unaligned(addr.len());

nginx-sys/src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod queue;
77

88
use core::fmt;
99
use core::mem::offset_of;
10-
use core::ptr::copy_nonoverlapping;
10+
use core::ptr::{self, copy_nonoverlapping};
1111
use core::slice;
1212

1313
#[doc(hidden)]
@@ -130,6 +130,16 @@ impl ngx_str_t {
130130
core::str::from_utf8(self.as_bytes()).unwrap()
131131
}
132132

133+
/// Creates an empty `ngx_str_t` instance.
134+
///
135+
/// This method replaces the `ngx_null_string` C macro.
136+
pub const fn empty() -> Self {
137+
ngx_str_t {
138+
len: 0,
139+
data: ptr::null_mut(),
140+
}
141+
}
142+
133143
/// Create an `ngx_str_t` instance from a byte slice.
134144
///
135145
/// # Safety
@@ -161,6 +171,12 @@ impl ngx_str_t {
161171
}
162172
}
163173

174+
impl Default for ngx_str_t {
175+
fn default() -> Self {
176+
Self::empty()
177+
}
178+
}
179+
164180
impl From<ngx_str_t> for &[u8] {
165181
fn from(s: ngx_str_t) -> Self {
166182
if s.len == 0 || s.data.is_null() {
@@ -202,13 +218,13 @@ impl ngx_module_t {
202218
Self {
203219
ctx_index: ngx_uint_t::MAX,
204220
index: ngx_uint_t::MAX,
205-
name: core::ptr::null_mut(),
221+
name: ptr::null_mut(),
206222
spare0: 0,
207223
spare1: 0,
208224
version: nginx_version as ngx_uint_t,
209225
signature: NGX_RS_MODULE_SIGNATURE.as_ptr(),
210-
ctx: core::ptr::null_mut(),
211-
commands: core::ptr::null_mut(),
226+
ctx: ptr::null_mut(),
227+
commands: ptr::null_mut(),
212228
type_: 0,
213229
init_master: None,
214230
init_module: None,

src/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use string::*;
1717
macro_rules! ngx_null_command {
1818
() => {
1919
$crate::ffi::ngx_command_t {
20-
name: $crate::ngx_null_string!(),
20+
name: $crate::ffi::ngx_str_t::empty(),
2121
type_: 0,
2222
set: None,
2323
conf: 0,

src/core/string.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use alloc::{borrow::Cow, string::String};
66
#[cfg(feature = "std")]
77
use std::{borrow::Cow, string::String};
88

9-
use crate::ffi::*;
9+
use crate::ffi::{ngx_str_t, u_char};
1010

1111
/// Static string initializer for [`ngx_str_t`].
1212
///
@@ -23,19 +23,6 @@ macro_rules! ngx_string {
2323
}};
2424
}
2525

26-
/// Static empty string initializer for [`ngx_str_t`].
27-
///
28-
/// [`ngx_str_t`]: https://nginx.org/en/docs/dev/development_guide.html#string_overview
29-
#[macro_export]
30-
macro_rules! ngx_null_string {
31-
() => {
32-
$crate::ffi::ngx_str_t {
33-
len: 0,
34-
data: ::core::ptr::null_mut(),
35-
}
36-
};
37-
}
38-
3926
/// Representation of a borrowed [Nginx string].
4027
///
4128
/// [Nginx string]: https://nginx.org/en/docs/dev/development_guide.html#string_overview
@@ -100,7 +87,7 @@ impl AsRef<[u8]> for NgxStr {
10087

10188
impl Default for &NgxStr {
10289
fn default() -> Self {
103-
// SAFETY: The null `ngx_str_t` is always a valid Nginx string.
104-
unsafe { NgxStr::from_ngx_str(ngx_null_string!()) }
90+
// SAFETY: The empty `ngx_str_t` is always a valid Nginx string.
91+
unsafe { NgxStr::from_ngx_str(ngx_str_t::default()) }
10592
}
10693
}

src/http/request.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::str::FromStr;
66
use crate::core::*;
77
use crate::ffi::*;
88
use crate::http::status::*;
9-
use crate::ngx_null_string;
109

1110
/// Define a static request handler.
1211
///
@@ -229,7 +228,7 @@ impl Request {
229228
// SAFETY: `ngx_http_complex_value` does not mutate `r` or `val` and guarentees that
230229
// a valid Nginx string is stored in `value` if it successfully returns.
231230
unsafe {
232-
let mut value = ngx_null_string!();
231+
let mut value = ngx_str_t::default();
233232
if ngx_http_complex_value(r, val, &mut value) != NGX_OK as ngx_int_t {
234233
return None;
235234
}

0 commit comments

Comments
 (0)