Skip to content

Commit 6b37357

Browse files
committed
feat(array): Entry API: tests
1 parent 88e7b0c commit 6b37357

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

guide/src/types/zend_hashtable.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ in-place.
2020
# extern crate ext_php_rs;
2121
use ext_php_rs::prelude::*;
2222
use ext_php_rs::types::ZendHashTable;
23+
use ext_php_rs::boxed::ZBox;
2324
2425
#[php_function]
2526
pub fn create_array() -> ZBox<ZendHashTable> {
@@ -76,6 +77,7 @@ exists. This is similar to Rust's `std::collections::hash_map::Entry` API.
7677
# extern crate ext_php_rs;
7778
use ext_php_rs::prelude::*;
7879
use ext_php_rs::types::ZendHashTable;
80+
use ext_php_rs::boxed::ZBox;
7981
8082
#[php_function]
8183
pub fn entry_example() -> ZBox<ZendHashTable> {
@@ -119,6 +121,7 @@ The `entry()` method returns an `Entry` enum with two variants:
119121
# extern crate ext_php_rs;
120122
use ext_php_rs::prelude::*;
121123
use ext_php_rs::types::{ZendHashTable, Entry};
124+
use ext_php_rs::boxed::ZBox;
122125
123126
#[php_function]
124127
pub fn match_entry() -> ZBox<ZendHashTable> {
@@ -152,6 +155,7 @@ pub fn match_entry() -> ZBox<ZendHashTable> {
152155
# extern crate ext_php_rs;
153156
use ext_php_rs::prelude::*;
154157
use ext_php_rs::types::ZendHashTable;
158+
use ext_php_rs::boxed::ZBox;
155159
156160
#[php_function]
157161
pub fn count_words(words: Vec<String>) -> ZBox<ZendHashTable> {
@@ -175,19 +179,18 @@ pub fn count_words(words: Vec<String>) -> ZBox<ZendHashTable> {
175179

176180
#### Caching computed values
177181

182+
This example demonstrates using `or_insert_with_key` for lazy computation:
183+
178184
```rust,no_run
179-
# #![cfg_attr(windows, feature(abi_vectorcall))]
180185
# extern crate ext_php_rs;
181-
use ext_php_rs::prelude::*;
182186
use ext_php_rs::types::ZendHashTable;
183187
184188
fn expensive_computation(key: &str) -> String {
185189
format!("computed_{}", key)
186190
}
187191
188-
#[php_function]
189-
pub fn get_or_compute(cache: &mut ZendHashTable, key: String) -> String {
190-
let value = cache.entry(key.as_str())
192+
fn get_or_compute(cache: &mut ZendHashTable, key: &str) -> String {
193+
let value = cache.entry(key)
191194
.or_insert_with_key(|k| expensive_computation(&k.to_string()))
192195
.unwrap();
193196
@@ -198,15 +201,14 @@ pub fn get_or_compute(cache: &mut ZendHashTable, key: String) -> String {
198201

199202
#### Updating existing values
200203

204+
This example shows how to conditionally update a value only if the key exists:
205+
201206
```rust,no_run
202-
# #![cfg_attr(windows, feature(abi_vectorcall))]
203207
# extern crate ext_php_rs;
204-
use ext_php_rs::prelude::*;
205208
use ext_php_rs::types::{ZendHashTable, Entry};
206209
207-
#[php_function]
208-
pub fn update_if_exists(ht: &mut ZendHashTable, key: String, new_value: String) -> bool {
209-
match ht.entry(key.as_str()) {
210+
fn update_if_exists(ht: &mut ZendHashTable, key: &str, new_value: &str) -> bool {
211+
match ht.entry(key) {
210212
Entry::Occupied(mut entry) => {
211213
entry.insert(new_value).unwrap();
212214
true

src/types/array/entry.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,7 @@ fn get_value_mut<'a>(key: &ArrayKey<'_>, ht: &'a mut ZendHashTable) -> Option<&'
596596
}
597597

598598
/// Helper function to insert a value into the hashtable by key.
599-
fn insert_value<V: IntoZval>(
600-
key: &ArrayKey<'_>,
601-
ht: &mut ZendHashTable,
602-
value: V,
603-
) -> Result<()> {
599+
fn insert_value<V: IntoZval>(key: &ArrayKey<'_>, ht: &mut ZendHashTable, value: V) -> Result<()> {
604600
let mut val = value.into_zval(false)?;
605601
match key {
606602
ArrayKey::Long(index) => {
@@ -620,7 +616,9 @@ fn insert_value<V: IntoZval>(
620616
};
621617
}
622618
ArrayKey::Str(key) => {
623-
unsafe { zend_hash_str_update(ht, CString::new(*key)?.as_ptr(), key.len(), &raw mut val) };
619+
unsafe {
620+
zend_hash_str_update(ht, CString::new(*key)?.as_ptr(), key.len(), &raw mut val)
621+
};
624622
}
625623
}
626624
val.release();

0 commit comments

Comments
 (0)