pub struct OccupiedEntry<'a> { /* private fields */ }
Expand description
A view into a single occupied location in a Object
.
Implementations§
Source§impl<'a> OccupiedEntry<'a>
impl<'a> OccupiedEntry<'a>
Sourcepub fn get(&self) -> &Value
pub fn get(&self) -> &Value
Gets a reference to the value in the entry.
§Examples
use sonic_rs::{object, value::object::Entry, Value};
let mut obj = object! {"a": 1, "b": true, "c": null};
if let Entry::Occupied(entry) = obj.entry(&"a") {
assert_eq!(entry.get(), 1);
}
if let Entry::Occupied(entry) = obj.entry(&"b") {
assert_eq!(entry.get(), true);
}
Sourcepub fn get_mut(&mut self) -> &mut Value
pub fn get_mut(&mut self) -> &mut Value
Gets a mutable reference to the value in the entry.
§Examples
use sonic_rs::{object, value::object::Entry, Value};
let mut obj = object! {"a": 1, "b": true, "c": null};
obj.insert(&"a", Value::from("hello"));
if let Entry::Occupied(mut entry) = obj.entry(&"a") {
assert_eq!(entry.get_mut(), &Value::from("hello"));
}
if let Entry::Occupied(mut entry) = obj.entry(&"b") {
assert_eq!(entry.get_mut(), &true);
}
Sourcepub fn into_mut(self) -> &'a mut Value
pub fn into_mut(self) -> &'a mut Value
Converts the entry into a mutable reference to its value.
§Examples
use sonic_rs::{object, value::object::Entry, Value};
let mut obj = object! {"a": 1, "b": true, "c": null};
obj.insert(&"a", Value::from("hello"));
if let Entry::Occupied(mut entry) = obj.entry(&"a") {
let vref = entry.into_mut();
assert_eq!(vref, &mut Value::from("hello"));
*vref = Value::from("world");
}
assert_eq!(obj["a"], "world");
Sourcepub fn insert<T: Into<Value>>(&mut self, val: T) -> Value
pub fn insert<T: Into<Value>>(&mut self, val: T) -> Value
Sets the value of the entry, and returns the entry’s old value.
§Examples
use sonic_rs::{object, value::object::Entry};
let mut obj = object! {"a": 1, "b": true, "c": null};
if let Entry::Occupied(mut entry) = obj.entry(&"a") {
assert_eq!(entry.insert("hello"), 1);
}
if let Entry::Occupied(mut entry) = obj.entry(&"a") {
assert_eq!(entry.insert("world"), "hello");
}
Sourcepub fn remove(self) -> Value
pub fn remove(self) -> Value
Takes the value out of the entry, and returns it.
§Examples
use sonic_rs::{object, value::object::Entry, Value};
let mut obj = object! {"a": 1, "b": true, "c": null};
if let Entry::Occupied(mut entry) = obj.entry(&"a") {
assert_eq!(entry.remove(), 1);
}
if let Entry::Occupied(mut entry) = obj.entry(&"b") {
assert_eq!(entry.remove(), true);
}
if let Entry::Occupied(mut entry) = obj.entry(&"c") {
assert_eq!(entry.remove(), Value::default());
}
assert!(obj.is_empty());
Auto Trait Implementations§
impl<'a> Freeze for OccupiedEntry<'a>
impl<'a> !RefUnwindSafe for OccupiedEntry<'a>
impl<'a> Send for OccupiedEntry<'a>
impl<'a> Sync for OccupiedEntry<'a>
impl<'a> Unpin for OccupiedEntry<'a>
impl<'a> !UnwindSafe for OccupiedEntry<'a>
Blanket Implementations§
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
Mutably borrows from an owned value. Read more