pub struct SocketAncillary<'a> { /* private fields */ }
🔬This is a nightly-only experimental API. (unix_socket_ancillary_data)
Available on 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]) -> SocketAncillary<'a>

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data)

Create 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)

Returns the capacity of the buffer.

source

pub fn is_empty(&self) -> bool

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data)

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)

Returns the number of used bytes.

source

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

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data)

Returns the iterator of the control messages.

source

pub fn truncated(&self) -> bool

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data)

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: &[i32]) -> bool

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data)

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)

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)

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<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for SocketAncillary<'a>

§

impl<'a> Send for SocketAncillary<'a>

§

impl<'a> Sync for SocketAncillary<'a>

§

impl<'a> Unpin for SocketAncillary<'a>

§

impl<'a> !UnwindSafe for SocketAncillary<'a>

Blanket Implementations§

source§

impl<T> Also for T

source§

fn also_mut<F: FnOnce(&mut Self)>(self, f: F) -> Self

Available on crate feature result only.
Applies a function which takes the parameter by exclusive reference, and then returns the (possibly) modified owned value. Read more
source§

fn also_ref<F: FnOnce(&Self)>(self, f: F) -> Self

Available on crate feature result only.
Applies a function which takes the parameter by shared reference, and then returns the (possibly) modified owned value. Read more
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> AnyExt for T
where T: Any,

source§

fn type_of(&self) -> TypeId

Available on crate feature any only.
Returns the TypeId of self. Read more
source§

fn type_name(&self) -> &'static str

Available on crate feature any only.
Returns the type name of self. Read more
source§

fn type_is<T: 'static>(&self) -> bool

Available on crate feature any only.
Returns true if Self is of type T. Read more
source§

fn as_any_ref(&self) -> &dyn Any
where Self: Sized,

Available on crate feature any only.
Upcasts &self as &dyn Any. Read more
source§

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Available on crate feature any only.
Upcasts &mut self as &mut dyn Any. Read more
source§

fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized,

Available on crate feature any only.
Upcasts Box<self> as Box<dyn Any>. Read more
source§

impl<T, Res> Apply<Res> for T
where T: ?Sized,

source§

fn apply<F: FnOnce(Self) -> Res>(self, f: F) -> Res
where Self: Sized,

Available on crate feature result only.
Apply a function which takes the parameter by value.
source§

fn apply_ref<F: FnOnce(&Self) -> Res>(&self, f: F) -> Res

Available on crate feature result only.
Apply a function which takes the parameter by shared reference.
source§

fn apply_mut<F: FnOnce(&mut Self) -> Res>(&mut self, f: F) -> Res

Available on crate feature result only.
Apply a function which takes the parameter by exclusive reference.
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> Mem for T
where T: ?Sized,

source§

const NEEDS_DROP: bool = _

Available on crate feature mem only.
Whether dropping values of this type matters.
source§

fn mem_needs_drop(&self) -> bool

Available on crate feature mem only.
Returns true if dropping values of this type matters.
source§

fn mem_drop(self)
where Self: Sized,

Available on crate feature mem only.
Drops self by running its destructor.
source§

fn mem_forget(self)
where Self: Sized,

Available on crate feature mem only.
Forgets about self without running its destructor.
source§

fn mem_replace(&mut self, other: Self) -> Self
where Self: Sized,

Available on crate feature mem only.
Replaces self with other, returning the previous value of self.
source§

fn mem_take(&mut self) -> Self
where Self: Default,

Available on crate feature mem only.
Replaces self with its default value, returning the previous value of self.
source§

fn mem_swap(&mut self, other: &mut Self)
where Self: Sized,

Available on crate feature mem only.
Swaps the value of self and other without deinitializing either one.
source§

fn mem_as_bytes(&self) -> &[u8]
where Self: Sync + Unpin,

Available on crate features mem and unsafe_mem only.
View a Sync + Unpin self as &[u8]. Read more
source§

fn mem_as_bytes_mut(&mut self) -> &mut [u8]
where Self: Sync + Unpin,

Available on crate features mem and unsafe_mem only.
View a Sync + Unpin self as &mut [u8].
source§

impl<T> Size for T

source§

const BYTE_ALIGN: usize = _

Available on crate feature mem only.
The alignment of this type in bytes.
source§

const BYTE_SIZE: usize = _

Available on crate feature mem only.
The size of this type in bytes.
source§

const PTR_SIZE: usize = 8usize

Available on crate feature mem only.
The size of a pointer in bytes, for the current platform.
source§

fn byte_align(&self) -> usize

Available on crate feature mem only.
Returns the alignment of this type in bytes.
source§

fn byte_size(&self) -> usize

Available on crate feature mem only.
Returns the size of this type in bytes. Read more
source§

fn ptr_ratio(&self) -> (usize, usize)

Available on crate feature mem only.
Returns the size ratio between PTR_SIZE and BYTE_SIZE. Read more
source§

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

§

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>,

§

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.