serde: Remove impl From<T> for Raw<T>
This commit is contained in:
parent
0126a7223a
commit
da14fdaf17
@ -131,7 +131,7 @@ mod helper_tests {
|
|||||||
}))
|
}))
|
||||||
.unwrap();
|
.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 incoming_request = IncomingRequest { txn_id: txn_id.clone(), events };
|
||||||
|
|
||||||
let response: sync_events::Response =
|
let response: sync_events::Response =
|
||||||
@ -168,7 +168,7 @@ mod tests {
|
|||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let dummy_event = Raw::from(dummy_event);
|
let dummy_event = Raw::new(&dummy_event).unwrap();
|
||||||
let events = vec![dummy_event];
|
let events = vec![dummy_event];
|
||||||
|
|
||||||
let req: http::Request<Vec<u8>> = Request { events: &events, txn_id: "any_txn_id" }
|
let req: http::Request<Vec<u8>> = Request { events: &events, txn_id: "any_txn_id" }
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
# [unreleased]
|
# [unreleased]
|
||||||
|
|
||||||
|
Breaking changes:
|
||||||
|
|
||||||
|
* Remove `From<T>` implementation for `Raw<T>`
|
||||||
|
* Replaced by the new fallible constructor `Raw::new`
|
||||||
|
|
||||||
# 0.5.0
|
# 0.5.0
|
||||||
|
|
||||||
Breaking changes:
|
Breaking changes:
|
||||||
|
@ -8,7 +8,7 @@ use serde::{
|
|||||||
de::{Deserialize, Deserializer, IgnoredAny, MapAccess, Visitor},
|
de::{Deserialize, Deserializer, IgnoredAny, MapAccess, Visitor},
|
||||||
ser::{Serialize, Serializer},
|
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;
|
use crate::cow::MyCowStr;
|
||||||
|
|
||||||
@ -36,35 +36,45 @@ use crate::cow::MyCowStr;
|
|||||||
/// .unwrap(); // finally get to the AnyRoomEvent
|
/// .unwrap(); // finally get to the AnyRoomEvent
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Raw<T> {
|
pub struct Raw<T> {
|
||||||
json: Box<RawValue>,
|
json: Box<RawJsonValue>,
|
||||||
_ev: PhantomData<T>,
|
_ev: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Raw<T> {
|
impl<T> Raw<T> {
|
||||||
fn new(json: Box<RawValue>) -> Self {
|
/// Create a `Raw` by serializing the given `T`.
|
||||||
Self { json, _ev: PhantomData }
|
///
|
||||||
|
/// 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<Self>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
to_raw_json_value(val).map(Self::from_json)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `Raw` from a boxed `RawValue`.
|
/// Create a `Raw` from a boxed `RawValue`.
|
||||||
pub fn from_json(raw: Box<RawValue>) -> Self {
|
pub fn from_json(json: Box<RawJsonValue>) -> Self {
|
||||||
Self::new(raw)
|
Self { json, _ev: PhantomData }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Access the underlying json value.
|
/// Access the underlying json value.
|
||||||
pub fn json(&self) -> &RawValue {
|
pub fn json(&self) -> &RawJsonValue {
|
||||||
&self.json
|
&self.json
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert `self` into the underlying json value.
|
/// Convert `self` into the underlying json value.
|
||||||
pub fn into_json(self) -> Box<RawValue> {
|
pub fn into_json(self) -> Box<RawJsonValue> {
|
||||||
self.json
|
self.json
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to access a given field inside this `Raw`, assuming it contains an
|
/// Try to access a given field inside this `Raw`, assuming it contains an object.
|
||||||
/// object.
|
|
||||||
///
|
///
|
||||||
/// Returns `Err(_)` when the contained value is not an object, or the field
|
/// Returns `Err(_)` when the contained value is not an object, or the field exists but is fails
|
||||||
/// exists but is fails to deserialize to the expected type.
|
/// to deserialize to the expected type.
|
||||||
///
|
///
|
||||||
/// Returns `Ok(None)` when the field doesn't exist or is `null`.
|
/// Returns `Ok(None)` when the field doesn't exist or is `null`.
|
||||||
///
|
///
|
||||||
@ -144,23 +154,9 @@ impl<T> Raw<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Serialize> From<&T> for Raw<T> {
|
|
||||||
fn from(val: &T) -> Self {
|
|
||||||
Self::new(serde_json::value::to_raw_value(val).unwrap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// With specialization a fast path from impl for `impl<T> From<Box<RawValue...`
|
|
||||||
// could be used. Until then there is a special constructor `from_json` for this.
|
|
||||||
impl<T: Serialize> From<T> for Raw<T> {
|
|
||||||
fn from(val: T) -> Self {
|
|
||||||
Self::from(&val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Clone for Raw<T> {
|
impl<T> Clone for Raw<T> {
|
||||||
fn clone(&self) -> Self {
|
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<T> {
|
|||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
Box::<RawValue>::deserialize(deserializer).map(Self::new)
|
Box::<RawJsonValue>::deserialize(deserializer).map(Self::from_json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user