[−][src]Trait manger::SelfConsumable
Trait which allows for consuming of instances and literals from a string.
This trait should be mostly used for types with a bijection to a string representation,
which includes the char
and &str
. This does not include floating points, because
"42" and "4.2e1" will both consume to 42.
Note
For the reason mentioned before, this is not implemented for f32
and f64
. Similarly,
this is also not implemented for u8
, u16
, u32
, u64
, i8
, `i1
Required methods
pub fn consume_item<'a>(
source: &'a str,
item: &Self
) -> Result<&'a str, ConsumeError>
[src]
source: &'a str,
item: &Self
) -> Result<&'a str, ConsumeError>
Attempt to consume a literal item
from a source
string. When consuming
is succesful, it will return the unconsumed part of the source
. When consuming
fails, it will return an error.
This is the core function implement when implementing SelfConsumable
.
Implementation note
It is highly recommended to take into account UTF-8 characters. This is
reasonably easy with .chars()
on &str
or with the crate
utf8-slice
.
Examples
use manger::{ Consumable, SelfConsumable }; let source = "scalar*42"; let unconsumed = <&str>::consume_item(source, &"scalar")?; assert_eq!(unconsumed, "*42"); let unconsumed = char::consume_item(unconsumed, &'*')?; assert_eq!(unconsumed, "42"); let (num, unconsumed) = u32::consume_from(unconsumed)?; assert_eq!(num, 42); assert_eq!(unconsumed, "");
Implementations on Foreign Types
impl SelfConsumable for char
[src]
pub fn consume_item<'a>(
source: &'a str,
item: &Self
) -> Result<&'a str, ConsumeError>
[src]
source: &'a str,
item: &Self
) -> Result<&'a str, ConsumeError>
impl SelfConsumable for &str
[src]
pub fn consume_item<'a>(
source: &'a str,
item: &Self
) -> Result<&'a str, ConsumeError>
[src]
source: &'a str,
item: &Self
) -> Result<&'a str, ConsumeError>