From f3498d0afe3bd36cf3e9f553776518fd458a39af Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 12 Mar 2021 11:46:14 -0500 Subject: simplify --- src/blocking/input.rs | 26 ++++++++++++-------------- src/input.rs | 33 +++++++++++++++------------------ 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/blocking/input.rs b/src/blocking/input.rs index ea34536..1381776 100644 --- a/src/blocking/input.rs +++ b/src/blocking/input.rs @@ -6,8 +6,7 @@ use std::os::unix::io::AsRawFd as _; use crate::private::Input as _; pub struct RawGuard { - termios: nix::sys::termios::Termios, - cleaned_up: bool, + termios: Option, } impl RawGuard { @@ -25,23 +24,22 @@ impl RawGuard { ) .map_err(Error::SetRaw)?; Ok(Self { - termios, - cleaned_up: false, + termios: Some(termios), }) } pub fn cleanup(&mut self) -> Result<()> { - if self.cleaned_up { - return Ok(()); + if let Some(termios) = self.termios.take() { + let stdin = std::io::stdin().as_raw_fd(); + nix::sys::termios::tcsetattr( + stdin, + nix::sys::termios::SetArg::TCSANOW, + &termios, + ) + .map_err(Error::UnsetRaw) + } else { + Ok(()) } - self.cleaned_up = true; - let stdin = std::io::stdin().as_raw_fd(); - nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &self.termios, - ) - .map_err(Error::UnsetRaw) } } diff --git a/src/input.rs b/src/input.rs index 7568de2..c881e44 100644 --- a/src/input.rs +++ b/src/input.rs @@ -6,8 +6,7 @@ use std::os::unix::io::AsRawFd as _; use crate::private::Input as _; pub struct RawGuard { - termios: nix::sys::termios::Termios, - cleaned_up: bool, + termios: Option, } impl RawGuard { @@ -30,27 +29,25 @@ impl RawGuard { }) .await?; Ok(Self { - termios, - cleaned_up: false, + termios: Some(termios), }) } pub async fn cleanup(&mut self) -> Result<()> { - if self.cleaned_up { - return Ok(()); + if let Some(termios) = self.termios.take() { + let stdin = std::io::stdin().as_raw_fd(); + blocking::unblock(move || { + nix::sys::termios::tcsetattr( + stdin, + nix::sys::termios::SetArg::TCSANOW, + &termios, + ) + .map_err(Error::UnsetRaw) + }) + .await + } else { + Ok(()) } - self.cleaned_up = true; - let stdin = std::io::stdin().as_raw_fd(); - let termios = self.termios.clone(); - blocking::unblock(move || { - nix::sys::termios::tcsetattr( - stdin, - nix::sys::termios::SetArg::TCSANOW, - &termios, - ) - .map_err(Error::UnsetRaw) - }) - .await } } -- cgit v1.2.3-54-g00ecf