Struct bytes::BytesMut[][src]

pub struct BytesMut { /* fields omitted */ }
Expand description

A unique reference to a contiguous slice of memory.

BytesMut represents a unique view into a potentially shared memory region. Given the uniqueness guarantee, owners of BytesMut handles are able to mutate the memory.

BytesMut can be thought of as containing a buf: Arc<Vec<u8>>, an offset into buf, a slice length, and a guarantee that no other BytesMut for the same buf overlaps with its slice. That guarantee means that a write lock is not required.

Growth

BytesMut’s BufMut implementation will implicitly grow its buffer as necessary. However, explicitly reserving the required space up-front before a series of inserts will be more efficient.

Examples

use bytes::{BytesMut, BufMut};

let mut buf = BytesMut::with_capacity(64);

buf.put_u8(b'h');
buf.put_u8(b'e');
buf.put(&b"llo"[..]);

assert_eq!(&buf[..], b"hello");

// Freeze the buffer so that it can be shared
let a = buf.freeze();

// This does not allocate, instead `b` points to the same memory.
let b = a.clone();

assert_eq!(&a[..], b"hello");
assert_eq!(&b[..], b"hello");

Implementations

Creates a new BytesMut with the specified capacity.

The returned BytesMut will be able to hold at least capacity bytes without reallocating.

It is important to note that this function does not specify the length of the returned BytesMut, but only the capacity.

Examples

use bytes::{BytesMut, BufMut};

let mut bytes = BytesMut::with_capacity(64);

// `bytes` contains no data, even though there is capacity
assert_eq!(bytes.len(), 0);

bytes.put(&b"hello world"[..]);

assert_eq!(&bytes[..], b"hello world");

Creates a new BytesMut with default capacity.

Resulting object has length 0 and unspecified capacity. This function does not allocate.

Examples

use bytes::{BytesMut, BufMut};

let mut bytes = BytesMut::new();

assert_eq!(0, bytes.len());

bytes.reserve(2);
bytes.put_slice(b"xy");

assert_eq!(&b"xy"[..], &bytes[..]);

Returns the number of bytes contained in this BytesMut.

Examples

use bytes::BytesMut;

let b = BytesMut::from(&b"hello"[..]);
assert_eq!(b.len(), 5);

Returns true if the BytesMut has a length of 0.

Examples

use bytes::BytesMut;

let b = BytesMut::with_capacity(64);
assert!(b.is_empty());

Returns the number of bytes the BytesMut can hold without reallocating.

Examples

use bytes::BytesMut;

let b = BytesMut::with_capacity(64);
assert_eq!(b.capacity(), 64);

Converts self into an immutable Bytes.

The conversion is zero cost and is used to indicate that the slice referenced by the handle will no longer be mutated. Once the conversion is done, the handle can be cloned and shared across threads.

Examples

use bytes::{BytesMut, BufMut};
use std::thread;

let mut b = BytesMut::with_capacity(64);
b.put(&b"hello world"[..]);
let b1 = b.freeze();
let b2 = b1.clone();

let th = thread::spawn(move || {
    assert_eq!(&b1[..], b"hello world");
});

assert_eq!(&b2[..], b"hello world");
th.join().unwrap();

Splits the bytes into two at the given index.

Afterwards self contains elements [0, at), and the returned BytesMut contains elements [at, capacity).

This is an O(1) operation that just increases the reference count and sets a few indices.

Examples

use bytes::BytesMut;

let mut a = BytesMut::from(&b"hello world"[..]);
let mut b = a.split_off(5);

a[0] = b'j';
b[0] = b'!';

assert_eq!(&a[..], b"jello");
assert_eq!(&b[..], b"!world");

Panics

Panics if at > capacity.

Removes the bytes from the current view, returning them in a new BytesMut handle.

Afterwards, self will be empty, but will retain any additional capacity that it had before the operation. This is identical to self.split_to(self.len()).

This is an O(1) operation that just increases the reference count and sets a few indices.

Examples

use bytes::{BytesMut, BufMut};

let mut buf = BytesMut::with_capacity(1024);
buf.put(&b"hello world"[..]);

let other = buf.split();

assert!(buf.is_empty());
assert_eq!(1013, buf.capacity());

assert_eq!(other, b"hello world"[..]);

Splits the buffer into two at the given index.

Afterwards self contains elements [at, len), and the returned BytesMut contains elements [0, at).

This is an O(1) operation that just increases the reference count and sets a few indices.

Examples

use bytes::BytesMut;

let mut a = BytesMut::from(&b"hello world"[..]);
let mut b = a.split_to(5);

a[0] = b'!';
b[0] = b'j';

assert_eq!(&a[..], b"!world");
assert_eq!(&b[..], b"jello");

Panics

Panics if at > len.

Shortens the buffer, keeping the first len bytes and dropping the rest.

