diff --git a/rust/src/macros.rs b/rust/src/macros.rs index c42ecbde0..de04f1082 100644 --- a/rust/src/macros.rs +++ b/rust/src/macros.rs @@ -4,9 +4,7 @@ #[allow(unused_imports)] // enjoy briefer doc comments -use crate::{ - notcurses_render, NcError, NcResult, Notcurses, NCRESULT_ERR, NCRESULT_OK, NcDirect, -}; +use crate::{notcurses_render, NcDirect, NcError, NcResult, Notcurses, NCRESULT_ERR, NCRESULT_OK}; // Sleep, Render & Flush Macros ------------------------------------------------ @@ -79,6 +77,16 @@ macro_rules! cstring { }; } +/// Converts a `*const c_char` into an `&str`. +#[macro_export] +macro_rules! rstring { + ($s:expr) => { + unsafe { std::ffi::CStr::from_ptr($s).to_str().unwrap() } + // possible alternative + // unsafe { std::ffi::CStr::from_ptr($s).to_string_lossy() } + }; +} + /// Wrapper around [libc::printf]. #[macro_export] macro_rules! printf { diff --git a/rust/src/notcurses/methods.rs b/rust/src/notcurses/methods.rs index 1d282cb7e..074b2f417 100644 --- a/rust/src/notcurses/methods.rs +++ b/rust/src/notcurses/methods.rs @@ -1,13 +1,12 @@ //! `Notcurses*` methods and associated functions. use core::ptr::{null, null_mut}; -use std::ffi::CStr; use crate::{ - cstring, error, notcurses_init, NcAlign, NcBlitter, NcChannelPair, NcDimension, NcEgc, NcError, - NcFile, NcInput, NcLogLevel, NcPlane, NcResult, NcScale, NcSignalSet, NcStats, NcStyleMask, - NcTime, Notcurses, NotcursesOptions, NCOPTION_NO_ALTERNATE_SCREEN, NCOPTION_SUPPRESS_BANNERS, - NCRESULT_ERR, + cstring, error, notcurses_init, rstring, NcAlign, NcBlitter, NcChannelPair, NcDimension, NcEgc, + NcError, NcFile, NcInput, NcLogLevel, NcPlane, NcResult, NcScale, NcSignalSet, NcStats, + NcStyleMask, NcTime, Notcurses, NotcursesOptions, NCOPTION_NO_ALTERNATE_SCREEN, + NCOPTION_SUPPRESS_BANNERS, NCRESULT_ERR, }; /// # `NotcursesOptions` Constructors @@ -554,22 +553,14 @@ impl Notcurses { /// /// *C style function: [notcurses_str_blitter()][crate::notcurses_str_blitter].* pub fn str_blitter(blitter: NcBlitter) -> String { - unsafe { - CStr::from_ptr(crate::notcurses_str_blitter(blitter)) - .to_string_lossy() - .into_owned() - } + rstring![crate::notcurses_str_blitter(blitter)].to_string() } /// Gets the name of an [NcScale] scaling mode. /// /// *C style function: [notcurses_str_scalemode()][crate::notcurses_str_scalemode].* pub fn str_scalemode(scalemode: NcScale) -> String { - unsafe { - CStr::from_ptr(crate::notcurses_str_scalemode(scalemode)) - .to_string_lossy() - .into_owned() - } + rstring![crate::notcurses_str_scalemode(scalemode)].to_string() } /// Returns an [NcStyleMask] with the supported curses-style attributes. @@ -602,11 +593,7 @@ impl Notcurses { /// /// *C style function: [notcurses_version()][crate::notcurses_version].* pub fn version() -> String { - unsafe { - CStr::from_ptr(crate::notcurses_version()) - .to_string_lossy() - .into_owned() - } + rstring![crate::notcurses_version()].to_string() } /// Returns the running Notcurses version components diff --git a/rust/src/notcurses/mod.rs b/rust/src/notcurses/mod.rs index 5dff1d03a..5dba5ee52 100644 --- a/rust/src/notcurses/mod.rs +++ b/rust/src/notcurses/mod.rs @@ -44,7 +44,7 @@ //W notcurses_supported_styles //W notcurses_top //~ notcurses_ucs32_to_utf8 (not needed in rust) -//W notcurses_version +//W# notcurses_version //W notcurses_version_components // // functions manually reimplemented: 6 diff --git a/rust/src/notcurses/test/reimplemented.rs b/rust/src/notcurses/test/reimplemented.rs index f0e7b2433..d816091dc 100644 --- a/rust/src/notcurses/test/reimplemented.rs +++ b/rust/src/notcurses/test/reimplemented.rs @@ -161,3 +161,12 @@ fn notcurses_debug() { assert_eq![&string1[0..string2.len()], &string2[..]]; } } + +#[test] +#[serial] +// TODO test version_components +fn notcurses_version() { + let c_str = unsafe { crate::notcurses_version() }; + assert!(!c_str.is_null()); + print!("v{} ", crate::rstring![c_str]); +} diff --git a/rust/src/plane/methods.rs b/rust/src/plane/methods.rs index c6e9cfec2..9db9dc91c 100644 --- a/rust/src/plane/methods.rs +++ b/rust/src/plane/methods.rs @@ -1,10 +1,9 @@ //! `NcPlane*` methods and associated functions. use core::ptr::{null, null_mut}; -use std::ffi::CStr; use crate::{ - cstring, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel, NcChannelPair, NcColor, + cstring, rstring, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel, NcChannelPair, NcColor, NcDimension, NcEgc, NcFadeCb, NcIntResult, NcOffset, NcPaletteIndex, NcPlane, NcPlaneOptions, NcResizeCb, NcRgb, NcStyleMask, NcTime, Notcurses, NCRESULT_OK, }; @@ -607,17 +606,14 @@ impl NcPlane { if through_x { len_x = -1; } - unsafe { - CStr::from_ptr(crate::ncplane_contents( - self, - beg_y as i32, - beg_x as i32, - len_y, - len_x, - )) - .to_string_lossy() - .into_owned() - } + rstring![crate::ncplane_contents( + self, + beg_y as i32, + beg_x as i32, + len_y, + len_x + )] + .to_string() } /// Erases every NcCell in this NcPlane, resetting all attributes to normal, diff --git a/rust/tests/notcurses.rs b/rust/tests/notcurses.rs deleted file mode 100644 index 740889a43..000000000 --- a/rust/tests/notcurses.rs +++ /dev/null @@ -1,48 +0,0 @@ -use core::ptr::{null, null_mut}; -use std::ffi::{CStr, CString}; - -use serial_test::serial; // serialize tests w/ sys::notcurses_init() - -use libnotcurses_sys as sys; - -#[test] -#[serial] -fn get_notcurses_version() { - let c_str = unsafe { - let s = sys::notcurses_version(); - assert!(!s.is_null()); - CStr::from_ptr(s) - }; - let r_str = c_str.to_str().unwrap(); - print!("rust-bound notcurses v{} ", r_str); -} - -#[test] -#[serial] -fn create_notcurses_context() { - unsafe { - let _ = libc::setlocale(libc::LC_ALL, CString::new("").unwrap().as_ptr()); - let opts = sys::NotcursesOptions { - loglevel: 0, - termtype: null(), - renderfp: null_mut(), - margin_t: 0, - margin_r: 0, - margin_b: 0, - margin_l: 0, - flags: (sys::NCOPTION_NO_ALTERNATE_SCREEN | sys::NCOPTION_INHIBIT_SETLOCALE | sys::NCOPTION_SUPPRESS_BANNERS), - }; - let nc = sys::notcurses_init(&opts, null_mut()); - sys::notcurses_stop(nc); - } -} - -#[test] -#[serial] -fn create_direct_context() { - unsafe { - let _ = libc::setlocale(libc::LC_ALL, CString::new("").unwrap().as_ptr()); - let nc = sys::ncdirect_init(null_mut(), null_mut(), 0); - sys::ncdirect_stop(nc); - } -}