Skip to content

Commit 4173182

Browse files
committed
chore: reduce boilerplate code for ngx_module_t declarations
1 parent 2d7fd00 commit 4173182

File tree

6 files changed

+57
-146
lines changed

6 files changed

+57
-146
lines changed

examples/async.rs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use std::time::Instant;
66

77
use ngx::core;
88
use ngx::core::prelude::*;
9-
use ngx::ffi::{
10-
nginx_version, ngx_array_push, ngx_http_core_run_phases, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_queue_s,
11-
NGX_RS_MODULE_SIGNATURE,
12-
};
9+
use ngx::ffi::{ngx_array_push, ngx_http_core_run_phases, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_queue_s};
1310
use ngx::http::prelude::*;
1411
use ngx::http::{self, HTTPModule, MergeConfigError};
1512
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
@@ -86,34 +83,10 @@ ngx::ngx_modules!(ngx_http_async_module);
8683
#[allow(non_upper_case_globals)]
8784
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
8885
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
89-
ctx_index: ngx_uint_t::MAX,
90-
index: ngx_uint_t::MAX,
91-
name: std::ptr::null_mut(),
92-
spare0: 0,
93-
spare1: 0,
94-
version: nginx_version as ngx_uint_t,
95-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
96-
97-
ctx: &NGX_HTTP_ASYNC_MODULE_CTX as *const _ as *mut _,
86+
ctx: std::ptr::addr_of!(NGX_HTTP_ASYNC_MODULE_CTX) as _,
9887
commands: unsafe { &NGX_HTTP_ASYNC_COMMANDS[0] as *const _ as *mut _ },
99-
type_: NGX_HTTP_MODULE as ngx_uint_t,
100-
101-
init_master: None,
102-
init_module: None,
103-
init_process: None,
104-
init_thread: None,
105-
exit_thread: None,
106-
exit_process: None,
107-
exit_master: None,
108-
109-
spare_hook0: 0,
110-
spare_hook1: 0,
111-
spare_hook2: 0,
112-
spare_hook3: 0,
113-
spare_hook4: 0,
114-
spare_hook5: 0,
115-
spare_hook6: 0,
116-
spare_hook7: 0,
88+
type_: NGX_HTTP_MODULE as _,
89+
..ngx_module_t::default()
11790
};
11891

