I wish to implement the Serialize
trait on a type in an extern crate, but it\'s forbidden. I had a look at serde\'s remote derive, but it seems a lot of work rewrit
Here's my try (note: I'm the OP):
use serde::{Serialize, Serializer};
use std::io::Error;
use std::fmt::Display;
#[derive(Debug, Serialize)]
pub enum MyError {
Custom,
#[serde(serialize_with = "use_display")]
Io(Error)
}
fn use_display<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: Display,
S: Serializer
{
serializer.collect_str(value)
}
playground
But there's maybe a more straightforward way of doing this?