@@ -20,6 +20,7 @@ in-place.
2020# extern crate ext_php_rs;
2121use ext_php_rs::prelude::*;
2222use ext_php_rs::types::ZendHashTable;
23+ use ext_php_rs::boxed::ZBox;
2324
2425#[php_function]
2526pub 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;
7778use ext_php_rs::prelude::*;
7879use ext_php_rs::types::ZendHashTable;
80+ use ext_php_rs::boxed::ZBox;
7981
8082#[php_function]
8183pub 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;
120122use ext_php_rs::prelude::*;
121123use ext_php_rs::types::{ZendHashTable, Entry};
124+ use ext_php_rs::boxed::ZBox;
122125
123126#[php_function]
124127pub fn match_entry() -> ZBox<ZendHashTable> {
@@ -152,6 +155,7 @@ pub fn match_entry() -> ZBox<ZendHashTable> {
152155# extern crate ext_php_rs;
153156use ext_php_rs::prelude::*;
154157use ext_php_rs::types::ZendHashTable;
158+ use ext_php_rs::boxed::ZBox;
155159
156160#[php_function]
157161pub 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::*;
182186use ext_php_rs::types::ZendHashTable;
183187
184188fn 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::*;
205208use 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
0 commit comments