Hacker News new | past | comments | ask | show | jobs | submit login
Serialization in Rust (rnestler.github.io)
4 points by rnestler on May 1, 2015 | hide | past | favorite | 1 comment



The Encodable implementation can be simplified further in this case:

- The match is not necessary for a single possible type. Simply use the "dot" syntax. - The return value of a closure can be derived automatically. `|enc| -> _ {` can be simplified to `|enc| {`. - An explicit return is not necessary, simply leave an expression at the end of the scope. - I'm not sure why `p_url` was dereferenced, but `p_type` not. Apparently it's not necessary at all. - Some redundant braces can be removed.

Before:

  match *self {
      Feed { _type: ref p_type, url: ref p_url } =>
          encoder.emit_struct("Feed", 2usize, |enc| -> _ {
              try!(enc.emit_struct_field( "type", 0usize, |enc| p_type.encode(enc)));
              return enc.emit_struct_field("url", 1usize, |enc| -> _ { (*p_url).encode(enc) });
          }),
  }
After:

  encoder.emit_struct("Feed", 2usize, |enc| {
      try!(
          enc.emit_struct_field("type", 0usize, |enc| self._type.encode(enc))
      );
      enc.emit_struct_field("url", 1usize, |enc| self.url.encode(enc))
  })




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: