Skip to main content

AsyncWriteAncillary

Trait AsyncWriteAncillary 

pub trait AsyncWriteAncillary {
    // Required methods
    async fn write_with_ancillary<T, C>(
        &mut self,
        buffer: T,
        control: C,
    ) -> BufResult<usize, (T, C)>
       where T: IoBuf,
             C: IoBuf;
    async fn write_vectored_with_ancillary<T, C>(
        &mut self,
        buffer: T,
        control: C,
    ) -> BufResult<usize, (T, C)>
       where T: IoVectoredBuf,
             C: IoBuf;
}
Available on crate features io and ancillary only.
Expand description

Trait for asynchronous write with ancillary (control) data. Intended for connected stream sockets (TCP, Unix streams) where no destination address is needed.

Required Methods§

async fn write_with_ancillary<T, C>( &mut self, buffer: T, control: C, ) -> BufResult<usize, (T, C)>
where T: IoBuf, C: IoBuf,

Write data with ancillary data from an owned buffer.

async fn write_vectored_with_ancillary<T, C>( &mut self, buffer: T, control: C, ) -> BufResult<usize, (T, C)>
where T: IoVectoredBuf, C: IoBuf,

Write data with ancillary data from a vectored buffer.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

§

impl<A> AsyncWriteAncillary for &mut A

§

async fn write_with_ancillary<T, C>( &mut self, buffer: T, control: C, ) -> BufResult<usize, (T, C)>
where T: IoBuf, C: IoBuf,

§

async fn write_vectored_with_ancillary<T, C>( &mut self, buffer: T, control: C, ) -> BufResult<usize, (T, C)>
where T: IoVectoredBuf, C: IoBuf,

Implementors§

Source§

impl AsyncWriteAncillary for &TcpStream

Source§

impl AsyncWriteAncillary for &UnixStream

Source§

impl AsyncWriteAncillary for TcpStream

Source§

impl AsyncWriteAncillary for UnixStream

§Example

Send and receive a file descriptor over a Unix socket pair using SCM_RIGHTS:

use std::os::unix::io::RawFd;

use compio_io::ancillary::*;
use compio_net::UnixStream;

const BUF_SIZE: usize = ancillary_space::<RawFd>();

// Create a socket pair.
let (std_a, std_b) = std::os::unix::net::UnixStream::pair().unwrap();
let mut a = UnixStream::from_std(std_a).unwrap();
let mut b = UnixStream::from_std(std_b).unwrap();

// Pass fd 0 (stdin) as ancillary data via SCM_RIGHTS.
let mut ctrl_send = AncillaryBuf::<BUF_SIZE>::new();
let mut builder = ctrl_send.builder();
builder
    .push(libc::SOL_SOCKET, libc::SCM_RIGHTS, &(0 as RawFd))
    .unwrap();

// Send the payload together with the ancillary data.
a.write_with_ancillary(b"hello", ctrl_send).await.0.unwrap();

// Receive on the other end.
let payload = Vec::with_capacity(5);
let ctrl_recv = AncillaryBuf::<BUF_SIZE>::new();
let ((_, ctrl_len), (payload, ctrl_recv)) =
    b.read_with_ancillary(payload, ctrl_recv).await.unwrap();

assert_eq!(&payload[..], b"hello");

// Parse the received ancillary messages.
let mut iter = unsafe { AncillaryIter::new(&ctrl_recv[..ctrl_len]) };
let msg = iter.next().unwrap();
assert_eq!(msg.level(), libc::SOL_SOCKET);
assert_eq!(msg.ty(), libc::SCM_RIGHTS);
// The kernel duplicates the fd, so the received value may differ.
let _received_fd = unsafe { msg.data::<RawFd>() };
assert!(iter.next().is_none());
§

impl<A, Alloc> AsyncWriteAncillary for Box<A, Alloc>
where A: AsyncWriteAncillary + ?Sized, Alloc: Allocator,