[][src]Struct cursed_collections::AppendOnlyVec

pub struct AppendOnlyVec<T> { /* fields omitted */ }

A collection onto which new values can be appended, while still keeping references to previous values valid.

Example

This is useful as a buffer on the side of another data structure that is built incrementally. For example, let's imagine we want to parse a JSON-like data format that contains only arrays and strings.

The advantage of having slices and strs instead of Vecs and Strings is that you'd then be to directly pattern match against values of this type.

use cursed_collections::AppendOnlyVec;

enum MyData<'buffers> {
  Array(&'buffers [MyData<'buffers>]),
  String(&'buffers str),
}

fn main() {
  let string_buf = AppendOnlyVec::<String>::new();
  let array_buf = AppendOnlyVec::<Vec<MyData>>::new();

  let my_key = MyData::String(string_buf.push("name".into()));
  let my_name = MyData::String(string_buf.push("Simon".into()));
  let my_array = MyData::Array(array_buf.push(vec![my_key, my_name]));

  match my_array {
    MyData::Array(&[MyData::String("name"), MyData::String(name)]) => {
      println!("Hello, {}", name)
    }
    _ => println!("Hello!"),
  }
}

Methods

impl<T> AppendOnlyVec<T>[src]

pub fn new() -> AppendOnlyVec<T>[src]

Creates an empty AppendOnlyVec.

pub fn push(&self, element: T) -> &T[src]

Consumes a T, appends it to the end of the vector, and returns a reference to the newly appended element.

pub fn len(&self) -> usize[src]

Return the number of elements in the vector.

Trait Implementations

impl<T> Default for AppendOnlyVec<T>[src]

impl<T> Index<usize> for AppendOnlyVec<T>[src]

type Output = T

The returned type after indexing.

Auto Trait Implementations

impl<T> Send for AppendOnlyVec<T> where
    T: Send

impl<T> !Sync for AppendOnlyVec<T>

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

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

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

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

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]