From 48ab7e09458634572f72302ef3ec58062f119759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Sat, 10 Apr 2021 19:26:55 +0200 Subject: [PATCH] [rust] fix #1509 - fix bugs in macros: error, error_ref & error_ref_mut - remove issue-1509 example - minor improvements - rustfmt --- rust/examples/issue-1509.rs | 31 ----------------- rust/src/cells/reimplemented.rs | 2 +- rust/src/macros.rs | 59 ++++++++++++++++++--------------- rust/src/plane/methods.rs | 5 ++- rust/src/plane/mod.rs | 2 +- rust/src/plane/reimplemented.rs | 2 +- 6 files changed, 38 insertions(+), 63 deletions(-) delete mode 100644 rust/examples/issue-1509.rs diff --git a/rust/examples/issue-1509.rs b/rust/examples/issue-1509.rs deleted file mode 100644 index 434e55e8d..000000000 --- a/rust/examples/issue-1509.rs +++ /dev/null @@ -1,31 +0,0 @@ -//! https://github.com/dankamongmen/notcurses/issues/1509 -//! strange color behaviour when moving planes -// -// TODO -// - -// - -use std::collections::BTreeMap; - -use libnotcurses_sys::*; - -fn main() -> NcResult<()> { - let mut nc = FullMode::new()?; - - // get the stdplane and color it green - let green = nc.stdplane(); - let mut channels = NcChannelPair::with_rgb8(0xFF, 0, 0, 0, 0x88, 0); - green.set_base("-", 0, channels)?; - - // create one 1x1 blue plane at 1,1 - let blue = NcPlane::new_bound(green, 1, 1, 1, 1)?; - blue.set_base("B", 0, channels.set_bg_rgb8(0, 0, 0x88))?; - rsleep![&mut nc, 1]; - - // move it to 4,4 - // BUG: here it shows something is wrong - blue.move_yx(4, 4)?; - rsleep![&mut nc, 1]; - - Ok(()) -} diff --git a/rust/src/cells/reimplemented.rs b/rust/src/cells/reimplemented.rs index afb653028..12614d676 100644 --- a/rust/src/cells/reimplemented.rs +++ b/rust/src/cells/reimplemented.rs @@ -3,7 +3,7 @@ use libc::strcmp; use crate::{ - nccell_release, cstring, NcAlphaBits, NcCell, NcChannel, NcChannelPair, NcColor, NcEgc, + cstring, nccell_release, NcAlphaBits, NcCell, NcChannel, NcChannelPair, NcColor, NcEgc, NcIntResult, NcPaletteIndex, NcPlane, NcRgb, NcStyleMask, NCCELL_ALPHA_OPAQUE, NCCELL_BGDEFAULT_MASK, NCCELL_BG_PALETTE, NCCELL_FGDEFAULT_MASK, NCCELL_FG_PALETTE, NCRESULT_ERR, NCRESULT_OK, NCSTYLE_MASK, diff --git a/rust/src/macros.rs b/rust/src/macros.rs index 8663da5a6..99a8c8c0f 100644 --- a/rust/src/macros.rs +++ b/rust/src/macros.rs @@ -120,13 +120,14 @@ macro_rules! printf { /// type `()`, and an empty `&str` `""`, respectively. #[macro_export] macro_rules! error { - ($res:expr, $msg:expr, $ok:expr) => { - if $res >= crate::NCRESULT_OK { + ($res:expr, $msg:expr, $ok:expr) => {{ + let res = $res; + if res >= crate::NCRESULT_OK { return Ok($ok); } else { - return Err(crate::NcError::with_msg($res, $msg)); + return Err(crate::NcError::with_msg(res, $msg)); } - }; + }}; ($res:expr, $msg:expr) => { error![$res, $msg, ()]; }; @@ -145,20 +146,23 @@ macro_rules! error { /// `$msg` is optional. By default it will be an empty `&str` `""`. #[macro_export] macro_rules! error_ref { - ($ptr:expr, $msg:expr, $ok:expr) => { - if $ptr != core::ptr::null() { + ($ptr:expr, $msg:expr, $ok:expr) => {{ + let ptr = $ptr; // avoid calling a function multiple times + if ptr.is_null() { + return Err(crate::NcError::with_msg(crate::NCRESULT_ERR, $msg)); + } else { #[allow(unused_unsafe)] return Ok(unsafe { $ok }); - } else { - return Err(crate::NcError::with_msg(crate::NCRESULT_ERR, $msg)); } - }; - ($ptr:expr, $msg:expr) => { - error_ref![$ptr, $msg, &*$ptr]; - }; - ($ptr:expr) => { - error_ref![$ptr, "", &*$ptr]; - }; + }}; + ($ptr:expr, $msg:expr) => {{ + let ptr = $ptr; + error_ref![$ptr, $msg, unsafe { &*ptr }]; + }}; + ($ptr:expr) => {{ + let ptr = $ptr; + error_ref![$ptr, "", unsafe { &*ptr }]; + }}; } /// Returns an `Ok(&mut T)` from a `*mut T` pointer, @@ -171,20 +175,23 @@ macro_rules! error_ref { /// `$msg` is optional. By default it will be an empty `&str` `""`. #[macro_export] macro_rules! error_ref_mut { - ($ptr:expr, $msg:expr, $ok:expr) => { - if $ptr != core::ptr::null_mut() { + ($ptr:expr, $msg:expr, $ok:expr) => {{ + let ptr = $ptr; // avoid calling a function multiple times + if ptr.is_null() { + return Err(crate::NcError::with_msg(crate::NCRESULT_ERR, $msg)); + } else { #[allow(unused_unsafe)] return Ok(unsafe { $ok }); - } else { - return Err(crate::NcError::with_msg(crate::NCRESULT_ERR, $msg)); } - }; - ($ptr:expr, $msg:expr) => { - error_ref_mut![$ptr, $msg, &mut *$ptr]; - }; - ($ptr:expr) => { - error_ref_mut![$ptr, "", &mut *$ptr]; - }; + }}; + ($ptr:expr, $msg:expr) => {{ + let ptr = $ptr; + error_ref_mut![ptr, $msg, unsafe { &mut *ptr }]; + }}; + ($ptr:expr) => {{ + let ptr = $ptr; + error_ref_mut![ptr, "", unsafe { &mut *ptr }]; + }}; } /// Returns an `Ok(String)` from a `*const` pointer to a C string, diff --git a/rust/src/plane/methods.rs b/rust/src/plane/methods.rs index 7b7f1b7b2..0d8d80f72 100644 --- a/rust/src/plane/methods.rs +++ b/rust/src/plane/methods.rs @@ -1,5 +1,4 @@ //! `NcPlane*` methods and associated functions. - use core::{ ptr::{null, null_mut}, slice::from_raw_parts_mut, @@ -105,7 +104,7 @@ impl NcPlane { ) -> NcResult<&'a mut NcPlane> { error_ref_mut![ unsafe { crate::ncpile_create(nc, &options) }, - &format!["NcPlane::with_options(Notcurses, {:?})", options] + &format!["NcPlane::with_options(Notcurses, {:?})", &options] ] } @@ -131,7 +130,7 @@ impl NcPlane { ) -> NcResult<&'a mut NcPlane> { error_ref_mut![ unsafe { crate::ncplane_create(bound_to, &options) }, - &format!("NcPlane::with_options_bound(NcPlane, {:?})", options) + &format!("NcPlane::with_options_bound(NcPlane, {:?})", &options) ] } diff --git a/rust/src/plane/mod.rs b/rust/src/plane/mod.rs index 272ecb74b..c045da88a 100644 --- a/rust/src/plane/mod.rs +++ b/rust/src/plane/mod.rs @@ -327,7 +327,7 @@ pub const NCBLIT_DEFAULT: NcBlitter = crate::bindings::ffi::ncblitter_e_NCBLIT_D /// See [Sixel in Wikipedia](https://en.wikipedia.org/wiki/Sixel). pub const NCBLIT_PIXEL: NcBlitter = crate::bindings::ffi::ncblitter_e_NCBLIT_PIXEL; -/// Contains the pixel geometry information as returned by the +/// Contains the pixel geometry information as returned by the /// NcPlane.[pixelgeom()][NcPlane#method.pixelgeom] method. /// /// If bitmaps are not supported, the fields `max_bitmap_*` will be 0. diff --git a/rust/src/plane/reimplemented.rs b/rust/src/plane/reimplemented.rs index d08a9e5e4..c3d5f1ec8 100644 --- a/rust/src/plane/reimplemented.rs +++ b/rust/src/plane/reimplemented.rs @@ -4,7 +4,7 @@ use core::ptr::null_mut; use crate::ffi::__va_list_tag; use crate::{ - nccell_release, cstring, ncplane_channels, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel, + cstring, nccell_release, ncplane_channels, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel, NcChannelPair, NcColor, NcDim, NcEgc, NcIntResult, NcPlane, NcRgb, NcStyleMask, NCRESULT_ERR, NCRESULT_OK, };