-
Notifications
You must be signed in to change notification settings - Fork 783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept paths in wrap_*
macros
#1709
Comments
Fwiw I had previously tried to implement this in the past, but handling the path in a When we bump MSRV past 1.42 then we can take advantage of proc macros in expression position and reimplement the |
blocked on #1782 |
This is unblocked once #2004 merges, so we can make this part of 0.16 |
(If anyone is interested in implementing this, I'm happy to provide some mentoring notes. It should be a reasonably gentle introduction to the proc-macro code.) |
@davidhewitt Some hints would be nice, I might take it. |
Sure thing. (Maybe assign this issue to yourself if you do start working on it so that others know not to start?) So at the moment e.g. To accept paths, I think the cleanest approach will be to convert it to a proc-macro. So the first step will be to re-implement the existing Once that's done, the new proc-macro implementation can be expanded to accept either a path or an ident. If a path is found, just the trailing element should be tweaked. e.g. ... once that's working, you'll need to do an equivalent rewrite for |
Okay, I tried a take but I'm getting confused with the part where #[proc_macro]
pub fn wrap_pyfunction(input: TokenStream) -> TokenStream {
let path = parse_macro_input!(input as syn::Path);
if let Some(ident) = path.get_ident() {
quote!(&|py| __pyo3_get_function_#ident(py))
.into()
} else {
let mut segments = path.segments.clone();
let function_segment = segments.pop().unwrap().value();
quote!(&|py| #segments __pyo3_get_function_#function_segment(py))
.into()
}
} Can I do recursive code also for my function? |
Hmm, to be honest I wouldn't bother with going recursion, instead with two arguments you could just expand To achieve that you'll probably want to have a struct something like this: #[derive(Clone, Debug, PartialEq)]
pub struct WrapPyFunctionArgs {
pub path: syn::Path,
pub py: Option<(Token![,], syn::Expr)>,
}
impl Parse for WrapPyFunctionArgs {
fn parse(input: ParseStream) -> Result<Self> {
Ok(WrapPyFunctionArgs {
path: input.parse()?,
py: input.parse()?.map(|comma| (comma, input.parse()?),
})
}
} ... and then go for the alternative expansion if I'm not totally sure that that |
@aviramha how did you get on with this? If you've got some code on a branch, want to push it and open a PR and I'll help finish it off? Otherwise, I'm happy to write an implementation for this; it would be nice to drop the |
Sorry I didn't make much progress that is worth a branch.. |
No problem; I managed to take a spin at this tonight in #2081! |
For example, for a python submodule defined in a separate rust module, the following fails to compile with an error saying that no rules expected the
::
.There was a tiny bit of discussion about this over on the matrix channel.
The text was updated successfully, but these errors were encountered: