Skip to main content

OccupiedEntry

Struct OccupiedEntry 

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

A view into an occupied entry in a ZendHashTable.

It is part of the Entry enum.

Implementations§

Source§

impl<'a, 'k> OccupiedEntry<'a, 'k>

Source

pub fn key(&self) -> &ArrayKey<'k>

Gets a reference to the key in the entry.

§Example
use ext_php_rs::types::{ZendHashTable, ArrayKey};

let mut ht = ZendHashTable::new();
ht.insert("key", "value");

if let ext_php_rs::types::array::Entry::Occupied(entry) = ht.entry("key") {
    assert_eq!(entry.key(), &ArrayKey::Str("key"));
}
Source

pub fn get(&self) -> Option<&Zval>

Gets a reference to the value in the entry.

§Returns

A result containing a reference to the value, or an error if the key conversion failed.

§Errors

Returns an error if the key contains a null byte (for string keys).

§Example
use ext_php_rs::types::{ZendHashTable, Zval};

let mut ht = ZendHashTable::new();
ht.insert("key", "value");

if let ext_php_rs::types::array::Entry::Occupied(entry) = ht.entry("key") {
    assert_eq!(entry.get().and_then(Zval::str), Some("value"));
}
Source

pub fn get_mut(&mut self) -> Option<&mut Zval>

Gets a mutable reference to the value in the entry.

§Returns

A result containing a mutable reference to the value, or an error if the key conversion failed.

§Errors

Returns an error if the key contains a null byte (for string keys).

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();
ht.insert("counter", 0i64);

if let ext_php_rs::types::array::Entry::Occupied(mut entry) = ht.entry("counter") {
    if let Some(v) = entry.get_mut() {
        if let Some(n) = v.long() {
            v.set_long(n + 1);
        }
    }
}
Source

pub fn into_mut(self) -> Option<&'a mut Zval>

Converts the entry into a mutable reference to the value.

If you need multiple references to the OccupiedEntry, see get_mut.

§Returns

A result containing a mutable reference to the value with the entry’s lifetime, or an error if the key conversion failed.

§Errors

Returns an error if the key contains a null byte (for string keys).

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();
ht.insert("key", "value");

if let ext_php_rs::types::array::Entry::Occupied(entry) = ht.entry("key") {
    let value = entry.into_mut().unwrap();
    // value has the lifetime of the hashtable borrow
}
Source

pub fn insert<V: IntoZval>(&mut self, value: V) -> Result<Option<Zval>>

Sets the value of the entry, returning the old value.

§Parameters
  • value - The new value to set.
§Returns

A result containing the old value (shallow cloned), or an error if the insertion failed.

§Errors

Returns an error if the value conversion to Zval fails or if the key contains a null byte (for string keys).

§Example
use ext_php_rs::types::{ZendHashTable, Zval};

let mut ht = ZendHashTable::new();
ht.insert("key", "old");

if let ext_php_rs::types::array::Entry::Occupied(mut entry) = ht.entry("key") {
    let old = entry.insert("new").unwrap();
    assert_eq!(old.as_ref().and_then(Zval::str), Some("old"));
}
Source

pub fn remove_entry(self) -> (ArrayKey<'k>, Option<Zval>)

Takes ownership of the key and value from the entry, removing it from the hashtable.

§Returns

A result containing a tuple of the key and the removed value (shallow cloned), or an error if the removal failed.

§Errors

Returns an error if the key contains a null byte (for string keys).

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();
ht.insert("key", "value");

if let ext_php_rs::types::array::Entry::Occupied(entry) = ht.entry("key") {
    let (key, value) = entry.remove_entry();
    assert!(ht.get("key").is_none());
}
Source

pub fn remove(self) -> Option<Zval>

Removes the value from the entry, returning it.

§Returns

A result containing the removed value (shallow cloned), or an error if the removal failed.

§Errors

Returns an error if the key contains a null byte (for string keys).

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();
ht.insert("key", "value");

if let ext_php_rs::types::array::Entry::Occupied(entry) = ht.entry("key") {
    let value = entry.remove().unwrap();
    assert_eq!(value.str(), Some("value"));
}

Auto Trait Implementations§

§

impl<'a, 'k> Freeze for OccupiedEntry<'a, 'k>

§

impl<'a, 'k> RefUnwindSafe for OccupiedEntry<'a, 'k>

§

impl<'a, 'k> !Send for OccupiedEntry<'a, 'k>

§

impl<'a, 'k> !Sync for OccupiedEntry<'a, 'k>

§

impl<'a, 'k> Unpin for OccupiedEntry<'a, 'k>

§

impl<'a, 'k> !UnwindSafe for OccupiedEntry<'a, 'k>

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.