当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust SocketAncillary.clear用法及代码示例


本文简要介绍rust语言中 std::os::unix::net::SocketAncillary.clear 的用法。

用法

pub fn clear(&mut self)

清除辅助数据,删除所有值。

示例

#![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(())
}

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 std::os::unix::net::SocketAncillary.clear。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。