Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Conflicting implementations for Decodable #112

Closed
RoxasShadow opened this issue May 21, 2015 · 3 comments
Closed

Conflicting implementations for Decodable #112

RoxasShadow opened this issue May 21, 2015 · 3 comments

Comments

@RoxasShadow
Copy link

Can anyone enlighten me about this?

use rustc_serialize::json::{Json, Decoder};
use rustc_serialize::Decodable;

#[derive(RustcDecodable)]
pub struct Artist {
  name: String,
  images: Vec<Image>
}

impl Decodable for Artist {
  fn decode<D: rustc_serialize::Decoder>(decoder: &mut D) -> Result<Artist, D::Error> {
    // stuff
  }
}
  src/main.rs:15:10: 15:24 error: conflicting implementations for trait rustc_serialize::serialize::Decodable` [E0119]
  src/main.rs:15 #[derive(RustcDecodable)]
                          ^~~~~~~~~~~~~~
  src/main.rs:15:10: 15:24 note: in expansion of #[derive_RustcDecodable]
  src/main.rs:15:10: 15:24 note: expansion site
  src/main.rs:20:1: 24:2 note: note conflicting implementation here
  src/main.rs:20 impl Decodable for Artist {
  src/main.rs:21   fn decode<D: rustc_serialize::Decoder>(decoder: &mut D) -> Result<Artist, D::Error> {
  src/main.rs:22     // stuff
  src/main.rs:23   }
  src/main.rs:24 }
  error: aborting due to previous error

Also, if I write rustc_serialize::Decoder (line 11) simply as Decoder, than I need to import rustc_serialize::Decoder and remove use rustc_serialize::json::Decoder;, but than I can't Decoder::new() anymore.

@BurntSushi
Copy link

Well, if you have derive(RustcDecodable), then that is going to implement Decodable for you automatically. If you also provide your own impl for Decodable, then you'll have two impls for the same type on the same trait. That will cause the "conflicting impls" error you're seeing.

The conflict with Decoder is due to the fact that rustc_serialize::Decoder is a trait and rustc_serialize::json::Decoder is a type specifically for decoding JSON.

@RoxasShadow
Copy link
Author

Ok, now that is clear to me.
About the second question, I was wondering a way to avoid expliciting the rustc_serialize for using the right Decoder, but it's not a great deal after all.

Many thanks for your help!

@BurntSushi
Copy link

@RoxasShadow You could do use rustc_serialize::Decoder as RustcDecoder and then use RustcDecoder instead of Decoder for your trait bound. Alternatively, use rustc_serialize::json::Decoder as JsonDecoder.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants