This commit is contained in:
Daniil Gentili 2023-11-24 18:48:13 +01:00
parent 0f401865f7
commit f449f4e4e1
2 changed files with 4 additions and 10 deletions

View File

@ -20,7 +20,7 @@ that implements the `Traversable` interface. This means that any value that can
#[php_function]
pub fn test_iterable(mut iterable: Iterable) {
for (k, v) in iterable.iter().expect("cannot rewind iterator") {
println!("k: {:?} v: {}", k, v.string().unwrap());
println!("k: {} v: {}", k.string().unwrap(), v.string().unwrap());
}
}
# fn main() {}
@ -34,14 +34,11 @@ pub fn test_iterable(mut iterable: Iterable) {
$generator = function() {
yield 'hello' => 'world';
yield 'rust' => 'php';
yield 'okk';
yield new class {} => new class {};
};
$array = [
'hello' => 'world',
'rust' => 'php',
'okk',
];
test_iterable($generator());
@ -53,8 +50,6 @@ Output:
```text
k: hello v: world
k: rust v: php
k: 0 v: okk
k: hello v: world
k: rust v: php
k: 0 v: okk
```

View File

@ -23,7 +23,9 @@ If you want a more universal `iterable` type that also supports arrays, see [Ite
#[php_function]
pub fn test_iterator(iterator: &mut ZendIterator) {
for (k, v) in iterator.iter().expect("cannot rewind iterator") {
println!("k: {:?} v: {}", k, v.string().unwrap());
// Note that the key can be anything, even an object
// when iterating over Traversables!
println!("k: {} v: {}", k.string().unwrap(), v.string().unwrap());
}
}
# fn main() {}
@ -37,8 +39,6 @@ pub fn test_iterator(iterator: &mut ZendIterator) {
$generator = function() {
yield 'hello' => 'world';
yield 'rust' => 'php';
yield 'okk';
yield new class {} => new class {};
};
test_iterator($generator());
@ -49,5 +49,4 @@ Output:
```text
k: hello v: world
k: rust v: php
k: 0 v: okk
```