From a8a37a2ad3ef72304a2495ce5fa9970839cc3341 Mon Sep 17 00:00:00 2001 From: Petr Portnov Date: Tue, 11 Jul 2023 13:45:32 +0300 Subject: [PATCH] chore(#92): use `cstr` crate --- crates/Cargo.toml | 1 + crates/flipperzero/examples/storage.rs | 4 ++-- crates/sys/Cargo.toml | 1 + crates/sys/src/lib.rs | 13 +++---------- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/crates/Cargo.toml b/crates/Cargo.toml index d3ad99e9..98e2a379 100644 --- a/crates/Cargo.toml +++ b/crates/Cargo.toml @@ -23,6 +23,7 @@ flipperzero-sys = { path = "sys", version = "0.10.0" } flipperzero-rt = { path = "rt", version = "0.10.0" } flipperzero-alloc = { path = "alloc", version = "0.10.0" } flipperzero-test = { path = "test", version = "0.10.0" } +cstr = "0.2.11" ufmt = "0.2.0" document-features = "0.2.0" diff --git a/crates/flipperzero/examples/storage.rs b/crates/flipperzero/examples/storage.rs index 17ec42ea..18ecb194 100644 --- a/crates/flipperzero/examples/storage.rs +++ b/crates/flipperzero/examples/storage.rs @@ -18,7 +18,7 @@ use flipperzero::io::*; use flipperzero::println; use flipperzero::storage::*; use flipperzero_rt::{entry, manifest}; -use flipperzero_sys::c_str; +use flipperzero_sys::cstr; manifest!(name = "Rust storage example"); entry!(main); @@ -43,7 +43,7 @@ fn main(_args: *mut u8) -> i32 { // Next, we'll open a file browser dialog and let the user select the file. let mut dialogs_app = DialogsApp::open(); - static EXTENSION: &CStr = c_str!("*"); + static EXTENSION: &CStr = cstr!("*"); let file_browser_options = DialogFileBrowserOptions::new(EXTENSION).set_hide_ext(false); let mut start_path = FuriString::from(path); let result_path = diff --git a/crates/sys/Cargo.toml b/crates/sys/Cargo.toml index 93a56f9e..f5914a5d 100644 --- a/crates/sys/Cargo.toml +++ b/crates/sys/Cargo.toml @@ -22,4 +22,5 @@ bench = false test = false [dependencies] +cstr.workspace = true ufmt.workspace = true diff --git a/crates/sys/src/lib.rs b/crates/sys/src/lib.rs index 5ee18941..309ce3d8 100644 --- a/crates/sys/src/lib.rs +++ b/crates/sys/src/lib.rs @@ -31,24 +31,17 @@ mod bindings; /// Create a static C string of type [`*const c_char`][core::ffi::c_char]. /// Will automatically add a NUL terminator. +// TODO: don't produce intermediate `CStr` whose `length` part we don't use #[macro_export] macro_rules! c_string { ($str:expr $(,)?) => {{ - ::core::concat!($str, "\0").as_ptr() as *const ::core::ffi::c_char + $crate::cstr!($str).as_ptr() }}; } /// Create a static C string of type [`&CStr`][`core::ffi::CStr`]. /// Will automatically add a NUL terminator. -#[macro_export] -macro_rules! c_str { - ($str:expr $(,)?) => {{ - match ::core::ffi::CStr::from_bytes_with_nul(::core::concat!($str, "\0").as_bytes()) { - Ok(c_str) => c_str, - Err(error) => panic!("invalid C-string literal"), - } - }}; -} +pub use cstr::cstr; /// Crash the system. #[macro_export]