Struct json_syntax::object::Entry
source · Expand description
Object entry.
Fields§
§key: Meta<Key, M>
§value: MetaValue<M>
Implementations§
source§impl<M> Entry<M>
impl<M> Entry<M>
sourcepub fn new(key: Meta<Key, M>, value: MetaValue<M>) -> Self
pub fn new(key: Meta<Key, M>, value: MetaValue<M>) -> Self
Examples found in repository?
src/object.rs (line 389)
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
pub fn push(&mut self, key: Meta<Key, M>, value: MetaValue<M>) -> bool {
self.push_entry(Entry::new(key, value))
}
pub fn push_entry(&mut self, entry: Entry<M>) -> bool {
let index = self.entries.len();
self.entries.push(entry);
self.indexes.insert(&self.entries, index)
}
/// Removes the entry at the given index.
pub fn remove_at(&mut self, index: usize) -> Option<Entry<M>> {
if index < self.entries.len() {
self.indexes.remove(&self.entries, index);
self.indexes.shift(index);
Some(self.entries.remove(index))
} else {
None
}
}
/// Inserts the given key-value pair.
///
/// If one or more entries are already matching the given key,
/// all of them are removed and returned in the resulting iterator.
/// Otherwise, `None` is returned.
pub fn insert(
&mut self,
key: Meta<Key, M>,
value: MetaValue<M>,
) -> Option<RemovedByInsertion<M>> {
match self.index_of(key.value()) {
Some(index) => {
let mut entry = Entry::new(key, value);
core::mem::swap(&mut entry, &mut self.entries[index]);
Some(RemovedByInsertion {
index,
first: Some(entry),
object: self,
})
}
None => {
self.push(key, value);
None
}
}
}
More examples
src/convert/serde_json.rs (line 38)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
pub fn from_serde_json(
value: serde_json::Value,
f: impl Clone + Fn(SerdeJsonFragment) -> M,
) -> Meta<Self, M> {
let meta = f(SerdeJsonFragment::Value(&value));
let v = match value {
serde_json::Value::Null => Self::Null,
serde_json::Value::Bool(b) => Self::Boolean(b),
serde_json::Value::Number(n) => Self::Number(n.into()),
serde_json::Value::String(s) => Self::String(s.into()),
serde_json::Value::Array(a) => Self::Array(
a.into_iter()
.map(|i| Self::from_serde_json(i, f.clone()))
.collect(),
),
serde_json::Value::Object(o) => Self::Object(
o.into_iter()
.map(|(k, v)| {
let k_meta = f(SerdeJsonFragment::Key(&k));
Entry::new(Meta(k.into(), k_meta), Self::from_serde_json(v, f.clone()))
})
.collect(),
),
};
Meta(v, meta)
}
pub fn as_key(&self) -> &Meta<Key, M>
pub fn as_value(&self) -> &MetaValue<M>
pub fn into_key(self) -> Meta<Key, M>
pub fn into_value(self) -> MetaValue<M>
pub fn stripped_key(&self) -> &Key
pub fn stripped_value(&self) -> &Value<M>
pub fn into_stripped_key(self) -> Key
pub fn into_stripped_value(self) -> Value<M>
pub fn key_metadata(&self) -> &M
pub fn value_metadata(&self) -> &M
pub fn into_key_metadata(self) -> M
pub fn into_value_metadata(self) -> M
sourcepub fn map_metadata<N>(self, f: impl FnMut(M) -> N) -> Entry<N>
pub fn map_metadata<N>(self, f: impl FnMut(M) -> N) -> Entry<N>
Examples found in repository?
src/object.rs (line 474)
470 471 472 473 474 475 476 477 478 479 480 481
pub fn map_metadata<N>(self, mut f: impl FnMut(M) -> N) -> Object<N> {
let entries = self
.entries
.into_iter()
.map(|entry| entry.map_metadata(&mut f))
.collect();
Object {
entries,
indexes: self.indexes,
}
}
sourcepub fn try_map_metadata<N, E>(
self,
f: impl FnMut(M) -> Result<N, E>
) -> Result<Entry<N>, E>
pub fn try_map_metadata<N, E>(
self,
f: impl FnMut(M) -> Result<N, E>
) -> Result<Entry<N>, E>
Examples found in repository?
src/object.rs (line 490)
484 485 486 487 488 489 490 491 492 493 494 495 496 497
pub fn try_map_metadata<N, E>(
self,
mut f: impl FnMut(M) -> Result<N, E>,
) -> Result<Object<N>, E> {
let mut entries = Vec::with_capacity(self.len());
for entry in self.entries {
entries.push(entry.try_map_metadata(&mut f)?)
}
Ok(Object {
entries,
indexes: self.indexes,
})
}
pub fn as_pair(&self) -> (&Meta<Key, M>, &MetaValue<M>)
Trait Implementations§
source§impl<M> Extend<Entry<M>> for Object<M>
impl<M> Extend<Entry<M>> for Object<M>
source§fn extend<I: IntoIterator<Item = Entry<M>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Entry<M>>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
source§impl<M> FromIterator<Entry<M>> for Object<M>
impl<M> FromIterator<Entry<M>> for Object<M>
source§impl<M: Ord> Ord for Entry<M>
impl<M: Ord> Ord for Entry<M>
source§impl<M: PartialEq> PartialEq<Entry<M>> for Entry<M>
impl<M: PartialEq> PartialEq<Entry<M>> for Entry<M>
source§impl<M: PartialOrd> PartialOrd<Entry<M>> for Entry<M>
impl<M: PartialOrd> PartialOrd<Entry<M>> for Entry<M>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl<M> StrippedHash for Entry<M>
impl<M> StrippedHash for Entry<M>
fn stripped_hash<H: Hasher>(&self, state: &mut H)
source§impl<M> StrippedOrd for Entry<M>
impl<M> StrippedOrd for Entry<M>
fn stripped_cmp(&self, other: &Self) -> Ordering
source§impl<M, __M> StrippedPartialEq<Entry<__M>> for Entry<M>
impl<M, __M> StrippedPartialEq<Entry<__M>> for Entry<M>
fn stripped_eq(&self, other: &Entry<__M>) -> bool
source§impl<M, __M> StrippedPartialOrd<Entry<__M>> for Entry<M>
impl<M, __M> StrippedPartialOrd<Entry<__M>> for Entry<M>
impl<M: Eq> Eq for Entry<M>
impl<M> StrippedEq for Entry<M>
impl<M> StructuralEq for Entry<M>
impl<M> StructuralPartialEq for Entry<M>
Auto Trait Implementations§
impl<M> RefUnwindSafe for Entry<M>where
M: RefUnwindSafe,
impl<M> Send for Entry<M>where
M: Send,
impl<M> Sync for Entry<M>where
M: Sync,
impl<M> Unpin for Entry<M>where
M: Unpin,
impl<M> UnwindSafe for Entry<M>where
M: UnwindSafe,
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.