mirror of
https://github.com/wez/wezterm.git
synced 2024-12-23 05:12:40 +03:00
failure -> anyhow + thiserror
This commit is contained in:
parent
b9ae1a709d
commit
55b4a08131
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "filedescriptor"
|
||||
version = "0.5.1"
|
||||
version = "0.6.0"
|
||||
authors = ["Wez Furlong"]
|
||||
edition = "2018"
|
||||
repository = "https://github.com/wez/wzsh"
|
||||
@ -11,8 +11,8 @@ readme = "README.md"
|
||||
keywords = ["socketpair", "pipe", "poll", "filedescriptor"]
|
||||
|
||||
[dependencies]
|
||||
failure = "0.1"
|
||||
failure_derive = "0.1"
|
||||
anyhow = "1.0"
|
||||
thiserror = "1.0"
|
||||
libc = "0.2"
|
||||
|
||||
[target."cfg(windows)".dependencies]
|
||||
|
@ -15,16 +15,15 @@ calling `as_raw_fd` and `as_raw_handle`:
|
||||
|
||||
```
|
||||
use filedescriptor::{FileDescriptor, FromRawFileDescriptor};
|
||||
use failure::Fallible;
|
||||
use std::io::Write;
|
||||
|
||||
fn get_stdout() -> Fallible<FileDescriptor> {
|
||||
fn get_stdout() -> anyhow::Result<FileDescriptor> {
|
||||
let stdout = std::io::stdout();
|
||||
let handle = stdout.lock();
|
||||
FileDescriptor::dup(&handle)
|
||||
}
|
||||
|
||||
fn print_something() -> Fallible<()> {
|
||||
fn print_something() -> anyhow::Result<()> {
|
||||
get_stdout()?.write(b"hello")?;
|
||||
Ok(())
|
||||
}
|
||||
@ -37,7 +36,7 @@ the lifetime of both the read and write ends of that pipe.
|
||||
```
|
||||
use filedescriptor::Pipe;
|
||||
use std::io::{Read, Write};
|
||||
use failure::Error;
|
||||
use anyhow::Error;
|
||||
|
||||
let mut pipe = Pipe::new()?;
|
||||
pipe.write.write(b"hello")?;
|
||||
@ -54,7 +53,7 @@ sockets and functions both on posix and windows systems.
|
||||
|
||||
```
|
||||
use std::io::{Read, Write};
|
||||
use failure::Error;
|
||||
use anyhow::Error;
|
||||
|
||||
let (mut a, mut b) = filedescriptor::socketpair()?;
|
||||
a.write(b"hello")?;
|
||||
@ -76,7 +75,7 @@ function is used instead.
|
||||
|
||||
```
|
||||
use filedescriptor::*;
|
||||
use failure::Error;
|
||||
use anyhow::Error;
|
||||
use std::time::Duration;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
|
@ -13,16 +13,15 @@
|
||||
//!
|
||||
//! ```
|
||||
//! use filedescriptor::{FileDescriptor, FromRawFileDescriptor};
|
||||
//! use failure::Fallible;
|
||||
//! use std::io::Write;
|
||||
//!
|
||||
//! fn get_stdout() -> Fallible<FileDescriptor> {
|
||||
//! fn get_stdout() -> anyhow::Result<FileDescriptor> {
|
||||
//! let stdout = std::io::stdout();
|
||||
//! let handle = stdout.lock();
|
||||
//! FileDescriptor::dup(&handle)
|
||||
//! }
|
||||
//!
|
||||
//! fn print_something() -> Fallible<()> {
|
||||
//! fn print_something() -> anyhow::Result<()> {
|
||||
//! get_stdout()?.write(b"hello")?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
@ -35,7 +34,7 @@
|
||||
//! ```
|
||||
//! use filedescriptor::Pipe;
|
||||
//! use std::io::{Read, Write};
|
||||
//! use failure::Error;
|
||||
//! use anyhow::Error;
|
||||
//!
|
||||
//! let mut pipe = Pipe::new()?;
|
||||
//! pipe.write.write(b"hello")?;
|
||||
@ -53,7 +52,7 @@
|
||||
//!
|
||||
//! ```
|
||||
//! use std::io::{Read, Write};
|
||||
//! use failure::Error;
|
||||
//! use anyhow::Error;
|
||||
//!
|
||||
//! let (mut a, mut b) = filedescriptor::socketpair()?;
|
||||
//! a.write(b"hello")?;
|
||||
@ -76,7 +75,7 @@
|
||||
//!
|
||||
//! ```
|
||||
//! use filedescriptor::*;
|
||||
//! use failure::Error;
|
||||
//! use anyhow::Error;
|
||||
//! use std::time::Duration;
|
||||
//! use std::io::{Read, Write};
|
||||
//!
|
||||
@ -96,7 +95,6 @@
|
||||
//!
|
||||
//! # Ok::<(), Error>(())
|
||||
//! ```
|
||||
use failure::Fallible;
|
||||
|
||||
#[cfg(unix)]
|
||||
mod unix;
|
||||
@ -169,7 +167,7 @@ impl OwnedHandle {
|
||||
/// potentially fallible operation.
|
||||
/// The returned handle has a separate lifetime from the source, but
|
||||
/// references the same object at the kernel level.
|
||||
pub fn try_clone(&self) -> Fallible<Self> {
|
||||
pub fn try_clone(&self) -> anyhow::Result<Self> {
|
||||
Self::dup_impl(self, self.handle_type)
|
||||
}
|
||||
|
||||
@ -180,7 +178,7 @@ impl OwnedHandle {
|
||||
/// potentially fallible operation.
|
||||
/// The returned handle has a separate lifetime from the source, but
|
||||
/// references the same object at the kernel level.
|
||||
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> Fallible<Self> {
|
||||
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> anyhow::Result<Self> {
|
||||
Self::dup_impl(f, Default::default())
|
||||
}
|
||||
}
|
||||
@ -194,16 +192,15 @@ impl OwnedHandle {
|
||||
///
|
||||
/// ```
|
||||
/// use filedescriptor::{FileDescriptor, FromRawFileDescriptor};
|
||||
/// use failure::Fallible;
|
||||
/// use std::io::Write;
|
||||
///
|
||||
/// fn get_stdout() -> Fallible<FileDescriptor> {
|
||||
/// fn get_stdout() -> anyhow::Result<FileDescriptor> {
|
||||
/// let stdout = std::io::stdout();
|
||||
/// let handle = stdout.lock();
|
||||
/// FileDescriptor::dup(&handle)
|
||||
/// }
|
||||
///
|
||||
/// fn print_something() -> Fallible<()> {
|
||||
/// fn print_something() -> anyhow::Result<()> {
|
||||
/// get_stdout()?.write(b"hello")?;
|
||||
/// Ok(())
|
||||
/// }
|
||||
@ -229,7 +226,7 @@ impl FileDescriptor {
|
||||
/// potentially fallible operation.
|
||||
/// The returned handle has a separate lifetime from the source, but
|
||||
/// references the same object at the kernel level.
|
||||
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> Fallible<Self> {
|
||||
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> anyhow::Result<Self> {
|
||||
OwnedHandle::dup(f).map(|handle| Self { handle })
|
||||
}
|
||||
|
||||
@ -239,7 +236,7 @@ impl FileDescriptor {
|
||||
/// potentially fallible operation.
|
||||
/// The returned handle has a separate lifetime from the source, but
|
||||
/// references the same object at the kernel level.
|
||||
pub fn try_clone(&self) -> Fallible<Self> {
|
||||
pub fn try_clone(&self) -> anyhow::Result<Self> {
|
||||
self.handle.try_clone().map(|handle| Self { handle })
|
||||
}
|
||||
|
||||
@ -247,7 +244,7 @@ impl FileDescriptor {
|
||||
/// to be used for eg: redirecting the stdio streams of a child
|
||||
/// process. The `Stdio` is created using a duplicated handle so
|
||||
/// that the source handle remains alive.
|
||||
pub fn as_stdio(&self) -> Fallible<std::process::Stdio> {
|
||||
pub fn as_stdio(&self) -> anyhow::Result<std::process::Stdio> {
|
||||
self.as_stdio_impl()
|
||||
}
|
||||
}
|
||||
@ -258,7 +255,7 @@ impl FileDescriptor {
|
||||
/// ```
|
||||
/// use filedescriptor::Pipe;
|
||||
/// use std::io::{Read,Write};
|
||||
/// use failure::Error;
|
||||
/// use anyhow::Error;
|
||||
///
|
||||
/// let mut pipe = Pipe::new()?;
|
||||
/// pipe.write.write(b"hello")?;
|
||||
@ -305,13 +302,13 @@ use std::time::Duration;
|
||||
///
|
||||
/// The `pfd` array is mutated and the `revents` field is updated to indicate
|
||||
/// which of the events were received.
|
||||
pub fn poll(pfd: &mut [pollfd], duration: Option<Duration>) -> Fallible<usize> {
|
||||
pub fn poll(pfd: &mut [pollfd], duration: Option<Duration>) -> anyhow::Result<usize> {
|
||||
poll_impl(pfd, duration)
|
||||
}
|
||||
|
||||
/// Create a pair of connected sockets
|
||||
///
|
||||
/// This implementation creates a pair of SOCK_STREAM sockets.
|
||||
pub fn socketpair() -> Fallible<(FileDescriptor, FileDescriptor)> {
|
||||
pub fn socketpair() -> anyhow::Result<(FileDescriptor, FileDescriptor)> {
|
||||
socketpair_impl()
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use crate::{
|
||||
AsRawFileDescriptor, AsRawSocketDescriptor, FileDescriptor, FromRawFileDescriptor,
|
||||
FromRawSocketDescriptor, IntoRawFileDescriptor, IntoRawSocketDescriptor, OwnedHandle, Pipe,
|
||||
};
|
||||
use failure::{bail, Fallible};
|
||||
use anyhow::bail;
|
||||
use std::os::unix::prelude::*;
|
||||
|
||||
pub(crate) type HandleType = ();
|
||||
@ -86,7 +86,7 @@ impl FromRawFd for OwnedHandle {
|
||||
|
||||
impl OwnedHandle {
|
||||
/// Helper function to set the close-on-exec flag for a raw descriptor
|
||||
fn cloexec(&mut self) -> Fallible<()> {
|
||||
fn cloexec(&mut self) -> anyhow::Result<()> {
|
||||
let flags = unsafe { libc::fcntl(self.handle, libc::F_GETFD) };
|
||||
if flags == -1 {
|
||||
bail!(
|
||||
@ -104,7 +104,7 @@ impl OwnedHandle {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn non_atomic_dup(fd: RawFd) -> Fallible<Self> {
|
||||
fn non_atomic_dup(fd: RawFd) -> anyhow::Result<Self> {
|
||||
let duped = unsafe { libc::dup(fd) };
|
||||
if duped == -1 {
|
||||
bail!(
|
||||
@ -126,7 +126,7 @@ impl OwnedHandle {
|
||||
pub(crate) fn dup_impl<F: AsRawFileDescriptor>(
|
||||
fd: &F,
|
||||
handle_type: HandleType,
|
||||
) -> Fallible<Self> {
|
||||
) -> anyhow::Result<Self> {
|
||||
let fd = fd.as_raw_file_descriptor();
|
||||
let duped = unsafe { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, 0) };
|
||||
if duped == -1 {
|
||||
@ -198,7 +198,7 @@ impl FromRawFd for FileDescriptor {
|
||||
|
||||
impl FileDescriptor {
|
||||
#[inline]
|
||||
pub(crate) fn as_stdio_impl(&self) -> Fallible<std::process::Stdio> {
|
||||
pub(crate) fn as_stdio_impl(&self) -> anyhow::Result<std::process::Stdio> {
|
||||
let duped = OwnedHandle::dup(self)?;
|
||||
let fd = duped.into_raw_fd();
|
||||
let stdio = unsafe { std::process::Stdio::from_raw_fd(fd) };
|
||||
@ -208,7 +208,7 @@ impl FileDescriptor {
|
||||
|
||||
impl Pipe {
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn new() -> Fallible<Pipe> {
|
||||
pub fn new() -> anyhow::Result<Pipe> {
|
||||
let mut fds = [-1i32; 2];
|
||||
let res = unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) };
|
||||
if res == -1 {
|
||||
@ -234,7 +234,7 @@ impl Pipe {
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub fn new() -> Fallible<Pipe> {
|
||||
pub fn new() -> anyhow::Result<Pipe> {
|
||||
let mut fds = [-1i32; 2];
|
||||
let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
|
||||
if res == -1 {
|
||||
@ -264,7 +264,7 @@ impl Pipe {
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[doc(hidden)]
|
||||
pub fn socketpair_impl() -> Fallible<(FileDescriptor, FileDescriptor)> {
|
||||
pub fn socketpair_impl() -> anyhow::Result<(FileDescriptor, FileDescriptor)> {
|
||||
let mut fds = [-1i32; 2];
|
||||
let res = unsafe {
|
||||
libc::socketpair(
|
||||
@ -298,7 +298,7 @@ pub fn socketpair_impl() -> Fallible<(FileDescriptor, FileDescriptor)> {
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
#[doc(hidden)]
|
||||
pub fn socketpair_impl() -> Fallible<(FileDescriptor, FileDescriptor)> {
|
||||
pub fn socketpair_impl() -> anyhow::Result<(FileDescriptor, FileDescriptor)> {
|
||||
let mut fds = [-1i32; 2];
|
||||
let res = unsafe { libc::socketpair(libc::PF_LOCAL, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
|
||||
if res == -1 {
|
||||
@ -330,7 +330,7 @@ use std::time::Duration;
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[doc(hidden)]
|
||||
pub fn poll_impl(pfd: &mut [pollfd], duration: Option<Duration>) -> Fallible<usize> {
|
||||
pub fn poll_impl(pfd: &mut [pollfd], duration: Option<Duration>) -> anyhow::Result<usize> {
|
||||
let poll_result = unsafe {
|
||||
libc::poll(
|
||||
pfd.as_mut_ptr(),
|
||||
@ -359,9 +359,9 @@ mod macos {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn check_fd(fd: RawFd) -> Fallible<()> {
|
||||
failure::ensure!(fd >= 0, "illegal fd value");
|
||||
failure::ensure!(
|
||||
fn check_fd(fd: RawFd) -> anyhow::Result<()> {
|
||||
anyhow::ensure!(fd >= 0, "illegal fd value");
|
||||
anyhow::ensure!(
|
||||
(fd as usize) < FD_SETSIZE,
|
||||
"fd value is too large to use with select(2) on macos"
|
||||
);
|
||||
@ -377,7 +377,7 @@ mod macos {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, fd: RawFd) -> Fallible<()> {
|
||||
pub fn add(&mut self, fd: RawFd) -> anyhow::Result<()> {
|
||||
check_fd(fd)?;
|
||||
unsafe {
|
||||
FD_SET(fd, &mut self.set);
|
||||
@ -405,7 +405,7 @@ mod macos {
|
||||
set.as_mut().map(|s| s.contains(fd)).unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn poll_impl(pfd: &mut [pollfd], duration: Option<Duration>) -> Fallible<usize> {
|
||||
pub fn poll_impl(pfd: &mut [pollfd], duration: Option<Duration>) -> anyhow::Result<usize> {
|
||||
let mut read_set = None;
|
||||
let mut write_set = None;
|
||||
let mut exception_set = None;
|
||||
|
@ -2,7 +2,7 @@ use crate::{
|
||||
AsRawFileDescriptor, AsRawSocketDescriptor, FileDescriptor, FromRawFileDescriptor,
|
||||
FromRawSocketDescriptor, IntoRawFileDescriptor, IntoRawSocketDescriptor, OwnedHandle, Pipe,
|
||||
};
|
||||
use failure::{bail, Fallible};
|
||||
use anyhow::bail;
|
||||
use std::io::{self, Error as IoError};
|
||||
use std::os::windows::prelude::*;
|
||||
use std::ptr;
|
||||
@ -155,7 +155,7 @@ impl OwnedHandle {
|
||||
pub(crate) fn dup_impl<F: AsRawFileDescriptor>(
|
||||
f: &F,
|
||||
handle_type: HandleType,
|
||||
) -> Fallible<Self> {
|
||||
) -> anyhow::Result<Self> {
|
||||
let handle = f.as_raw_file_descriptor();
|
||||
if handle == INVALID_HANDLE_VALUE as _ || handle.is_null() {
|
||||
return Ok(OwnedHandle {
|
||||
@ -206,7 +206,7 @@ impl IntoRawHandle for OwnedHandle {
|
||||
|
||||
impl FileDescriptor {
|
||||
#[inline]
|
||||
pub(crate) fn as_stdio_impl(&self) -> Fallible<std::process::Stdio> {
|
||||
pub(crate) fn as_stdio_impl(&self) -> anyhow::Result<std::process::Stdio> {
|
||||
let duped = self.handle.try_clone()?;
|
||||
let handle = duped.into_raw_handle();
|
||||
let stdio = unsafe { std::process::Stdio::from_raw_handle(handle) };
|
||||
@ -307,7 +307,7 @@ impl io::Write for FileDescriptor {
|
||||
}
|
||||
|
||||
impl Pipe {
|
||||
pub fn new() -> Fallible<Pipe> {
|
||||
pub fn new() -> anyhow::Result<Pipe> {
|
||||
let mut sa = SECURITY_ATTRIBUTES {
|
||||
nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
|
||||
lpSecurityDescriptor: ptr::null_mut(),
|
||||
@ -347,7 +347,7 @@ fn init_winsock() {
|
||||
});
|
||||
}
|
||||
|
||||
fn socket(af: i32, sock_type: i32, proto: i32) -> Fallible<FileDescriptor> {
|
||||
fn socket(af: i32, sock_type: i32, proto: i32) -> anyhow::Result<FileDescriptor> {
|
||||
let s = unsafe {
|
||||
WSASocketW(
|
||||
af,
|
||||
@ -370,7 +370,7 @@ fn socket(af: i32, sock_type: i32, proto: i32) -> Fallible<FileDescriptor> {
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn socketpair_impl() -> Fallible<(FileDescriptor, FileDescriptor)> {
|
||||
pub fn socketpair_impl() -> anyhow::Result<(FileDescriptor, FileDescriptor)> {
|
||||
init_winsock();
|
||||
|
||||
let s = socket(AF_INET, SOCK_STREAM, 0)?;
|
||||
@ -439,7 +439,7 @@ pub fn socketpair_impl() -> Fallible<(FileDescriptor, FileDescriptor)> {
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn poll_impl(pfd: &mut [pollfd], duration: Option<Duration>) -> Fallible<usize> {
|
||||
pub fn poll_impl(pfd: &mut [pollfd], duration: Option<Duration>) -> anyhow::Result<usize> {
|
||||
let poll_result = unsafe {
|
||||
WSAPoll(
|
||||
pfd.as_mut_ptr(),
|
||||
|
Loading…
Reference in New Issue
Block a user