Skip to content

Commit aed061d

Browse files
Added section on exceptions to guide (#81)
1 parent daef57b commit aed061d

File tree

7 files changed

+65
-25
lines changed

7 files changed

+65
-25
lines changed

guide/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
- [Structs](./macros/structs.md)
2222
- [`impl`s](./macros/impl.md)
2323
- [Constants](./macros/constant.md)
24+
- [Exceptions](./exceptions.md)

guide/src/exceptions.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Exceptions
2+
3+
Exceptions can be thrown from Rust to PHP. The inverse (catching a PHP exception
4+
in Rust) is currently being worked on.
5+
6+
## Throwing exceptions
7+
8+
[`PhpException`] is the type that represents an exception. It contains the
9+
message contained in the exception, the type of exception and a status code to
10+
go along with the exception.
11+
12+
You can create a new exception with the `new()`, `default()`, or
13+
`from_class::<T>()` methods. `Into<PhpException>` is implemented for `String`
14+
and `&str`, which creates an exception of the type `Exception` with a code of 0.
15+
It may be useful to implement `Into<PhpException>` for your error type.
16+
17+
Calling the `throw()` method on a `PhpException` attempts to throw the exception
18+
in PHP. This function can fail if the type of exception is invalid (i.e. does
19+
not implement `Exception` or `Throwable`). Upon success, nothing will be
20+
returned.
21+
22+
`IntoZval` is also implemented for `Result<T, E>`, where `T: IntoZval` and
23+
`E: Into<PhpException>`. If the result contains the error variant, the exception
24+
is thrown. This allows you to return a result from a PHP function annotated with
25+
the `#[php_function]` attribute.
26+
27+
### Examples
28+
29+
```rust
30+
# extern crate ext_php_rs;
31+
use ext_php_rs::prelude::*;
32+
use std::convert::TryInto;
33+
34+
// Trivial example - PHP represents all integers as `u64` on 64-bit systems
35+
// so the `u32` would be converted back to `u64`, but that's okay for an example.
36+
#[php_function]
37+
pub fn something_fallible(n: u64) -> PhpResult<u32> {
38+
let n: u32 = n.try_into().map_err(|_| "Could not convert into u32")?;
39+
Ok(n)
40+
}
41+
42+
#[php_module]
43+
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
44+
module
45+
}
46+
```
47+
48+
[`PhpException`]: https://docs.rs/ext-php-rs/0.5.0/ext_php_rs/php/exceptions/struct.PhpException.html

guide/src/macros/function.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,8 @@ pub fn greet(name: String, age: Option<i32>, description: Option<String>) -> Str
8888
}
8989
```
9090

91-
## Throwing exceptions
91+
## Returning `Result<T, E>`
9292

93-
Exceptions can be thrown from inside a function which returns a `Result<T, E>`,
94-
where `E` implements `Into<PhpException>`. The `PhpException` class allows you
95-
to customise the type of exception thrown, along with the exception code and
96-
message.
97-
98-
By default, `String` and `&str` are both implemented with `Into<PhpException>`,
99-
and in both cases a regular `Exception` is thrown.
100-
101-
```rust
102-
# extern crate ext_php_rs;
103-
# use ext_php_rs::prelude::*;
104-
#[php_function]
105-
pub fn example_exception() -> Result<i64, &'static str> {
106-
Err("Bad!!!")
107-
}
108-
```
93+
You can also return a `Result` from the function. The error variant will be
94+
translated into an exception and thrown. See the section on
95+
[exceptions](../exceptions.md) for more details.

guide/src/types/closure.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
# Closure
22

33
Rust closures can be passed to PHP through a wrapper class `PhpClosure`. The
4-
Rust closure must be static (i.e. can only reference things with a `static`
4+
Rust closure must be static (i.e. can only reference things with a `'static`
55
lifetime, so not `self` in methods), and can take up to 8 parameters, all of
66
which must implement `FromZval`. The return type must implement `IntoZval`.
77

88
Passing closures from Rust to PHP is feature-gated behind the `closure` feature.
9+
Enable it in your `Cargo.toml`:
10+
11+
```toml
12+
ext-php-rs = { version = "...", features = ["closure"] }
13+
```
914

1015
PHP callables (which includes closures) can be passed to Rust through the
1116
`Callable` type. When calling a callable, you must provide it with a `Vec` of
1217
arguemnts, all of which must implement `IntoZval` and `Clone`.
1318

14-
| `T` parameter | `&T` parameter | `T` Return type | `&T` Return type | PHP representation |
15-
| ------------- | -------------- | -------------------------------------- | ---------------- | ------------------------------------------------------------------------------------------ |
16-
| `Callable` | No | `Closure` and `Callable` for functions | No | Callables are implemented in PHP, closures are represented as an instance of `PhpClosure`. |
19+
| `T` parameter | `&T` parameter | `T` Return type | `&T` Return type | PHP representation |
20+
| ------------- | -------------- | ----------------------------------- | ---------------- | ------------------------------------------------------------------------------------------ |
21+
| `Callable` | No | `Closure`, `Callable` for functions | No | Callables are implemented in PHP, closures are represented as an instance of `PhpClosure`. |
1722

1823
Internally, when you enable the `closure` feature, a class `PhpClosure` is
1924
registered alongside your other classes:

guide/src/types/hashmap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
| `T` parameter | `&T` parameter | `T` Return type | PHP representation |
66
| ------------- | -------------- | --------------- | ------------------ |
7-
| Yes | No | Yes | `ZendHashTable` |
7+
| Yes | No | Yes | `HashTable` |
88

99
Converting from a zval to a `HashMap` is valid when the key is a `String`, and
1010
the value implements `FromZval`. The key and values are copied into Rust types

guide/src/types/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ Return types can also include:
3131
as an exception.
3232

3333
For a type to be returnable, it must implement `IntoZval`, while for it to be
34-
valid as a parameter, it must implement `FromZval` (and `TryFrom<&Zval>` by
35-
proxy).
34+
valid as a parameter, it must implement `FromZval`.

guide/src/types/vec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ vector. The internal representation of a PHP array is discussed below.
66

77
| `T` parameter | `&T` parameter | `T` Return type | PHP representation |
88
| ------------- | -------------- | --------------- | ------------------ |
9-
| Yes | No | Yes | `ZendHashTable` |
9+
| Yes | No | Yes | `HashTable` |
1010

1111
Internally, PHP arrays are hash tables where the key can be an unsigned long or
1212
a string. Zvals are contained inside arrays therefore the data does not have to

0 commit comments

Comments
 (0)