Skip to content

Commit

Permalink
refactor(flipperzero-rsGH-92): rollback all changes to c_string in …
Browse files Browse the repository at this point in the history
…favor of flipperzero-rs#120
  • Loading branch information
JarvisCraft committed Dec 3, 2023
1 parent 9e72f8b commit 6804d1e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 50 deletions.
1 change: 0 additions & 1 deletion crates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ flipperzero-sys = { path = "sys", version = "0.11.0" }
flipperzero-rt = { path = "rt", version = "0.11.0" }
flipperzero-alloc = { path = "alloc", version = "0.11.0" }
flipperzero-test = { path = "test", version = "0.11.0" }
cstr = "0.2.11"
ufmt = "0.2.0"
document-features = "0.2.0"

Expand Down
2 changes: 1 addition & 1 deletion crates/flipperzero/examples/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

let file_browser_options = DialogFileBrowserOptions::new(c"*").set_hide_ext(false);
let file_browser_options = DialogFileBrowserOptions::new().set_hide_ext(false);
let mut start_path = FuriString::from(path);
let result_path =
dialogs_app.show_file_browser(Some(&mut start_path), Some(&file_browser_options));
Expand Down
40 changes: 34 additions & 6 deletions crates/flipperzero/src/dialogs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,28 @@ impl DialogMessageButton {
}
}

impl<'a> Default for DialogFileBrowserOptions<'a> {
fn default() -> Self {
Self::new()
}
}

