Initial commit with lexer
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
38
src/util.rs
Normal file
38
src/util.rs
Normal 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");
|
||||
}
|
||||
Reference in New Issue
Block a user