Skip to content

Instantly share code, notes, and snippets.

@gftea
Last active April 11, 2023 22:13
Show Gist options
  • Save gftea/53a42878250f2e60f16b726b89887fb7 to your computer and use it in GitHub Desktop.
Save gftea/53a42878250f2e60f16b726b89887fb7 to your computer and use it in GitHub Desktop.
use std::error::Error;
type Result<T> = std::result::Result<T, Box<dyn Error>>;
pub trait Parser: Sized {
type Output;
fn parse(self) -> Result<Self::Output>;
}
impl<F, T> Parser for F
where
F: FnOnce() -> Result<T>, // fn -> Fn -> FnMut -> FnOnce
{
type Output = T;
fn parse(self) -> Result<Self::Output> {
todo!()
}
}
pub trait ParseMacroInput: Sized {
fn parse() -> Result<Self>;
}
impl<T> ParseMacroInput for T {
fn parse() -> Result<Self> {
todo!()
}
}
pub fn parse<T: ParseMacroInput>() -> Result<T> {
let x = T::parse; // fn parse<T>() -> Result<T>, x is a function pointer
x.parse() // The Parser trait is implemented for all pointer of functions that return a Result
}
// mod modular_err;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment