Skip to content

Commit 31a8c6a

Browse files
committed
added optional variadic arguments example in docs and added directly passed arguments tests
1 parent 5338356 commit 31a8c6a

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

guide/src/macros/function.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,27 @@ the `...$args` syntax.
108108
# use ext_php_rs::prelude::*;
109109
# use ext_php_rs::types::Zval;
110110
/// This can be called from PHP as `add(1, 2, 3, 4, 5)`
111+
/// note: it requires to set numbers with one or arguments
111112
#[php_function]
112113
pub fn add(number: u32, numbers:&[&Zval]) -> u32 {
114+
println!("Extra numbers: {:?}", numbers);
115+
// numbers is a slice of 4 Zvals all of type long
116+
number
117+
}
118+
119+
/// Having optional numbers can be done like:
120+
/// This can be called from PHP as `add(1)`, with no addional numbers given
121+
#[php_function(optional = "numbers")]
122+
pub fn add(number: u32, numbers:&[&Zval]) -> u32 {
123+
println!("Optional numbers: {:?}", numbers);
113124
// numbers is a slice of 4 Zvals all of type long
114125
number
115126
}
116127
# fn main() {}
117128
```
118129

130+
Checkout more example in our [tests](https://github.com/davidcole1340/ext-php-rs/tree/master/tests/src/integration/variadic_args.php) location.
131+
119132
## Returning `Result<T, E>`
120133

121134
You can also return a `Result` from the function. The error variant will be

tests/src/integration/variadic_args.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@
2525
// } catch (ArgumentCountError $e) {
2626
// var_dump($e->getMessage());
2727
// }
28+
29+
// Values directly passed
30+
test_variadic_add_optional(1, 2, 3); // 1
31+
32+
$count = test_variadic_add_optional(11); // 11
33+
assert($count === 11, 'Allow only one argument');
34+
35+
$numbers = test_variadic_add_required(1, 2, 3, 4);
36+
assert($numbers === [1, 2, 3, 4], 'Must return a array of numbers');
37+
38+
$types = test_variadic_all_types('a', 1, ['abc', 'def', 0.01], true, new stdClass);
39+
assert(gettype(end($types[2])) === 'double', 'Type of argument 2 and its last element should be a float of 0.01');
40+
assert($types[3], 'Arg 4 should be boolean true');
41+
assert($types[4] instanceof stdClass, 'Last argument is an instance of an StdClass');

tests/src/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,33 @@ pub fn test_variadic_args(params: &[&Zval]) -> Vec<Zval> {
8888
params.iter().map(|x| x.shallow_clone()).collect()
8989
}
9090

91+
#[php_function(optional = "numbers")]
92+
pub fn test_variadic_add_optional(number: u32, numbers:&[&Zval]) -> u32 {
93+
println!("Optional numbers: {:?}", numbers);
94+
// numbers is a slice of 4 Zvals all of type long
95+
number
96+
}
97+
98+
#[php_function]
99+
pub fn test_variadic_add_required(numbers:&[&Zval]) -> Vec<Zval> {
100+
// numbers is a slice of 4 Zvals all of type long
101+
// *numbers[0].as_number()
102+
numbers.iter().map(|x| x.shallow_clone()).collect()
103+
}
104+
105+
#[php_function(optional = "everything")]
106+
pub fn test_variadic_all_types(everything: &[&Zval]) -> Vec<Zval> {
107+
everything.iter().map(|x| x.shallow_clone()).collect()
108+
}
109+
91110
// Rust type &[&Zval] must be converted because to Vec<Zval> because
92111
// of lifetime hell... #[php_function] does not support lifetime.
93112
// error1: error[E0261]: use of undeclared lifetime name `'a`
94113
// error2: lifetime `'a` is missing in item created through this procedural
95114
// macro #[php_function]
96-
// pub fn test_variadic_optional2_args<'a>(params: &'a [&'a Zval]) -> &'a [&'a
97-
// Zval] { println!("Hiero: {:#?}", params);
115+
// pub fn test_variadic_optional_args<'a>(params: &'a [&'a Zval]) -> &'a [&'a
116+
// Zval] {
117+
// println!("Params: {:#?}", params);
98118
// params
99119
// }
100120

0 commit comments

Comments
 (0)