diff --git a/matchbox_protocol/src/lib.rs b/matchbox_protocol/src/lib.rs index 94515922..30e14def 100644 --- a/matchbox_protocol/src/lib.rs +++ b/matchbox_protocol/src/lib.rs @@ -34,10 +34,12 @@ cfg_if! { if #[cfg(feature = "json")] { pub type JsonPeerRequest = PeerRequest; pub type JsonPeerEvent = PeerEvent; + use std::fmt; - impl ToString for JsonPeerRequest { - fn to_string(&self) -> String { - serde_json::to_string(self).expect("error serializing message") + + impl fmt::Display for JsonPeerRequest { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", serde_json::to_string(self).map_err(|_| fmt::Error)?) } } impl std::str::FromStr for JsonPeerRequest { @@ -48,9 +50,9 @@ cfg_if! { } } - impl ToString for JsonPeerEvent { - fn to_string(&self) -> String { - serde_json::to_string(self).expect("error serializing message") + impl fmt::Display for JsonPeerEvent { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", serde_json::to_string(self).map_err(|_| fmt::Error)?) } } impl std::str::FromStr for JsonPeerEvent {