Struct json_syntax::object::Entry

source ·
pub struct Entry<M = ()> {
    pub key: Meta<Key, M>,
    pub value: MetaValue<M>,
}
Expand description

Object entry.

Fields§

§key: Meta<Key, M>§value: MetaValue<M>

Implementations§

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
Hide additional 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)
	}
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,
		}
	}
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,
		})
	}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Wraps self inside a Meta<Self, M> using the given metadata. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.