From 3d624311ea2c0941f81984819cbbdb0479f5972d Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Thu, 19 Apr 2018 22:08:56 -0700 Subject: [PATCH] Make publicly exported types implement Debug trait It seems to have become common practice for publicly exported types in a library to implement the Debug trait. Doing so potentially simplifies trouble shooting in client code directly but it also is a requirement in case said client code embeds such objects and wants the wrappers to implement this trait. For a deeper discussion of this topic please refer to https://github.com/rust-lang/rust/pull/32054 To that end, this change adjust all publicly exported types to derive from Debug. It also adds a crate wide lint enforcing this constraint. --- src/async.rs | 1 + src/cursor.rs | 11 ++++++----- src/input.rs | 4 ++++ src/lib.rs | 1 + src/macros.rs | 2 +- src/raw.rs | 21 +++++++++++++++++---- src/screen.rs | 3 +++ src/scroll.rs | 4 ++-- 8 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/async.rs b/src/async.rs index ea02489..c0bd3d1 100644 --- a/src/async.rs +++ b/src/async.rs @@ -53,6 +53,7 @@ pub fn async_stdin() -> AsyncReader { /// /// This acts as any other stream, with the exception that reading from it won't block. Instead, /// the buffer will only be partially updated based on how much the internal buffer holds. +#[derive(Debug)] pub struct AsyncReader { /// The underlying mpsc receiver. recv: mpsc::Receiver>, diff --git a/src/cursor.rs b/src/cursor.rs index bbc0394..0021d59 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -31,7 +31,7 @@ derive_csi_sequence!("Save the cursor.", Save, "s"); /// print!("{}{}Stuff", termion::clear::All, termion::cursor::Goto(5, 3)); /// } /// ``` -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Goto(pub u16, pub u16); impl From for String { @@ -55,7 +55,7 @@ impl fmt::Display for Goto { } /// Move cursor left. -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Left(pub u16); impl From for String { @@ -72,7 +72,7 @@ impl fmt::Display for Left { } /// Move cursor right. -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Right(pub u16); impl From for String { @@ -89,7 +89,7 @@ impl fmt::Display for Right { } /// Move cursor up. -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Up(pub u16); impl From for String { @@ -106,7 +106,7 @@ impl fmt::Display for Up { } /// Move cursor down. -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Down(pub u16); impl From for String { @@ -178,6 +178,7 @@ impl DetectCursorPos for W { /// Hide the cursor for the lifetime of this struct. /// It will hide the cursor on creation with from() and show it back on drop(). +#[derive(Debug)] pub struct HideCursor { /// The output target. output: W, diff --git a/src/input.rs b/src/input.rs index 5c8ecf4..c3a46e2 100644 --- a/src/input.rs +++ b/src/input.rs @@ -7,6 +7,7 @@ use event::{self, Event, Key}; use raw::IntoRawMode; /// An iterator over input keys. +#[derive(Debug)] pub struct Keys { iter: Events, } @@ -27,6 +28,7 @@ impl Iterator for Keys { } /// An iterator over input events. +#[derive(Debug)] pub struct Events { inner: EventsAndRaw } @@ -40,6 +42,7 @@ impl Iterator for Events { } /// An iterator over input events and the bytes that define them. +#[derive(Debug)] pub struct EventsAndRaw { source: R, leftover: Option, @@ -182,6 +185,7 @@ const EXIT_MOUSE_SEQUENCE: &'static str = csi!("?1006l\x1b[?1015l\x1b[?1002l\x1b /// A terminal with added mouse support. /// /// This can be obtained through the `From` implementations. +#[derive(Debug)] pub struct MouseTerminal { term: W, } diff --git a/src/lib.rs b/src/lib.rs index 1d8f66f..e135679 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ //! Supports Redox, Mac OS X, and Linux (or, in general, ANSI terminals). //! //! For more information refer to the [README](https://github.com/redox-os/termion). +#![deny(missing_debug_implementations)] #![warn(missing_docs)] extern crate numtoa; diff --git a/src/macros.rs b/src/macros.rs index 5fd70b9..1053638 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -7,7 +7,7 @@ macro_rules! csi { macro_rules! derive_csi_sequence { ($doc:expr, $name:ident, $value:expr) => { #[doc = $doc] - #[derive(Copy, Clone)] + #[derive(Copy, Clone, Debug)] pub struct $name; impl fmt::Display for $name { diff --git a/src/raw.rs b/src/raw.rs index 0dbfb56..096ea09 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -22,19 +22,32 @@ //! } //! ``` +use std::fmt; use std::io::{self, Write}; use std::ops; -use sys::Termios; +use sys::Termios as SysTermios; use sys::attr::{get_terminal_attr, raw_terminal_attr, set_terminal_attr}; /// The timeout of an escape code control sequence, in milliseconds. pub const CONTROL_SEQUENCE_TIMEOUT: u64 = 100; +/// A wrapper around sys::Termios that implements std::fmt::Debug. +// This workaround can go once libc gets adjusted so that all public +// types implement Debug https://github.com/rust-lang/rfcs/pull/2235. +struct Termios(SysTermios); + +impl fmt::Debug for Termios { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "termion::sys::Termios") + } +} + /// A terminal restorer, which keeps the previous state of the terminal, and restores it, when /// dropped. /// /// Restoring will entirely bring back the old TTY state. +#[derive(Debug)] pub struct RawTerminal { prev_ios: Termios, output: W, @@ -42,7 +55,7 @@ pub struct RawTerminal { impl Drop for RawTerminal { fn drop(&mut self) { - set_terminal_attr(&self.prev_ios).unwrap(); + set_terminal_attr(&self.prev_ios.0).unwrap(); } } @@ -95,7 +108,7 @@ impl IntoRawMode for W { set_terminal_attr(&ios)?; Ok(RawTerminal { - prev_ios: prev_ios, + prev_ios: Termios(prev_ios), output: self, }) } @@ -103,7 +116,7 @@ impl IntoRawMode for W { impl RawTerminal { pub fn suspend_raw_mode(&self) -> io::Result<()> { - set_terminal_attr(&self.prev_ios)?; + set_terminal_attr(&self.prev_ios.0)?; Ok(()) } diff --git a/src/screen.rs b/src/screen.rs index 822399e..7c27f85 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -24,6 +24,7 @@ use std::ops; use std::fmt; /// Switch to the main screen buffer of the terminal. +#[derive(Debug)] pub struct ToMainScreen; impl fmt::Display for ToMainScreen { @@ -33,6 +34,7 @@ impl fmt::Display for ToMainScreen { } /// Switch to the alternate screen buffer of the terminal. +#[derive(Debug)] pub struct ToAlternateScreen; impl fmt::Display for ToAlternateScreen { @@ -46,6 +48,7 @@ impl fmt::Display for ToAlternateScreen { /// /// This is achieved by switching the terminal to the alternate screen on creation and /// automatically switching it back to the original screen on drop. +#[derive(Debug)] pub struct AlternateScreen { /// The output target. output: W, diff --git a/src/scroll.rs b/src/scroll.rs index 2744507..3ab97ac 100644 --- a/src/scroll.rs +++ b/src/scroll.rs @@ -3,7 +3,7 @@ use std::fmt; /// Scroll up. -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Up(pub u16); impl fmt::Display for Up { @@ -13,7 +13,7 @@ impl fmt::Display for Up { } /// Scroll down. -#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Down(pub u16); impl fmt::Display for Down { -- GitLab