[−][src]Struct cursed_collections::AppendOnlyVec
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 str
s instead of Vec
s and String
s 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.
Trait Implementations
impl<T> Default for AppendOnlyVec<T>
[src]
impl<T> Index<usize> for AppendOnlyVec<T>
[src]
Auto Trait Implementations
impl<T> Send for AppendOnlyVec<T> where
T: Send,
T: Send,
impl<T> !Sync for AppendOnlyVec<T>
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
🔬 This is a nightly-only experimental API. (
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
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.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,