Struct SocketAncillary

Source
pub struct SocketAncillary<'a> { /* private fields */ }
๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)
Available on (Android or Linux) and Unix only.
Expand description

A Unix socket Ancillary data struct.

ยงExample

#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut fds = [0; 8];
    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;

    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }
    Ok(())
}

Implementationsยง

Sourceยง

impl<'a> SocketAncillary<'a>

Source

pub fn new(buffer: &'a mut [u8]) -> Self

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Creates an ancillary data with the given buffer.

ยงExample
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::SocketAncillary;
let mut ancillary_buffer = [0; 128];
let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
Source

pub fn capacity(&self) -> usize

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Returns the capacity of the buffer.

Source

pub fn is_empty(&self) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Returns true if the ancillary data is empty.

Source

pub fn len(&self) -> usize

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Returns the number of used bytes.

Source

pub fn messages(&self) -> Messages<'_> โ“˜

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Returns the iterator of the control messages.

Source

pub fn truncated(&self) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Is true if during a recv operation the ancillary was truncated.

ยงExample
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;

    println!("Is truncated: {}", ancillary.truncated());
    Ok(())
}
Source

pub fn add_fds(&mut self, fds: &[RawFd]) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Add file descriptors to the ancillary data.

The function returns true if there was enough space in the buffer. If there was not enough space then no file descriptors was appended. Technically, that means this operation adds a control message with the level SOL_SOCKET and type SCM_RIGHTS.

ยงExample
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary};
use std::os::unix::io::AsRawFd;
use std::io::IoSlice;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
    ancillary.add_fds(&[sock.as_raw_fd()][..]);

    let buf = [1; 8];
    let mut bufs = &mut [IoSlice::new(&buf[..])][..];
    sock.send_vectored_with_ancillary(bufs, &mut ancillary)?;
    Ok(())
}
Source

pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Add credentials to the ancillary data.

The function returns true if there is enough space in the buffer. If there is not enough space then no credentials will be appended. Technically, that means this operation adds a control message with the level SOL_SOCKET and type SCM_CREDENTIALS, SCM_CREDS, or SCM_CREDS2.

Source

pub fn clear(&mut self)

๐Ÿ”ฌThis is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

Clears the ancillary data, removing all values.

ยงExample
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut fds1 = [0; 8];
    let mut fds2 = [0; 8];
    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];

    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }

    ancillary.clear();

    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }
    Ok(())
}

Trait Implementationsยง

Sourceยง

impl<'a> Debug for SocketAncillary<'a>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.