diff options
Diffstat (limited to 'src/raw.rs')
-rw-r--r-- | src/raw.rs | 21 |
1 files changed, 18 insertions, 3 deletions
@@ -1,5 +1,9 @@ use crate::error::Error; -use crate::lib::*; +use alloc::borrow::ToOwned; +use alloc::boxed::Box; +use alloc::string::String; +use core::fmt::{self, Debug, Display}; +use core::mem; use serde::de::value::BorrowedStrDeserializer; use serde::de::{ self, Deserialize, DeserializeSeed, Deserializer, IntoDeserializer, MapAccess, Unexpected, @@ -122,6 +126,10 @@ impl RawValue { fn from_owned(json: Box<str>) -> Box<Self> { unsafe { mem::transmute::<Box<str>, Box<RawValue>>(json) } } + + fn into_owned(raw_value: Box<Self>) -> Box<str> { + unsafe { mem::transmute::<Box<RawValue>, Box<str>>(raw_value) } + } } impl Clone for Box<RawValue> { @@ -216,6 +224,12 @@ impl RawValue { } } +impl From<Box<RawValue>> for Box<str> { + fn from(raw_value: Box<RawValue>) -> Self { + RawValue::into_owned(raw_value) + } +} + /// Convert a `T` into a boxed `RawValue`. /// /// # Example @@ -271,7 +285,7 @@ impl RawValue { #[cfg_attr(docsrs, doc(cfg(feature = "raw_value")))] pub fn to_raw_value<T>(value: &T) -> Result<Box<RawValue>, Error> where - T: Serialize, + T: ?Sized + Serialize, { let json_string = crate::to_string(value)?; Ok(RawValue::from_owned(json_string.into_boxed_str())) @@ -435,9 +449,10 @@ impl<'de> Visitor<'de> for BoxedFromString { where E: de::Error, { - self.visit_string(s.to_owned()) + Ok(RawValue::from_owned(s.to_owned().into_boxed_str())) } + #[cfg(any(feature = "std", feature = "alloc"))] fn visit_string<E>(self, s: String) -> Result<Self::Value, E> where E: de::Error, |