Conflicting requirements, with From trait

trying to make it so that a tuple and anything that implements the Serialize trait can be converted into a response

impl From<(u16, &'static str)> for Response {
	fn from(tuple: (u16, &'static str)) -> Response {
		res()
			.with_header(ContentType::plaintext())
			.with_status(toStatusCode(tuple.0))
			.with_body(tuple.1)
	}
}

impl<T: Serialize> From<T> for Response {
	fn from(json: T) -> Response {
		res()
			.with_header(ContentType::json())
			.with_status(toStatusCode(200))
			.with_body(to_string(&json).unwrap())
	}
}

not sure why I'm getting a conflicting requirements error.


➜ cargo build
   Compiling arc-reactor v0.1.0 (file:///home/seun/Projects/arc-reactor)
error[E0119]: conflicting implementations of trait `std::convert::From<(u16, &'static str)>` for type `core::response::Response`:
  --> src/proto/convert.rs:23:1
   |
14 | impl From<(u16, &'static str)> for Response {
   | ------------------------------------------- first implementation here
...
23 | impl<T: Serialize> From<T> for Response {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `core::response::Response`

error: aborting due to previous error

error: Could not compile `arc-reactor`.

To learn more, run the command again with --verbose.

(u16, &'static str) implements Serialize.

wait wut. Hold on, i need to check this.

just checked, Is there a way to get around this? Possibly detect if we're attempting to convert a tuple?

Specialization allows overlapping implementations like this but it's nightly-only right now.

I only use nightly, how do i enable it?

#![feature(specialization)]

1 Like

Specialization requires opt-in on the blanket impl (ie it has to say default). Is From’s blanket impl changed to that? I’d imagine no as that’s a change in behavior (potentially). Or did this aspect change?

1 Like