Skip to content

Instantly share code, notes, and snippets.

@Carreau
Last active March 3, 2018 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Carreau/644fc960af97a6ef0b90d10ad956ab0e to your computer and use it in GitHub Desktop.
Save Carreau/644fc960af97a6ef0b90d10ad956ab0e to your computer and use it in GitHub Desktop.
Rust From error
// 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