2

So I am trying to serialize a struct using bincode following these instructions, and I was able to get that to work.

But then I wanted to serialize a struct with an IpAddr enum. Since IpAddr doesn't implement the Encodable trait needed, I downloaded the rustc_serialize crate from git and implemented encodable for IpAddr myself. I then changed my Cargo.toml file to:

[dependencies]
# rustc-serialize = "0.3"
byteorder = "0.3"
bincode = "0.4"

[dependencies.rustc-serialize]
path = "src/rustc-serialize-master"

But now, the same code from the struct I was using doesn't compile saying that rustc_serialize::serialize::Encodable is not implemented for my struct even though i have #[derive(RustcEncodable)] above the struct.

Even when I get rid of the code I added to the local version of rustc_serialize, I still get that error.

I think it might be due to something being screwed up with the way bincode and a local rustc_serialize interact, but I'm not sure.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Tenzin Rigden
  • 109
  • 2
  • 8
  • For what it's worth, it's probably a bad idea to fork `rustc-serialize` to add an encoding for another type. It's likely more sane to implement the `Encodable` trait by hand for your struct that contains the `IpAddr`. – Shepmaster Sep 26 '15 at 01:13
  • I tried that but I got an "only traits defined in the current crate can be implemented for arbitrary types" error. It seems that you can't implement traits for traits you didn't define (imported rustc-serialize 'encodable` trait) for structs that you didn't define (imported IpAddr). If you have been able to do that, please tell me. Thanks! – Tenzin Rigden Sep 28 '15 at 16:21
  • Check out [How do I implement a trait I don't own for a type I don't own?](http://stackoverflow.com/q/25413201/155423). The wrapper type is the correct solution here. In your case, you already have a wrapper type (*serialize **a struct** with an IpAddr enum*). You can just manually implement `Encodable` for whatever your struct is. – Shepmaster Sep 28 '15 at 16:45
  • Thanks for the quick response. So i've been trying to implement that, but I've been running into an error. So based on what the stack overflow you linked, I have ```impl Encodable for myStruct { fn encode ..... { match *self { IpAddr(_) => {}, _ => {}, } } }``` But I keep getting an `Unresolved enum variant, struct or const 'IpAddr'` error even though I've explicitly imported it using use, and I get the same error on built in types too. (P.S. How can you do code blocks in comments?) And thanks! – Tenzin Rigden Sep 28 '15 at 17:32
  • That's just some syntax that isn't needed in this case (I added a comment to the other question as well). You want something closer [to this](http://is.gd/2DwLtS). Also, you can't do code blocks in comments, as comments are supposed to be short and may be deleted. – Shepmaster Sep 28 '15 at 17:46
  • Where you wrote the `// add appropriate error handling, pick the correct emit_* methods`, did you mean have a match statement for the `IpAddr` type and `String` type, in your example? Thanks – Tenzin Rigden Sep 28 '15 at 18:06
  • at this point, we are far off topic from your original question. I'd encourage you to ask a new one. That will allow future searchers to find it, instead of being buried in comments. – Shepmaster Sep 28 '15 at 18:20
  • Ok. I will keep messing around with it first. If I still can't get it, I'll ask another question. Thanks for all your help! – Tenzin Rigden Sep 28 '15 at 18:56
  • To those that find this, I was able to implement the trait for the struct by following this site. http://valve.github.io/blog/2014/08/25/json-serialization-in-rust-part-1/ – Tenzin Rigden Sep 28 '15 at 20:22

1 Answers1

4

Please review the Cargo documentation on overriding dependencies:

To specify overrides, create a .cargo/config file in some ancestor of your project's directory (common places to put it is in the root of your code directory or in your home directory).

Inside that file, put this:

paths = ["/path/to/project/rand"]

Going deeper, you are likely running into issue 22750 - two different versions of a crate interacting leads to unhelpful error messages. When you add rustc-serialize to your dependencies, you aren't replacing the old version, you are adding a new one.

In general, this is a good feature. If my project relies on crates A and B and they both rely on crate Z but with different versions, Rust can handle that just fine. The problem arises when they re-export items from those crates.

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366