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

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!"),
  }
}

Implementations

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]

Returns 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> !RefUnwindSafe for AppendOnlyVec<T>

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

impl<T> !Sync for AppendOnlyVec<T>

impl<T> Unpin for AppendOnlyVec<T>

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

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.