Struct std::bytes::Bytes

Overview

A vector of bytes.

Methods

fn new() -> Bytes

Construct a new byte array.

Examples

let bytes = Bytes::new();
assert_eq!(bytes, b"");
fn with_capacity(capacity: u64) -> Bytes

Construct a byte array with the given preallocated capacity.

Examples

let bytes = Bytes::with_capacity(32);
assert_eq!(bytes, b"");
bytes.extend(b"abcd");
assert_eq!(bytes, b"abcd");
fn from_vec(bytes: Vec) -> Bytes

Convert a byte array into bytes.

Examples

let bytes = Bytes::from_vec([b'a', b'b', b'c', b'd']);
assert_eq!(bytes, b"abcd");
fn into_vec(self) -> Vec

Convert the byte array into a vector of bytes.

Examples

let bytes = b"abcd";
assert_eq!([b'a', b'b', b'c', b'd'], bytes.into_vec());

assert!(!is_readable(bytes));
fn as_vec(self) -> Vec

Convert the byte array into a vector of bytes without consuming it.

Examples

let bytes = b"abcd";
assert_eq!([b'a', b'b', b'c', b'd'], bytes.as_vec());

assert!(is_readable(bytes));
fn extend(self, other: Bytes)

Extend these bytes with another collection of bytes.

Examples

let bytes = b"abcd";
bytes.extend(b"efgh");
assert_eq!(bytes, b"abcdefgh");

Extend this bytes collection with a string.

Examples

let bytes = b"abcd";
bytes.extend_str("efgh");
assert_eq!(bytes, b"abcdefgh");
fn pop(self) -> Option

Pop the last byte.

Examples

let bytes = b"abcd";
assert_eq!(bytes.pop(), Some(b'd'));
assert_eq!(bytes, b"abc");
fn last(self) -> Option

Get the last byte.

Examples

let bytes = b"abcd";
assert_eq!(bytes.last(), Some(b'd'));
fn len(self) -> u64

Get the length of the bytes collection.

Examples

let bytes = Bytes::new();
assert_eq!(bytes.len(), 0);
bytes.extend(b"abcd");
assert_eq!(bytes.len(), 4);
fn is_empty(self) -> bool

Test if the collection is empty.

Examples

let bytes = Bytes::new();
assert!(bytes.is_empty());
fn capacity(self) -> u64

Returns the total number of elements the vector can hold without reallocating.

Examples

let bytes = Bytes::with_capacity(10);
bytes.extend(b"abc");
assert!(bytes.capacity() >= 10);
fn clear(self)

Clears the vector, removing all values.

Note that this method has no effect on the allocated capacity of the vector.

Examples

let bytes = b"abc";
bytes.clear();
assert!(bytes.is_empty());
fn reserve(self, additional: u64)

Reserves capacity for at least additional more elements to be inserted in the given Bytes. The collection may reserve more space to speculatively avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

Panics

Panics if the new capacity exceeds isize::MAX bytes.

Examples

let vec = b"a";
vec.reserve(10);
assert!(vec.capacity() >= 11);
fn reserve_exact(self, additional: u64)

Reserves the minimum capacity for at least additional more elements to be inserted in the given Bytes. Unlike reserve, this will not deliberately over-allocate to speculatively avoid frequent allocations. After calling reserve_exact, capacity will be greater than or equal to self.len() + additional. Does nothing if the capacity is already sufficient.

Note that the allocator may give the collection more space than it requests. Therefore, capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.

Panics

Panics if the new capacity exceeds isize::MAX bytes.

Examples

let vec = b"a";
vec.reserve_exact(10);
assert!(vec.capacity() >= 11);

Shrinks the capacity of the byte array as much as possible.

It will drop down as close as possible to the length but the allocator may still inform the byte array that there is space for a few more elements.

Examples

let bytes = Bytes::with_capacity(10);
bytes.extend(b"abc");
assert!(bytes.capacity() >= 10);
bytes.shrink_to_fit();
assert!(bytes.capacity() >= 3);

Trait Implementations

impl Clone for Bytes
fn clone(value: any) -> any

Clone the specified value.

Examples

let a = 42;
let b = a;
let c = a.clone();

a += 1;

assert_eq!(a, 43);
assert_eq!(b, 42);
assert_eq!(c, 42);
impl PartialEq for Bytes
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for Bytes
impl PartialOrd for Bytes
fn partial_cmp(value: any, value1: any) -> Option

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.partial_cmp(2), Some(Ordering::Less));
assert_eq!(2.partial_cmp(2), Some(Ordering::Equal));
assert_eq!(2.partial_cmp(1), Some(Ordering::Greater));
fn lt(value: any, value1: any) -> bool

