diff --git a/crates/ruma-appservice-api/src/event/push_events/v1.rs b/crates/ruma-appservice-api/src/event/push_events/v1.rs index 2eab1939..0cd3869b 100644 --- a/crates/ruma-appservice-api/src/event/push_events/v1.rs +++ b/crates/ruma-appservice-api/src/event/push_events/v1.rs @@ -131,7 +131,7 @@ mod helper_tests { })) .unwrap(); - let events = vec![Raw::from(state_event), Raw::from(message_event)]; + let events = vec![Raw::new(&state_event).unwrap(), Raw::new(&message_event).unwrap()]; let incoming_request = IncomingRequest { txn_id: txn_id.clone(), events }; let response: sync_events::Response = @@ -168,7 +168,7 @@ mod tests { }, })) .unwrap(); - let dummy_event = Raw::from(dummy_event); + let dummy_event = Raw::new(&dummy_event).unwrap(); let events = vec![dummy_event]; let req: http::Request> = Request { events: &events, txn_id: "any_txn_id" } diff --git a/crates/ruma-serde/CHANGELOG.md b/crates/ruma-serde/CHANGELOG.md index 90292d11..963d59a0 100644 --- a/crates/ruma-serde/CHANGELOG.md +++ b/crates/ruma-serde/CHANGELOG.md @@ -1,5 +1,10 @@ # [unreleased] +Breaking changes: + +* Remove `From` implementation for `Raw` + * Replaced by the new fallible constructor `Raw::new` + # 0.5.0 Breaking changes: diff --git a/crates/ruma-serde/src/raw.rs b/crates/ruma-serde/src/raw.rs index 1f9de598..42e94617 100644 --- a/crates/ruma-serde/src/raw.rs +++ b/crates/ruma-serde/src/raw.rs @@ -8,7 +8,7 @@ use serde::{ de::{Deserialize, Deserializer, IgnoredAny, MapAccess, Visitor}, ser::{Serialize, Serializer}, }; -use serde_json::value::RawValue; +use serde_json::value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue}; use crate::cow::MyCowStr; @@ -36,35 +36,45 @@ use crate::cow::MyCowStr; /// .unwrap(); // finally get to the AnyRoomEvent /// ``` pub struct Raw { - json: Box, + json: Box, _ev: PhantomData, } impl Raw { - fn new(json: Box) -> Self { - Self { json, _ev: PhantomData } + /// Create a `Raw` by serializing the given `T`. + /// + /// Shorthand for `serde_json::value::to_raw_value(val).map(Raw::from_json)`, but specialized to + /// `T`. + /// + /// # Errors + /// + /// Fails if `T`s [`Serialize`] implementation fails. + pub fn new(val: &T) -> serde_json::Result + where + T: Serialize, + { + to_raw_json_value(val).map(Self::from_json) } /// Create a `Raw` from a boxed `RawValue`. - pub fn from_json(raw: Box) -> Self { - Self::new(raw) + pub fn from_json(json: Box) -> Self { + Self { json, _ev: PhantomData } } /// Access the underlying json value. - pub fn json(&self) -> &RawValue { + pub fn json(&self) -> &RawJsonValue { &self.json } /// Convert `self` into the underlying json value. - pub fn into_json(self) -> Box { + pub fn into_json(self) -> Box { self.json } - /// Try to access a given field inside this `Raw`, assuming it contains an - /// object. + /// Try to access a given field inside this `Raw`, assuming it contains an object. /// - /// Returns `Err(_)` when the contained value is not an object, or the field - /// exists but is fails to deserialize to the expected type. + /// Returns `Err(_)` when the contained value is not an object, or the field exists but is fails + /// to deserialize to the expected type. /// /// Returns `Ok(None)` when the field doesn't exist or is `null`. /// @@ -144,23 +154,9 @@ impl Raw { } } -impl From<&T> for Raw { - fn from(val: &T) -> Self { - Self::new(serde_json::value::to_raw_value(val).unwrap()) - } -} - -// With specialization a fast path from impl for `impl From From for Raw { - fn from(val: T) -> Self { - Self::from(&val) - } -} - impl Clone for Raw { fn clone(&self) -> Self { - Self::new(self.json.clone()) + Self::from_json(self.json.clone()) } } @@ -176,7 +172,7 @@ impl<'de, T> Deserialize<'de> for Raw { where D: Deserializer<'de>, { - Box::::deserialize(deserializer).map(Self::new) + Box::::deserialize(deserializer).map(Self::from_json) } }