Skip to content

Commit

Permalink
Make publicly exported types implement Debug trait
Browse files Browse the repository at this point in the history
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 rust-lang/rust#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.
  • Loading branch information
d-e-s-o committed Apr 20, 2018
1 parent b3e0bbd commit 8794675
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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<io::Result<u8>>,
Expand Down
10 changes: 5 additions & 5 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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 Default for Goto {
Expand All @@ -47,7 +47,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 fmt::Display for Left {
Expand All @@ -57,7 +57,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 fmt::Display for Right {
Expand All @@ -67,7 +67,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 fmt::Display for Up {
Expand All @@ -77,7 +77,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 fmt::Display for Down {
Expand Down
4 changes: 4 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use event::{self, Event, Key};
use raw::IntoRawMode;

/// An iterator over input keys.
#[derive(Debug)]
pub struct Keys<R> {
iter: Events<R>,
}
Expand All @@ -27,6 +28,7 @@ impl<R: Read> Iterator for Keys<R> {
}

/// An iterator over input events.
#[derive(Debug)]
pub struct Events<R> {
inner: EventsAndRaw<R>
}
Expand All @@ -40,6 +42,7 @@ impl<R: Read> Iterator for Events<R> {
}

/// An iterator over input events and the bytes that define them.
#[derive(Debug)]
pub struct EventsAndRaw<R> {
source: R,
leftover: Option<u8>,
Expand Down Expand Up @@ -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<W: Write> {
term: W,
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/ticki/termion).
#![deny(missing_debug_implementations)]
#![warn(missing_docs)]

#[cfg(target_os = "redox")]
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 23 additions & 3 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,47 @@
//! }
//! ```

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.
struct Termios(SysTermios);

impl fmt::Debug for Termios {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "termion::sys::Termios")
}
}

impl ops::Deref for Termios {
type Target = SysTermios;

fn deref(&self) -> &SysTermios {
&self.0
}

}

/// 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<W: Write> {
prev_ios: Termios,
output: W,
}

impl<W: Write> Drop for RawTerminal<W> {
fn drop(&mut self) {
set_terminal_attr(&self.prev_ios).unwrap();
set_terminal_attr(&*self.prev_ios).unwrap();
}
}

Expand Down Expand Up @@ -95,7 +115,7 @@ impl<W: Write> IntoRawMode for W {
set_terminal_attr(&ios)?;

Ok(RawTerminal {
prev_ios: prev_ios,
prev_ios: Termios(prev_ios),
output: self,
})
}
Expand Down
3 changes: 3 additions & 0 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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<W: Write> {
/// The output target.
output: W,
Expand Down
4 changes: 2 additions & 2 deletions src/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 8794675

Please sign in to comment.