smol-bytes 0.1.7

High-performance, clone-efficient byte buffers optimized for small data.
Documentation
#![warn(rust_2018_idioms)]

use smol_bytes::{Buf, Bytes};
#[cfg(feature = "std")]
use std::io::IoSlice;

#[test]
fn collect_two_bufs() {
  let a = Bytes::from(&b"hello"[..]);
  let b = Bytes::from(&b"world"[..]);

  let res = a.chain(b).copy_to_bytes(10);
  assert_eq!(res, &b"helloworld"[..]);
}

#[test]
fn iterating_two_bufs() {
  let a = Bytes::from(&b"hello"[..]);
  let b = Bytes::from(&b"world"[..]);

  let res: Vec<u8> = a.chain(b).into_iter().collect();
  assert_eq!(res, &b"helloworld"[..]);
}

#[cfg(feature = "std")]
#[test]
fn vectored_read() {
  let a = Bytes::from(&b"hello"[..]);
  let b = Bytes::from(&b"world"[..]);

  let mut buf = a.chain(b);

  {
    let b1: &[u8] = &mut [];
    let b2: &[u8] = &mut [];
    let b3: &[u8] = &mut [];
    let b4: &[u8] = &mut [];
    let mut iovecs = [
      IoSlice::new(b1),
      IoSlice::new(b2),
      IoSlice::new(b3),
      IoSlice::new(b4),
    ];

    assert_eq!(2, buf.chunks_vectored(&mut iovecs));
    assert_eq!(iovecs[0][..], b"hello"[..]);
    assert_eq!(iovecs[1][..], b"world"[..]);
    assert_eq!(iovecs[2][..], b""[..]);
    assert_eq!(iovecs[3][..], b""[..]);
  }

  buf.advance(2);

  {
    let b1: &[u8] = &mut [];
    let b2: &[u8] = &mut [];
    let b3: &[u8] = &mut [];
    let b4: &[u8] = &mut [];
    let mut iovecs = [
      IoSlice::new(b1),
      IoSlice::new(b2),
      IoSlice::new(b3),
      IoSlice::new(b4),
    ];

    assert_eq!(2, buf.chunks_vectored(&mut iovecs));
    assert_eq!(iovecs[0][..], b"llo"[..]);
    assert_eq!(iovecs[1][..], b"world"[..]);
    assert_eq!(iovecs[2][..], b""[..]);
    assert_eq!(iovecs[3][..], b""[..]);
  }

  buf.advance(3);

  {
    let b1: &[u8] = &mut [];
    let b2: &[u8] = &mut [];
    let b3: &[u8] = &mut [];
    let b4: &[u8] = &mut [];
    let mut iovecs = [
      IoSlice::new(b1),
      IoSlice::new(b2),
      IoSlice::new(b3),
      IoSlice::new(b4),
    ];

    assert_eq!(1, buf.chunks_vectored(&mut iovecs));
    assert_eq!(iovecs[0][..], b"world"[..]);
    assert_eq!(iovecs[1][..], b""[..]);
    assert_eq!(iovecs[2][..], b""[..]);
    assert_eq!(iovecs[3][..], b""[..]);
  }

  buf.advance(3);

  {
    let b1: &[u8] = &mut [];
    let b2: &[u8] = &mut [];
    let b3: &[u8] = &mut [];
    let b4: &[u8] = &mut [];
    let mut iovecs = [
      IoSlice::new(b1),
      IoSlice::new(b2),
      IoSlice::new(b3),
      IoSlice::new(b4),
    ];

    assert_eq!(1, buf.chunks_vectored(&mut iovecs));
    assert_eq!(iovecs[0][..], b"ld"[..]);
    assert_eq!(iovecs[1][..], b""[..]);
    assert_eq!(iovecs[2][..], b""[..]);
    assert_eq!(iovecs[3][..], b""[..]);
  }
}

#[test]
fn chain_get_bytes() {
  let mut ab = Bytes::copy_from_slice(b"ab");
  let mut cd = Bytes::copy_from_slice(b"cd");
  let mut chain = (&mut ab).chain(&mut cd);
  let a = chain.copy_to_bytes(1);
  let bc = chain.copy_to_bytes(2);
  let d = chain.copy_to_bytes(1);

  assert_eq!(Bytes::copy_from_slice(b"a"), a);
  assert_eq!(Bytes::copy_from_slice(b"bc"), bc);
  assert_eq!(Bytes::copy_from_slice(b"d"), d);
}