If len is greater than the buffer’s current length, this has no effect.

Existing underlying capacity is preserved.

The split_off method can emulate truncate, but this causes the excess bytes to be returned instead of dropped.

Examples

use bytes::BytesMut;

let mut buf = BytesMut::from(&b"hello world"[..]);
buf.truncate(5);
assert_eq!(buf, b"hello"[..]);

Clears the buffer, removing all data. Existing capacity is preserved.

Examples

use bytes::BytesMut;

let mut buf = BytesMut::from(&b"hello world"[..]);
buf.clear();
assert!(buf.is_empty());

Resizes the buffer so that len is equal to new_len.

If new_len is greater than len, the buffer is extended by the difference with each additional byte set to value. If new_len is less than len, the buffer is simply truncated.

Examples

use bytes::BytesMut;

let mut buf = BytesMut::new();

buf.resize(3, 0x1);
assert_eq!(&buf[..], &[0x1, 0x1, 0x1]);

buf.resize(2, 0x2);
assert_eq!(&buf[..], &[0x1, 0x1]);

buf.resize(4, 0x3);
assert_eq!(&buf[..], &[0x1, 0x1, 0x3, 0x3]);

Sets the length of the buffer.

This will explicitly set the size of the buffer without actually modifying the data, so it is up to the caller to ensure that the data has been initialized.

Examples

use bytes::BytesMut;

let mut b = BytesMut::from(&b"hello world"[..]);

unsafe {
    b.set_len(5);
}

assert_eq!(&b[..], b"hello");

unsafe {
    b.set_len(11);
}

assert_eq!(&b[..], b"hello world");

Reserves capacity for at least additional more bytes to be inserted into the given BytesMut.

More than additional bytes may be reserved in order to avoid frequent reallocations. A call to reserve may result in an allocation.

Before allocating new buffer space, the function will attempt to reclaim space in the existing buffer. If the current handle references a small view in the original buffer and all other handles have been dropped, and the requested capacity is less than or equal to the existing buffer’s capacity, then the current view will be copied to the front of the buffer and the handle will take ownership of the full buffer.

Examples

In the following example, a new buffer is allocated.

use bytes::BytesMut;

let mut buf = BytesMut::from(&b"hello"[..]);
buf.reserve(64);
assert!(buf.capacity() >= 69);

In the following example, the existing buffer is reclaimed.

use bytes::{BytesMut, BufMut};

let mut buf = BytesMut::with_capacity(128);
buf.put(&[0; 64][..]);

let ptr = buf.as_ptr();
let other = buf.split();

assert!(buf.is_empty());
assert_eq!(buf.capacity(), 64);

drop(other);
buf.reserve(128);

assert_eq!(buf.capacity(), 128);
assert_eq!(buf.as_ptr(), ptr);

Panics

Panics if the new capacity overflows usize.

Appends given bytes to this BytesMut.

If this BytesMut object does not have enough capacity, it is resized first.

Examples

use bytes::BytesMut;

let mut buf = BytesMut::with_capacity(0);
buf.extend_from_slice(b"aaabbb");
buf.extend_from_slice(b"cccddd");

assert_eq!(b"aaabbbcccddd", &buf[..]);

Absorbs a BytesMut that was previously split off.

If the two BytesMut objects were previously contiguous, i.e., if other was created by calling split_off on this BytesMut, then this is an O(1) operation that just decreases a reference count and sets a few indices. Otherwise this method degenerates to self.extend_from_slice(other.as_ref()).

Examples

use bytes::BytesMut;

let mut buf = BytesMut::with_capacity(64);
buf.extend_from_slice(b"aaabbbcccddd");

let split = buf.split_off(6);
assert_eq!(b"aaabbb", &buf[..]);
assert_eq!(b"cccddd", &split[..]);

buf.unsplit(split);
assert_eq!(b"aaabbbcccddd", &buf[..]);

Trait Implementations

Performs the conversion.

Performs the conversion.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the number of bytes between the current position and the end of the buffer. Read more

Returns a slice starting at the current position and of length between 0 and Buf::remaining(). Note that this can return shorter slice (this allows non-continuous internal representation). Read more

Advance the internal cursor of the Buf Read more

Consumes len bytes inside self and returns new instance of Bytes with this data. Read more

Fills dst with potentially multiple slices starting at self’s current position. Read more

Returns true if there are any more bytes to consume Read more

Copies bytes from self into dst. Read more

Gets an unsigned 8 bit integer from self. Read more

Gets a signed 8 bit integer from self. Read more

Gets an unsigned 16 bit integer from self in big-endian byte order. Read more

Gets an unsigned 16 bit integer from self in little-endian byte order. Read more

Gets a signed 16 bit integer from self in big-endian byte order. Read more

