pub struct ObjectAsVec<'ctx, P: Property, E: Element>(/* private fields */);
Expand description
Represents a JSON key/value type.
For performance reasons we use a Vec instead of a Hashmap. This comes with a tradeoff of slower key accesses as we need to iterate and compare.
The ObjectAsVec struct is a wrapper around a Vec of (&str, Value) pairs. It provides methods to make it easy to migrate from serde_json::Value::Object or serde_json::Map.
Implementations§
Source§impl<'ctx, P: Property, E: Element> ObjectAsVec<'ctx, P, E>
impl<'ctx, P: Property, E: Element> ObjectAsVec<'ctx, P, E>
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn as_vec(&self) -> &Vec<(Key<'ctx, P>, Value<'ctx, P, E>)>
pub fn as_vec(&self) -> &Vec<(Key<'ctx, P>, Value<'ctx, P, E>)>
Access to the underlying Vec.
§Note
Since KeyStrType can be changed via a feature flag avoid using as_vec
and use other
methods instead. This could be a problem with feature unification, when one crate uses it
as &str
and another uses it as Cow<str>
, both will get Cow<str>
pub fn as_mut_vec(&mut self) -> &mut Vec<(Key<'ctx, P>, Value<'ctx, P, E>)>
Sourcepub fn into_vec(self) -> Vec<(Key<'ctx, P>, Value<'ctx, P, E>)>
pub fn into_vec(self) -> Vec<(Key<'ctx, P>, Value<'ctx, P, E>)>
Access to the underlying Vec. Keys are normalized to Cow.
Sourcepub fn get(&self, key: &Key<'_, P>) -> Option<&Value<'ctx, P, E>>
pub fn get(&self, key: &Key<'_, P>) -> Option<&Value<'ctx, P, E>>
Returns a reference to the value corresponding to the key.
§Performance
As this is backed by a Vec, this searches linearly through the Vec as may be much more
expensive than a Hashmap
for larger Objects.
pub fn get_ignore_case(&self, key: &str) -> Option<&Value<'ctx, P, E>>
Sourcepub fn get_mut(&mut self, key: &Key<'ctx, P>) -> Option<&mut Value<'ctx, P, E>>
pub fn get_mut(&mut self, key: &Key<'ctx, P>) -> Option<&mut Value<'ctx, P, E>>
Returns a mutable reference to the value corresponding to the key, if it exists.
§Performance
As this is backed by a Vec, this searches linearly through the Vec as may be much more
expensive than a Hashmap
for larger Objects.
Sourcepub fn get_key_value(
&self,
key: &Key<'_, P>,
) -> Option<(&Key<'_, P>, &Value<'ctx, P, E>)>
pub fn get_key_value( &self, key: &Key<'_, P>, ) -> Option<(&Key<'_, P>, &Value<'ctx, P, E>)>
Returns the key-value pair corresponding to the supplied key.
§Performance
As this is backed by a Vec, this searches linearly through the Vec as may be much more
expensive than a Hashmap
for larger Objects.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&Key<'ctx, P>, &Value<'ctx, P, E>)>
pub fn iter(&self) -> impl Iterator<Item = (&Key<'ctx, P>, &Value<'ctx, P, E>)>
An iterator visiting all key-value pairs
pub fn iter_mut( &mut self, ) -> impl Iterator<Item = (&mut Key<'ctx, P>, &mut Value<'ctx, P, E>)>
Sourcepub fn values(&self) -> impl Iterator<Item = &Value<'ctx, P, E>>
pub fn values(&self) -> impl Iterator<Item = &Value<'ctx, P, E>>
An iterator visiting all values
Sourcepub fn contains_key(&self, key: &Key<'ctx, P>) -> bool
pub fn contains_key(&self, key: &Key<'ctx, P>) -> bool
Returns true if the object contains a value for the specified key.
§Performance
As this is backed by a Vec, this searches linearly through the Vec as may be much more
expensive than a Hashmap
for larger Objects.
pub fn contains_key_value( &self, key: &Key<'ctx, P>, value: &Value<'ctx, P, E>, ) -> bool
pub fn contains_any_key(&self, keys: &[Key<'ctx, P>]) -> bool
pub fn remove(&mut self, key: &Key<'ctx, P>) -> Option<Value<'ctx, P, E>>
Sourcepub fn insert(
&mut self,
key: impl Into<Key<'ctx, P>>,
value: impl Into<Value<'ctx, P, E>>,
) -> Option<Value<'ctx, P, E>>
pub fn insert( &mut self, key: impl Into<Key<'ctx, P>>, value: impl Into<Value<'ctx, P, E>>, ) -> Option<Value<'ctx, P, E>>
Inserts a key-value pair into the object.
If the object did not have this key present, None
is returned.
If the object did have this key present, the value is updated, and the old value is
returned.
§Performance
This operation is linear in the size of the Vec because it potentially requires iterating through all elements to find a matching key.
Sourcepub fn insert_or_get_mut(
&mut self,
key: impl Into<Key<'ctx, P>>,
value: impl Into<Value<'ctx, P, E>>,
) -> &mut Value<'ctx, P, E>
pub fn insert_or_get_mut( &mut self, key: impl Into<Key<'ctx, P>>, value: impl Into<Value<'ctx, P, E>>, ) -> &mut Value<'ctx, P, E>
Inserts a key-value pair into the object if the key does not yet exist, otherwise returns a mutable reference to the existing value.
§Performance
This operation might be linear in the size of the Vec because it requires iterating through all elements to find a matching key, and might add to the end if not found.
pub fn insert_unchecked( &mut self, key: impl Into<Key<'ctx, P>>, value: impl Into<Value<'ctx, P, E>>, )
pub fn with_key_value( self, key: impl Into<Key<'ctx, P>>, value: impl Into<Value<'ctx, P, E>>, ) -> Self
pub fn insert_named( &mut self, key: Option<String>, value: Value<'ctx, P, E>, ) -> String
pub fn extend<I>(&mut self, iter: I)
Sourcepub fn insert_unchecked_and_get_mut(
&mut self,
key: impl Into<Key<'ctx, P>>,
value: impl Into<Value<'ctx, P, E>>,
) -> &mut Value<'ctx, P, E>
pub fn insert_unchecked_and_get_mut( &mut self, key: impl Into<Key<'ctx, P>>, value: impl Into<Value<'ctx, P, E>>, ) -> &mut Value<'ctx, P, E>
Inserts a key-value pair into the object and returns the mutable reference of the inserted value.
§Note
The key must not exist in the object. If the key already exists, the object will contain multiple keys afterwards.
§Performance
This operation is amortized constant time, worst case linear time in the size of the Vec because it potentially requires a reallocation to grow the Vec.
Source§impl<'ctx, P: Property, E: Element<Property = P>> ObjectAsVec<'ctx, P, E>
impl<'ctx, P: Property, E: Element<Property = P>> ObjectAsVec<'ctx, P, E>
pub fn into_expanded_boolean_set(self) -> impl Iterator<Item = Key<'ctx, P>>
Trait Implementations§
Source§impl<'ctx, P: Clone + Property, E: Clone + Element> Clone for ObjectAsVec<'ctx, P, E>
impl<'ctx, P: Clone + Property, E: Clone + Element> Clone for ObjectAsVec<'ctx, P, E>
Source§fn clone(&self) -> ObjectAsVec<'ctx, P, E>
fn clone(&self) -> ObjectAsVec<'ctx, P, E>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more