Skip to content

Commit 29225fb

Browse files
committed
Fix and format.
1 parent 4c2406f commit 29225fb

File tree

19 files changed

+579
-226
lines changed

19 files changed

+579
-226
lines changed

examples/simple/src/lib.rs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
use phper::{c_str_ptr, php_fn, ebox};
2-
use phper::sys::{ZEND_RESULT_CODE_SUCCESS, zend_parse_parameters, zend_internal_arg_info, zend_function_entry, PHP_INI_SYSTEM};
3-
use phper::sys::{zend_ini_entry_def, zend_module_entry, zend_register_ini_entries, zend_unregister_ini_entries, OnUpdateBool, phper_zval_string};
4-
use phper::sys::{OnUpdateString, zend_class_entry, zend_register_internal_class, zend_declare_property_string, ZEND_ACC_PUBLIC, zend_type, zend_read_property};
5-
use phper::zend::api::{FunctionEntries, ModuleGlobals, function_entry_end};
6-
use phper::zend::compile::{MultiInternalArgInfo, internal_arg_info_begin};
7-
use phper::zend::ini::{IniEntryDefs, ini_entry_def_end};
8-
use phper::zend::modules::{ModuleEntry, create_zend_module_entry, ModuleArgs};
9-
use phper::zend::types::{ExecuteData, Val, SetVal, Value, ClassEntry};
1+
use phper::sys::OnUpdateBool;
2+
use phper::sys::{php_info_print_table_end, php_info_print_table_row, php_info_print_table_start};
3+
use phper::sys::{zend_function_entry, zend_internal_arg_info, PHP_INI_SYSTEM};
4+
use phper::sys::{zend_read_property, zend_type, OnUpdateString, ZEND_ACC_PUBLIC};
5+
use phper::zend::api::{function_entry_end, FunctionEntries, ModuleGlobals};
6+
use phper::zend::compile::{internal_arg_info_begin, MultiInternalArgInfo};
7+
use phper::zend::ini::{ini_entry_def_end, IniEntryDefs};
8+
use phper::zend::modules::{create_zend_module_entry, ModuleArgs, ModuleEntry};
9+
use phper::zend::types::{ClassEntry, ExecuteData, SetVal, Val};
10+
use phper::{c_str_ptr, php_fn};
1011
use phper::{
1112
php_function, php_minit, php_minit_function, php_mshutdown, php_mshutdown_function,
1213
php_rinit_function, php_rshutdown_function,
1314
};
1415
use phper::{php_minfo, php_minfo_function, php_rinit, php_rshutdown, zend_get_module};
15-
use std::ffi::{CStr, CString};
16-
use std::mem;
1716
use std::mem::{size_of, transmute};
18-
use std::os::raw::{c_char, c_int, c_uchar, c_uint, c_ushort};
17+
use std::os::raw::c_char;
1918
use std::ptr::{null, null_mut};
20-
use phper::zend::exceptions::MyException;
21-
use phper::sys::{php_info_print_table_start, php_info_print_table_row, php_info_print_table_end, phper_init_class_entry};
2219

2320
static MY_CLASS_CE: ClassEntry = ClassEntry::new();
2421

@@ -46,12 +43,12 @@ fn m_shutdown_simple(args: ModuleArgs) -> bool {
4643
}
4744

