sonic_rs::value::object

Struct OccupiedEntry

Source
pub struct OccupiedEntry<'a> { /* private fields */ }
Expand description

A view into a single occupied location in a Object.

Implementations§

Source§

impl<'a> OccupiedEntry<'a>

Source

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);
}
Source

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);
}
Source

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");
Source

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");
}
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.