impl From<Map> for Value { fn from(value: Map) -> Self { Self::Map(value)
}
}
impl<T: Into<Number>> From<T> for Value { fn from(value: T) -> Self { Self::Number(value.into())
}
}
impl<T: Into<Value>> From<Option<T>> for Value { fn from(value: Option<T>) -> Self { Self::Option(value.map(Into::into).map(Box::new))
}
}
impl<'a> From<&'a str> for Value { fn from(value: &'a str) -> Self {
String::from(value).into()
}
}
impl<'a> From<Cow<'a, str>> for Value { fn from(value: Cow<'a, str>) -> Self {
String::from(value).into()
}
}
impl From<String> for Value { fn from(value: String) -> Self { Self::String(value)
}
}
/// Special case to allow `Value::from(b"byte string")` impl<const N: usize> From<&'static [u8; N]> for Value { fn from(value: &'static [u8; N]) -> Self { Self::Bytes(Vec::from(*value))
}
}
impl<T: Into<Value>> FromIterator<T> for Value { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { Self::Seq(iter.into_iter().map(Into::into).collect())
}
}
impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value { fn from(value: &'a [T]) -> Self {
value.iter().map(Clone::clone).map(Into::into).collect()
}
}
impl<T: Into<Value>> From<Vec<T>> for Value { fn from(value: Vec<T>) -> Self {
value.into_iter().collect()
}
}
impl From<()> for Value { fn from(_value: ()) -> Self {
Value::Unit
}
}
impl Value { /// Tries to deserialize this [`Value`] into `T`. pubfn into_rust<T>(self) -> Result<T> where
T: DeserializeOwned,
{
T::deserialize(self)
}
}
/// Deserializer implementation for RON [`Value`]. /// This does not support enums (because [`Value`] does not store them). impl<'de> Deserializer<'de> for Value { type Error = Error;
impl<'a, 'de> SeqAccess<'de> for SeqAccessor<'a> { type Error = Error;
fn next_element_seed<T>(&mutself, seed: T) -> Result<Option<T::Value>> where
T: DeserializeSeed<'de>,
{ // The `Vec` is reversed, so we can pop to get the originally first element self.seq
.pop()
.map_or(Ok(None), |v| seed.deserialize(v).map(Some))
}
impl<'a, 'de> MapAccess<'de> for MapAccessor<'a> { type Error = Error;
fn next_key_seed<K>(&mutself, seed: K) -> Result<Option<K::Value>> where
K: DeserializeSeed<'de>,
{ // The `Vec` is reversed, so we can pop to get the originally first element matchself.items.pop() {
Some((key, value)) => { self.value = Some(value);
seed.deserialize(key).map(Some)
}
None => Ok(None),
}
}
#[allow(clippy::panic)] fn next_value_seed<V>(&mutself, seed: V) -> Result<V::Value> where
V: DeserializeSeed<'de>,
{ matchself.value.take() {
Some(value) => seed.deserialize(value),
None => panic!("Contract violation: value before key"),
}
}
#[test] fn string() {
assert_same::<String>(r#""hello world""#);
assert_same::<String>(r#""this is a Rusty string""#);
assert_same::<String>(r#""this is now valid UTF-8 \xf0\x9f\xa6\x80""#);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.