4845
#[php_rinit_function]
49-
fn r_init_simple(args: ModuleArgs) -> bool {
46+
fn r_init_simple(_args: ModuleArgs) -> bool {
5047
true
5148
}
5249

5350
#[php_rshutdown_function]
54-
fn r_shutdown_simple(args: ModuleArgs) -> bool {
51+
fn r_shutdown_simple(_args: ModuleArgs) -> bool {
5552
true
5653
}
5754

@@ -60,24 +57,38 @@ fn m_info_simple(module: &ModuleEntry) {
6057
unsafe {
6158
php_info_print_table_start();
6259
php_info_print_table_row(2, c_str_ptr!("simple.version"), (*module.as_ptr()).version);
63-
php_info_print_table_row(2, c_str_ptr!("simple.build_id"), (*module.as_ptr()).build_id);
64-
php_info_print_table_row(2, c_str_ptr!("simple.enable"), if SIMPLE_ENABLE.get() { c_str_ptr!("1") } else { c_str_ptr!("0") });
60+
php_info_print_table_row(
61+
2,
62+
c_str_ptr!("simple.build_id"),
63+
(*module.as_ptr()).build_id,
64+
);
65+
php_info_print_table_row(
66+
2,
67+
c_str_ptr!("simple.enable"),
68+
if SIMPLE_ENABLE.get() {
69+
c_str_ptr!("1")
70+
} else {
71+
c_str_ptr!("0")
72+
},
73+
);
6574
php_info_print_table_row(2, c_str_ptr!("simple.text"), SIMPLE_TEXT.get());
6675
php_info_print_table_end();
6776
}
6877
}
6978

7079
#[php_function]
7180
pub fn test_simple(execute_data: ExecuteData) -> impl SetVal {
72-
execute_data.parse_parameters::<(&str, &str)>().map(|(a, b)| {
73-
format!(
74-
"a = {}, a_len = {}, b = {}, b_len = {}",
75-
a,
76-
a.len(),
77-
b,
78-
b.len(),
79-
)
80-
})
81+
execute_data
82+
.parse_parameters::<(&str, &str)>()
83+
.map(|(a, b)| {
84+
format!(
85+
"a = {}, a_len = {}, b = {}, b_len = {}",
86+
a,
87+
a.len(),
88+
b,
89+
b.len(),
90+
)
91+
})
8192
}
8293

8394
static ARG_INFO_TEST_SIMPLE: MultiInternalArgInfo<3> = MultiInternalArgInfo::new([
@@ -107,7 +118,6 @@ static FUNCTION_ENTRIES: FunctionEntries<2> = FunctionEntries::new([
107118
function_entry_end(),
108119
]);
109120

110-
111121
static ARG_INFO_MY_CLASS_FOO: MultiInternalArgInfo<2> = MultiInternalArgInfo::new([
112122
zend_internal_arg_info {
113123
name: 1 as *const _,
@@ -134,7 +144,6 @@ static MY_CLASS_METHODS: FunctionEntries<2> = FunctionEntries::new([
134144
unsafe { transmute([0u8; size_of::<zend_function_entry>()]) },
135145
]);
136146

137-
138147
#[php_function]
139148
pub fn my_class_foo(execute_data: ExecuteData) -> impl SetVal {
140149
execute_data.parse_parameters::<&str>().map(|prefix| {
@@ -145,7 +154,7 @@ pub fn my_class_foo(execute_data: ExecuteData) -> impl SetVal {
145154
};
146155

147156
let foo = unsafe {
148-
zend_read_property(MY_CLASS_CE.get(), this, c_str_ptr!("foo"), 3, 1, null_mut())
157+
zend_read_property(MY_CLASS_CE.get(), this, c_str_ptr!("foo"), 3, 1, null_mut())
149158
};
150159
let foo = Val::from_raw(foo);
151160
let foo = foo.as_c_str().unwrap().to_str().unwrap();

phper-alloc/src/lib.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![feature(allocator_api)]
22

3-
use std::alloc::{AllocRef, Layout, AllocError};
4-
use std::ptr::{NonNull, slice_from_raw_parts_mut};
5-
use phper_sys::{_emalloc, _efree};
3+
use phper_sys::{_efree, _emalloc};
4+
use std::alloc::{AllocError, AllocRef, Layout};
65
use std::os::raw::c_char;
6+
use std::ptr::{slice_from_raw_parts_mut, NonNull};
77

88
pub type EBox<T> = Box<T, Allocator>;
99

@@ -20,14 +20,10 @@ pub struct Allocator {
2020

2121
impl Allocator {
2222
pub const fn new(
23-
#[cfg(phper_debug)]
24-
zend_filename: *const c_char,
25-
#[cfg(phper_debug)]
26-
zend_lineno: u32,
27-
#[cfg(phper_debug)]
28-
zend_orig_filename: *const c_char,
29-
#[cfg(phper_debug)]
30-
zend_orig_lineno: u32,
23+
#[cfg(phper_debug)] zend_filename: *const c_char,
24+
#[cfg(phper_debug)] zend_lineno: u32,
25+
#[cfg(phper_debug)] zend_orig_filename: *const c_char,
26+
#[cfg(phper_debug)] zend_orig_lineno: u32,
3127
) -> Self {
3228
Self {
3329
#[cfg(phper_debug)]
@@ -46,7 +42,13 @@ unsafe impl AllocRef for Allocator {
4642
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
4743
unsafe {
4844
#[cfg(phper_debug)]
49-
let ptr = _emalloc(layout.size(), self.zend_filename, self.zend_lineno, self.zend_orig_filename, self.zend_orig_lineno);
45+
let ptr = _emalloc(
46+
layout.size(),
47+
self.zend_filename,
48+
self.zend_lineno,
49+
self.zend_orig_filename,
50+
self.zend_orig_lineno,
51+
);
5052
#[cfg(not(phper_debug))]
5153
let ptr = _emalloc(layout.size());
5254

@@ -62,7 +64,13 @@ unsafe impl AllocRef for Allocator {
6264
unsafe fn dealloc(&self, ptr: NonNull<u8>, _layout: Layout) {
6365
// Not the correct position of `efree`, but can work!.
6466
#[cfg(phper_debug)]
65-
_efree(ptr.as_ptr().cast(), self.zend_filename, self.zend_lineno, self.zend_orig_filename, self.zend_orig_lineno);
67+
_efree(
68+
ptr.as_ptr().cast(),
69+
self.zend_filename,
70+
self.zend_lineno,
71+
self.zend_orig_filename,
72+
self.zend_orig_lineno,
73+
);
6674
#[cfg(not(phper_debug))]
6775
_efree(ptr.as_ptr().cast());
6876
}

phper-build/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
//!
33
//!
44
5-
use phper_sys::{
6-
USING_ZTS,
7-
PHP_DEBUG,
8-
PHP_MAJOR_VERSION,
9-
PHP_MINOR_VERSION,
10-
PHP_RELEASE_VERSION,
11-
};
5+
use phper_sys::{PHP_DEBUG, PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, USING_ZTS};
126

137
/// Register useful rust cfg for project using phper.
148
pub fn register_configures() {
159
// versions
16-
println!("cargo:rustc-cfg=phper_major_version=\"{}\"", PHP_MAJOR_VERSION);
17-
println!("cargo:rustc-cfg=phper_minor_version=\"{}\"", PHP_MINOR_VERSION);
18-
println!("cargo:rustc-cfg=phper_release_version=\"{}\"", PHP_RELEASE_VERSION);
10+
println!(
11+
"cargo:rustc-cfg=phper_major_version=\"{}\"",
12+
PHP_MAJOR_VERSION
13+
);
14+
println!(
15+
"cargo:rustc-cfg=phper_minor_version=\"{}\"",
16+
PHP_MINOR_VERSION
17+
);
18+
println!(
19+
"cargo:rustc-cfg=phper_release_version=\"{}\"",
20+
PHP_RELEASE_VERSION
21+
);
1922

2023
if PHP_DEBUG > 0 {
2124
println!("cargo:rustc-cfg=phper_debug");

phper-macros/src/inner.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ pub(crate) fn php_function(_attr: TokenStream, input: TokenStream) -> TokenStrea
7777
let vis = &input.vis;
7878
let ret = &input.sig.output;
7979
let inputs = &input.sig.inputs;
80-
let name = Ident::new(&format!("zif_{}", &input.sig.ident.to_string()), input.sig.ident.span().clone());
80+
let name = Ident::new(
81+
&format!("zif_{}", &input.sig.ident.to_string()),
82+
input.sig.ident.span().clone(),
83+
);
8184
let body = &input.block;
8285
let attrs = &input.attrs;
8386

@@ -93,7 +96,7 @@ pub(crate) fn php_function(_attr: TokenStream, input: TokenStream) -> TokenStrea
9396
fn internal(#inputs) #ret {
9497
#body
9598
}
96-
// let internal: fn(::phper::zend::types::ExecuteData) -> impl ::phper::zend::types::SetVal = internal;
99+
let internal: fn(::phper::zend::types::ExecuteData) -> _ = internal;
97100
let value = internal(::phper::zend::types::ExecuteData::from_raw(execute_data));
98101
::phper::zend::types::SetVal::set_val(value, &mut ::phper::zend::types::Val::from_raw(return_value));
99102
}

phper-macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ mod alloc;
22
mod inner;
33
mod utils;
44

5+
use crate::inner::{hook_fn, info_fn, rename};
56
use proc_macro::TokenStream;
6-
use crate::inner::{rename, hook_fn, info_fn};
77

88
#[proc_macro]
99
pub fn c_str(input: TokenStream) -> TokenStream {

phper-macros/src/utils.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use proc_macro::TokenStream;
22
use quote::quote;
3-
use syn::{parse_macro_input, Expr, Fields};
4-
use syn::parse::Nothing;
5-
use syn::ItemStruct;
6-
use proc_macro2::{TokenTree, Group};
3+
use syn::{parse_macro_input, Expr};
74

85
pub(crate) fn c_str(input: TokenStream) -> TokenStream {
96
let input = parse_macro_input!(input as Expr);

phper-sys/build.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,49 @@ fn main() {
2121
let php_bin = execute_command(&[php_config.as_str(), "--php-binary"]);
2222
let php_info = execute_command(&[php_bin.as_str(), "-i"]);
2323

24-
println!("cargo:rustc-env=ZEND_MODULE_BUILD_ID={}", php_info
25-
.lines()
26-
.find_map(|line| {
27-
if line.starts_with("Zend Extension Build") {
28-
Some(
29-
line.chars()
30-
.skip_while(|c| *c != 'A')
31-
.collect::<String>()
32-
.trim()
33-
.to_owned(),
34-
)
35-
} else {
36-
None
37-
}
38-
})
39-
.expect("Can't found the field `Zend Extension Build`"));
24+
println!(
25+
"cargo:rustc-env=ZEND_MODULE_BUILD_ID={}",
26+
php_info
27+
.lines()
28+
.find_map(|line| {
29+
if line.starts_with("Zend Extension Build") {
30+
Some(
31+
line.chars()
32+
.skip_while(|c| *c != 'A')
33+
.collect::<String>()
34+
.trim()
35+
.to_owned(),
36+
)
37+
} else {
38+
None
39+
}
40+
})
41+
.expect("Can't found the field `Zend Extension Build`")
42+
);
4043

41-
println!("cargo:rustc-env=PHP_MODULE_BUILD_ID={}", php_info
42-
.lines()
43-
.find_map(|line| {
44-
if line.starts_with("PHP Extension Build") {
45-
Some(
46-
line.chars()
47-
.skip_while(|c| *c != 'A')
48-
.collect::<String>()
49-
.trim()
50-
.to_owned(),
51-
)
52-
} else {
53-
None
54-
}
55-
})
56-
.expect("Can't found the field `PHP Extension Build`"));
44+
println!(
45+
"cargo:rustc-env=PHP_MODULE_BUILD_ID={}",
46+
php_info
47+
.lines()
48+
.find_map(|line| {
49+
if line.starts_with("PHP Extension Build") {
50+
Some(
51+
line.chars()
52+
.skip_while(|c| *c != 'A')
53+
.collect::<String>()
54+
.trim()
55+
.to_owned(),
56+
)
57+
} else {
58+
None
59+
}
60+
})
61+
.expect("Can't found the field `PHP Extension Build`")
62+
);
5763

5864
// Generate libphpwrapper.a.
5965

60-
let mut builder = cc::Build::new();
66+
let mut builder = cc::Build::new();
6167
for include in &includes {
6268
builder.flag(include);
6369
}

phper-sys/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#![allow(non_camel_case_types)]
33
#![allow(non_snake_case)]
44

5-
use std::os::raw::{c_char, c_int};
5+
use std::os::raw::c_char;
66

77
include!(concat!(env!("OUT_DIR"), "/php_bindings.rs"));
88

9-
pub const PHP_MODULE_BUILD_ID: *const c_char = concat!(env!("PHP_MODULE_BUILD_ID"), "\0").as_ptr().cast();
10-
pub const ZEND_MODULE_BUILD_ID: *const c_char = concat!(env!("ZEND_MODULE_BUILD_ID"), "\0").as_ptr().cast();
9+
pub const PHP_MODULE_BUILD_ID: *const c_char =
10+
concat!(env!("PHP_MODULE_BUILD_ID"), "\0").as_ptr().cast();
11+
pub const ZEND_MODULE_BUILD_ID: *const c_char =
12+
concat!(env!("ZEND_MODULE_BUILD_ID"), "\0").as_ptr().cast();

phper/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
22
phper_build::register_configures();
3-
}
3+
}

phper/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ Now the library don't support `ZTS`, the template is using `thread_local!` inste
2828
Version `0.1.x` will be a preview version.
2929
*/
3030

31-
pub mod zend;
3231
mod error;
3332
mod utils;
33+
pub mod zend;
3434

35+
pub use crate::error::*;
3536
pub use phper_alloc as alloc;
36-
pub use phper_sys as sys;
3737
pub use phper_macros::*;
38-
pub use crate::error::*;
38+
pub use phper_sys as sys;

0 commit comments

Comments
 (0)