Tests less than (for self and other) and is used by the < operator.

Examples

assert_eq!(1.0 < 1.0, false);
assert_eq!(1.0 < 2.0, true);
assert_eq!(2.0 < 1.0, false);
fn le(value: any, value1: any) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator.

Examples

assert_eq!(1.0 <= 1.0, true);
assert_eq!(1.0 <= 2.0, true);
assert_eq!(2.0 <= 1.0, false);
fn gt(value: any, value1: any) -> bool

Tests greater than (for self and other) and is used by the > operator.

Examples

assert_eq!(1.0 > 1.0, false);
assert_eq!(1.0 > 2.0, false);
assert_eq!(2.0 > 1.0, true);
fn ge(value: any, value1: any) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator.

Examples

assert_eq!(1.0 >= 1.0, true);
assert_eq!(1.0 >= 2.0, false);
assert_eq!(2.0 >= 1.0, true);
impl Ord for Bytes
fn cmp(value: any, value1: any) -> Ordering

Compare two values.

Examples

use std::cmp::Ordering;

assert_eq!(1.cmp(2), Ordering::Less);
assert_eq!(2.cmp(2), Ordering::Equal);
assert_eq!(2.cmp(1), Ordering::Greater);
fn min(value: any, value1: any) -> Ordering

Return the minimum of two values.

Examples

assert_eq!(1.min(2), 1);
assert_eq!(2.min(2), 2);
assert_eq!(2.min(1), 1);
fn max(value: any, value1: any) -> Ordering

Return the maximum of two values.

Examples

assert_eq!(1.max(2), 2);
assert_eq!(2.max(2), 2);
assert_eq!(2.max(1), 2);

Protocols

protocol CLONE
let $out = clone(value)

Clone the byte array.

Examples

let a = b"hello world";
let b = a.clone();

a.extend(b"!");

assert_eq!(a, b"hello world!");
assert_eq!(b, b"hello world");
protocol PARTIAL_EQ
if value == b { }

Test two byte arrays for partial equality.

Examples

use std::ops::partial_eq;

assert_eq!(partial_eq(b"a", b"a"), true);
assert_eq!(partial_eq(b"a", b"ab"), false);
assert_eq!(partial_eq(b"ab", b"a"), false);
protocol EQ
if value == b { }

Test two byte arrays for total equality.

Examples

use std::ops::eq;

assert_eq!(eq(b"a", b"a"), true);
assert_eq!(eq(b"a", b"ab"), false);
assert_eq!(eq(b"ab", b"a"), false);
protocol PARTIAL_CMP
if value < b { }

Perform a partial ordered comparison between two byte arrays.

Examples

assert!(b"a" < b"ab");
assert!(b"ab" > b"a");
assert!(b"a" == b"a");

Using explicit functions:

use std::cmp::Ordering;
use std::ops::partial_cmp;

assert_eq!(partial_cmp(b"a", b"ab"), Some(Ordering::Less));
assert_eq!(partial_cmp(b"ab", b"a"), Some(Ordering::Greater));
assert_eq!(partial_cmp(b"a", b"a"), Some(Ordering::Equal));
protocol CMP
if value < b { }

Perform a totally ordered comparison between two byte arrays.

Examples

use std::cmp::Ordering;
use std::ops::cmp;

assert_eq!(cmp(b"a", b"ab"), Ordering::Less);
assert_eq!(cmp(b"ab", b"a"), Ordering::Greater);
assert_eq!(cmp(b"a", b"a"), Ordering::Equal);
protocol HASH
let $out = hash(value)

Hash the byte array.

Examples

use std::ops::hash;

let a = "hello";
let b = "hello";

assert_eq!(hash(a), hash(b));
protocol DEBUG_FMT
format!("{:?}", value)

Write a debug representation of a byte array.

Examples

println!("{:?}", b"Hello");
protocol LT
if $a < $b { }

The protocol behind the < operator.

protocol LE
if $a <= $b { }

The protocol behind the <= operator.

protocol GT
if $a > $b { }

The protocol behind the > operator.

protocol GE
if $a >= $b { }

The protocol behind the >= operator.

protocol MIN
$a.min($b)

The implementation protocol for the PartialOrd::min method.

protocol MAX
$a.max($b)

The implementation protocol for the PartialOrd::max method.