SHARE
TWEET

Untitled

a guest Jun 19th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. trait Parser<E> {
  2.     fn begin_field(&mut self);
  3.     fn end_field(&mut self);
  4. }
  5.  
  6. trait ParseFrom<I,E> : Sized {
  7.     fn parse_from(parser: &mut Parser<E>, from: I) -> Result<Self,E>;
  8. }
  9.  
  10. trait ParserExt<E> : Parser<E> {
  11.     fn parse_field_FAILS<I,O:ParseFrom<I,E>>(&mut self, field: String, from: I) -> Result<O,E> {
  12.         self.begin_field();
  13.         let retv = O::parse_from(self, from);
  14.         self.end_field();
  15.         retv
  16.     }
  17. }
  18.  
  19. fn parse_field_WORKS<E,I,O:ParseFrom<I,E>>(parser: &mut Parser<E>, field: String, from: I) -> Result<O,E> {
  20.     parser.begin_field();
  21.     let retv = O::parse_from(parser, from);
  22.     parser.end_field();
  23.     retv
  24. }
  25.  
  26. struct DummyParser { }
  27.  
  28. impl Parser<String> for DummyParser {
  29.     fn begin_field(&mut self) { }
  30.     fn end_field(&mut self) { }
  31. }
  32.  
  33. impl ParseFrom<String,String> for i64 {
  34.     fn parse_from(parser: &mut Parser<String>, from: String) -> Result<i64,String> {
  35.         from.parse::<i64>().map_err(|err|err.to_string())
  36.     }
  37. }
  38.  
  39. fn main() {
  40.     let mut p = DummyParser { };
  41.     let i : i64 = parse_field_WORKS(&mut p, "field".to_string(), "123".to_string()).unwrap();
  42.     println!("i={}",i)
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top