Skip to content

Commit 07ce829

Browse files
committed
Fix option args
1 parent 3fb905a commit 07ce829

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

crates/macros/src/method.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{anyhow, bail, Result};
22
use quote::ToTokens;
33
use std::collections::HashMap;
4-
use syn::{ReturnType, parse_quote};
4+
use syn::{ReturnType, parse_quote, PathArguments, GenericArgument};
55

66
use crate::helpers::get_docs;
77
use crate::{
@@ -380,22 +380,41 @@ fn build_args(
380380
_ => bail!("Invalid parameter type."),
381381
};
382382
let name = param.to_string();
383-
384-
if let Type::Reference(t) = &*ty.ty {
383+
384+
let mut ty_inner = &*ty.ty;
385+
let mut is_option = false;
386+
387+
if let Type::Path(t) = ty_inner {
388+
if t.path.segments[0].ident.to_string() == "Option" {
389+
if let PathArguments::AngleBracketed(t) = &t.path.segments[0].arguments {
390+
if let GenericArgument::Type(t) = &t.args[0] {
391+
ty_inner = t;
392+
is_option = true;
393+
}
394+
}
395+
}
396+
}
397+
let mut is_str = false;
398+
if let Type::Reference(t) = ty_inner {
385399
if t.mutability.is_none() {
386400
if let Type::Path(t) = &*t.elem {
387-
if t.path.is_ident("str") {
388-
hack_tokens.append_all(
389-
quote! { let #param = unsafe { ::core::mem::transmute::<&str, &'static str>(#param) }; }
390-
);
391-
} else {
392-
hack_tokens.append_all(
393-
quote! { let #param = unsafe { ::ext_php_rs::zend::borrow_unchecked(#param) }; }
394-
);
395-
}
401+
is_str = t.path.is_ident("str");
396402
}
397403
}
398-
};
404+
}
405+
hack_tokens.append_all(if is_str {
406+
if is_option {
407+
quote! { let #param = #param.and_then(|__temp| Some(unsafe { ::core::mem::transmute::<&str, &'static str>(__temp) })); }
408+
} else {
409+
quote! { let #param = unsafe { ::core::mem::transmute::<&str, &'static str>(#param) }; }
410+
}
411+
} else {
412+
if is_option {
413+
quote! { let #param = #param.and_then(|__temp| Some(unsafe { ::ext_php_rs::zend::borrow_unchecked(__temp) })); }
414+
} else {
415+
quote! { let #param = unsafe { ::ext_php_rs::zend::borrow_unchecked(#param) }; }
416+
}
417+
});
399418

400419
let default = defaults.get(&name);
401420
let mut ty = ty.ty.clone();

src/zend/globals.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use lazy_static::lazy_static;
66
use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard};
77
use tokio::runtime::Runtime;
88

9+
use crate::binary_slice::{BinarySlice, PackSlice};
910
use crate::boxed::ZBox;
1011
use crate::ffi::{_zend_executor_globals, ext_php_rs_executor_globals};
1112

@@ -158,3 +159,14 @@ unsafe impl<'original, 'unbounded, T: 'unbounded> BorrowUnchecked<'original, 'un
158159
unsafe { ::core::mem::transmute(self) }
159160
}
160161
}
162+
163+
unsafe impl<'original, 'unbounded, T: 'unbounded + PackSlice> BorrowUnchecked<'original, 'unbounded>
164+
for BinarySlice<'original, T>
165+
{
166+
type Unbounded = BinarySlice<'unbounded, T>;
167+
168+
#[inline(always)]
169+
unsafe fn borrow_unchecked(self) -> Self::Unbounded {
170+
unsafe { ::core::mem::transmute(self) }
171+
}
172+
}

0 commit comments

Comments
 (0)