Skip to content

Commit 2cf492f

Browse files
committed
examples/rust: Fix some new rustc warnings
With at least rustc 1.79.0 (129f3b996 2024-06-10) (Fedora 1.79.0-3.fc40) We were getting warnings when building the rust examples like warning: creating a mutable reference to mutable static is discouraged --> src/lib.rs:75:40 | 75 | let ctx: *mut luw_ctx_t = unsafe { &mut CTX }; | ^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #114447 <rust-lang/rust#114447> = note: this will be a hard error in the 2024 edition = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior = note: `#[warn(static_mut_refs)]` on by default help: use `addr_of_mut!` instead to create a raw pointer | 75 | let ctx: *mut luw_ctx_t = unsafe { addr_of_mut!(CTX) }; | ~~~~~~~~~~~~~~~~~ So do like it says and use the addr_of_mut!() macro. Signed-off-by: Andrew Clayton <[email protected]>
1 parent 3a751f7 commit 2cf492f

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

examples/rust/echo-request/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use unit_wasm::rusty::*;
1111
use std::ffi::CStr;
1212
use std::os::raw::c_char;
1313
use std::os::raw::c_void;
14-
use std::ptr::null_mut;
14+
use std::ptr::{addr_of_mut, null_mut};
1515

1616
// Buffer of some size to store the copy of the request
1717
static mut REQUEST_BUF: *mut u8 = null_mut();
@@ -51,7 +51,7 @@ pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 {
5151
uwr_init_ctx(ctx, addr, 4096);
5252

5353
// Set where we will copy the request into
54-
uwr_set_req_buf(ctx, unsafe { &mut REQUEST_BUF }, LUW_SRB_NONE);
54+
uwr_set_req_buf(ctx, unsafe { addr_of_mut!(REQUEST_BUF) }, LUW_SRB_NONE);
5555

5656
// Define the Response Body Text.
5757

examples/rust/large-upload/src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use unit_wasm::rusty::*;
1010

1111
use std::fs::File;
12-
use std::ptr::null_mut;
12+
use std::ptr::{addr_of_mut, null_mut};
1313

1414
static mut CTX: luw_ctx_t = UWR_CTX_INITIALIZER();
1515
static mut REQUEST_BUF: *mut u8 = null_mut();
@@ -32,14 +32,18 @@ pub unsafe extern "C" fn uwr_response_end_handler() {
3232

3333
#[no_mangle]
3434
pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 {
35-
let ctx: *mut luw_ctx_t = unsafe { &mut CTX };
35+
let ctx: *mut luw_ctx_t = unsafe { addr_of_mut!(CTX) };
3636
let mut f;
3737
let bytes_wrote: isize;
3838
let mut total = unsafe { TOTAL_BYTES_WROTE };
3939

4040
if total == 0 {
4141
uwr_init_ctx(ctx, addr, 0);
42-
uwr_set_req_buf(ctx, unsafe { &mut REQUEST_BUF }, LUW_SRB_NONE);
42+
uwr_set_req_buf(
43+
ctx,
44+
unsafe { addr_of_mut!(REQUEST_BUF) },
45+
LUW_SRB_NONE,
46+
);
4347

4448
f = File::create("/var/tmp/large-file.dat").unwrap();
4549
} else {

examples/rust/upload-reflector/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
use unit_wasm::rusty::*;
1010

11-
use std::ptr::null_mut;
11+
use std::ptr::{addr_of_mut, null_mut};
1212

1313
static mut CTX: luw_ctx_t = UWR_CTX_INITIALIZER();
1414

@@ -72,7 +72,7 @@ pub fn upload_reflector(ctx: *mut luw_ctx_t) -> i32 {
7272

7373
#[no_mangle]
7474
pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 {
75-
let ctx: *mut luw_ctx_t = unsafe { &mut CTX };
75+
let ctx: *mut luw_ctx_t = unsafe { addr_of_mut!(CTX) };
7676

7777
if unsafe { REQUEST_BUF.is_null() } {
7878
uwr_init_ctx(ctx, addr, 0 /* Response offset */);
@@ -87,7 +87,7 @@ pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 {
8787
*/
8888
uwr_set_req_buf(
8989
ctx,
90-
unsafe { &mut REQUEST_BUF },
90+
unsafe { addr_of_mut!(REQUEST_BUF) },
9191
LUW_SRB_APPEND | LUW_SRB_ALLOC | LUW_SRB_FULL_SIZE,
9292
);
9393
} else {

0 commit comments

Comments
 (0)