PayloadSlice

Struct PayloadSlice 

Source
pub struct PayloadSlice { /* private fields */ }
Expand description

Payload slice is a slice of variable-sized elements (payloads) encoded in a single block of memory. This way, sequences of, say, strings, can be indexed into without loading all the elements in memory, but rather using a memory mapped buffer.

§Use case

The primary use case of this struct is not necessarily to limit the bytes that are loaded in memory, but rather to limit the time to initialize it. For example, one can use it in a command line program to quickly look up one or several values from a file-encoded vector, without the overhead of parsing the entire file and loading it in memory.

§Examples

use memmap::Mmap;

// We will store out vector to a temporary directory
let temp = TempDir::new()?;
let file_path = temp.path().join("words.bin");

// We can use any elements that implement `AsRef<[u8]>`.
let words = vec!["dog", "cat", "gnu"];

// One way of encoding is to collect elements to a PayloadVector in memory
let payloads: PayloadVector = words.into_iter().collect();

// Write to file
let mut output = File::create(&file_path)?;
payloads.write(&mut output);
drop(output);

// Load as payload slice
let input = File::open(&file_path)?;
let bytes = unsafe { Mmap::map(&input)? };
let payloads = PayloadSlice::new(&bytes);

// Note that it returns byte slices.
assert_eq!(&payloads[0], b"dog");
assert_eq!(&payloads[1], b"cat");
assert_eq!(&payloads[2], b"gnu");

// Non-panicing access.
assert_eq!(payloads.get(3), None);

// Collect to a vector of strings
let items: Vec<_> = payloads
    .iter()
    .map(|b| String::from_utf8(b.to_vec()).unwrap())
    .collect();
assert_eq!(items, vec![
    "dog".to_string(),
    "cat".to_string(),
    "gnu".to_string()
]);

Implementations§

Source§

impl PayloadSlice

Source

pub fn new(data: &[u8]) -> &Self

Conctructs a new slice using the given underlying data.

Source

pub fn write<W: Write>(&self, writer: &mut W) -> Result<()>

Writes the underlying memory to the output.

§Errors

Will return an error if an error occurs while writing to the output.

Source

pub fn get(&self, index: u64) -> Option<&[u8]>

Returns the element at position index or None if index is out of bounds.

Source

pub fn len(&self) -> u64

Returns the length of the slice.

Source

pub fn is_empty(&self) -> bool

Checks if the slice is empty.

Source

pub fn iter(&self) -> impl Iterator<Item = &[u8]>

Returns the iterator over all items.

Trait Implementations§

Source§

impl AsRef<PayloadSlice> for PayloadSlice

Source§

fn as_ref(&self) -> &PayloadSlice

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<PayloadSlice> for PayloadVector

Source§

fn as_ref(&self) -> &PayloadSlice

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Index<usize> for &PayloadSlice

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more