2

I'm trying to serialize a struct into bytes in order to send it down a pipe. I found How to convert 'struct' to '&[u8]'?, which looks good. I'm using stable Rust, and trying to derive RustcEncodable or RustcDecodable results in a compiler error:

error: `#[derive]` for custom traits is not stable enough for use and is subject to change (see issue #29644)
  --> src/main.rs:26:10
   |
26 | #[derive(RustEncodable)]
   |          ^^^^^^^^^^^^^

error: aborting due to previous error

My initial instinct would be to implement the trait for my struct instead of deriving it, but the documentation for rustc-serialize is not very clear

I'm using Rust 1.12.1.

Community
  • 1
  • 1
Dash83
  • 1,357
  • 2
  • 17
  • 31
  • 1
    You might be interesting in looking up "serde", this is the best-of-breed serialization/deserialization crate currently available in Rust. Custom derives (ie, the use of `derive` for user-defined traits) will only be available come Rust 1.15, so it is only available on the nightly and beta channels but not the stable one for a couple days/weeks still. – Matthieu M. Jan 27 '17 at 16:53
  • Thanks, @MatthieuM, you are always on top of everything Rust related. I'll check serde out, although the fact that their builds are not passing seems like a bad start. – Dash83 Jan 27 '17 at 16:55
  • 1
    To be more precise, Rust 1.15 should be coming out on Thursday 2nd Feb, 2017. So, you have just under a week to wait till Serde comes to stable Rust :) – Joe Clay Jan 27 '17 at 16:57
  • 1
    @Dash83: Shepmaster is even more on top than I am ;) – Matthieu M. Jan 27 '17 at 16:59
  • @JoeClay, you make a compelling case. Will the serialization support for Rust 1.15 be literally the serde crate? If so, I can get started with the code right now using it as an external crate, and adjust when Rust 1.15 comes out. – Dash83 Jan 27 '17 at 17:00
  • 1
    @MatthieuM: I know, he even dragged me to the Rust chat channel once. Anyways, all of us Rust newbies really appreciate the work both of you do, thanks! – Dash83 Jan 27 '17 at 17:01
  • @Dash83 serialization will continue to be external to the standard library (IMO a good thing). The difference is that 1.15 will allow using Serde with most of it's wonderful syntactic sugar with the stable compiler. For the next week or so, you should use the beta compiler to test out serde. – Shepmaster Jan 27 '17 at 17:11
  • Your concrete problem here is just a typo: `RustcEncodable` (with a **C**), not `RustEncodable`. – Shepmaster Jan 27 '17 at 17:16
  • Thank you, @Shepmaster, you were correct. I want to raise something for future reference though, and that is that using #derive[RustcEncodable] did not work straight away. Seems like it won't work if the extern crate import is being from within a module. I had to move the import to the lib.rs file and then it worked out great. Not sure why. – Dash83 Jan 29 '17 at 16:27

0 Answers0