rust: Don't derive the Copy trait

- implement the Drop trait for the all the obvious types.
- resolve lifetime issues revealed from not being Copy.
  - return
pull/1181/head
joseLuís 4 years ago
parent 3893f085e3
commit 2483771af1

@ -33,6 +33,19 @@ fn main() {
// Remove warnings about improper_ctypes
.blacklist_function("strtold")
.blacklist_function("wcstold")
// Don't derive the Copy trait on types with destructors.
.no_copy("ncdirect")
.no_copy("ncdplot")
.no_copy("ncfdplane")
.no_copy("ncmenu")
.no_copy("ncmultiselector")
.no_copy("ncplane")
.no_copy("ncreader")
.no_copy("ncreel")
.no_copy("ncselector")
.no_copy("ncuplot")
.no_copy("ncvisual")
.no_copy("notcurses")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks));

@ -28,14 +28,10 @@ impl NcDirect {
}
}
// NOTE: Explicitly implementing both `Drop` and `Copy` trait on a type is
// currently disallowed (rustc --explain E0184)
// See: https://github.com/rust-lang/rust/issues/20126
//
// impl Drop for NcDirect {
// fn drop(&mut self) {
// unsafe {
// ncdirect_stop(self);
// }
// }
// }
impl Drop for NcDirect {
fn drop(&mut self) {
unsafe {
crate::ncdirect_stop(self);
}
}
}

@ -3,12 +3,8 @@
use core::ptr::{null, null_mut};
use crate::{
notcurses_init, NcLogLevel, Notcurses, NotcursesOptions, NCOPTION_NO_ALTERNATE_SCREEN,
NCOPTION_SUPPRESS_BANNERS,
};
use crate::{
notcurses_render, notcurses_stdplane, notcurses_stdplane_const, notcurses_stop, NcPlane,
NcResult,
notcurses_init, NcLogLevel, NcPlane, NcResult, Notcurses, NotcursesOptions,
NCOPTION_NO_ALTERNATE_SCREEN, NCOPTION_SUPPRESS_BANNERS,
};
/// # `NotcursesOptions` Constructors
@ -82,31 +78,31 @@ impl NotcursesOptions {
/// # `Notcurses` Constructors
impl Notcurses {
/// Simple constructor with clean output
/// Returns a Notcurses context (without banners).
pub fn new<'a>() -> &'a mut Notcurses {
let options = NotcursesOptions::with_flags(NCOPTION_SUPPRESS_BANNERS);
unsafe { &mut *notcurses_init(&options, null_mut()) }
}
/// Simple constructor, showing banners
/// Returns a Notcurses context, with banners.
pub fn with_banners<'a>() -> &'a mut Notcurses {
unsafe { &mut *notcurses_init(&NotcursesOptions::new(), null_mut()) }
}
/// Simple constructor without an alternate screen
/// Returns a Notcurses context, without an alternate screen (nor banners).
pub fn without_altscreen<'a>() -> &'a mut Notcurses {
let options = NotcursesOptions::with_flags(NCOPTION_NO_ALTERNATE_SCREEN);
let options =
NotcursesOptions::with_flags(NCOPTION_NO_ALTERNATE_SCREEN | NCOPTION_SUPPRESS_BANNERS);
unsafe { &mut *notcurses_init(&options, null_mut()) }
}
/// Simple constructor without an alternate screen
/// Returns a Notcurses context, without an alternate screen, with banners.
pub fn without_altscreen_nor_banners<'a>() -> &'a mut Notcurses {
let options =
NotcursesOptions::with_flags(NCOPTION_NO_ALTERNATE_SCREEN | NCOPTION_SUPPRESS_BANNERS);
let options = NotcursesOptions::with_flags(NCOPTION_NO_ALTERNATE_SCREEN);
unsafe { &mut *notcurses_init(&options, null_mut()) }
}
/// Constructor with all the options
/// Returns a Notcurses context, expects [NotcursesOptions].
pub fn with_options<'a>(options: &NotcursesOptions) -> &'a mut Notcurses {
unsafe { &mut *notcurses_init(options, null_mut()) }
}
@ -123,25 +119,38 @@ impl Notcurses {
///
/// The standard plane always exists, and its origin is always at the
/// uppermost, leftmost cell.
pub fn stdplane_mut<'a>(&mut self) -> &'a mut NcPlane {
unsafe { &mut *notcurses_stdplane(self) }
pub fn stdplane<'a>(&mut self) -> &'a mut NcPlane {
unsafe { &mut *crate::notcurses_stdplane(self) }
}
/// Returns a reference to the standard [NcPlane] for this terminal.
///
/// The standard plane always exists, and its origin is always at the
/// uppermost, leftmost cell.
pub fn stdplane<'a>(&self) -> &'a NcPlane {
unsafe { &*notcurses_stdplane_const(self) }
pub fn stdplane_const<'a>(&self) -> &'a NcPlane {
unsafe { &*crate::notcurses_stdplane_const(self) }
}
///
/// Destroy the Notcurses context.
pub fn stop(&mut self) -> NcResult {
unsafe { notcurses_stop(self) }
unsafe { crate::notcurses_stop(self) }
}
///
pub fn render(&mut self) -> NcResult {
unsafe { notcurses_render(self) }
unsafe { crate::notcurses_render(self) }
}
}
// Common Traits ---------------------------------------------------------------
impl Drop for Notcurses {
fn drop(&mut self) {
unsafe {
crate::notcurses_stop(self);
}
}
}
// impl fmt::Display for Notcurses {
// }

@ -62,21 +62,21 @@ pub fn notcurses_getc_nblocking(nc: &mut Notcurses, input: &mut NcInput) -> char
/// notcurses_stdplane(), plus free bonus dimensions written to non-NULL y/x!
#[inline]
pub fn notcurses_stddim_yx(nc: &mut Notcurses, y: &mut i32, x: &mut i32) -> NcPlane {
pub fn notcurses_stddim_yx<'a>(nc: &mut Notcurses, y: &mut i32, x: &mut i32) -> &'a mut NcPlane {
unsafe {
let s = notcurses_stdplane(nc);
ncplane_dim_yx(s, y, x);
*s
&mut *s
}
}
/// notcurses_stdplane_const(), plus free bonus dimensions written to non-NULL y/x!
#[inline]
pub fn notcurses_stddim_yx_const(nc: &Notcurses, y: &mut i32, x: &mut i32) -> NcPlane {
pub fn notcurses_stddim_yx_const<'a>(nc: &'a Notcurses, y: &mut i32, x: &mut i32) -> &'a NcPlane {
unsafe {
let s = notcurses_stdplane_const(nc);
ncplane_dim_yx(s, y, x);
*s
&*s
}
}

@ -2,17 +2,7 @@
use core::ptr::{null, null_mut};
// for constructors
use crate::{
ncpile_create, ncplane_create, notcurses_term_dim_yx, NcAlign, NcPlane, NcPlaneOptions,
Notcurses, NCPLANE_OPTION_HORALIGNED,
};
// for methods
use crate::{
ncpile_bottom, ncpile_top, ncplane_cursor_yx, ncplane_dim_yx, ncplane_erase, ncplane_putc,
ncplane_putc_yx, NcCell, NcResult,
};
use crate::{cstring, NcAlign, NcCell, NcPlane, NcPlaneOptions, NcResult, Notcurses};
/// # `NcPlaneOptions` Constructors
impl NcPlaneOptions {
@ -23,7 +13,13 @@ impl NcPlaneOptions {
/// New NcPlaneOptions with horizontal alignment.
pub fn new_halign(y: i32, align: NcAlign, rows: u32, cols: u32) -> Self {
Self::with_flags(y, align as i32, rows, cols, NCPLANE_OPTION_HORALIGNED)
Self::with_flags(
y,
align as i32,
rows,
cols,
crate::NCPLANE_OPTION_HORALIGNED,
)
}
/// New NcPlaneOptions, with flags.
@ -49,29 +45,20 @@ impl NcPlane {
/// New NcPlane.
///
/// The returned plane will be the top, bottom, and root of this new pile.
pub unsafe fn new<'a>(
nc: &mut Notcurses,
y: i32,
x: i32,
rows: u32,
cols: u32,
) -> &'a mut NcPlane {
pub fn new<'a>(nc: &mut Notcurses, y: i32, x: i32, rows: u32, cols: u32) -> &'a mut NcPlane {
let options = NcPlaneOptions::new(y, x, rows, cols);
&mut *ncpile_create(nc, &options)
unsafe { &mut *crate::ncpile_create(nc, &options) }
}
/// New NcPlane, expects an [NcPlaneOptions] struct.
///
/// The returned plane will be the top, bottom, and root of this new pile.
pub unsafe fn with_options<'a>(
nc: &mut Notcurses,
options: &NcPlaneOptions,
) -> &'a mut NcPlane {
&mut *ncpile_create(nc, options)
pub fn with_options<'a>(nc: &mut Notcurses, options: &NcPlaneOptions) -> &'a mut NcPlane {
unsafe { &mut *crate::ncpile_create(nc, options) }
}
/// New NcPlane, bound to another NcPlane.
pub unsafe fn new_bound<'a>(
pub fn new_bound<'a>(
bound_to: &mut NcPlane,
y: i32,
x: i32,
@ -79,36 +66,32 @@ impl NcPlane {
cols: u32,
) -> &'a mut NcPlane {
let options = NcPlaneOptions::new(y, x, rows, cols);
&mut *ncplane_create(bound_to, &options)
unsafe { &mut *crate::ncplane_create(bound_to, &options) }
}
/// New NcPlane, bound to another plane, expects an [NcPlaneOptions] struct.
///
/// The returned plane will be the top, bottom, and root of this new pile.
pub unsafe fn with_options_bound<'a>(
nc: &mut Notcurses,
options: &NcPlaneOptions,
) -> &'a mut NcPlane {
&mut *ncpile_create(nc, options)
pub fn with_options_bound<'a>(nc: &mut Notcurses, options: &NcPlaneOptions) -> &'a mut NcPlane {
unsafe { &mut *crate::ncpile_create(nc, options) }
}
/// New NcPlane, with the same dimensions of the terminal.
///
/// The returned plane will be the top, bottom, and root of this new pile.
pub unsafe fn new_termsize<'a>(nc: &mut Notcurses) -> &'a mut NcPlane {
pub fn new_termsize<'a>(nc: &mut Notcurses) -> &'a mut NcPlane {
let (mut trows, mut tcols) = (0, 0);
notcurses_term_dim_yx(nc, &mut trows, &mut tcols);
crate::notcurses_term_dim_yx(nc, &mut trows, &mut tcols);
assert![(trows > 0) & (tcols > 0)];
&mut *ncpile_create(nc, &NcPlaneOptions::new(0, 0, trows as u32, tcols as u32))
unsafe {
&mut *crate::ncpile_create(nc, &NcPlaneOptions::new(0, 0, trows as u32, tcols as u32))
}
}
}
/// # `NcPlane` Methods
impl NcPlane {
/// Returns the bottommost [NcPlane] of the pile that contains this [NnPlane].
pub fn bottom<'a>(&mut self) -> &'a mut NcPlane {
unsafe { &mut *ncpile_bottom(self) }
}
// Cursor ------------------------------------------------------------------
/// Returns the current position of the cursor within the [NcPlane].
///
@ -116,28 +99,30 @@ impl NcPlane {
//
// NOTE: y and/or x may be NULL.
// FIXME: CHECK for NULL and return Some() or None.
pub fn cursor_yx(&self) -> (u32, u32) {
pub fn cursor_yx(&self) -> (i32, i32) {
let (mut y, mut x) = (0, 0);
unsafe { ncplane_cursor_yx(self, &mut y, &mut x) };
(y as u32, x as u32)
unsafe { crate::ncplane_cursor_yx(self, &mut y, &mut x) };
(y, x)
}
/// Returns the current row of the cursor within the [NcPlane].
pub fn cursor_y(&self) -> u32 {
pub fn cursor_y(&self) -> i32 {
self.cursor_yx().0
}
/// Returns the current column of the cursor within the [NcPlane].
pub fn cursor_x(&self) -> u32 {
pub fn cursor_x(&self) -> i32 {
self.cursor_yx().1
}
// Size --------------------------------------------------------------------
/// Return the dimensions of this [NcPlane].
///
/// Unlike [ncplane_dim_yx] which uses `i32`, this uses [u32].
pub fn dim_yx(&self) -> (u32, u32) {
let (mut y, mut x) = (0, 0);
unsafe { ncplane_dim_yx(self, &mut y, &mut x) };
unsafe { crate::ncplane_dim_yx(self, &mut y, &mut x) };
(y as u32, x as u32)
}
@ -157,21 +142,46 @@ impl NcPlane {
/// All cells associated with this ncplane are invalidated, and must not be
/// used after the call, excluding the base cell. The cursor is homed.
pub fn erase(&mut self) {
unsafe { ncplane_erase(self) }
unsafe { crate::ncplane_erase(self) }
}
// Write -------------------------------------------------------------------
///
pub fn putc_yx(&mut self, y: i32, x: i32, cell: &NcCell) -> NcResult {
unsafe { ncplane_putc_yx(self, y, x, cell) }
unsafe { crate::ncplane_putc_yx(self, y, x, cell) }
}
///
pub fn putc(&mut self, cell: &NcCell) -> NcResult {
ncplane_putc(self, cell)
crate::ncplane_putc(self, cell)
}
///
pub fn putstr(&mut self, string: &str) -> NcResult {
crate::ncplane_putstr(self, string)
}
///
pub fn putstr_yx(&mut self, y: i32, x: i32, string: &str) -> NcResult {
unsafe { crate::ncplane_putstr_yx(self, y, x, cstring![string]) }
}
// Pile --------------------------------------------------------------------
/// Returns the bottommost [NcPlane] of the pile that contains this [NnPlane].
pub fn bottom<'a>(&mut self) -> &'a mut NcPlane {
unsafe { &mut *crate::ncpile_bottom(self) }
}
/// Returns the topmost [NcPlane] of the pile that contains this [NnPlane].
pub fn top<'a>(&mut self) -> &'a mut NcPlane {
unsafe { &mut *ncpile_top(self) }
unsafe { &mut *crate::ncpile_top(self) }
}
}
impl Drop for NcPlane {
fn drop(&mut self) {
unsafe { crate::ncplane_destroy(self); }
}
}

@ -1,5 +1,7 @@
//! `ncvisual`
// TODO: implement constructors & Drop
// functions already exported by bindgen : 17
// -----------------------------------------
// (#) test: 0 / 17

@ -20,6 +20,14 @@ impl NcMenu {
}
}
impl Drop for NcMenu {
fn drop(&mut self) {
unsafe {
crate::ncmenu_destroy(self);
}
}
}
/// # `NcMenuOptions` Constructors
impl NcMenuOptions {
/// `NcMenuOptions` simple constructor

@ -1,5 +1,7 @@
//! `NcMultiSelector` widget.
// TODO: implement constructors & Drop
/// high-level widget for selecting items from a set
pub type NcMultiSelector = crate::bindings::bindgen::ncmultiselector;

@ -1,5 +1,7 @@
//! `NcPlot[F|U]64` widget.
// TODO: implement constructors & Drop
/// A histogram, bound to an [`NcPlane`][crate::NcPlane]
/// (uses non-negative `f64`s)
pub type NcPlotF64 = crate::bindings::bindgen::ncdplot;

@ -2,6 +2,21 @@
use crate::{ncreader_create, NcPlane, NcReader, NcReaderOptions};
/// # `NcReaderOptions` Constructors
impl NcReaderOptions {
/// `NcReaderOptions` simple constructor
pub fn new() -> Self {
Self {
// channels used for input
tchannels: 0,
// attributes used for input
tattrword: 0,
// bitfield of NCREADER_OPTION_*
flags: 0,
}
}
}
/// # `NcReader` Constructors
impl NcReader {
/// `NcReader` simple constructor
@ -15,17 +30,11 @@ impl NcReader {
}
}
/// # `NcReaderOptions` Constructors
impl NcReaderOptions {
/// `NcReaderOptions` simple constructor
pub fn new() -> Self {
Self {
// channels used for input
tchannels: 0,
// attributes used for input
tattrword: 0,
// bitfield of NCREADER_OPTION_*
flags: 0,
}
impl Drop for NcReader {
/// Destroys the NcReader and its bound [NcPlane].
///
/// See the `destroy` method or `ncreader_destroy` for more options.
fn drop(&mut self) {
unsafe { crate::ncreader_destroy(self, core::ptr::null_mut()); }
}
}

@ -1,5 +1,7 @@
//! `NcReel` widget.
// TODO: implement constructors & Drop
/// A wheel with `NcTablet`s on the outside
///
/// An `NcReel` is projected onto the 2d rendering area, showing some portion of

@ -1,5 +1,7 @@
//! `NcSelector` widget.
// TODO: implement constructors & Drop
/// high-level widget for selecting one item from a set
pub type NcSelector = crate::bindings::bindgen::ncselector;

Loading…
Cancel
Save