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>
impl<'a, 'k> OccupiedEntry<'a, 'k>
Sourcepub fn key(&self) -> &ArrayKey<'k>
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"));
}Sourcepub fn get(&self) -> Option<&Zval>
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"));
}Sourcepub fn get_mut(&mut self) -> Option<&mut Zval>
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);
}
}
}Sourcepub fn into_mut(self) -> Option<&'a mut Zval>
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
}Sourcepub fn insert<V: IntoZval>(&mut self, value: V) -> Result<Option<Zval>>
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"));
}Sourcepub fn remove_entry(self) -> (ArrayKey<'k>, Option<Zval>)
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());
}Sourcepub fn remove(self) -> Option<Zval>
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"));
}