Initial commit with lexer

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-04-27 12:42:17 -04:00
commit 58421a0469
10 changed files with 759 additions and 0 deletions

38
src/util.rs Normal file
View File

@@ -0,0 +1,38 @@
use std::{
fmt::{Display, Formatter, self},
};
pub struct LazyString<'f, F>
where F: Fn() -> String + 'f
{
source: F,
_lifetime: std::marker::PhantomData<dyn Fn() + 'f>,
}
impl<'f, F> LazyString<'f, F>
where F: Fn() -> String + 'f
{
pub fn new(source: F) -> Self {
LazyString {
source,
_lifetime: Default::default(),
}
}
}
impl<'f, F> Display for LazyString<'f, F>
where F: Fn() -> String + 'f
{
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
let s = (self.source)();
Display::fmt(&s, fmt)
}
}
#[test]
fn test_lazy_string() {
let i = 10;
let lzstr = LazyString::new(|| format!("the value is {}", i));
assert_eq!(lzstr.to_string(), "the value is 10");
}