Struct arrow2::buffer::MutableBuffer[][src]

pub struct MutableBuffer<T: NativeType> { /* fields omitted */ }
Expand description

A MutableBuffer is this crates’ interface to store types that are byte-like, such as i32. It behaves like a Vec, with the following differences:

  • memory is allocated along cache lines and in multiple of 64 bytes.
  • it can only hold types supported by the arrow format (u8-u64, i8-i128, f32,f64 and crate::types::days_ms) A MutableBuffer can be converted to a Buffer via .into.

Example

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

Implementations

Creates an empty MutableBuffer. This does not allocate in the heap.

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::<u8>::from_len_zeroed(127);
assert_eq!(buffer.len(), 127);
assert!(buffer.capacity() >= 127);
let data = buffer.as_mut_slice();
assert_eq!(data[126], 0u8);

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

Example

let mut buffer = MutableBuffer::<u8>::new();
buffer.reserve(253); // allocates for the first time
(0..253u8).for_each(|i| buffer.push(i)); // no reallocation
let buffer: Buffer<u8> = 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::<u8>::new();
buffer.resize(253, 2); // allocates for the first time
assert_eq!(buffer.as_slice()[252], 2u8);

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.

Shortens the buffer. If len is greater or equal to the buffers’ current length, this has no effect.

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.

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();
buffer.extend_from_slice(&[2u32, 0]);
assert_eq!(buffer.len(), 2)

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

Example

let mut buffer = MutableBuffer::new();
buffer.push(256u32);
assert_eq!(buffer.len(), 1)

Safety

The caller must ensure that the buffer was properly initialized up to len.

Shrinks the capacity of the MutableBuffer to fit its current length. The new capacity will be a multiple of 64 bytes.

Example


let mut buffer = MutableBuffer::<u64>::with_capacity(16);
assert_eq!(buffer.capacity(), 16);
buffer.push(1);
buffer.push(2);

buffer.shrink_to_fit();
assert!(buffer.capacity() == 8);

Extends self from a TrustedLen iterator.

Extends self from an iterator.

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. 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(), 1)

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. 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(), 1)

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 TrustedLen iterator, or errors if any of the items of the iterator is an error.

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. The only difference between this and [try_from_trusted_len_iter] is that this works on any iterator, while try_from_trusted_len_iter requires the iterator to implement the trait TrustedLen, which not every iterator currently implements due to limitations of the Rust compiler.

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.

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

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

Performs the conversion.

Performs the conversion.

Performs the conversion.

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.