Struct IoSlice

#[repr(transparent)]
pub struct IoSlice<'a>(pub(in ::io::io_slice) IoSlice<'a>);

A buffer type used with Write::write_vectored.

It is semantically a wrapper around a &[u8], but is guaranteed to be ABI compatible with the iovec type on Unix platforms and WSABUF on Windows.

Fields

0: IoSlice<'a>

Implementations

impl<'a> IoSlice<'a>

fn new(buf: &'a [u8]) -> IoSlice<'a>

Creates a new IoSlice wrapping a byte slice.

Panics

Panics on Windows if the slice is larger than 4GB.

fn advance(&mut self, n: usize)

Advance the internal cursor of the slice.

Also see IoSlice::advance_slices to advance the cursors of multiple buffers.

Panics

Panics when trying to advance beyond the end of the slice.

Examples

use std::io::IoSlice;
use std::ops::Deref;

let data = [1; 8];
let mut buf = IoSlice::new(&data);

// Mark 3 bytes as read.
buf.advance(3);
assert_eq!(buf.deref(), [1; 5].as_ref());
fn advance_slices(bufs: &mut &mut [IoSlice<'a>], n: usize)

Advance a slice of slices.

Shrinks the slice to remove any IoSlices that are fully advanced over. If the cursor ends up in the middle of an IoSlice, it is modified to start at that cursor.

For example, if we have a slice of two 8-byte IoSlices, and we advance by 10 bytes, the result will only include the second IoSlice, advanced by 2 bytes.

Panics

Panics when trying to advance beyond the end of the slices.

Examples

use std::io::IoSlice;
use std::ops::Deref;

let buf1 = [1; 8];
let buf2 = [2; 16];
let buf3 = [3; 8];
let mut bufs = &mut [
    IoSlice::new(&buf1),
    IoSlice::new(&buf2),
    IoSlice::new(&buf3),
][..];

// Mark 10 bytes as written.
IoSlice::advance_slices(&mut bufs, 10);
assert_eq!(bufs[0].deref(), [2; 14].as_ref());
assert_eq!(bufs[1].deref(), [3; 8].as_ref());
const fn as_slice(self) -> &'a [u8]

Get the underlying bytes as a slice with the original lifetime.

This doesn't borrow from self, so is less restrictive than calling .deref(), which does.

Examples

#![feature(io_slice_as_bytes)]
use std::io::IoSlice;

let data = b"abcdef";

let mut io_slice = IoSlice::new(data);
let tail = &io_slice.as_slice()[3..];

// This works because `tail` doesn't borrow `io_slice`
io_slice = IoSlice::new(tail);

assert_eq!(io_slice.as_slice(), b"def");

Trait Implementations

impl<'a> Clone for IoSlice<'a>

fn clone(&self) -> IoSlice<'a>

impl<'a> Copy for IoSlice<'a>

impl<'a> Debug for IoSlice<'a>

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

impl<'a> Deref for IoSlice<'a>

type Target = [u8];
fn deref(&self) -> &[u8]

impl<'a> Send for IoSlice<'a>

impl<'a> Sync for IoSlice<'a>

impl<'a> TrivialClone for IoSlice<'a>

Auto Trait Implementations

impl<'a> Freeze for IoSlice<'a>

impl<'a> RefUnwindSafe for IoSlice<'a>

impl<'a> Unpin for IoSlice<'a>

impl<'a> UnsafeUnpin for IoSlice<'a>

impl<'a> UnwindSafe for IoSlice<'a>

Blanket Implementations

impl<P, T> Receiver for IoSlice<'a> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for IoSlice<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for IoSlice<'a> where T: ?Sized,

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

impl<T> CloneToUninit for IoSlice<'a> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for IoSlice<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Printable for IoSlice<'a> where T: Copy + Debug,

impl<T> SizeHint for IoSlice<'a> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for IoSlice<'a>

impl<T, U> Into<U> for IoSlice<'a> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for IoSlice<'a> where U: Into<T>,

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

impl<T, U> TryInto<U> for IoSlice<'a> where U: TryFrom<T>,

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