Skip to content

Commit 1193cc0

Browse files
Replace struct derive macro with attribute macro (#54)
* Replace `ZendObjectOverride` derive macro with `#[php_class]` * Updated guide with `#[php_class]` macro * Panic rather than return error when implementing interface It's gonna panic either way so might as well make it easier for the builder * Remove `libc` dependency
1 parent 7f1e952 commit 1193cc0

File tree

19 files changed

+452
-248
lines changed

19 files changed

+452
-248
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ edition = "2018"
1111
categories = ["api-bindings"]
1212

1313
[dependencies]
14-
libc = "0.2.88"
1514
bitflags = "1.2.1"
1615
ext-php-rs-derive = { version = "=0.2.0", path = "./ext-php-rs-derive" }
1716

example/skel/src/lib.rs

Lines changed: 99 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,194 +1,128 @@
11
mod allocator;
22

3-
use std::{collections::HashMap, convert::TryFrom};
3+
use std::{collections::HashMap, convert::TryFrom, mem::MaybeUninit};
44

55
use allocator::PhpAllocator;
66
use ext_php_rs::{
77
call_user_func, info_table_end, info_table_row, info_table_start,
88
php::{
9+
class::ClassEntry,
910
exceptions::PhpException,
1011
module::ModuleEntry,
1112
types::{array::ZendHashTable, callable::Callable, zval::Zval},
1213
},
14+
php_class,
1315
prelude::*,
1416
};
1517

1618
#[global_allocator]
1719
static GLOBAL: PhpAllocator = PhpAllocator::new();
1820

19-
#[php_function]
20-
pub fn hello_world() -> String {
21-
let call = Callable::try_from_name("strpos").unwrap();
22-
23-
eprintln!("im callin");
24-
let val = call.try_call(vec![&"hello world", &"w"]);
25-
dbg!(val);
26-
"Ok".into()
27-
}
28-
29-
#[derive(Debug, Default, ZendObjectHandler)]
30-
struct Human {
31-
name: String,
32-
age: i32,
33-
}
34-
35-
#[php_impl]
36-
impl Human {
37-
const AGE_LIMIT: i32 = 100;
38-
39-
#[optional(age)]
40-
#[defaults(age = 10)]
41-
pub fn __construct(&mut self, name: String, age: i32) {
42-
self.name = name;
43-
self.age = age;
44-
}
45-
46-
pub fn get_name(&self) -> String {
47-
self.name.to_string()
48-
}
49-
50-
pub fn get_age(&self) -> i32 {
51-
self.age
52-
}
53-
54-
pub fn get_age_limit() -> i32 {
55-
Self::AGE_LIMIT
56-
}
57-
}
58-
59-
#[derive(Debug, ZendObjectHandler)]
60-
struct Test {
61-
a: u32,
62-
b: u32,
63-
}
64-
65-
#[php_impl]
66-
impl Test {
67-
const TEST: &'static str = "Hello, world!";
68-
69-
pub fn __construct(&self) {
70-
dbg!(self);
71-
println!("Inside constructor");
72-
}
73-
74-
pub fn set(&mut self, a: u32) {
75-
self.a = a;
76-
dbg!(self.get());
77-
}
78-
79-
pub fn get(&self) -> u32 {
80-
self.a
81-
}
82-
83-
pub fn call(&self, func: Callable) {
84-
let result = call_user_func!(func);
85-
86-
if let Ok(r) = result {
87-
dbg!(r);
88-
}
89-
90-
println!("Ready for call!");
91-
}
92-
}
93-
94-
impl Default for Test {
95-
fn default() -> Self {
96-
Self { a: 1, b: 2 }
97-
}
98-
}
99-
100-
#[php_const]
101-
const SKEL_TEST_CONST: &str = "Test constant";
102-
#[php_const]
103-
const SKEL_TEST_LONG_CONST: i32 = 1234;
104-
105-
#[php_function(optional = "z")]
106-
pub fn skeleton_version(x: ZendHashTable, y: f64, z: Option<f64>) -> String {
107-
dbg!(x, y, z);
108-
"Hello".into()
109-
}
110-
111-
#[php_function(optional = "z")]
112-
pub fn skeleton_array(
113-
arr: ZendHashTable,
114-
x: i32,
115-
y: f64,
116-
z: Option<f64>,
117-
) -> Result<ZendHashTable, String> {
118-
for (k, x, y) in arr.iter() {
119-
println!("{:?} {:?} {:?}", k, x, y.string());
120-
}
121-
122-
dbg!(x, y, z);
123-
124-
let mut new = ZendHashTable::new();
125-
new.insert("Hello", &"World")
126-
.map_err(|_| "Couldn't insert into hashtable")?;
127-
Ok(new)
128-
}
129-
130-
#[php_function(optional = "i", defaults(i = 5))]
131-
pub fn test_array(i: i32, b: Option<i32>) -> Vec<i32> {
132-
dbg!(i, b);
133-
vec![1, 2, 3, 4]
134-
}
135-
136-
#[php_function(optional = "offset", defaults(offset = 0))]
137-
pub fn rust_strpos(haystack: &str, needle: &str, offset: i64) -> Option<usize> {
138-
let haystack = haystack.chars().skip(offset as usize).collect::<String>();
139-
haystack.find(needle)
140-
}
141-
142-
#[php_function]
143-
pub fn example_exception() -> Result<i32, &'static str> {
144-
Err("Bad here")
145-
}
21+
// #[php_function]
22+
// pub fn hello_world() -> String {
23+
// let call = Callable::try_from_name("strpos").unwrap();
24+
25+
// eprintln!("im callin");
26+
// let val = call.try_call(vec![&"hello world", &"w"]);
27+
// dbg!(val);
28+
// "Ok".into()
29+
// }
30+
31+
// #[php_const]
32+
// const SKEL_TEST_CONST: &str = "Test constant";
33+
// #[php_const]
34+
// const SKEL_TEST_LONG_CONST: i32 = 1234;
35+
36+
// #[php_function(optional = "z")]
37+
// pub fn skeleton_version(x: ZendHashTable, y: f64, z: Option<f64>) -> String {
38+
// dbg!(x, y, z);
39+
// "Hello".into()
40+
// }
41+
42+
// #[php_function(optional = "z")]
43+
// pub fn skeleton_array(
44+
// arr: ZendHashTable,
45+
// x: i32,
46+
// y: f64,
47+
// z: Option<f64>,
48+
// ) -> Result<ZendHashTable, String> {
49+
// for (k, x, y) in arr.iter() {
50+
// println!("{:?} {:?} {:?}", k, x, y.string());
51+
// }
52+
53+
// dbg!(x, y, z);
54+
55+
// let mut new = ZendHashTable::new();
56+
// new.insert("Hello", &"World")
57+
// .map_err(|_| "Couldn't insert into hashtable")?;
58+
// Ok(new)
59+
// }
60+
61+
// #[php_function(optional = "i", defaults(i = 5))]
62+
// pub fn test_array(i: i32, b: Option<i32>) -> Vec<i32> {
63+
// dbg!(i, b);
64+
// vec![1, 2, 3, 4]
65+
// }
66+
67+
// #[php_function(optional = "offset", defaults(offset = 0))]
68+
// pub fn rust_strpos(haystack: &str, needle: &str, offset: i64) -> Option<usize> {
69+
// let haystack = haystack.chars().skip(offset as usize).collect::<String>();
70+
// haystack.find(needle)
71+
// }
72+
73+
// #[php_function]
74+
// pub fn example_exception() -> Result<i32, &'static str> {
75+
// Err("Bad here")
76+
// }
77+
78+
// #[php_function]
79+
// pub fn skel_unpack<'a>(
80+
// mut arr: HashMap<String, String>,
81+
// ) -> Result<HashMap<String, String>, PhpException<'a>> {
82+
// arr.insert("hello".into(), "not world".into());
83+
// Ok(arr)
84+
// }
85+
86+
// #[php_function]
87+
// pub fn test_extern() -> i32 {
88+
// // let y = unsafe { strpos("hello", "e", None) };
89+
// // dbg!(y);
90+
// // let x = unsafe { test_func() };
91+
// // dbg!(x.try_call(vec![]));
92+
// 0
93+
// }
94+
95+
// #[php_function]
96+
// pub fn test_lifetimes<'a>() -> ZendHashTable<'a> {
97+
// ZendHashTable::try_from(&HashMap::<String, String>::new()).unwrap()
98+
// }
14699

147100
#[php_function]
148-
pub fn skel_unpack<'a>(
149-
mut arr: HashMap<String, String>,
150-
) -> Result<HashMap<String, String>, PhpException<'a>> {
151-
arr.insert("hello".into(), "not world".into());
152-
Ok(arr)
101+
pub fn test_str(input: &str) -> &str {
102+
input
153103
}
154104

155-
#[php_function]
156-
pub fn test_extern() -> i32 {
157-
let y = unsafe { strpos("hello", "e", None) };
158-
dbg!(y);
159-
// let x = unsafe { test_func() };
160-
// dbg!(x.try_call(vec![]));
161-
0
162-
}
105+
// #[no_mangle]
106+
// pub extern "C" fn php_module_info(_module: *mut ModuleEntry) {
107+
// info_table_start!();
108+
// info_table_row!("skeleton extension", "enabled");
109+
// info_table_end!();
110+
// }
163111

164-
#[php_function]
165-
pub fn test_lifetimes<'a>() -> ZendHashTable<'a> {
166-
ZendHashTable::try_from(&HashMap::<String, String>::new()).unwrap()
167-
}
112+
#[php_class(name = "Redis\\Exception\\RedisClientException")]
113+
#[extends(ClassEntry::exception())]
114+
#[derive(Default)]
115+
struct RedisException;
168116

169117
#[php_function]
170-
pub fn test_str(input: &str) -> &str {
171-
input
172-
}
173-
174-
#[no_mangle]
175-
pub extern "C" fn php_module_info(_module: *mut ModuleEntry) {
176-
info_table_start!();
177-
info_table_row!("skeleton extension", "enabled");
178-
info_table_end!();
118+
pub fn test_exception() -> Result<i32, PhpException<'static>> {
119+
Err(PhpException::from_class::<RedisException>(
120+
"Hello world".into(),
121+
))
179122
}
180123

181-
#[php_startup]
182-
pub fn startup() {}
183-
184124
#[php_module]
185125
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
186-
module.info_function(php_module_info)
187-
}
188-
189-
#[php_extern]
190-
extern "C" {
191-
fn test_func<'a>() -> Callable<'a>;
192-
fn strpos2(haystack: &str, needle: &str, offset: Option<i32>) -> Zval;
193-
pub fn strpos(haystack: &str, needle: &str, offset: Option<i32>) -> Zval;
126+
// module.info_function(php_module_info)
127+
module
194128
}

example/skel/test.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@
22

33
include __DIR__.'/vendor/autoload.php';
44

5-
function test_func() {
6-
return "Hello wqorld";
7-
}
8-
9-
// var_dump(hello_world(5));
10-
var_dump(test_extern());
5+
var_dump(test_str("Hello wordl"));
6+
# var_dump(test_exception());

0 commit comments

Comments
 (0)