Skip to content

Commit

Permalink
chore(flipperzero-rs#92): use cstr crate
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft committed Jul 11, 2023
1 parent 70bf1c6 commit a8a37a2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions crates/flipperzero/examples/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions crates/sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ bench = false
test = false

[dependencies]
cstr.workspace = true
ufmt.workspace = true
13 changes: 3 additions & 10 deletions crates/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit a8a37a2

Please sign in to comment.