Skip to content

Commit cd1940f

Browse files
committed
refactor: changes to support edition = "2024"
enforced by workspace level lints, now specified in each Cargo.toml, except nginx-sys, because bindgen can't be set to Edition2024 until MSRV hits 1.85 this change set is created automatically by `cargo fix --edition --all`, then with manual fixes afterwards: * remove uses of expr_2021 in macros, which is not available in the MSRV. * Manual fix to ngx_container_of! macro for safety rules in 2024, as well as various conditional and in-macro uses of no_mangle - these weren't migrated automatically by `cargo fix` * Manual fixes to several other macros that created an &mut Request in an expression, Rust 2024 is stricter about taking &mut of temporaries, so instead the request is let-bound first.
1 parent 07ce1f7 commit cd1940f

30 files changed

+561
-420
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ homepage = "https://github.com/nginx/ngx-rust"
1212
repository = "https://github.com/nginx/ngx-rust"
1313
rust-version = "1.81.0"
1414

15+
[workspace.lints.rust]
16+
rust-2024-compatibility = "warn"
17+
edition-2024-expr-fragment-specifier = "allow"
18+
1519
[package]
1620
name = "ngx"
1721
version = "0.5.0-beta"
@@ -70,3 +74,6 @@ maintenance = { status = "experimental" }
7074

7175
[dev-dependencies]
7276
tempfile = { version = "3.20.0", default-features = false }
77+
78+
[lints]
79+
workspace = true

examples/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ default = ["export-modules", "ngx/vendored"]
6565
# See https://github.com/rust-lang/rust/issues/20267
6666
export-modules = []
6767
linux = []
68+
69+
[lints]
70+
workspace = true

examples/async.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ impl http::HttpModule for Module {
2424
}
2525

2626
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
27-
// SAFETY: this function is called with non-NULL cf always
28-
let cf = &mut *cf;
29-
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
30-
31-
let h = ngx_array_push(
32-
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
33-
) as *mut ngx_http_handler_pt;
34-
if h.is_null() {
35-
return core::Status::NGX_ERROR.into();
27+
unsafe {
28+
// SAFETY: this function is called with non-NULL cf always
29+
let cf = &mut *cf;
30+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
31+
32+
let h = ngx_array_push(
33+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
34+
) as *mut ngx_http_handler_pt;
35+
if h.is_null() {
36+
return core::Status::NGX_ERROR.into();
37+
}
38+
// set an Access phase handler
39+
*h = Some(async_access_handler);
40+
core::Status::NGX_OK.into()
3641
}
37-
// set an Access phase handler
38-
*h = Some(async_access_handler);
39-
core::Status::NGX_OK.into()
4042
}
4143
}
4244

@@ -79,7 +81,7 @@ ngx::ngx_modules!(ngx_http_async_module);
7981

8082
#[used]
8183
#[allow(non_upper_case_globals)]
82-
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
84+
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
8385
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
8486
ctx: std::ptr::addr_of!(NGX_HTTP_ASYNC_MODULE_CTX) as _,
8587
commands: unsafe { &NGX_HTTP_ASYNC_COMMANDS[0] as *const _ as *mut _ },
@@ -98,16 +100,16 @@ impl http::Merge for ModuleConfig {
98100

99101
unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) {
100102
let ctx = ngx::ngx_container_of!(event, RequestCTX, event);
101-
let c: *mut ngx_connection_t = (*event).data.cast();
103+
let c: *mut ngx_connection_t = unsafe { (*event).data.cast() };
102104

103-
if (*ctx).done.load(Ordering::Relaxed) {
105+
if unsafe { (*ctx).done.load(Ordering::Relaxed) } {
104106
// Triggering async_access_handler again
105-
ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events));
107+
unsafe { ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events)) };
106108
} else {
107109
// this doesn't have have good performance but works as a simple thread-safe example and
108110
// doesn't causes segfault. The best method that provides both thread-safety and
109111
// performance requires an nginx patch.
110-
ngx_post_event(event, addr_of_mut!(ngx_posted_next_events));
112+
unsafe { ngx_post_event(event, addr_of_mut!(ngx_posted_next_events)) };
111113
}
112114
}
113115

