pub struct Bytes {
/// A barebones struct for the bytes.
buf: RawBytes,
/// The number of bytes being stored.
len: u64,
}
Expand description
A type used to represent raw bytes. It has ownership over its buffer.
Fields
buf: RawBytes
A barebones struct for the bytes.
len: u64
The number of bytes being stored.
Implementations
fn new() -> Self
fn new() -> Self
Constructs a new, empty Bytes
.
Additional Information
The struct will not allocate until elements are pushed onto it.
Returns
- [Bytes] - A new, empty
Bytes
.
Examples
use std::bytes::Bytes;
fn foo() {
let bytes = Bytes::new();
assert(bytes.len() == 0);
assert(bytes.capacity() == 0);
}
fn with_capacity(capacity: u64) -> Self
fn with_capacity(capacity: u64) -> Self
Constructs a new, empty Bytes
with the specified capacity.
Additional Information
The Bytes
will be able to hold exactly capacity
bytes without
reallocating. If capacity
is zero, the Bytes
will not allocate.
It is important to note that although the returned Bytes
has the
capacity specified, the type will have a zero length.
Arguments
capacity
: [u64] - The capacity with which to initialize theBytes
.
Returns
- [Bytes] - A new, empty
Bytes
with the specified capacity.
Examples
use std::bytes::Bytes;
fn foo() {
let mut bytes = Bytes::with_capacity(2);
// does not allocate
bytes.push(5);
// does not re-allocate
bytes.push(10);
}
fn push(refmut self, byte: u8)
fn push(refmut self, byte: u8)
Appends an element to the back of a Bytes
collection.
Arguments
byte
: [u8] - The element to be pushed onto theBytes
.
Examples
use std::bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
let a = 5u8;
let b = 7u8;
bytes.push(a);
bytes.push(b);
assert(bytes.len() == 2);
}
fn pop(refmut self) -> Option<u8>
fn pop(refmut self) -> Option<u8>
Removes the last element from a Bytes
and returns it, or None
if it
is empty.
Returns
- [Option] - The last element of the
Bytes
, orNone
if it is empty.
Examples
use std::bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
let res = bytes.pop();
assert(res.is_none());
bytes.push(5);
let res = bytes.pop();
assert(res.unwrap() == 5);
assert(bytes.is_empty());
}
fn get(self, index: u64) -> Option<u8>
fn get(self, index: u64) -> Option<u8>
Returns Some(byte)
at index
, or None
if index
is out of
bounds.
Arguments
index
: [u64] - The index of the element to be returned.
Returns
- [Option] - The element at the specified index, or
None
if the index is out of bounds.
Examples
use std::bytes::Byte;
fn foo() {
let mut bytes = Bytes::new();
bytes.push(5u8);
bytes.push(10u8);
bytes.push(15u8);
let item = bytes.get(1).unwrap();
assert(item == 10u8);
let opt = bytes.get(10);
assert(opt.is_none()); // index out of bounds
}
fn set(
refmut self,
index: u64,
value: u8,
)
fn set(
refmut self,
index: u64,
value: u8,
)
Updates an element at position index
with a new element value
.
Arguments
index
: [u64] - The index of the element to be set.value
: [u8] - The value of the element to be set.
Reverts
- When
index
is greater than or equal to the length of Bytes.
Examples
use std::bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
let a = 5u8;
let b = 7u8;
let c = 9u8;
bytes.push(a);
bytes.push(b);
bytes.push(c);
let d = 11u8;
bytes.set(1, d);
assert(bytes.len() == 3);
assert(bytes.get(0).unwrap() == a);
assert(bytes.get(1).unwrap() == d);
assert(bytes.get(2).unwrap() == c);
}
fn insert(
refmut self,
index: u64,
element: u8,
)
fn insert(
refmut self,
index: u64,
element: u8,
)
Inserts an element at position index
within the Bytes, shifting all
elements after it to the right.
Arguments
index
: [u64] - The index at which to insert the element.element
: [u8] - The element to be inserted.
Reverts
- When
index > len
.
Examples
use std::bytes::Byte;
fn foo() {
let mut bytes = Bytes::new();
let a = 11u8;
let b = 11u8;
let c = 11u8;
let d = 11u8;
bytes.push(a);
bytes.push(b);
bytes.push(c);
bytes.insert(1, d);
assert(bytes.get(0).unwrap() == a);
assert(bytes.get(1).unwrap() == d);
assert(bytes.get(2).unwrap() == b);
assert(bytes.get(3).unwrap() == c);
}
fn remove(refmut self, index: u64) -> u8
fn remove(refmut self, index: u64) -> u8
Removes and returns the element at position index
within the Bytes,
shifting all elements after it to the left.
Arguments
index
: [u64] - The index of the element to be removed.
Returns
- [u8] - The element at the specified index.
Reverts
- When
index >= self.len
.
Examples
use std::bytes::Byte;
fn foo() {
let mut bytes = Byte::new();
bytes.push(5);
bytes.push(10);
bytes.push(15);
let item = bytes.remove(1);
assert(item == 10);
assert(bytes.get(0).unwrap() == 5);
assert(bytes.get(1).unwrap() == 15);
assert(bytes.get(2).is_none());
}
fn swap(
refmut self,
element1_index: u64,
element2_index: u64,
)
fn swap(
refmut self,
element1_index: u64,
element2_index: u64,
)
Swaps two elements.
Arguments
element1_index
: [u64] - The index of the first element.element2_index
: [u64] - The index of the second element.
Reverts
- When
element1_index
orelement2_index
is greater than or equal to the length ofBytes
.
Examples
use std::bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
let a = 5u8;
let b = 7u8;
let c = 9u8;
bytes.push(a);
bytes.push(b);
bytes.push(c);
bytes.swap(0, 1);
assert(bytes.get(0).unwrap() == b);
assert(bytes.get(1).unwrap() == a);
assert(bytes.get(2).unwrap() == c);
}
fn capacity(self) -> u64
fn capacity(self) -> u64
Gets the capacity of the allocation.
Returns
- [u64] - The capacity of the allocation.
Examples
use std::bytes::Bytes;
fn foo() {
let bytes = Bytes::with_capacity(5);
let cap = bytes.capacity();
assert(cap == 5);
}
fn len(self) -> u64
fn len(self) -> u64
Gets the length of the Bytes
.
Returns
- [u64] - The length of the
Bytes
.
Examples
use std::bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
assert(bytes.len() == 0);
bytes.push(5);
assert(bytes.len() == 1);
}
fn clear(refmut self)
fn clear(refmut self)
Clears the Bytes
, removing all values.
Examples
use std:bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
bytes.push(5);
bytes.clear()
assert(bytes.is_empty());
}
fn is_empty(self) -> bool
fn is_empty(self) -> bool
Returns true
if the type contains no elements.
Returns
- [bool] -
true
if the type contains no elements,false
otherwise.
Examples
use std:bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
assert(bytes.is_empty());
bytes.push(5);
assert(!bytes.is_empty());
bytes.clear()
assert(bytes.is_empty());
}
fn ptr(self) -> raw_ptr
fn ptr(self) -> raw_ptr
Gets the pointer of the allocation.
Returns
[raw_ptr] - The location in memory that the allocated bytes live.
Examples
use std::bytes::Bytes;
fn foo() {
let bytes = Bytes::new();
assert(!bytes.ptr().is_null());
}
fn split_at(self, mid: u64) -> (Self, Self)
fn split_at(self, mid: u64) -> (Self, Self)
Divides one Bytes into two at an index.
Additional Information
The first will contain all indices from [0, mid)
(excluding the index
mid
itself) and the second will contain all indices from [mid, len)
(excluding the index len
itself).
Arguments
mid
: [u64] - Index at which the Bytes is to be split.
Reverts
- When
mid > self.len
.
Examples
use std:bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
bytes.push(5u8);
bytes.push(7u8);
bytes.push(9u8);
assert(bytes.len() == 3);
let mid = 1;
let (left, right) = bytes.split_at(mid);
assert(left.capacity() == mid);
assert(right.capacity() == bytes.len() - mid);
assert(left.len() == 1);
assert(right.len() == 2);
}
fn append(
refmut self,
refmut other: self,
)
fn append(
refmut self,
refmut other: self,
)
Copies all elements of other
into self
Additional Information
NOTE: Appending self
to itself will duplicate the Bytes
. i.e. [0, 1, 2] => [0, 1, 2, 0, 1, 2]
This function differs from the rust append
function in that it does not clear the other
Bytes
Arguments
other
: [Bytes] - The Bytes to append to self.
Examples
use std:bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
bytes.push(5u8);
bytes.push(7u8);
bytes.push(9u8);
assert(bytes.len() == 3);
let mut bytes2 = Bytes::new();
bytes2.push(5u8);
bytes2.push(7u8);
bytes2.push(9u8);
assert(bytes2.len() == 3);
let first_length = bytes.len();
let second_length = bytes2.len();
let first_cap = bytes.capacity();
let second_cap = bytes2.capacity();
bytes.append(bytes2);
assert(bytes.len() == first_length + second_length);
assert(bytes.capacity() == first_cap + second_cap);
}
Trait Implementations
impl Eq for Bytes
impl Eq for Bytes
fn eq(self, other: Self) -> bool
fn neq(self, other: Self) -> bool
fn neq(self, other: Self) -> bool
Evaluates if two values of the same type are not equal.
Additional Information
This function is inherited when eq()
is implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
if the two values are not equal, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 2 };
let result = struct1 != struct2;
assert(result);
}
impl AsRawSlice for Bytes
impl AsRawSlice for Bytes
fn as_raw_slice(self) -> raw_slice
fn as_raw_slice(self) -> raw_slice
Returns a raw slice of all of the elements in the type.
impl From<raw_slice> for Bytes
impl From<raw_slice> for Bytes
fn from(slice: raw_slice) -> Self
fn from(slice: raw_slice) -> Self
Creates a Bytes
from a raw_slice
.
Examples
use std:bytes::Bytes;
let mut vec = Vec::new();
let a = 5u8;
let b = 7u8;
let c = 9u8
vec.push(a);
vec.push(b);
vec.push(c);
let vec_as_raw_slice = vec.as_raw_slice();
let bytes = Bytes::from(vec_as_raw_slice);
assert(bytes.len == 3);
assert(bytes.get(0).unwrap() == a);
assert(bytes.get(1).unwrap() == b);
assert(bytes.get(2).unwrap() == c);
impl From<Vec<u8>> for Bytes
impl From<Vec<u8>> for Bytes
fn from(vec: Vec<u8>) -> Self
fn from(vec: Vec<u8>) -> Self
Creates a Bytes
from a Vec<u8>
.
Examples
use std:bytes::Bytes;
let mut vec = Vec::new();
let a = 5u8;
let b = 7u8;
let c = 9u8
vec.push(a);
vec.push(b);
vec.push(c);
let bytes = Bytes::from(vec);
assert(bytes.len == 3);
assert(bytes.get(0).unwrap() == a);
assert(bytes.get(1).unwrap() == b);
assert(bytes.get(2).unwrap() == c);