Created
February 25, 2022 17:37
-
-
Save linuskmr/3bc9561e0dbd5f28a9da3ab6ebe76c27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::{Deref, DerefMut, Range}; | |
/// Position is a smart-pointer kind of type that wraps an inner value | |
/// with position information. | |
#[derive(Debug, Eq, PartialEq)] | |
pub struct Position<T> { | |
position: Range<usize>, | |
inner: T, | |
} | |
impl<T> Position<T> { | |
pub fn new(inner: T, position: Range<usize>) -> Self { | |
Self { inner, position } | |
} | |
pub fn position(&self) -> Range<usize> { | |
self.position.clone() | |
} | |
} | |
impl<T> Deref for Position<T> { | |
type Target = T; | |
fn deref(&self) -> &Self::Target { | |
&self.inner | |
} | |
} | |
impl<T> DerefMut for Position<T> { | |
fn deref_mut(&mut self) -> &mut Self::Target { | |
&mut self.inner | |
} | |
} | |
#[derive(Debug)] | |
enum Token { | |
If, | |
Else, | |
Ident(String), | |
Int(i64), | |
Uint(u64), | |
Float(f64) | |
} | |
fn main() { | |
let token = Position::new( | |
Token::Ident("hallo".to_string()), | |
42..45 | |
); | |
println!("position {:?}", token.position()); | |
println!("token {:?}", token) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment