pub struct OwnedValue(pub Value<'static>);Expand description
A Value<'static> wrapper that supports
DeserializeOwned.
Because Value<'a> can borrow strings and bytes during deserialization,
Value<'static> can’t be used when DeserializeOwned is needed.
OwnedValue implements Deserialize by first deserializing a
Value<'a> and then using Value::into_static to convert borrowed data
to owned data.
Tuple Fields§
§0: Value<'static>Methods from Deref<Target = Value<'static>>§
Sourcepub fn deserialize_as<'de, T>(&'de self) -> Result<T, ValueError>where
T: Deserialize<'de>,
pub fn deserialize_as<'de, T>(&'de self) -> Result<T, ValueError>where
T: Deserialize<'de>,
Attempts to create an instance of T from this value.
use pot::Value;
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
enum Example {
Hello,
World,
}
let original = vec![Example::Hello, Example::World];
let serialized = Value::from_serialize(&original)?;
let deserialized: Vec<Example> = serialized.deserialize_as()?;
assert_eq!(deserialized, original);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the value contained is considered empty.
// Value::None is always empty.
assert_eq!(Value::None.is_empty(), true);
// All primitive values, including Unit, are always not empty, even if they contain the value 0.
assert_eq!(Value::Unit.is_empty(), false);
assert_eq!(Value::from(false).is_empty(), false);
assert_eq!(Value::from(0_u8).is_empty(), false);
assert_eq!(Value::from(0_f32).is_empty(), false);
// For all other types, having a length of 0 will result in is_empty returning true.
assert_eq!(Value::from(Vec::<u8>::new()).is_empty(), true);
assert_eq!(Value::from(b"").is_empty(), true);
assert_eq!(Value::from(vec![0_u8]).is_empty(), false);
assert_eq!(Value::from("").is_empty(), true);
assert_eq!(Value::from("hi").is_empty(), false);
assert_eq!(Value::Sequence(Vec::new()).is_empty(), true);
assert_eq!(Value::from(vec![Value::None]).is_empty(), false);
assert_eq!(Value::Mappings(Vec::new()).is_empty(), true);
assert_eq!(
Value::from(vec![(Value::None, Value::None)]).is_empty(),
false
);Sourcepub fn as_bool(&self) -> bool
pub fn as_bool(&self) -> bool
Returns the value as a bool.
// Value::None is always false.
assert_eq!(Value::None.as_bool(), false);
// Value::Unit is always true.
assert_eq!(Value::Unit.as_bool(), true);
// Value::Bool will return the contained value
assert_eq!(Value::from(false).as_bool(), false);
assert_eq!(Value::from(true).as_bool(), true);
// All primitive values return true if the value is non-zero.
assert_eq!(Value::from(0_u8).as_bool(), false);
assert_eq!(Value::from(1_u8).as_bool(), true);
assert_eq!(Value::from(0_f32).as_bool(), false);
assert_eq!(Value::from(1_f32).as_bool(), true);
// For all other types, as_bool() returns the result of `!is_empty()`.
assert_eq!(Value::from(Vec::<u8>::new()).as_bool(), false);
assert_eq!(Value::from(b"").as_bool(), false);
assert_eq!(Value::from(vec![0_u8]).as_bool(), true);
assert_eq!(Value::from("").as_bool(), false);
assert_eq!(Value::from("hi").as_bool(), true);
assert_eq!(Value::Sequence(Vec::new()).as_bool(), false);
assert_eq!(Value::from(vec![Value::None]).as_bool(), true);
assert_eq!(Value::Mappings(Vec::new()).as_bool(), false);
assert_eq!(
Value::from(vec![(Value::None, Value::None)]).as_bool(),
true
);Sourcepub fn as_integer(&self) -> Option<Integer>
pub fn as_integer(&self) -> Option<Integer>
Returns the value as an Integer. Returns None if the value is not a
Self::Float or Self::Integer. Also returns None if the value is
a float, but cannot be losslessly converted to an integer.
Sourcepub fn as_float(&self) -> Option<Float>
pub fn as_float(&self) -> Option<Float>
Returns the value as an Float. Returns None if the value is not a
Self::Float or Self::Integer. Also returns None if the value is
an integer, but cannot be losslessly converted to a float.
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Returns the value as a string, or None if the value is not representable
by a string. This will only return a value with variants
Self::String and Self::Bytes. Bytes will only be returned if the
contained bytes can be safely interpretted as UTF-8.
Sourcepub fn as_bytes(&self) -> Option<&[u8]>
pub fn as_bytes(&self) -> Option<&[u8]>
Returns the value as bytes, or None if the value is not stored as a
representation of bytes. This will only return a value with variants
Self::String and Self::Bytes.
Sourcepub fn values(&self) -> ValueIter<'_> ⓘ
pub fn values(&self) -> ValueIter<'_> ⓘ
Returns an iterator that iterates over all values contained inside of
this value. Returns an empty iterator if not a Self::Sequence or
Self::Mappings. If a Self::Mappings, only the value portion of
the mapping is returned.
Sourcepub fn mappings(&self) -> Iter<'_, (Value<'a>, Value<'a>)>
pub fn mappings(&self) -> Iter<'_, (Value<'a>, Value<'a>)>
Returns an iterator that iterates over all mappings contained inside of
this value. Returns an empty iterator if not a Self::Sequence or
Self::Mappings. If a Self::Sequence, the key will always be
Self::None.
Trait Implementations§
Source§impl Clone for OwnedValue
impl Clone for OwnedValue
Source§fn clone(&self) -> OwnedValue
fn clone(&self) -> OwnedValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OwnedValue
impl Debug for OwnedValue
Source§impl Deref for OwnedValue
impl Deref for OwnedValue
Source§impl DerefMut for OwnedValue
impl DerefMut for OwnedValue
Source§impl<'de> Deserialize<'de> for OwnedValue
impl<'de> Deserialize<'de> for OwnedValue
Source§fn deserialize<D>(
deserializer: D,
) -> Result<OwnedValue, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<OwnedValue, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<'a> From<&'a Value<'a>> for OwnedValue
impl<'a> From<&'a Value<'a>> for OwnedValue
Source§fn from(value: &'a Value<'a>) -> OwnedValue
fn from(value: &'a Value<'a>) -> OwnedValue
Source§impl<'a> From<Value<'a>> for OwnedValue
impl<'a> From<Value<'a>> for OwnedValue
Source§fn from(value: Value<'a>) -> OwnedValue
fn from(value: Value<'a>) -> OwnedValue
Source§impl PartialEq for OwnedValue
impl PartialEq for OwnedValue
Source§impl Serialize for OwnedValue
impl Serialize for OwnedValue
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl StructuralPartialEq for OwnedValue
Auto Trait Implementations§
impl Freeze for OwnedValue
impl RefUnwindSafe for OwnedValue
impl Send for OwnedValue
impl Sync for OwnedValue
impl Unpin for OwnedValue
impl UnwindSafe for OwnedValue
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more