impl<'a> DialogFileBrowserOptions<'a> {
pub fn new() -> Self {
// SAFETY: the string is a valid UTF-8
unsafe { Self::with_extension(c"*") }
}

/// Creates a new dialog file browser options and initializes to default values.
///
/// # Safety
///
/// `extension` should be a valid UTF-8 string
///
/// # Compatibility
///
/// This function's signature may change in the future to make it safe.
///
/// # Examples
///
/// Basic usage:
Expand All @@ -221,14 +240,14 @@ impl<'a> DialogFileBrowserOptions<'a> {
/// ```
/// # use core::ffi::CStr;
/// # use flipperzero::dialogs::DialogFileBrowserOptions;
/// # use flipperzero_sys::{cstr, DialogsFileBrowserOptions};
/// // has `'static` lifetime
/// const EXTENSION: &CStr = cstr!("*");
/// const EXTENSION: &CStr = c"txt";
/// // has "local" lifetime, aka `'a`
/// let base_path_bytes = [b'/', b'r', b'o', b'o', b't'];
/// let base_path = CStr::from_bytes_with_nul(&base_path_bytes).unwrap();
/// // the most appropriate lifetime `'a` is used
/// let mut options = DialogFileBrowserOptions::new(EXTENSION)
/// // SAFETY: `EXTENSION` is a valid UTF-8 string
/// let mut options = unsafe { DialogFileBrowserOptions::new(EXTENSION) }
/// .set_base_path(base_path);
/// ```
///
Expand All @@ -239,14 +258,15 @@ impl<'a> DialogFileBrowserOptions<'a> {
/// # use flipperzero::dialogs::DialogFileBrowserOptions;
/// # use flipperzero_sys::{cstr, DialogsFileBrowserOptions};
/// const EXTENSION: &CStr = cstr!("*");
/// let mut options = DialogFileBrowserOptions::new(EXTENSION);
/// // SAFETY: `EXTENSION` is a valid UTF-8 string
/// let mut options = unsafe { DialogFileBrowserOptions::new(EXTENSION) };
/// {
/// let base_path_bytes = [b'/', b'r', b'o', b'o', b't'];
/// let base_path = CStr::from_bytes_with_nul(&base_path_bytes).unwrap();
/// options = options.set_base_path(base_path);
/// }
/// ```
pub fn new(extension: &'a CStr) -> Self {
pub unsafe fn with_extension(extension: &'a CStr) -> Self {
let mut options = MaybeUninit::<sys::DialogsFileBrowserOptions>::uninit();
let uninit_options = options.as_mut_ptr();
let extension = extension.as_ptr();
Expand All @@ -267,7 +287,15 @@ impl<'a> DialogFileBrowserOptions<'a> {
}

/// Set file extension to be offered for selection.
pub fn set_extension(mut self, extension: &'a CStr) -> Self {
///
/// # Safety
///
/// `extension` should be a valid UTF-8 string
///
/// # Compatibility
///
/// This function's signature may change in the future to make it safe.
pub unsafe fn set_extension(mut self, extension: &'a CStr) -> Self {
self.data.extension = extension.as_ptr();
self
}
Expand Down
49 changes: 17 additions & 32 deletions crates/flipperzero/src/furi/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,24 @@ pub use metadata::{Level, LevelFilter};
/// ```
#[macro_export]
macro_rules! log {
(target: $target:literal, $lvl:expr, $msg:literal $(, $arg:expr)*) => ({
$crate::log!(@unsafe {
target: $crate::__macro_support::__sys::c_string!($target),
$lvl, $msg $(, $arg)*
})
});

($lvl:expr, $msg:literal $(, $arg:expr)*) => (
$crate::log!(@unsafe {
target: concat!(module_path!(), "\0").as_ptr() as *const core::ffi::c_char,
$lvl, $msg $(, $arg)*
})
);

(@unsafe {
target: $target_cstr_ptr:expr,
$lvl:expr, $msg:literal $(, $arg:expr)*
}) => ({
(target: $target:expr, $lvl:expr, $msg:expr $(, $arg:expr)*) => ({
if $lvl <= $crate::furi::log::LevelFilter::current() {
let mut buf = $crate::__macro_support::FuriString::new();
$crate::__macro_support::ufmt::uwrite!(&mut buf, $msg $(, $arg)*)
.expect("can append to FuriString");
// SAFETY: `$target_cstr_ptr` should be a valid pointer
// and `buf` has just been created safely
unsafe {
$crate::__macro_support::__sys::furi_log_print_format(
$crate::__macro_support::__level_to_furi($lvl),
$target_cstr_ptr,
$crate::__macro_support::__sys::c_string!($target),
buf.as_c_str().as_ptr(),
);
}
}
})
});

($lvl:expr, $msg:expr $(, $arg:expr)*) => (
$crate::log!(target: module_path!(), $lvl, $msg $(, $arg)*)
);
}

/// Logs a message at the error level.
Expand All @@ -74,11 +59,11 @@ macro_rules! log {
/// ```
#[macro_export]
macro_rules! error {
(target: $target:expr, $msg:literal $(, $arg:expr)*) => (
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
$crate::log!(target: $target, $crate::furi::log::Level::ERROR, $msg $(, $arg)*)
);

($msg:literal $(, $arg:expr)*) => (
($msg:expr $(, $arg:expr)*) => (
$crate::log!($crate::furi::log::Level::ERROR, $msg $(, $arg)*)
);
}
Expand All @@ -99,11 +84,11 @@ macro_rules! error {
/// ```
#[macro_export]
macro_rules! warn {
(target: $target:expr, $msg:literal $(, $arg:expr)*) => (
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
$crate::log!(target: $target, $crate::furi::log::Level::WARN, $msg $(, $arg)*)
);

($msg:literal $(, $arg:expr)*) => (
($msg:expr $(, $arg:expr)*) => (
$crate::log!($crate::furi::log::Level::WARN, $msg $(, $arg)*)
);
}
Expand All @@ -124,11 +109,11 @@ macro_rules! warn {
/// ```
#[macro_export]
macro_rules! info {
(target: $target:expr, $msg:literal $(, $arg:expr)*) => (
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
$crate::log!(target: $target, $crate::furi::log::Level::INFO, $msg $(, $arg)*)
);

($msg:literal $(, $arg:expr)*) => (
($msg:expr $(, $arg:expr)*) => (
$crate::log!($crate::furi::log::Level::INFO, $msg $(, $arg)*)
);
}
Expand All @@ -149,11 +134,11 @@ macro_rules! info {
/// ```
#[macro_export]
macro_rules! debug {
(target: $target:expr, $msg:literal $(, $arg:expr)*) => (
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
$crate::log!(target: $target, $crate::furi::log::Level::DEBUG, $msg $(, $arg)*)
);

($msg:literal $(, $arg:expr)*) => (
($msg:expr $(, $arg:expr)*) => (
$crate::log!($crate::furi::log::Level::DEBUG, $msg $(, $arg)*)
);
}
Expand All @@ -174,11 +159,11 @@ macro_rules! debug {
/// ```
#[macro_export]
macro_rules! trace {
(target: $target:expr, $msg:literal $(, $arg:expr)*) => (
(target: $target:expr, $msg:expr $(, $arg:expr)*) => (
$crate::log!(target: $target, $crate::furi::log::Level::TRACE, $msg $(, $arg)*)
);

($msg:literal $(, $arg:expr)*) => (
($msg:expr $(, $arg:expr)*) => (
$crate::log!($crate::furi::log::Level::TRACE, $msg $(, $arg)*)
);
}
1 change: 0 additions & 1 deletion crates/sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ bench = false
test = false

[dependencies]
cstr.workspace = true
ufmt.workspace = true
12 changes: 3 additions & 9 deletions crates/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,12 @@ mod inlines;
)]
mod bindings;

/// Create a static C string of type [`&'static CStr`][`core::ffi::CStr`].
/// Create a static C string.
/// Will automatically add a NUL terminator.
pub use cstr::cstr;

/// Create a static C string of type [`*const c_char`][core::ffi::c_char]
/// referring to a `'static` string.
/// 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 $(,)?) => {{
$crate::cstr!($str).as_ptr()
concat!($str, "\0").as_ptr() as *const core::ffi::c_char
}};
}

Expand All @@ -53,7 +47,7 @@ macro_rules! crash {
let msg = $crate::c_string!($msg);
core::arch::asm!("", in("r12") msg, options(nomem, nostack));

$crate::__furi_crash();
$crate::__furi_crash_implementation();
core::hint::unreachable_unchecked();
}
};
Expand Down

0 comments on commit 6804d1e

Please sign in to comment.