examples/awssig.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Suppress spurrions lint for 2024 compatibility
2+
#![allow(tail_expr_drop_order)]
3+
14
use std::ffi::{c_char, c_void};
25

36
use http::HeaderMap;
@@ -19,19 +22,21 @@ impl HttpModule for Module {
1922
}
2023

2124
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
22-
// SAFETY: this function is called with non-NULL cf always
23-
let cf = &mut *cf;
24-
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
25-
26-
let h = ngx_array_push(
27-
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers,
28-
) as *mut ngx_http_handler_pt;
29-
if h.is_null() {
30-
return core::Status::NGX_ERROR.into();
25+
unsafe {
26+
// SAFETY: this function is called with non-NULL cf always
27+
let cf = &mut *cf;
28+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
29+
30+
let h = ngx_array_push(
31+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers,
32+
) as *mut ngx_http_handler_pt;
33+
if h.is_null() {
34+
return core::Status::NGX_ERROR.into();
35+
}
36+
// set an phase handler
37+
*h = Some(awssigv4_header_handler);
38+
core::Status::NGX_OK.into()
3139
}
32-
// set an phase handler
33-
*h = Some(awssigv4_header_handler);
34-
core::Status::NGX_OK.into()
3540
}
3641
}
3742

@@ -110,7 +115,7 @@ ngx::ngx_modules!(ngx_http_awssigv4_module);
110115

111116
#[used]
112117
#[allow(non_upper_case_globals)]
113-
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
118+
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
114119
pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t {
115120
ctx: std::ptr::addr_of!(NGX_HTTP_AWSSIGV4_MODULE_CTX) as _,
116121
commands: unsafe { &NGX_HTTP_AWSSIGV4_COMMANDS[0] as *const _ as *mut _ },
@@ -298,10 +303,13 @@ http_request_handler!(awssigv4_header_handler, |request: &mut Request| {
298303
for (name, value) in request.headers_in_iterator() {
299304
if let Ok(name) = name.to_str() {
300305
if name.to_lowercase() == "host" {
301-
if let Ok(value) = http::HeaderValue::from_bytes(value.as_bytes()) {
302-
headers.insert(http::header::HOST, value);
303-
} else {
304-
return core::Status::NGX_DECLINED;
306+
match http::HeaderValue::from_bytes(value.as_bytes()) {
307+
Ok(value) => {
308+
headers.insert(http::header::HOST, value);
309+
}
310+
_ => {
311+
return core::Status::NGX_DECLINED;
312+
}
305313
}
306314
}
307315
} else {

examples/curl.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ impl http::HttpModule for Module {
1818
}
1919

2020
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
21-
// SAFETY: this function is called with non-NULL cf always
22-
let cf = &mut *cf;
23-
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
24-
25-
let h = ngx_array_push(
26-
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
27-
) as *mut ngx_http_handler_pt;
28-
if h.is_null() {
29-
return core::Status::NGX_ERROR.into();
21+
unsafe {
22+
// SAFETY: this function is called with non-NULL cf always
23+
let cf = &mut *cf;
24+
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");
25+
26+
let h = ngx_array_push(
27+
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
28+
) as *mut ngx_http_handler_pt;
29+
if h.is_null() {
30+
return core::Status::NGX_ERROR.into();
31+
}
32+
// set an Access phase handler
33+
*h = Some(curl_access_handler);
34+
core::Status::NGX_OK.into()
3035
}
31-
// set an Access phase handler
32-
*h = Some(curl_access_handler);
33-
core::Status::NGX_OK.into()
3436
}
3537
}
3638

@@ -73,7 +75,7 @@ ngx::ngx_modules!(ngx_http_curl_module);
7375

7476
#[used]
7577
#[allow(non_upper_case_globals)]
76-
#[cfg_attr(not(feature = "export-modules"), no_mangle)]
78+
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
7779
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
7880
ctx: std::ptr::addr_of!(NGX_HTTP_CURL_MODULE_CTX) as _,
7981
commands: unsafe { &NGX_HTTP_CURL_COMMANDS[0] as *const _ as *mut _ },

0 commit comments

Comments
 (0)