rust: update more NcPlane methods' return type to NcResult.

- rustfmt, comments…
pull/1259/head
joseLuís 4 years ago
parent b6fcd2783d
commit 5d90e7de15

@ -384,9 +384,9 @@ pub use ffi::{
ncplane_reparent, ncplane_reparent,
ncplane_reparent_family, ncplane_reparent_family,
ncplane_resize, ncplane_resize,
ncplane_resizecb,
ncplane_resize_maximize, ncplane_resize_maximize,
ncplane_resize_realign, ncplane_resize_realign,
ncplane_resizecb,
ncplane_rgba, ncplane_rgba,
ncplane_rotate_ccw, ncplane_rotate_ccw,
ncplane_rotate_cw, ncplane_rotate_cw,
@ -676,8 +676,8 @@ pub use ffi::{
notcurses_canfade, notcurses_canfade,
notcurses_canopen_images, notcurses_canopen_images,
notcurses_canopen_videos, notcurses_canopen_videos,
notcurses_cansixel,
notcurses_cansextant, notcurses_cansextant,
notcurses_cansixel,
notcurses_cantruecolor, notcurses_cantruecolor,
notcurses_canutf8, notcurses_canutf8,
notcurses_cursor_disable, notcurses_cursor_disable,

@ -3,9 +3,10 @@
use core::ptr::{null, null_mut}; use core::ptr::{null, null_mut};
use crate::{ use crate::{
cstring, error, error_ref_mut, rstring, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel, cstring, error, error_ref, error_ref_mut, rstring, NcAlign, NcAlphaBits, NcBoxMask, NcCell,
NcChannelPair, NcColor, NcDimension, NcEgc, NcFadeCb, NcOffset, NcPaletteIndex, NcPlane, NcChannel, NcChannelPair, NcColor, NcDimension, NcEgc, NcFadeCb, NcOffset, NcPaletteIndex,
NcPlaneOptions, NcResizeCb, NcResult, NcRgb, NcStyleMask, NcTime, Notcurses, NcPlane, NcPlaneOptions, NcResizeCb, NcResult, NcRgb, NcStyleMask, NcTime, Notcurses,
NCRESULT_ERR,
}; };
/// # NcPlaneOptions Constructors /// # NcPlaneOptions Constructors
@ -232,7 +233,7 @@ impl NcPlane {
/// Sets the given [NcChannelPair]s throughout the specified region, /// Sets the given [NcChannelPair]s throughout the specified region,
/// keeping content and attributes unchanged. /// keeping content and attributes unchanged.
/// ///
/// Returns the number of cells set, or [NCRESULT_ERR][crate::NCRESULT_ERR] /// Returns the number of cells set, or [NCRESULT_ERR]
/// on failure. /// on failure.
/// ///
/// *C style function: [ncplane_stain()][crate::ncplane_stain].* /// *C style function: [ncplane_stain()][crate::ncplane_stain].*
@ -397,7 +398,7 @@ impl NcPlane {
/// Sets the given style throughout the specified region, keeping content /// Sets the given style throughout the specified region, keeping content
/// and channels unchanged. /// and channels unchanged.
/// ///
/// Returns the number of cells set, or [NCRESULT_ERR][crate::NCRESULT_ERR] /// Returns the number of cells set, or [NCRESULT_ERR]
/// on failure. /// on failure.
/// ///
/// *C style function: [ncplane_format()][crate::ncplane_format].* /// *C style function: [ncplane_format()][crate::ncplane_format].*
@ -484,13 +485,13 @@ impl NcPlane {
&mut self, &mut self,
stylemask: &mut NcStyleMask, stylemask: &mut NcStyleMask,
channels: &mut NcChannelPair, channels: &mut NcChannelPair,
) -> Option<NcEgc> { ) -> NcResult<NcEgc> {
let egc = unsafe { crate::ncplane_at_cursor(self, stylemask, channels) }; let egc = unsafe { crate::ncplane_at_cursor(self, stylemask, channels) };
if egc.is_null() { if egc.is_null() {
return None; return Err(crate::NcError::new(NCRESULT_ERR));
} }
let egc = core::char::from_u32(unsafe { *egc } as u32).expect("wrong char"); let egc = core::char::from_u32(unsafe { *egc } as u32).expect("wrong char");
Some(egc) Ok(egc)
} }
/// Retrieves the current contents of the [NcCell] under the cursor /// Retrieves the current contents of the [NcCell] under the cursor
@ -517,13 +518,13 @@ impl NcPlane {
x: NcDimension, x: NcDimension,
stylemask: &mut NcStyleMask, stylemask: &mut NcStyleMask,
channels: &mut NcChannelPair, channels: &mut NcChannelPair,
) -> Option<NcEgc> { ) -> NcResult<NcEgc> {
let egc = unsafe { crate::ncplane_at_yx(self, y as i32, x as i32, stylemask, channels) }; let egc = unsafe { crate::ncplane_at_yx(self, y as i32, x as i32, stylemask, channels) };
if egc.is_null() { if egc.is_null() {
return None; return Err(crate::NcError::new(NCRESULT_ERR));
} }
let egc = core::char::from_u32(unsafe { *egc } as u32).expect("wrong char"); let egc = core::char::from_u32(unsafe { *egc } as u32).expect("wrong char");
Some(egc) Ok(egc)
} }
/// Retrieves the current contents of the specified [NcCell] into `cell`. /// Retrieves the current contents of the specified [NcCell] into `cell`.
@ -836,28 +837,20 @@ impl NcPlane {
/// Returns the NcPlane above this one, or None if already at the top. /// Returns the NcPlane above this one, or None if already at the top.
/// ///
/// *C style function: [ncplane_above()][crate::ncplane_above].* /// *C style function: [ncplane_above()][crate::ncplane_above].*
pub fn above<'a>(&'a mut self) -> Option<&'a mut NcPlane> { pub fn above<'a>(&'a mut self) -> NcResult<&'a mut NcPlane> {
let plane = unsafe { crate::ncplane_above(self) }; error_ref_mut![unsafe { crate::ncplane_above(self) }]
if plane.is_null() {
return None;
}
Some(unsafe { &mut *plane })
} }
/// Returns the NcPlane below this one, or None if already at the bottom. /// Returns the NcPlane below this one, or None if already at the bottom.
/// ///
/// *C style function: [ncplane_below()][crate::ncplane_below].* /// *C style function: [ncplane_below()][crate::ncplane_below].*
pub fn below<'a>(&'a mut self) -> Option<&'a mut NcPlane> { pub fn below<'a>(&'a mut self) -> NcResult<&'a mut NcPlane> {
let plane = unsafe { crate::ncplane_below(self) }; error_ref_mut![unsafe { crate::ncplane_below(self) }]
if plane.is_null() {
return None;
}
Some(unsafe { &mut *plane })
} }
/// Relocates this NcPlane above the `above` NcPlane, in the z-buffer. /// Relocates this NcPlane above the `above` NcPlane, in the z-buffer.
/// ///
/// Returns [NCRESULT_ERR][crate::NCRESULT_ERR] if the current plane is /// Returns [NCRESULT_ERR] if the current plane is
/// already in the desired location. Both planes must not be the same. /// already in the desired location. Both planes must not be the same.
/// ///
/// *C style function: [ncplane_move_above()][crate::ncplane_move_above].* /// *C style function: [ncplane_move_above()][crate::ncplane_move_above].*
@ -867,7 +860,7 @@ impl NcPlane {
/// Relocates this NcPlane below the `below` NcPlane, in the z-buffer. /// Relocates this NcPlane below the `below` NcPlane, in the z-buffer.
/// ///
/// Returns [NCRESULT_ERR][crate::NCRESULT_ERR] if the current plane is /// Returns [NCRESULT_ERR] if the current plane is
/// already in the desired location. Both planes must not be the same. /// already in the desired location. Both planes must not be the same.
/// ///
/// *C style function: [ncplane_move_below()][crate::ncplane_move_below].* /// *C style function: [ncplane_move_below()][crate::ncplane_move_below].*
@ -933,9 +926,8 @@ impl NcPlane {
/// *C style function: [ncplane_parent()][crate::ncplane_parent].* /// *C style function: [ncplane_parent()][crate::ncplane_parent].*
// //
// TODO: CHECK: what happens when it's bound to itself. // TODO: CHECK: what happens when it's bound to itself.
// pub fn parent<'a>(&'a mut self) -> Option<&'a mut NcPlane> { pub fn parent<'a>(&'a mut self) -> NcResult<&'a mut NcPlane> {
pub fn parent<'a>(&'a mut self) -> &'a mut NcPlane { error_ref_mut![unsafe { crate::ncplane_parent(self) }]
unsafe { &mut *crate::ncplane_parent(self) }
} }
/// Gets the parent to which this NcPlane is bound, if any. /// Gets the parent to which this NcPlane is bound, if any.
@ -943,9 +935,8 @@ impl NcPlane {
/// *C style function: [ncplane_parent_const()][crate::ncplane_parent_const].* /// *C style function: [ncplane_parent_const()][crate::ncplane_parent_const].*
// //
// TODO: CHECK: what happens when it's bound to itself. // TODO: CHECK: what happens when it's bound to itself.
// pub fn parent<'a>(&'a mut self) -> Option<&'a mut NcPlane> { pub fn parent_const<'a>(&'a self) -> NcResult<&'a NcPlane> {
pub fn parent_const<'a>(&'a self) -> &'a NcPlane { error_ref![unsafe { crate::ncplane_parent_const(self) }]
unsafe { &*crate::ncplane_parent_const(self) }
} }
/// Unbounds this NcPlane from its parent, makes it a bound child of /// Unbounds this NcPlane from its parent, makes it a bound child of
@ -960,8 +951,8 @@ impl NcPlane {
/// The standard plane cannot be reparented. /// The standard plane cannot be reparented.
/// ///
/// *C style function: [ncplane_reparent()][crate::ncplane_reparent].* /// *C style function: [ncplane_reparent()][crate::ncplane_reparent].*
pub fn reparent<'a>(&'a mut self, newparent: &'a mut NcPlane) -> &'a mut NcPlane { pub fn reparent<'a>(&mut self, newparent: &'a mut NcPlane) -> NcResult<&'a mut NcPlane> {
unsafe { &mut *crate::ncplane_reparent(self, newparent) } error_ref_mut![unsafe { crate::ncplane_reparent(self, newparent) }]
} }
/// Like [`reparent`][NcPlane#method.reparent], except any bound /// Like [`reparent`][NcPlane#method.reparent], except any bound
@ -972,8 +963,8 @@ impl NcPlane {
/// *C style function: [ncplane_reparent_family()][crate::ncplane_reparent_family].* /// *C style function: [ncplane_reparent_family()][crate::ncplane_reparent_family].*
// //
// TODO:CHECK: If 'newparent' is an ancestor, NULL is returned & no changes're made. // TODO:CHECK: If 'newparent' is an ancestor, NULL is returned & no changes're made.
pub fn reparent_family<'a>(&'a mut self, newparent: &'a mut NcPlane) -> &'a mut NcPlane { pub fn reparent_family<'a>(&mut self, newparent: &'a mut NcPlane) -> NcResult<&'a mut NcPlane> {
unsafe { &mut *crate::ncplane_reparent_family(self, newparent) } error_ref_mut![unsafe { crate::ncplane_reparent_family(self, newparent) }]
} }
/// Makes the physical screen match the last rendered frame from the pile of /// Makes the physical screen match the last rendered frame from the pile of
@ -1006,8 +997,8 @@ impl NcPlane {
/// Gets an immutable reference to the [Notcurses] context of this NcPlane. /// Gets an immutable reference to the [Notcurses] context of this NcPlane.
/// ///
/// *C style function: [ncplane_notcurses_const()][crate::ncplane_notcurses_const].* /// *C style function: [ncplane_notcurses_const()][crate::ncplane_notcurses_const].*
pub fn notcurses_const<'a>(&self) -> &'a Notcurses { pub fn notcurses_const<'a>(&self) -> NcResult<&'a Notcurses> {
unsafe { &*crate::ncplane_notcurses_const(self) } error_ref![unsafe { crate::ncplane_notcurses_const(self) }]
} }
} }
@ -1571,7 +1562,7 @@ impl NcPlane {
/// position, stopping at `y_stop` * `xstop`. /// position, stopping at `y_stop` * `xstop`.
/// ///
/// Returns the number of cells filled on success, /// Returns the number of cells filled on success,
/// or [NCRESULT_ERR][crate::NCRESULT_ERR] on error. /// or [NCRESULT_ERR] on error.
/// ///
/// The glyph composed of `egc` and `stylemask` is used for all cells. /// The glyph composed of `egc` and `stylemask` is used for all cells.
/// The channels specified by `ul`, `ur`, `ll`, and `lr` are composed into /// The channels specified by `ul`, `ur`, `ll`, and `lr` are composed into
@ -1647,7 +1638,7 @@ impl NcPlane {
/// Draws a high-resolution gradient using upper blocks and synced backgrounds. /// Draws a high-resolution gradient using upper blocks and synced backgrounds.
/// ///
/// Returns the number of cells filled on success, /// Returns the number of cells filled on success,
/// or [NCRESULT_ERR][crate::NCRESULT_ERR] on error. /// or [NCRESULT_ERR] on error.
/// ///
/// This doubles the number of vertical gradations, but restricts you to /// This doubles the number of vertical gradations, but restricts you to
/// half blocks (appearing to be full blocks). /// half blocks (appearing to be full blocks).

@ -22,6 +22,8 @@ pub fn ncresizecb_to_rust(resizecb: Option<NcResizeCbUnsafe>) -> Option<NcResize
/// Converts [NcResizeCb] to [NcResizeCbUnsafe]. /// Converts [NcResizeCb] to [NcResizeCbUnsafe].
/// ///
// waiting for https://github.com/rust-lang/rust/issues/53605
// to make this function const, and then NcPlaneOptions constructors.
pub fn ncresizecb_to_c(resizecb: Option<NcResizeCb>) -> Option<NcResizeCbUnsafe> { pub fn ncresizecb_to_c(resizecb: Option<NcResizeCb>) -> Option<NcResizeCbUnsafe> {
if let Some(cb) = resizecb { if let Some(cb) = resizecb {
return Some(unsafe { core::mem::transmute(cb) }); return Some(unsafe { core::mem::transmute(cb) });

Loading…
Cancel
Save