[][src]Struct manger::common::OneOrMore

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

Collection struct which stores one or more items of type T.

This collection is used within the manger crate to express consuming one of more of an item from a string. This would be equivalent to the + operator in EBNF syntax or RegEx.

Note

While OneOrMore is not iterable, the into_iter, into_vec, ref_vec and mut_vec can be used to iterate/create iterators over the items contained within the structs and do further data manipulation.

Examples

use manger::{Consumable, consume_struct};
use manger::common::OneOrMore;

let source = "(2)(3)(7)";

// EncasedInteger will be consuming strings like "(123)" and "(42)"
struct EncasedInteger { value: u32 };
consume_struct!(
    EncasedInteger => [
        > '(',
        value: u32,
        > ')';
    ]
);

let (encased_integers, _) = <OneOrMore<EncasedInteger>>::consume_from(source)?;
let product: u32 = encased_integers
        .into_iter()
        .map(|encased_integer| encased_integer.value)
        .product();

assert_eq!(product, 42);

Implementations

impl<T> OneOrMore<T>[src]

pub fn head(&self) -> &T[src]

Getter for the first item of a OneOrMore<T>.

This will return a reference to the item that is first consumed and therefore always contains an item.

Examples

use manger::Consumable;
use manger::common::OneOrMore;

let (items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;

assert_eq!(*items.head(), 'a');

pub fn tail(&self) -> &Vec<T>[src]

Getter for the non-first items of a OneOrMore<T>.

This will return references to the items that is were consumed after the first item and will be in order of they position within the source string. The returned vector possibly has _NO items.

Examples

use manger::Consumable;
use manger::common::OneOrMore;

let (items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;

assert_eq!(&items.tail().iter().collect::<String>(), "BcdEFg");

pub fn into_vec(self) -> Vec<T>[src]

Take ownership self of type OneOrMore<T> and return a Vec<T> owning all the items self used to contain.

Since the ownership of the items contained within OneOrMore<T> will be transfered into the vector. The OneOrMore<T> instance cannot be used anymore afterwards.

Examples

use manger::Consumable;
use manger::common::OneOrMore;

let (items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;

let uppercased: String = items
    .into_vec()
    .into_iter()
    .filter(|character| character.is_ascii_uppercase())
    .collect();

assert_eq!(uppercased, "BEF");

pub fn ref_vec(&self) -> Vec<&T>[src]

Returns a vector with references to the items in the OneOrMore<T>. This will not take ownership of the the items in self.

Examples

use manger::Consumable;
use manger::common::OneOrMore;

let (items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;

let uppercased: String = items
    .ref_vec()
    .into_iter()
    .filter(|character| character.is_ascii_uppercase())
    .collect();

assert_eq!(uppercased, "BEF");

pub fn mut_vec(&mut self) -> Vec<&mut T>[src]

Returns a vector with mutable references to the items in the OneOrMore<T>. This will not take ownership of the the items in self.

Examples

use manger::Consumable;
use manger::common::OneOrMore;

let (mut items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;

items
    .mut_vec()
    .iter_mut()
    .filter(|character| character.is_ascii_uppercase())
    .for_each(|character| **character = character.to_ascii_lowercase());

let lowercased: String = items.into_iter().collect();

assert_eq!(lowercased, "abcdefg");

Trait Implementations

impl<T: Consumable> Consumable for OneOrMore<T>[src]

impl<T: Debug> Debug for OneOrMore<T>[src]

impl<T: Consumable> IntoIterator for OneOrMore<T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<Self::Item>

Which kind of iterator are we turning this into?

Auto Trait Implementations

impl<T> RefUnwindSafe for OneOrMore<T> where
    T: RefUnwindSafe
[src]

impl<T> Send for OneOrMore<T> where
    T: Send
[src]

impl<T> Sync for OneOrMore<T> where
    T: Sync
[src]

impl<T> Unpin for OneOrMore<T> where
    T: Unpin
[src]

impl<T> UnwindSafe for OneOrMore<T> where
    T: UnwindSafe
[src]

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.