Last active
March 3, 2018 23:34
-
-
Save Carreau/644fc960af97a6ef0b90d10ad956ab0e to your computer and use it in GitHub Desktop.
Rust From error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in the following snippet, cargo complains: | |
// | |
// error[E0277]: the trait bound `B: std::convert::From<&A>` is not satisfied | |
// --> src/main.rs:54:21 | |
// | | |
// 54 | let dn: B = B::from(n); | |
// | ^^^^^^^ the trait `std::convert::From<&A>` is not implemented for `B` | |
// | | |
// = help: the following implementations were found: | |
// <B as std::convert::From<A>> | |
// = note: required by `std::convert::From::from` | |
// | |
// | |
// While what is missing in the `Clone` trait on A. | |
// | |
// Why is there no error on self.clone() ? | |
extern crate serde; | |
use serde::Serialize; | |
use serde::ser::Serializer; | |
#[macro_use] | |
extern crate serde_derive; | |
// #[derive(Clone)] | |
struct A { | |
vec: i32, | |
} | |
impl From<A> for B { | |
fn from(n: A) -> Self { | |
Self { vec: n.vec.into() } | |
} | |
} | |
#[derive(Serialize)] | |
struct B { | |
vec: i32, | |
} | |
impl Serialize for A | |
where | |
B: From<A>+Serialize, | |
{ | |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | |
where | |
S: Serializer, | |
{ | |
let n = self.clone(); | |
let dn: B = B::from(n); | |
dn.serialize(serializer) | |
} | |
} | |
fn main() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment