pub struct SocketAncillary<'a> { /* private fields */ }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(())
}RunImplementations
impl<'a> SocketAncillary<'a>
source
impl<'a> SocketAncillary<'a>
sourcepub fn new(buffer: &'a mut [u8]) -> Self
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn new(buffer: &'a mut [u8]) -> Self
sourcepub fn capacity(&self) -> usize
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn capacity(&self) -> usize
sourceReturns the capacity of the buffer.
pub fn is_empty(&self) -> bool
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn is_empty(&self) -> bool
sourceReturns true if the ancillary data is empty.
pub fn len(&self) -> usize
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn len(&self) -> usize
sourceReturns the number of used bytes.
pub fn messages(&self) -> Messages<'_>ⓘNotable traits for Messages<'a>impl<'a> Iterator for Messages<'a> type Item = Result<AncillaryData<'a>, AncillaryError>;
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn messages(&self) -> Messages<'_>ⓘNotable traits for Messages<'a>impl<'a> Iterator for Messages<'a> type Item = Result<AncillaryData<'a>, AncillaryError>;
sourceReturns the iterator of the control messages.
pub fn truncated(&self) -> bool
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn truncated(&self) -> bool
sourceIs 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(())
}Runpub fn add_fds(&mut self, fds: &[RawFd]) -> bool
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn add_fds(&mut self, fds: &[RawFd]) -> bool
sourceAdd 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 mut buf = [1; 8];
let mut bufs = &mut [IoSlice::new(&mut buf[..])][..];
sock.send_vectored_with_ancillary(bufs, &mut ancillary)?;
Ok(())
}Runpub fn add_creds(&mut self, creds: &[SocketCred]) -> bool
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool
sourceAdd credentials to the ancillary data.
The function returns true if there was enough space in the buffer.
If there was not enough space then no credentials was appended.
Technically, that means this operation adds a control message with the level SOL_SOCKET
and type SCM_CREDENTIALS or SCM_CREDS.
pub fn clear(&mut self)
source Available on Android or DragonFly BSD or Emscripten or FreeBSD or Linux or NetBSD or OpenBSD only.
pub fn clear(&mut self)
sourceClears 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(())
}RunTrait Implementations
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
impl<T> BorrowMut<T> for T where
T: ?Sized,
source
impl<T> BorrowMut<T> for T where
T: ?Sized,
sourcefn borrow_mut(&mut self) -> &mut T
const: unstable · source
fn borrow_mut(&mut self) -> &mut T
const: unstable · sourceMutably borrows from an owned value. Read more