pub trait SelfConsumable {
// Required method
fn consume_item<'a>(
source: &'a str,
item: &Self,
) -> Result<&'a str, ConsumeError>;
}
Expand description
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§
Sourcefn consume_item<'a>(
source: &'a str,
item: &Self,
) -> Result<&'a str, ConsumeError>
fn consume_item<'a>( 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, "");
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.