11992
impl http::Merge for ModuleConfig {

examples/awssig.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ptr::addr_of;
44
use http::HeaderMap;
55
use ngx::core;
66
use ngx::core::prelude::*;
7-
use ngx::ffi::{nginx_version, ngx_array_push, ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, NGX_RS_MODULE_SIGNATURE};
7+
use ngx::ffi::{ngx_array_push, ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE};
88
use ngx::http::prelude::*;
99
use ngx::http::*;
1010
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
@@ -103,34 +103,10 @@ ngx::ngx_modules!(ngx_http_awssigv4_module);
103103
#[allow(non_upper_case_globals)]
104104
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
105105
pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t {
106-
ctx_index: ngx_uint_t::MAX,
107-
index: ngx_uint_t::MAX,
108-
name: std::ptr::null_mut(),
109-
spare0: 0,
110-
spare1: 0,
111-
version: nginx_version as ngx_uint_t,
112-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
113-
114-
ctx: &NGX_HTTP_AWSSIGV4_MODULE_CTX as *const _ as *mut _,
106+
ctx: std::ptr::addr_of!(NGX_HTTP_AWSSIGV4_MODULE_CTX) as _,
115107
commands: unsafe { &NGX_HTTP_AWSSIGV4_COMMANDS[0] as *const _ as *mut _ },
116-
type_: NGX_HTTP_MODULE as ngx_uint_t,
117-
118-
init_master: None,
119-
init_module: None,
120-
init_process: None,
121-
init_thread: None,
122-
exit_thread: None,
123-
exit_process: None,
124-
exit_master: None,
125-
126-
spare_hook0: 0,
127-
spare_hook1: 0,
128-
spare_hook2: 0,
129-
spare_hook3: 0,
130-
spare_hook4: 0,
131-
spare_hook5: 0,
132-
spare_hook6: 0,
133-
spare_hook7: 0,
108+
type_: NGX_HTTP_MODULE as _,
109+
..ngx_module_t::default()
134110
};
135111

136112
impl Merge for ModuleConfig {

examples/curl.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ptr::addr_of;
33

44
use ngx::core;
55
use ngx::core::prelude::*;
6-
use ngx::ffi::{nginx_version, ngx_array_push, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, NGX_RS_MODULE_SIGNATURE};
6+
use ngx::ffi::{ngx_array_push, ngx_http_phases_NGX_HTTP_ACCESS_PHASE};
77
use ngx::http::prelude::*;
88
use ngx::http::{self, HTTPModule, MergeConfigError};
99
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
@@ -66,34 +66,10 @@ ngx::ngx_modules!(ngx_http_curl_module);
6666
#[allow(non_upper_case_globals)]
6767
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
6868
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
69-
ctx_index: ngx_uint_t::MAX,
70-
index: ngx_uint_t::MAX,
71-
name: std::ptr::null_mut(),
72-
spare0: 0,
73-
spare1: 0,
74-
version: nginx_version as ngx_uint_t,
75-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
76-
77-
ctx: &NGX_HTTP_CURL_MODULE_CTX as *const _ as *mut _,
69+
ctx: std::ptr::addr_of!(NGX_HTTP_CURL_MODULE_CTX) as _,
7870
commands: unsafe { &NGX_HTTP_CURL_COMMANDS[0] as *const _ as *mut _ },
79-
type_: NGX_HTTP_MODULE as ngx_uint_t,
80-
81-
init_master: None,
82-
init_module: None,
83-
init_process: None,
84-
init_thread: None,
85-
exit_thread: None,
86-
exit_process: None,
87-
exit_master: None,
88-
89-
spare_hook0: 0,
90-
spare_hook1: 0,
91-
spare_hook2: 0,
92-
spare_hook3: 0,
93-
spare_hook4: 0,
94-
spare_hook5: 0,
95-
spare_hook6: 0,
96-
spare_hook7: 0,
71+
type_: NGX_HTTP_MODULE as _,
72+
..ngx_module_t::default()
9773
};
9874

9975
impl http::Merge for ModuleConfig {

examples/httporigdst.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::ffi::{c_char, c_int, c_void};
1+
use std::ffi::{c_int, c_void};
22
use std::ptr::addr_of;
33

44
use ngx::core;
55
use ngx::core::prelude::*;
66
use ngx::ffi::{
7-
in_port_t, nginx_version, ngx_connection_local_sockaddr, ngx_inet_get_port, ngx_sock_ntop, sockaddr,
8-
sockaddr_storage, INET_ADDRSTRLEN, NGX_RS_MODULE_SIGNATURE,
7+
in_port_t, ngx_connection_local_sockaddr, ngx_inet_get_port, ngx_sock_ntop, sockaddr, sockaddr_storage,
8+
INET_ADDRSTRLEN,
99
};
1010
use ngx::http::prelude::*;
1111
use ngx::http::{self, HTTPModule};
@@ -97,33 +97,10 @@ ngx::ngx_modules!(ngx_http_orig_dst_module);
9797
#[allow(non_upper_case_globals)]
9898
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
9999
pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t {
100-
ctx_index: ngx_uint_t::MAX,
101-
index: ngx_uint_t::MAX,
102-
name: std::ptr::null_mut(),
103-
spare0: 0,
104-
spare1: 0,
105-
version: nginx_version as ngx_uint_t,
106-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
107-
ctx: &NGX_HTTP_ORIG_DST_MODULE_CTX as *const _ as *mut _,
100+
ctx: std::ptr::addr_of!(NGX_HTTP_ORIG_DST_MODULE_CTX) as _,
108101
commands: std::ptr::null_mut(),
109-
type_: NGX_HTTP_MODULE as ngx_uint_t,
110-
111-
init_master: None,
112-
init_module: None,
113-
init_process: None,
114-
init_thread: None,
115-
exit_thread: None,
116-
exit_process: None,
117-
exit_master: None,
118-
119-
spare_hook0: 0,
120-
spare_hook1: 0,
121-
spare_hook2: 0,
122-
spare_hook3: 0,
123-
spare_hook4: 0,
124-
spare_hook5: 0,
125-
spare_hook6: 0,
126-
spare_hook7: 0,
102+
type_: NGX_HTTP_MODULE as _,
103+
..ngx_module_t::default()
127104
};
128105

129106
static mut NGX_HTTP_ORIG_DST_VARS: [ngx_http_variable_t; 3] = [

examples/upstream.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use std::slice;
1414
use ngx::core::prelude::*;
1515
use ngx::core::{Pool, Status};
1616
use ngx::ffi::{
17-
nginx_version, ngx_atoi, ngx_event_free_peer_pt, ngx_event_get_peer_pt, ngx_http_upstream_init_peer_pt,
18-
ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin, ngx_http_upstream_module,
19-
ngx_http_upstream_srv_conf_t, ngx_http_upstream_t, ngx_peer_connection_t, NGX_ERROR, NGX_RS_MODULE_SIGNATURE,
17+
ngx_atoi, ngx_event_free_peer_pt, ngx_event_get_peer_pt, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt,
18+
ngx_http_upstream_init_round_robin, ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t,
19+
ngx_peer_connection_t, NGX_ERROR,
2020
};
2121
use ngx::http::prelude::*;
2222
use ngx::http::{
@@ -109,34 +109,10 @@ ngx::ngx_modules!(ngx_http_upstream_custom_module);
109109
#[allow(non_upper_case_globals)]
110110
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
111111
pub static mut ngx_http_upstream_custom_module: ngx_module_t = ngx_module_t {
112-
ctx_index: ngx_uint_t::MAX,
113-
index: ngx_uint_t::MAX,
114-
name: std::ptr::null_mut(),
115-
spare0: 0,
116-
spare1: 0,
117-
version: nginx_version as ngx_uint_t,
118-
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const c_char,
119-
120-
ctx: &NGX_HTTP_UPSTREAM_CUSTOM_CTX as *const _ as *mut _,
112+
ctx: std::ptr::addr_of!(NGX_HTTP_UPSTREAM_CUSTOM_CTX) as _,
121113
commands: unsafe { &NGX_HTTP_UPSTREAM_CUSTOM_COMMANDS[0] as *const _ as *mut _ },
122-
type_: NGX_HTTP_MODULE as ngx_uint_t,
123-
124-
init_master: None,
125-
init_module: None,
126-
init_process: None,
127-
init_thread: None,
128-
exit_thread: None,
129-
exit_process: None,
130-
exit_master: None,
131-
132-
spare_hook0: 0,
133-
spare_hook1: 0,
134-
spare_hook2: 0,
135-
spare_hook3: 0,
136-
spare_hook4: 0,
137-
spare_hook5: 0,
138-
spare_hook6: 0,
139-
spare_hook7: 0,
114+
type_: NGX_HTTP_MODULE as _,
115+
..ngx_module_t::default()
140116
};
141117

142118
// http_upstream_init_custom_peer

nginx-sys/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,39 @@ impl TryFrom<ngx_str_t> for &str {
188188
}
189189
}
190190

191+
impl ngx_module_t {
192+
/// Create a new `ngx_module_t` instance with default values.
193+
pub const fn default() -> Self {
194+
Self {
195+
ctx_index: ngx_uint_t::MAX,
196+
index: ngx_uint_t::MAX,
197+
name: core::ptr::null_mut(),
198+
spare0: 0,
199+
spare1: 0,
200+
version: nginx_version as ngx_uint_t,
201+
signature: NGX_RS_MODULE_SIGNATURE.as_ptr() as *const core::ffi::c_char,
202+
ctx: core::ptr::null_mut(),
203+
commands: core::ptr::null_mut(),
204+
type_: 0,
205+
init_master: None,
206+
init_module: None,
207+
init_process: None,
208+
init_thread: None,
209+
exit_thread: None,
210+
exit_process: None,
211+
exit_master: None,
212+
spare_hook0: 0,
213+
spare_hook1: 0,
214+
spare_hook2: 0,
215+
spare_hook3: 0,
216+
spare_hook4: 0,
217+
spare_hook5: 0,
218+
spare_hook6: 0,
219+
spare_hook7: 0,
220+
}
221+
}
222+
}
223+
191224
/// Add a key-value pair to an nginx table entry (`ngx_table_elt_t`) in the given nginx memory pool.
192225
///
193226
/// # Arguments

0 commit comments

Comments
 (0)