Struct arrow::buffer::MutableBuffer[][src]

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

A MutableBuffer is Arrow’s interface to build a Buffer out of items or slices of items. Buffers created from MutableBuffer (via into) are guaranteed to have its pointer aligned along cache lines and in multiple of 64 bytes. Use MutableBuffer::push to insert an item, MutableBuffer::extend_from_slice to insert many items, and into to convert it to Buffer.

Example

let mut buffer = MutableBuffer::new(0);
buffer.push(256u32);
buffer.extend_from_slice(&[1u32]);
let buffer: Buffer = buffer.into();
assert_eq!(buffer.as_slice(), &[0u8, 1, 0, 0, 1, 0, 0, 0])

Implementations

Allocate a new MutableBuffer with initial capacity to be at least capacity.

Allocate a new MutableBuffer with initial capacity to be at least capacity.

Allocates a new MutableBuffer with len and capacity to be at least len where all bytes are guaranteed to be 0u8.

Example

let mut buffer = MutableBuffer::from_len_zeroed(127);
assert_eq!(buffer.len(), 127);
assert!(buffer.capacity() >= 127);
let data = buffer.as_slice_mut();
assert_eq!(data[126], 0u8);

creates a new MutableBuffer with capacity and length capable of holding len bits. This is useful to create a buffer for packed bitmaps.

Set the bits in the range of [0, end) to 0 (if val is false), or 1 (if val is true). Also extend the length of this buffer to be end.

This is useful when one wants to clear (or set) the bits and then manipulate the buffer directly (e.g., modifying the buffer by holding a mutable reference from data_mut()).

Ensure that count bytes from start contain zero bits

This is used to initialize the bits in a buffer, however, it has no impact on the len of the buffer and so can be used to initialize the memory region from len to capacity.

Ensures that this buffer has at least self.len + additional bytes. This re-allocates iff self.len + additional > capacity.

Example

let mut buffer = MutableBuffer::new(0);
buffer.reserve(253); // allocates for the first time
(0..253u8).for_each(|i| buffer.push(i)); // no reallocation
let buffer: Buffer = buffer.into();
assert_eq!(buffer.len(), 253);

Resizes the buffer, either truncating its contents (with no change in capacity), or growing it (potentially reallocating it) and writing value in the newly available bytes.

Example

let mut buffer = MutableBuffer::new(0);
buffer.resize(253, 2); // allocates for the first time
assert_eq!(buffer.as_slice()[252], 2u8);

Shrinks the capacity of the buffer as much as possible. The new capacity will aligned to the nearest 64 bit alignment.

Example

// 2 cache lines
let mut buffer = MutableBuffer::new(128);
assert_eq!(buffer.capacity(), 128);
buffer.push(1);
buffer.push(2);

buffer.shrink_to_fit();
assert!(buffer.capacity() >= 64 && buffer.capacity() < 128);

Returns whether this buffer is empty or not.

Returns the length (the number of bytes written) in this buffer. The invariant buffer.len() <= buffer.capacity() is always upheld.

Returns the total capacity in this buffer. The invariant buffer.len() <= buffer.capacity() is always upheld.

Clear all existing data from this buffer.

Returns the data stored in this buffer as a slice.

Returns the data stored in this buffer as a mutable slice.

Returns a raw pointer to this buffer’s internal memory This pointer is guaranteed to be aligned along cache-lines.

Returns a mutable raw pointer to this buffer’s internal memory This pointer is guaranteed to be aligned along cache-lines.

👎 Deprecated since 2.0.0:

This method is deprecated in favour of into from the trait Into.

Freezes this buffer and return an immutable version of it.

View this buffer asa slice of a specific type.

Safety

This function must only be used when this buffer was extended with items of type T. Failure to do so results in undefined behavior.

Extends this buffer from a slice of items that can be represented in bytes, increasing its capacity if needed.

Example

let mut buffer = MutableBuffer::new(0);
buffer.extend_from_slice(&[2u32, 0]);
assert_eq!(buffer.len(), 8) // u32 has 4 bytes

Extends the buffer with a new item, increasing its capacity if needed.

Example

let mut buffer = MutableBuffer::new(0);
buffer.push(256u32);
assert_eq!(buffer.len(), 4) // u32 has 4 bytes

Extends the buffer with a new item, without checking for sufficient capacity

Safety

Caller must ensure that the capacity()-len()>=size_of()

Extends the buffer by additional bytes equal to 0u8, incrementing its capacity if needed.

Creates a MutableBuffer from an Iterator with a trusted (upper) length. Prefer this to collect whenever possible, as it is faster ~60% faster.

Example

let v = vec![1u32];
let iter = v.iter().map(|x| x * 2);
let buffer = unsafe { MutableBuffer::from_trusted_len_iter(iter) };
assert_eq!(buffer.len(), 4) // u32 has 4 bytes

Safety

This method assumes that the iterator’s size is correct and is undefined behavior to use it on an iterator that reports an incorrect length.

Creates a MutableBuffer from a boolean Iterator with a trusted (upper) length.

use arrow::buffer::MutableBuffer;

Example

let v = vec![false, true, false];
let iter = v.iter().map(|x| *x || true);
let buffer = unsafe { MutableBuffer::from_trusted_len_iter_bool(iter) };
assert_eq!(buffer.len(), 1) // 3 booleans have 1 byte

Safety

This method assumes that the iterator’s size is correct and is undefined behavior to use it on an iterator that reports an incorrect length.

Creates a MutableBuffer from an Iterator with a trusted (upper) length or errors if any of the items of the iterator is an error. Prefer this to collect whenever possible, as it is faster ~60% faster.

Safety

This method assumes that the iterator’s size is correct and is undefined behavior to use it on an iterator that reports an incorrect length.

Trait Implementations

Formats the value using the given formatter. 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

Performs the conversion.

Creating a MutableBuffer instance by setting bits according to the boolean values

Creates a value from an iterator. Read more

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

This method tests for !=.

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 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.