Last active
April 11, 2023 22:13
-
-
Save gftea/53a42878250f2e60f16b726b89887fb7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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::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