Skip to content

Commit d4c7814

Browse files
authored
Remove the implement of From<Result> to ZVal. (#92)
1 parent 941224b commit d4c7814

File tree

20 files changed

+266
-205
lines changed

20 files changed

+266
-205
lines changed

examples/complex/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use phper::{
1818
php_get_module,
1919
values::ZVal,
2020
};
21-
use std::ffi::CStr;
21+
use std::{convert::Infallible, ffi::CStr};
2222

2323
fn say_hello(arguments: &mut [ZVal]) -> phper::Result<String> {
2424
let name = &mut arguments[0];
@@ -68,8 +68,7 @@ pub fn get_module() -> Module {
6868

6969
let complex_description = ZVal::from(ini_get::<Option<&CStr>>("complex.description"));
7070
arr.insert("complex.description", complex_description);
71-
72-
arr
71+
Ok::<_, Infallible>(arr)
7372
});
7473

7574
// register classes

examples/http-client/src/client.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use phper::{
1515
functions::Argument,
1616
};
1717
use reqwest::blocking::{Client, ClientBuilder};
18-
use std::{mem::take, time::Duration};
18+
use std::{convert::Infallible, mem::take, time::Duration};
1919

2020
const HTTP_CLIENT_BUILDER_CLASS_NAME: &str = "HttpClient\\HttpClientBuilder";
2121
const HTTP_CLIENT_CLASS_NAME: &str = "HttpClient\\HttpClient";
@@ -66,7 +66,9 @@ pub fn make_client_class() -> ClassEntity<Option<Client>> {
6666
let mut class =
6767
ClassEntity::<Option<Client>>::new_with_default_state_constructor(HTTP_CLIENT_CLASS_NAME);
6868

69-
class.add_method("__construct", Visibility::Private, |_, _| {});
69+
class.add_method("__construct", Visibility::Private, |_, _| {
70+
Ok::<_, Infallible>(())
71+
});
7072

7173
class
7274
.add_method("get", Visibility::Public, |this, arguments| {

examples/http-client/src/request.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use phper::{
1414
errors::ThrowObject,
1515
};
1616
use reqwest::blocking::RequestBuilder;
17-
use std::mem::take;
17+
use std::{convert::Infallible, mem::take};
1818

1919
pub const REQUEST_BUILDER_CLASS_NAME: &str = "HttpClient\\RequestBuilder";
2020

@@ -23,7 +23,9 @@ pub fn make_request_builder_class() -> ClassEntity<Option<RequestBuilder>> {
2323
REQUEST_BUILDER_CLASS_NAME,
2424
);
2525

26-
class.add_method("__construct", Visibility::Private, |_, _| {});
26+
class.add_method("__construct", Visibility::Private, |_, _| {
27+
Ok::<_, Infallible>(())
28+
});
2729

2830
class.add_method("send", Visibility::Public, |this, _arguments| {
2931
let state = take(this.as_mut_state());

phper-build/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
use phper_sys::*;
1515

16+
pub fn register_all() {
17+
register_link_args();
18+
register_configures();
19+
}
20+
1621
/// Register useful rust cfg for project using phper.
1722
pub fn register_configures() {
1823
// versions
@@ -37,3 +42,11 @@ pub fn register_configures() {
3742
println!("cargo:rustc-cfg=phper_zts");
3843
}
3944
}
45+
46+
pub fn register_link_args() {
47+
#[cfg(target_os = "macos")]
48+
{
49+
println!("cargo:rustc-link-arg=-undefined");
50+
println!("cargo:rustc-link-arg=dynamic_lookup");
51+
}
52+
}

phper-doc/doc/_04_zval/index.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ Otherwise, there are also composite types that implement `From`.
4040
- `From<Option<T>>`: if Some(T), T will be converted to PHP type like `From<T>`,
4141
or `None` wll be converted to `null`.
4242

43-
- `From<Result<T, E>>`: if Ok(T), T will be converted to PHP type like `From<T>`,
44-
or `Err(e)` will throw an Exception by calling `zend_throw_exception`.
45-
4643
### Example
4744

4845
```rust,no_run

phper-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn main() {
5959
builder
6060
.generate()
6161
.expect("Unable to generate bindings")
62-
.write_to_file(&generated_path)
62+
.write_to_file(generated_path)
6363
.expect("Unable to write output file");
6464
}
6565

phper-test/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn test_php_scripts_with_condition(
6969
stderr.push_str("<empty>");
7070
}
7171

72-
println!(
72+
eprintln!(
7373
"===== command =====\n{} {}\n===== stdout ======\n{}\n===== stderr ======\n{}",
7474
&context.php_bin,
7575
cmd.get_args().join(" "),
@@ -79,7 +79,7 @@ pub fn test_php_scripts_with_condition(
7979
#[cfg(target_os = "linux")]
8080
if output.status.code().is_none() {
8181
use std::os::unix::process::ExitStatusExt;
82-
println!(
82+
eprintln!(
8383
"===== signal ======\nExitStatusExt is None, the signal is: {:?}",
8484
output.status.signal()
8585
);

phper-test/src/fpm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ pub fn setup(lib_path: impl AsRef<Path>) {
6666
"-y",
6767
fpm_conf_file.path().to_str().unwrap(),
6868
];
69-
println!("===== setup php-fpm =====\n{}", argv.join(" "));
69+
eprintln!("===== setup php-fpm =====\n{}", argv.join(" "));
7070

7171
let child = spawn_command(&argv, Some(Duration::from_secs(3)));
7272
let log = fs::read_to_string("/tmp/.php-fpm.log").unwrap();
73-
println!("===== php-fpm log =====\n{}", log);
73+
eprintln!("===== php-fpm log =====\n{}", log);
7474
// fs::remove_file("/tmp/.php-fpm.log").unwrap();
7575

7676
Mutex::new(FpmHandle {
@@ -165,7 +165,7 @@ pub fn test_fpm_request(
165165
.unwrap_or_else(|_| "<not utf8 string>".to_owned())
166166
};
167167

168-
println!(
168+
eprintln!(
169169
"===== request =====\n{}\n===== stdout ======\n{}\n===== stderr ======\n{}",
170170
request_uri,
171171
f(stdout),

phper/src/arrays.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ pub enum IterKey<'a> {
375375
ZStr(&'a ZStr),
376376
}
377377

378+
#[allow(clippy::unnecessary_cast)]
378379
unsafe extern "C" fn for_each_callback(
379380
idx: zend_ulong, key: *mut zend_string, val: *mut zval, argument: *mut c_void,
380381
) {

phper/src/classes.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use crate::{
1414
arrays::ZArr,
15-
errors::{ClassNotFoundError, InitializeObjectError},
15+
errors::{ClassNotFoundError, InitializeObjectError, Throwable},
1616
functions::{Function, FunctionEntry, Method, MethodEntity},
1717
objects::{StateObj, StateObject, ZObj, ZObject},
1818
strings::ZStr,
@@ -206,24 +206,26 @@ impl<T: 'static> ClassEntity<T> {
206206
}
207207
}
208208

209-
pub fn add_method<F, R>(
209+
pub fn add_method<F, Z, E>(
210210
&mut self, name: impl Into<String>, vis: Visibility, handler: F,
211211
) -> &mut MethodEntity
212212
where
213-
F: Fn(&mut StateObj<T>, &mut [ZVal]) -> R + Send + Sync + 'static,
214-
R: Into<ZVal> + 'static,
213+
F: Fn(&mut StateObj<T>, &mut [ZVal]) -> Result<Z, E> + 'static,
214+
Z: Into<ZVal> + 'static,
215+
E: Throwable + 'static,
215216
{
216217
self.method_entities
217218
.push(MethodEntity::new(name, Rc::new(Method::new(handler)), vis));
218219
self.method_entities.last_mut().unwrap()
219220
}
220221

221-
pub fn add_static_method<F, R>(
222+
pub fn add_static_method<F, Z, E>(
222223
&mut self, name: impl Into<String>, vis: Visibility, handler: F,
223224
) -> &mut MethodEntity
224225
where
225-
F: Fn(&mut [ZVal]) -> R + Send + Sync + 'static,
226-
R: Into<ZVal> + 'static,
226+
F: Fn(&mut [ZVal]) -> Result<Z, E> + 'static,
227+
Z: Into<ZVal> + 'static,
228+
E: Throwable + 'static,
227229
{
228230
let mut entity = MethodEntity::new(name, Rc::new(Function::new(handler)), vis);
229231
entity.r#static(true);

0 commit comments

Comments
 (0)