Gets a signed 16 bit integer from self in little-endian byte order. Read more

Gets an unsigned 32 bit integer from self in the big-endian byte order. Read more

Gets an unsigned 32 bit integer from self in the little-endian byte order. Read more

Gets a signed 32 bit integer from self in big-endian byte order. Read more

Gets a signed 32 bit integer from self in little-endian byte order. Read more

Gets an unsigned 64 bit integer from self in big-endian byte order. Read more

Gets an unsigned 64 bit integer from self in little-endian byte order. Read more

Gets a signed 64 bit integer from self in big-endian byte order. Read more

Gets a signed 64 bit integer from self in little-endian byte order. Read more

Gets an unsigned 128 bit integer from self in big-endian byte order. Read more

Gets an unsigned 128 bit integer from self in little-endian byte order. Read more

Gets a signed 128 bit integer from self in big-endian byte order. Read more

Gets a signed 128 bit integer from self in little-endian byte order. Read more

Gets an unsigned n-byte integer from self in big-endian byte order. Read more

Gets an unsigned n-byte integer from self in little-endian byte order. Read more

Gets a signed n-byte integer from self in big-endian byte order. Read more

Gets a signed n-byte integer from self in little-endian byte order. Read more

Gets an IEEE754 single-precision (4 bytes) floating point number from self in big-endian byte order. Read more

Gets an IEEE754 single-precision (4 bytes) floating point number from self in little-endian byte order. Read more

Gets an IEEE754 double-precision (8 bytes) floating point number from self in big-endian byte order. Read more

Gets an IEEE754 double-precision (8 bytes) floating point number from self in little-endian byte order. Read more

Creates an adaptor which will read at most limit bytes from self. Read more

Creates an adaptor which will chain this buffer with another. Read more

Creates an adaptor which implements the Read trait for self. Read more

Returns the number of bytes that can be written from the current position until the end of the buffer is reached. Read more

Advance the internal cursor of the BufMut Read more

Returns a mutable slice starting at the current BufMut position and of length between 0 and BufMut::remaining_mut(). Note that this can be shorter than the whole remainder of the buffer (this allows non-continuous implementation). Read more

Transfer bytes into self from src and advance the cursor by the number of bytes written. Read more

Transfer bytes into self from src and advance the cursor by the number of bytes written. Read more

Put cnt bytes val into self. Read more

Returns true if there is space in self for more bytes. Read more

Writes an unsigned 8 bit integer to self. Read more

Writes a signed 8 bit integer to self. Read more

Writes an unsigned 16 bit integer to self in big-endian byte order. Read more

Writes an unsigned 16 bit integer to self in little-endian byte order. Read more

Writes a signed 16 bit integer to self in big-endian byte order. Read more

Writes a signed 16 bit integer to self in little-endian byte order. Read more

Writes an unsigned 32 bit integer to self in big-endian byte order. Read more

Writes an unsigned 32 bit integer to self in little-endian byte order. Read more

Writes a signed 32 bit integer to self in big-endian byte order. Read more

Writes a signed 32 bit integer to self in little-endian byte order. Read more

Writes an unsigned 64 bit integer to self in the big-endian byte order. Read more

Writes an unsigned 64 bit integer to self in little-endian byte order. Read more

Writes a signed 64 bit integer to self in the big-endian byte order. Read more

Writes a signed 64 bit integer to self in little-endian byte order. Read more

Writes an unsigned 128 bit integer to self in the big-endian byte order. Read more

Writes an unsigned 128 bit integer to self in little-endian byte order. Read more

Writes a signed 128 bit integer to self in the big-endian byte order. Read more

Writes a signed 128 bit integer to self in little-endian byte order. Read more

Writes an unsigned n-byte integer to self in big-endian byte order. Read more

Writes an unsigned n-byte integer to self in the little-endian byte order. Read more

Writes low nbytes of a signed integer to self in big-endian byte order. Read more

Writes low nbytes of a signed integer to self in little-endian byte order. Read more

Writes an IEEE754 single-precision (4 bytes) floating point number to self in big-endian byte order. Read more

Writes an IEEE754 single-precision (4 bytes) floating point number to self in little-endian byte order. Read more

Writes an IEEE754 double-precision (8 bytes) floating point number to self in big-endian byte order. Read more

Writes an IEEE754 double-precision (8 bytes) floating point number to self in little-endian byte order. Read more

Creates an adaptor which can write at most limit bytes to self. Read more

Creates an adaptor which implements the Write trait for self. Read more

Creates an adapter which will chain this buffer with another. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Executes the destructor for this type. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Formats the value using the given formatter.

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Formats the value using the given formatter.

Writes a string slice into this writer, returning whether the write succeeded. Read more

Glue for usage of the write! macro with implementors of this trait. Read more

Writes a char into this writer, returning whether the write succeeded. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.