#![cfg_attr(docsrs, feature(doc_cfg))]
use std::io::{Error, Result, Write};
pub mod fs;
pub trait Close: Write {
fn close(self) -> Result<()>;
}
macro_rules! unix_impl_close_raw_fd {
($ty:ty, "std" $(,$lt:lifetime)* $(,$id:ident)*) => {
unix_impl_close_raw_fd!($ty, "unix" $(,$lt)* $(,$id)*);
};
($ty:ty, $ft_fm:literal $(,$lt:lifetime)* $(,$id:ident)*) => {
#[cfg(unix)]
#[cfg(any(feature = $ft_fm, target_family = $ft_fm))]
#[cfg_attr(all(docsrs, feature = $ft_fm), doc(cfg(feature = $ft_fm)))]
impl<$($lt,)* $($id,)*> Close for $ty {
fn close(mut self) -> Result<()> {
use std::io::ErrorKind;
use std::os::unix::io::IntoRawFd;
self.flush()?;
let fd = self.into_raw_fd();
let rv = unsafe { libc::close(fd) };
if rv != -1 {
Ok(())
} else {
match Error::last_os_error() {
e if e.kind() == ErrorKind::Interrupted => Ok(()),
e => Err(e),
}
}
}
}
};
}
macro_rules! windows_impl_close_raw_handle {
($ty:ty, "std" $(,$lt:lifetime)* $(,$id:ident)*) => {
windows_impl_close_raw_handle!($ty, "windows" $(,$lt)* $(,$id)*);
};
($ty:ty, $ft_fm:literal $(,$lt:lifetime)* $(,$id:ident)*) => {
#[cfg(windows)]
#[cfg(any(feature = $ft_fm, target_family = $ft_fm))]
#[cfg_attr(all(docsrs, feature = $ft_fm), doc(cfg(feature = $ft_fm)))]
impl<$($lt,)* $($id,)*> Close for $ty {
fn close(mut self) -> Result<()> {
use std::os::windows::io::IntoRawHandle;
use winapi::um::handleapi;
self.flush()?;
let handle = self.into_raw_handle();
let rv = unsafe { handleapi::CloseHandle(handle) };
if rv != 0 {
Ok(())
} else {
Err(Error::last_os_error())
}
}
}
};
}
macro_rules! windows_impl_close_raw_socket {
($ty:ty, "std" $(,$lt:lifetime)* $(,$id:ident)*) => {
windows_impl_close_raw_socket!($ty, "windows" $(,$lt)* $(,$id)*);
};
($ty:ty, $ft_fm:literal $(,$lt:lifetime)* $(,$id:ident)*) => {
#[cfg(windows)]
#[cfg(any(feature = $ft_fm, target_family = $ft_fm))]
#[cfg_attr(all(docsrs, feature = $ft_fm), doc(cfg(feature = $ft_fm)))]
impl<$($lt,)* $($id,)*> Close for $ty {
fn close(mut self) -> Result<()> {
use std::convert::TryInto;
use std::os::windows::io::IntoRawSocket;
use winapi::um::winsock2;
self.flush()?;
let socket = self.into_raw_socket().try_into().unwrap();
let rv = unsafe { winsock2::closesocket(socket) };
if rv == 0 {
Ok(())
} else {
Err(Error::from_raw_os_error(unsafe {
winsock2::WSAGetLastError()
}))
}
}
}
};
}
macro_rules! impl_close_no_error_no_flush {
($ty:ty, "std" $(,$lt:lifetime)* $(,$id:ident)*) => {
#[cfg(unix)]
impl_close_no_error_no_flush!($ty, "unix" $(,$lt)* $(,$id)*);
#[cfg(windows)]
impl_close_no_error_no_flush!($ty, "windows" $(,$lt)* $(,$id)*);
};
($ty:ty, $ft_fm:literal $(,$lt:lifetime)* $(,$id:ident)*) => {
#[cfg(any(unix, windows))]
#[cfg(any(feature = $ft_fm, target_family = $ft_fm))]
#[cfg_attr(all(docsrs, feature = $ft_fm), doc(cfg(feature = $ft_fm)))]
impl<$($lt,)* $($id,)*> Close for $ty {
#[inline]
fn close(self) -> Result<()> {
Ok(())
}
}
};
}
macro_rules! impl_close_into_inner {
($ty:ty, "std" $(,$lt:lifetime)* $(,$id:ident)*) => {
#[cfg(unix)]
impl_close_into_inner!($ty, "unix" $(,$lt)* $(,$id)*);
#[cfg(windows)]
impl_close_into_inner!($ty, "windows" $(,$lt)* $(,$id)*);
};
($ty:ty, $ft_fm:literal $(,$lt:lifetime)* $(,$id:ident)*) => {
#[cfg(any(unix, windows))]
#[cfg(any(feature = $ft_fm, target_family = $ft_fm))]
#[cfg_attr(all(docsrs, feature = $ft_fm), doc(cfg(feature = $ft_fm)))]
impl<$($lt,)* W: Close, $($id,)*> Close for $ty {
fn close(self) -> Result<()> {
self.into_inner()?.close()
}
}
};
}
unix_impl_close_raw_fd!(std::fs::File, "std");
unix_impl_close_raw_fd!(std::net::TcpStream, "std");
unix_impl_close_raw_fd!(std::os::unix::net::UnixStream, "std");
unix_impl_close_raw_fd!(std::process::ChildStdin, "std");
unix_impl_close_raw_fd!(os_pipe::PipeWriter, "os_pipe");
windows_impl_close_raw_handle!(std::fs::File, "std");
windows_impl_close_raw_socket!(std::net::TcpStream, "std");
windows_impl_close_raw_handle!(std::process::ChildStdin, "std");
windows_impl_close_raw_handle!(os_pipe::PipeWriter, "os_pipe");
impl_close_no_error_no_flush!(&mut [u8], "std");
impl_close_no_error_no_flush!(std::io::Cursor<&mut Vec<u8>>, "std");
impl_close_no_error_no_flush!(std::io::Cursor<&mut [u8]>, "std");
impl_close_no_error_no_flush!(std::io::Cursor<Box<[u8]>>, "std");
impl_close_no_error_no_flush!(std::io::Cursor<Vec<u8>>, "std");
impl_close_no_error_no_flush!(std::io::Sink, "std");
impl_close_no_error_no_flush!(Vec<u8>, "std");
impl_close_into_inner!(std::io::BufWriter<W>, "std");
impl_close_into_inner!(std::io::LineWriter<W>, "std");