-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from joelwurtz/feat/iterator
feat(iterator): add helper for zend_object_iterator and iterable type
- Loading branch information
Showing
15 changed files
with
818 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# `Iterable` | ||
|
||
`Iterable`s are represented either by an `array` or `Traversable` type. | ||
|
||
| `T` parameter | `&T` parameter | `T` Return type | `&T` Return type | PHP representation | | ||
|---------------|----------------|-----------------| ---------------- |----------------------------------| | ||
| Yes | No | No | No | `ZendHashTable` or `ZendIterator` | | ||
|
||
Converting from a zval to a `Iterable` is valid when the value is either an array or an object | ||
that implements the `Traversable` interface. This means that any value that can be used in a | ||
`foreach` loop can be converted into a `Iterable`. | ||
|
||
## Rust example | ||
|
||
```rust,no_run | ||
# #![cfg_attr(windows, feature(abi_vectorcall))] | ||
# extern crate ext_php_rs; | ||
# use ext_php_rs::prelude::*; | ||
# use ext_php_rs::types::Iterable; | ||
#[php_function] | ||
pub fn test_iterable(mut iterable: Iterable) { | ||
for (k, v) in iterable.iter().expect("cannot rewind iterator") { | ||
println!("k: {} v: {}", k.string().unwrap(), v.string().unwrap()); | ||
} | ||
} | ||
# fn main() {} | ||
``` | ||
|
||
## PHP example | ||
|
||
```php | ||
<?php | ||
|
||
$generator = function() { | ||
yield 'hello' => 'world'; | ||
yield 'rust' => 'php'; | ||
}; | ||
|
||
$array = [ | ||
'hello' => 'world', | ||
'rust' => 'php', | ||
]; | ||
|
||
test_iterable($generator()); | ||
test_iterable($array); | ||
``` | ||
|
||
Output: | ||
|
||
```text | ||
k: hello v: world | ||
k: rust v: php | ||
k: hello v: world | ||
k: rust v: php | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# `ZendIterator` | ||
|
||
`ZendIterator`s are represented by the `Traversable` type. | ||
|
||
| `T` parameter | `&T` parameter | `T` Return type | `&T` Return type | PHP representation | | ||
|---------------| -------------- |-----------------| ---------------- | ------------------ | | ||
| No | Yes | No | No | `ZendIterator` | | ||
|
||
Converting from a zval to a `ZendIterator` is valid when there is an associated iterator to | ||
the variable. This means that any value, at the exception of an `array`, that can be used in | ||
a `foreach` loop can be converted into a `ZendIterator`. As an example, a `Generator` can be | ||
used but also a the result of a `query` call with `PDO`. | ||
|
||
If you want a more universal `iterable` type that also supports arrays, see [Iterable](./iterable.md). | ||
|
||
## Rust example | ||
|
||
```rust,no_run | ||
# #![cfg_attr(windows, feature(abi_vectorcall))] | ||
# extern crate ext_php_rs; | ||
# use ext_php_rs::prelude::*; | ||
# use ext_php_rs::types::ZendIterator; | ||
#[php_function] | ||
pub fn test_iterator(iterator: &mut ZendIterator) { | ||
for (k, v) in iterator.iter().expect("cannot rewind iterator") { | ||
// 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() {} | ||
``` | ||
|
||
## PHP example | ||
|
||
```php | ||
<?php | ||
|
||
$generator = function() { | ||
yield 'hello' => 'world'; | ||
yield 'rust' => 'php'; | ||
}; | ||
|
||
test_iterator($generator()); | ||
``` | ||
|
||
Output: | ||
|
||
```text | ||
k: hello v: world | ||
k: rust v: php | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.