From 826f85eeed936137a0535a217df03e0fa7bc84f7 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 14 Dec 2021 17:30:42 -0500 Subject: clippy --- src/command.rs | 42 +++++++++++++++++++++++++++++------------- src/command/async_process.rs | 2 +- src/command/std.rs | 2 +- src/command/tokio.rs | 2 +- src/lib.rs | 14 ++++++++++++++ src/pty.rs | 30 ++++++++++++++++++------------ src/pty/async_io.rs | 17 +++++++++-------- src/pty/std.rs | 14 +++++++------- src/pty/tokio.rs | 25 +++++++++++++------------ 9 files changed, 93 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/command.rs b/src/command.rs index df5ed62..d810d79 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,4 +1,3 @@ -use crate::error::*; use crate::pty::Pty as _; use ::std::os::unix::io::AsRawFd as _; @@ -21,18 +20,26 @@ pub trait Command { /// Creates a new pty, associates the command's stdin/stdout/stderr with /// that pty, and then calls `spawn`. This will override any previous /// calls to `stdin`/`stdout`/`stderr`. + /// + /// # Errors + /// * `Error::AsyncPty`: error making pty async + /// * `Error::AsyncPtyNix`: error making pty async + /// * `Error::CreatePty`: error creating pty + /// * `Error::OpenPts`: error opening pts + /// * `Error::SetTermSize`: error setting terminal size + /// * `Error::Spawn`: error spawning subprocess + /// * `Error::SpawnNix`: error spawning subprocess fn spawn_pty( &mut self, size: Option<&crate::pty::Size>, - ) -> Result>; + ) -> crate::error::Result>; } impl Command for T where - T: CommandImpl, + T: Impl, T::Pty: crate::pty::Pty, - <::Pty as crate::pty::Pty>::Pt: - ::std::os::unix::io::AsRawFd, + <::Pty as crate::pty::Pty>::Pt: ::std::os::unix::io::AsRawFd, { type Child = T::Child; type Pty = T::Pty; @@ -40,7 +47,7 @@ where fn spawn_pty( &mut self, size: Option<&crate::pty::Size>, - ) -> Result> { + ) -> crate::error::Result> { let (pty, pts, stdin, stdout, stderr) = setup_pty::(size)?; let pt_fd = pty.pt().as_raw_fd(); @@ -74,7 +81,7 @@ where // async-signal-safe). unsafe { self.pre_exec_impl(pre_exec) }; - let child = self.spawn_impl().map_err(Error::Spawn)?; + let child = self.spawn_impl().map_err(crate::error::Error::Spawn)?; Ok(Child { child, pty }) } @@ -116,7 +123,13 @@ where /// /// This will additionally cause a `SIGWINCH` signal to be sent to the /// running process. - pub fn resize_pty(&self, size: &crate::pty::Size) -> Result<()> { + /// + /// # Errors + /// * `Error::SetTermSize`: error setting terminal size + pub fn resize_pty( + &self, + size: &crate::pty::Size, + ) -> crate::error::Result<()> { self.pty.resize(size) } } @@ -136,7 +149,7 @@ impl ::std::ops::DerefMut for Child { } // XXX shouldn't be pub? -pub trait CommandImpl { +pub trait Impl { type Child; type Pty; @@ -154,7 +167,7 @@ pub trait CommandImpl { fn setup_pty

( size: Option<&crate::pty::Size>, -) -> Result<( +) -> crate::error::Result<( P, ::std::fs::File, ::std::os::unix::io::RawFd, @@ -172,9 +185,12 @@ where let pts = pty.pts()?; let pts_fd = pts.as_raw_fd(); - let stdin = nix::unistd::dup(pts_fd).map_err(Error::SpawnNix)?; - let stdout = nix::unistd::dup(pts_fd).map_err(Error::SpawnNix)?; - let stderr = nix::unistd::dup(pts_fd).map_err(Error::SpawnNix)?; + let stdin = + nix::unistd::dup(pts_fd).map_err(crate::error::Error::SpawnNix)?; + let stdout = + nix::unistd::dup(pts_fd).map_err(crate::error::Error::SpawnNix)?; + let stderr = + nix::unistd::dup(pts_fd).map_err(crate::error::Error::SpawnNix)?; Ok((pty, pts, stdin, stdout, stderr)) } diff --git a/src/command/async_process.rs b/src/command/async_process.rs index 4248402..4a1d042 100644 --- a/src/command/async_process.rs +++ b/src/command/async_process.rs @@ -1,7 +1,7 @@ use async_process::unix::CommandExt as _; use std::os::unix::io::FromRawFd as _; -impl super::CommandImpl for async_process::Command { +impl super::Impl for async_process::Command { type Child = async_process::Child; type Pty = crate::pty::async_io::Pty; diff --git a/src/command/std.rs b/src/command/std.rs index f1c5418..5c08808 100644 --- a/src/command/std.rs +++ b/src/command/std.rs @@ -1,7 +1,7 @@ use std::os::unix::io::FromRawFd as _; use std::os::unix::process::CommandExt as _; -impl super::CommandImpl for std::process::Command { +impl super::Impl for std::process::Command { type Child = std::process::Child; type Pty = crate::pty::std::Pty; diff --git a/src/command/tokio.rs b/src/command/tokio.rs index 67baf74..58d2c26 100644 --- a/src/command/tokio.rs +++ b/src/command/tokio.rs @@ -1,6 +1,6 @@ use std::os::unix::io::FromRawFd as _; -impl super::CommandImpl for tokio::process::Command { +impl super::Impl for tokio::process::Command { type Child = tokio::process::Child; type Pty = crate::pty::tokio::Pty; diff --git a/src/lib.rs b/src/lib.rs index a9672ce..5d98841 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,20 @@ //! //! Any number of backends may be enabled, depending on your needs. +#![warn(clippy::cargo)] +#![warn(clippy::pedantic)] +#![warn(clippy::nursery)] +#![warn(clippy::unwrap_used)] +#![warn(clippy::expect_used)] +#![warn(clippy::indexing_slicing)] +#![warn(clippy::as_conversions)] +#![allow(clippy::cognitive_complexity)] +#![allow(clippy::missing_const_for_fn)] +#![allow(clippy::similar_names)] +#![allow(clippy::struct_excessive_bools)] +#![allow(clippy::too_many_arguments)] +#![allow(clippy::too_many_lines)] + mod command; pub use command::{Child, Command}; mod error; diff --git a/src/pty.rs b/src/pty.rs index bf34d61..fde86b1 100644 --- a/src/pty.rs +++ b/src/pty.rs @@ -1,5 +1,3 @@ -use crate::error::*; - use ::std::os::unix::io::IntoRawFd as _; #[cfg(any(feature = "backend-async-std", feature = "backend-smol"))] @@ -12,13 +10,13 @@ pub mod tokio; pub trait Pty { type Pt; - fn new() -> Result + fn new() -> crate::error::Result where Self: Sized; fn pt(&self) -> &Self::Pt; fn pt_mut(&mut self) -> &mut Self::Pt; - fn pts(&self) -> Result<::std::fs::File>; - fn resize(&self, size: &super::Size) -> Result<()>; + fn pts(&self) -> crate::error::Result<::std::fs::File>; + fn resize(&self, size: &super::Size) -> crate::error::Result<()>; } /// Represents the size of the pty. @@ -32,6 +30,7 @@ pub struct Size { impl Size { /// Returns a [`Size`](Size) instance with the given number of rows and /// columns. + #[must_use] pub fn new(row: u16, col: u16) -> Self { Self { row, @@ -43,6 +42,7 @@ impl Size { /// Returns a [`Size`](Size) instance with the given number of rows and /// columns, as well as the given pixel dimensions. + #[must_use] pub fn new_with_pixel( row: u16, col: u16, @@ -69,15 +69,19 @@ impl From<&Size> for nix::pty::Winsize { } } -fn create_pt() -> Result<(::std::os::unix::io::RawFd, ::std::path::PathBuf)> { +fn create_pt( +) -> crate::error::Result<(::std::os::unix::io::RawFd, ::std::path::PathBuf)> +{ let pt = nix::pty::posix_openpt( nix::fcntl::OFlag::O_RDWR | nix::fcntl::OFlag::O_NOCTTY, ) - .map_err(Error::CreatePty)?; - nix::pty::grantpt(&pt).map_err(Error::CreatePty)?; - nix::pty::unlockpt(&pt).map_err(Error::CreatePty)?; + .map_err(crate::error::Error::CreatePty)?; + nix::pty::grantpt(&pt).map_err(crate::error::Error::CreatePty)?; + nix::pty::unlockpt(&pt).map_err(crate::error::Error::CreatePty)?; - let ptsname = nix::pty::ptsname_r(&pt).map_err(Error::CreatePty)?.into(); + let ptsname = nix::pty::ptsname_r(&pt) + .map_err(crate::error::Error::CreatePty)? + .into(); let pt_fd = pt.into_raw_fd(); @@ -99,6 +103,8 @@ fn set_term_size( // descriptor and size is guaranteed to be initialized because it's a // normal rust value, and nix::pty::Winsize is a repr(C) struct with the // same layout as `struct winsize` from sys/ioctl.h. - unsafe { set_term_size_unsafe(fd, &size as *const nix::pty::Winsize) } - .map(|_| ()) + unsafe { + set_term_size_unsafe(fd, ::std::ptr::NonNull::from(&size).as_ptr()) + } + .map(|_| ()) } diff --git a/src/pty/async_io.rs b/src/pty/async_io.rs index ebba0bf..985b2af 100644 --- a/src/pty/async_io.rs +++ b/src/pty/async_io.rs @@ -1,5 +1,3 @@ -use crate::error::*; - use std::os::unix::io::{AsRawFd as _, FromRawFd as _}; pub struct Pty { @@ -10,7 +8,7 @@ pub struct Pty { impl super::Pty for Pty { type Pt = async_io::Async; - fn new() -> Result { + fn new() -> crate::error::Result { let (pt_fd, ptsname) = super::create_pt()?; // safe because posix_openpt (or the previous functions operating on @@ -20,7 +18,8 @@ impl super::Pty for Pty { // File object to take full ownership. let pt = unsafe { std::fs::File::from_raw_fd(pt_fd) }; - let pt = async_io::Async::new(pt).map_err(Error::AsyncPty)?; + let pt = async_io::Async::new(pt) + .map_err(crate::error::Error::AsyncPty)?; Ok(Self { pt, ptsname }) } @@ -33,17 +32,19 @@ impl super::Pty for Pty { &mut self.pt } - fn pts(&self) -> Result { + fn pts(&self) -> crate::error::Result { let fh = std::fs::OpenOptions::new() .read(true) .write(true) .open(&self.ptsname) - .map_err(|e| Error::OpenPts(e, self.ptsname.clone()))?; + .map_err(|e| { + crate::error::Error::OpenPts(e, self.ptsname.clone()) + })?; Ok(fh) } - fn resize(&self, size: &super::Size) -> Result<()> { + fn resize(&self, size: &super::Size) -> crate::error::Result<()> { super::set_term_size(self.pt().as_raw_fd(), size) - .map_err(Error::SetTermSize) + .map_err(crate::error::Error::SetTermSize) } } diff --git a/src/pty/std.rs b/src/pty/std.rs index e76dcf5..612c9b6 100644 --- a/src/pty/std.rs +++ b/src/pty/std.rs @@ -1,5 +1,3 @@ -use crate::error::*; - use std::os::unix::io::{AsRawFd as _, FromRawFd as _}; pub struct Pty { @@ -10,7 +8,7 @@ pub struct Pty { impl super::Pty for Pty { type Pt = std::fs::File; - fn new() -> Result { + fn new() -> crate::error::Result { let (pt_fd, ptsname) = super::create_pt()?; // safe because posix_openpt (or the previous functions operating on @@ -31,17 +29,19 @@ impl super::Pty for Pty { &mut self.pt } - fn pts(&self) -> Result { + fn pts(&self) -> crate::error::Result { let fh = std::fs::OpenOptions::new() .read(true) .write(true) .open(&self.ptsname) - .map_err(|e| Error::OpenPts(e, self.ptsname.clone()))?; + .map_err(|e| { + crate::error::Error::OpenPts(e, self.ptsname.clone()) + })?; Ok(fh) } - fn resize(&self, size: &super::Size) -> Result<()> { + fn resize(&self, size: &super::Size) -> crate::error::Result<()> { super::set_term_size(self.pt().as_raw_fd(), size) - .map_err(Error::SetTermSize) + .map_err(crate::error::Error::SetTermSize) } } diff --git a/src/pty/tokio.rs b/src/pty/tokio.rs index c5af6bb..116ec6e 100644 --- a/src/pty/tokio.rs +++ b/src/pty/tokio.rs @@ -1,5 +1,3 @@ -use crate::error::*; - use std::io::{Read as _, Write as _}; use std::os::unix::io::{AsRawFd as _, FromRawFd as _}; @@ -41,14 +39,14 @@ impl tokio::io::AsyncRead for AsyncPty { ) -> std::task::Poll> { loop { let mut guard = futures::ready!(self.0.poll_read_ready(cx))?; - let mut b = [0u8; 4096]; + let mut b = [0_u8; 4096]; match guard.try_io(|inner| inner.get_ref().read(&mut b)) { Ok(Ok(bytes)) => { // XXX this is safe, but not particularly efficient buf.clear(); buf.initialize_unfilled_to(bytes); buf.set_filled(bytes); - buf.filled_mut().copy_from_slice(&b[..bytes]); + buf.filled_mut().copy_from_slice(b.get(..bytes).unwrap()); return std::task::Poll::Ready(Ok(())); } Ok(Err(e)) => return std::task::Poll::Ready(Err(e)), @@ -102,11 +100,11 @@ pub struct Pty { impl super::Pty for Pty { type Pt = AsyncPty; - fn new() -> Result { + fn new() -> crate::error::Result { let (pt_fd, ptsname) = super::create_pt()?; let bits = nix::fcntl::fcntl(pt_fd, nix::fcntl::FcntlArg::F_GETFL) - .map_err(Error::AsyncPtyNix)?; + .map_err(crate::error::Error::AsyncPtyNix)?; // this should be safe because i am just using the return value of // F_GETFL directly, but for whatever reason nix doesn't like // from_bits(bits) (it claims it has an unknown field) @@ -116,7 +114,7 @@ impl super::Pty for Pty { ) }; nix::fcntl::fcntl(pt_fd, nix::fcntl::FcntlArg::F_SETFL(opts)) - .map_err(Error::AsyncPtyNix)?; + .map_err(crate::error::Error::AsyncPtyNix)?; // safe because posix_openpt (or the previous functions operating on // the result) would have returned an Err (causing us to return early) @@ -126,7 +124,8 @@ impl super::Pty for Pty { let pt = unsafe { std::fs::File::from_raw_fd(pt_fd) }; let pt = AsyncPty( - tokio::io::unix::AsyncFd::new(pt).map_err(Error::AsyncPty)?, + tokio::io::unix::AsyncFd::new(pt) + .map_err(crate::error::Error::AsyncPty)?, ); Ok(Self { pt, ptsname }) @@ -140,17 +139,19 @@ impl super::Pty for Pty { &mut self.pt } - fn pts(&self) -> Result { + fn pts(&self) -> crate::error::Result { let fh = std::fs::OpenOptions::new() .read(true) .write(true) .open(&self.ptsname) - .map_err(|e| Error::OpenPts(e, self.ptsname.clone()))?; + .map_err(|e| { + crate::error::Error::OpenPts(e, self.ptsname.clone()) + })?; Ok(fh) } - fn resize(&self, size: &super::Size) -> Result<()> { + fn resize(&self, size: &super::Size) -> crate::error::Result<()> { super::set_term_size(self.pt().as_raw_fd(), size) - .map_err(Error::SetTermSize) + .map_err(crate::error::Error::SetTermSize) } } -- cgit v1.2.3-54-g00ecf