#[cfg(uds_supported)]
use super::c_wrappers;
use super::{OwnedReadHalf, ReuniteError, UdStream};
use crate::os::unix::imports::*;
use std::{
io,
net::Shutdown,
pin::Pin,
task::{Context, Poll},
};
#[derive(Debug)]
pub struct BorrowedWriteHalf<'a>(pub(super) TokioUdStreamWriteHalf<'a>);
impl<'a> BorrowedWriteHalf<'a> {
#[cfg(any(doc, uds_peercred))]
#[cfg_attr( // uds_peercred template
feature = "doc_cfg",
doc(cfg(any(
all(
target_os = "linux",
any(
target_env = "gnu",
target_env = "musl",
target_env = "musleabi",
target_env = "musleabihf"
)
),
target_os = "emscripten",
target_os = "redox",
target_os = "haiku"
)))
)]
pub fn get_peer_credentials(&self) -> io::Result<ucred> {
c_wrappers::get_peer_ucred(self.as_stream_raw_fd().as_ref())
}
pub fn shutdown(&self) -> io::Result<()> {
c_wrappers::shutdown(self.as_stream_raw_fd().as_ref(), Shutdown::Write)
}
fn as_stream_raw_fd(&self) -> c_int {
let stream: &TokioUdStream = self.0.as_ref();
stream.as_raw_fd()
}
fn pinproject(self: Pin<&mut Self>) -> Pin<&mut TokioUdStreamWriteHalf<'a>> {
Pin::new(&mut self.get_mut().0)
}
tokio_wrapper_conversion_methods!(tokio_norawfd TokioUdStreamWriteHalf<'a>);
}
#[cfg(feature = "tokio_support")]
impl TokioAsyncWrite for BorrowedWriteHalf<'_> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
self.pinproject().poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.pinproject().poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.pinproject().poll_shutdown(cx)
}
}
#[cfg(feature = "tokio_support")]
impl FuturesAsyncWrite for BorrowedWriteHalf<'_> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
self.pinproject().poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.pinproject().poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.shutdown()?;
Poll::Ready(Ok(()))
}
}
tokio_wrapper_trait_impls!(
for BorrowedWriteHalf<'a>, tokio_norawfd_lt 'a TokioUdStreamWriteHalf<'a>);
#[derive(Debug)]
pub struct OwnedWriteHalf(pub(super) TokioUdStreamOwnedWriteHalf);
impl OwnedWriteHalf {
pub fn reunite_with(self, read: OwnedReadHalf) -> Result<UdStream, ReuniteError> {
UdStream::reunite(read, self)
}
#[cfg(any(doc, uds_peercred))]
#[cfg_attr( // uds_peercred template
feature = "doc_cfg",
doc(cfg(any(
all(
target_os = "linux",
any(
target_env = "gnu",
target_env = "musl",
target_env = "musleabi",
target_env = "musleabihf"
)
),
target_os = "emscripten",
target_os = "redox",
target_os = "haiku"
)))
)]
pub fn get_peer_credentials(&self) -> io::Result<ucred> {
c_wrappers::get_peer_ucred(self.as_stream_raw_fd().as_ref())
}
pub fn shutdown(&self) -> io::Result<()> {
c_wrappers::shutdown(self.as_stream_raw_fd().as_ref(), Shutdown::Write)
}
fn as_stream_raw_fd(&self) -> c_int {
let stream: &TokioUdStream = self.0.as_ref();
stream.as_raw_fd()
}
fn pinproject(self: Pin<&mut Self>) -> Pin<&mut TokioUdStreamOwnedWriteHalf> {
Pin::new(&mut self.get_mut().0)
}
tokio_wrapper_conversion_methods!(tokio_norawfd TokioUdStreamOwnedWriteHalf);
}
#[cfg(feature = "tokio_support")]
impl TokioAsyncWrite for OwnedWriteHalf {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
self.pinproject().poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.pinproject().poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.shutdown()?;
Poll::Ready(Ok(()))
}
}
#[cfg(feature = "tokio_support")]
impl FuturesAsyncWrite for OwnedWriteHalf {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
self.pinproject().poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.pinproject().poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
self.shutdown()?;
Poll::Ready(Ok(()))
}
}
tokio_wrapper_trait_impls!(
for OwnedWriteHalf, tokio_norawfd TokioUdStreamOwnedWriteHalf);