pub enum Entry<'a, 'k> {
Occupied(OccupiedEntry<'a, 'k>),
Vacant(VacantEntry<'a, 'k>),
}Expand description
A view into a single entry in a ZendHashTable, which may either be vacant or
occupied.
This enum is constructed from the entry method on ZendHashTable.
Variants§
Implementations§
Source§impl<'a, 'k> Entry<'a, 'k>
impl<'a, 'k> Entry<'a, 'k>
Sourcepub fn or_insert<V: IntoZval>(self, default: V) -> Result<&'a mut Zval>
pub fn or_insert<V: IntoZval>(self, default: V) -> Result<&'a mut Zval>
Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.
§Parameters
default- The default value to insert if the entry is vacant.
§Returns
A result containing a mutable reference to the value, 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;
let mut ht = ZendHashTable::new();
ht.entry("key").or_insert("default value");
assert_eq!(ht.get("key").and_then(|v| v.str()), Some("default value"));Sourcepub fn or_insert_with<V: IntoZval, F: FnOnce() -> V>(
self,
default: F,
) -> Result<&'a mut Zval>
pub fn or_insert_with<V: IntoZval, F: FnOnce() -> V>( self, default: F, ) -> Result<&'a mut Zval>
Ensures a value is in the entry by inserting the result of the default function if empty, and returns a mutable reference to the value in the entry.
§Parameters
default- A function that returns the default value to insert.
§Returns
A result containing a mutable reference to the value, 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;
let mut ht = ZendHashTable::new();
ht.entry("key").or_insert_with(|| "computed value");Sourcepub fn or_insert_with_key<V: IntoZval, F: FnOnce(&ArrayKey<'k>) -> V>(
self,
default: F,
) -> Result<&'a mut Zval>
pub fn or_insert_with_key<V: IntoZval, F: FnOnce(&ArrayKey<'k>) -> V>( self, default: F, ) -> Result<&'a mut Zval>
Ensures a value is in the entry by inserting the result of the default function if empty. The function receives a reference to the key.
§Parameters
default- A function that takes the key and returns the default value.
§Returns
A result containing a mutable reference to the value, 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;
let mut ht = ZendHashTable::new();
ht.entry("key").or_insert_with_key(|k| format!("value for {}", k));Sourcepub fn key(&self) -> &ArrayKey<'k>
pub fn key(&self) -> &ArrayKey<'k>
Returns a reference to this entry’s key.
§Example
use ext_php_rs::types::{ZendHashTable, ArrayKey};
let mut ht = ZendHashTable::new();
assert_eq!(ht.entry("key").key(), &ArrayKey::Str("key"));Sourcepub fn and_modify<F: FnOnce(&mut Zval)>(self, f: F) -> Self
pub fn and_modify<F: FnOnce(&mut Zval)>(self, f: F) -> Self
Provides in-place mutable access to an occupied entry before any potential inserts into the map.
§Parameters
f- A function that modifies the value in place.
§Returns
The entry, allowing for method chaining.
§Example
use ext_php_rs::types::ZendHashTable;
let mut ht = ZendHashTable::new();
ht.insert("counter", 0i64);
ht.entry("counter")
.and_modify(|v| {
if let Some(n) = v.long() {
v.set_long(n + 1);
}
})
.or_insert(0i64);Source§impl<'a, 'k> Entry<'a, 'k>where
'k: 'a,
impl<'a, 'k> Entry<'a, 'k>where
'k: 'a,
Sourcepub fn or_default(self) -> Result<&'a mut Zval>
pub fn or_default(self) -> Result<&'a mut Zval>
Ensures a value is in the entry by inserting the default value if empty, and returns a mutable reference to the value in the entry.
This is a convenience method that uses Default::default() as the
default value.
§Returns
A result containing a mutable reference to the value, or an error if the insertion 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();
// Inserts a null Zval if the key doesn't exist
ht.entry("key").or_default();