From 11607643adbb45675ce3c1487959ba7a6e2313c8 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 28 Aug 2024 20:34:32 +0200 Subject: [PATCH 1/2] treewide: Reduce use of cstr! macro in favor of c"" literals --- Cargo.toml | 2 ++ src/gcoap.rs | 2 +- src/panic.rs | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b8c240b..4d8109e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,8 @@ mutex-trait = "0.2" bare-metal = "1" +# Only used for shell, where a macro is described to take a string literal and +# in the macro implementation turns it into a CStr. cstr = "^0.2.11" heapless = "^0.8" diff --git a/src/gcoap.rs b/src/gcoap.rs index 4e530d0..8d90768 100644 --- a/src/gcoap.rs +++ b/src/gcoap.rs @@ -155,7 +155,7 @@ where /// [coap_handler::Handler], you can wrap it in [crate::coap_handler::GcoapHandler] to for adaptation. pub fn new_catch_all(handler: &'a mut H) -> Self { Self::new( - cstr::cstr!("/"), + c"/", riot_sys::COAP_GET | riot_sys::COAP_POST | riot_sys::COAP_PUT diff --git a/src/panic.rs b/src/panic.rs index 688d5da..0176125 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -22,7 +22,7 @@ fn panic(info: &::core::panic::PanicInfo) -> ! { unsafe { riot_sys::core_panic( riot_sys::core_panic_t_PANIC_GENERAL_ERROR, - cstr::cstr!("RUST PANIC").as_ptr() as _, + c"RUST PANIC".as_ptr() as _, ) }; } else { From a4b15cee9d2e6d943df04c4b256783ac74c2f3b2 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 28 Aug 2024 20:42:12 +0200 Subject: [PATCH 2/2] cleanup: Get null pointers through core rather than casting 0 --- src/gnrc/ipv6.rs | 2 +- src/gnrc/mod.rs | 2 +- src/gnrc_pktbuf.rs | 2 +- src/saul/registration.rs | 2 +- src/ztimer/periodic.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/gnrc/ipv6.rs b/src/gnrc/ipv6.rs index d8d401a..2580533 100644 --- a/src/gnrc/ipv6.rs +++ b/src/gnrc/ipv6.rs @@ -215,7 +215,7 @@ impl Pktsnip { // unsafe: C API, and requirement on a Pktsnip that typed snips follow that type's // conventions let ptr = unsafe { riot_sys::gnrc_ipv6_get_header(self.ptr) }; - if ptr == 0 as _ { + if ptr.is_null() { None } else { // unsafe: Header is a transparent wrapper around the actual ipv6_hdr_t, and the diff --git a/src/gnrc/mod.rs b/src/gnrc/mod.rs index 088edb8..0255226 100644 --- a/src/gnrc/mod.rs +++ b/src/gnrc/mod.rs @@ -47,7 +47,7 @@ impl Netif { #[doc(alias = "gnrc_netif_get_by_pid")] pub fn by_pid(pid: KernelPID) -> Option { - const NULL: *mut riot_sys::gnrc_netif_t = 0 as _; + const NULL: *mut riot_sys::gnrc_netif_t = core::ptr::null_mut(); // Not using as_ref: We can't guarantee that even for the short period between we're making // it into a reference and casting it back to a pointer again, it is not used by anyone // else diff --git a/src/gnrc_pktbuf.rs b/src/gnrc_pktbuf.rs index 4d2aa47..5155c91 100644 --- a/src/gnrc_pktbuf.rs +++ b/src/gnrc_pktbuf.rs @@ -201,7 +201,7 @@ impl<'a> Pktsnip { // unsafe: The C functions justify the new type unsafe { let new = riot_sys::gnrc_pktbuf_start_write(self.to_ptr()); - if new == 0 as _ { + if new.is_null() { Err(NotEnoughSpace) } else { Ok(Pktsnip::::from_ptr(new)) diff --git a/src/saul/registration.rs b/src/saul/registration.rs index cc2d22b..ba0e6bc 100644 --- a/src/saul/registration.rs +++ b/src/saul/registration.rs @@ -195,7 +195,7 @@ where ) -> Self { Registration { reg: riot_sys::saul_reg_t { - next: 0 as _, + next: core::ptr::null_mut(), dev: device as *const _ as *mut _, name: name.map(|n| n.as_ptr() as _).unwrap_or(core::ptr::null()), driver: &driver.driver as *const _, diff --git a/src/ztimer/periodic.rs b/src/ztimer/periodic.rs index 0694990..13393b0 100644 --- a/src/ztimer/periodic.rs +++ b/src/ztimer/periodic.rs @@ -80,7 +80,7 @@ impl Timer { clock.0, timer.as_mut_ptr(), Some(Self::callback), - 0 as _, + core::ptr::null_mut(), ticks.0, ); timer.assume_init()