Skip to content

Commit 9b17f64

Browse files
committed
examples/rust: Do some simplification around unsafe {} blocks
We can put the unsafe keyword as part of the function definition, getting rid of the unsafe {} blocks in the functions themselves. Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
1 parent 73d0914 commit 9b17f64

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@ use std::ptr::null_mut;
1717
static mut REQUEST_BUF: *mut u8 = null_mut();
1818

1919
#[no_mangle]
20-
pub extern "C" fn uwr_module_end_handler() {
21-
unsafe {
22-
uwr_free(REQUEST_BUF);
23-
}
20+
pub unsafe extern "C" fn uwr_module_end_handler() {
21+
uwr_free(REQUEST_BUF);
2422
}
2523

2624
#[no_mangle]
27-
pub extern "C" fn uwr_module_init_handler() {
28-
unsafe {
29-
REQUEST_BUF = uwr_malloc(uwr_mem_get_init_size());
30-
}
25+
pub unsafe extern "C" fn uwr_module_init_handler() {
26+
REQUEST_BUF = uwr_malloc(uwr_mem_get_init_size());
3127
}
3228

3329
pub extern "C" fn hdr_iter_func(

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ static mut REQUEST_BUF: *mut u8 = null_mut();
1616
static mut TOTAL_BYTES_WROTE: u64 = 0;
1717

1818
#[no_mangle]
19-
pub extern "C" fn uwr_module_end_handler() {
20-
unsafe { uwr_free(REQUEST_BUF); }
19+
pub unsafe extern "C" fn uwr_module_end_handler() {
20+
uwr_free(REQUEST_BUF);
2121
}
2222

2323
#[no_mangle]
24-
pub extern "C" fn uwr_module_init_handler() {
25-
unsafe { REQUEST_BUF = uwr_malloc(uwr_mem_get_init_size()); }
24+
pub unsafe extern "C" fn uwr_module_init_handler() {
25+
REQUEST_BUF = uwr_malloc(uwr_mem_get_init_size());
2626
}
2727

2828
#[no_mangle]
29-
pub extern "C" fn uwr_response_end_handler() {
30-
unsafe { TOTAL_BYTES_WROTE = 0; }
29+
pub unsafe extern "C" fn uwr_response_end_handler() {
30+
TOTAL_BYTES_WROTE = 0;
3131
}
3232

3333
#[no_mangle]

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,18 @@ static mut TOTAL_RESPONSE_SENT: usize = 0;
1818
static mut REQUEST_BUF: *mut u8 = null_mut();
1919

2020
#[no_mangle]
21-
pub extern "C" fn uwr_response_end_handler() {
22-
unsafe {
23-
TOTAL_RESPONSE_SENT = 0;
24-
}
21+
pub unsafe extern "C" fn uwr_response_end_handler() {
22+
TOTAL_RESPONSE_SENT = 0;
2523
}
2624

2725
#[no_mangle]
28-
pub extern "C" fn uwr_request_end_handler() {
29-
unsafe {
30-
if REQUEST_BUF.is_null() {
31-
return;
32-
}
33-
34-
uwr_free(REQUEST_BUF);
35-
REQUEST_BUF = null_mut();
26+
pub unsafe extern "C" fn uwr_request_end_handler() {
27+
if REQUEST_BUF.is_null() {
28+
return;
3629
}
30+
31+
uwr_free(REQUEST_BUF);
32+
REQUEST_BUF = null_mut();
3733
}
3834

3935
pub fn upload_reflector(ctx: *mut luw_ctx_t) -> i32 {

0 commit comments

Comments
 (0)