-
Notifications
You must be signed in to change notification settings - Fork 829
Use PyMethodsImpl instead of *ProtocolImpl::methods #917
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ pub fn build_py_proto(ast: &mut syn::ItemImpl) -> syn::Result<TokenStream> { | |
)); | ||
}; | ||
|
||
let tokens = impl_proto_impl(&ast.self_ty, &mut ast.items, proto); | ||
let tokens = impl_proto_impl(&ast.self_ty, &mut ast.items, proto)?; | ||
|
||
// attach lifetime | ||
let mut seg = path.segments.pop().unwrap().into_value(); | ||
|
@@ -57,53 +57,54 @@ fn impl_proto_impl( | |
ty: &syn::Type, | ||
impls: &mut Vec<syn::ImplItem>, | ||
proto: &defs::Proto, | ||
) -> TokenStream { | ||
let mut tokens = TokenStream::new(); | ||
) -> syn::Result<TokenStream> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
let mut trait_impls = TokenStream::new(); | ||
let mut py_methods = Vec::new(); | ||
|
||
for iimpl in impls.iter_mut() { | ||
if let syn::ImplItem::Method(ref mut met) = iimpl { | ||
if let Some(m) = proto.get_proto(&met.sig.ident) { | ||
impl_method_proto(ty, &mut met.sig, m).to_tokens(&mut tokens); | ||
impl_method_proto(ty, &mut met.sig, m).to_tokens(&mut trait_impls); | ||
} | ||
if let Some(m) = proto.get_method(&met.sig.ident) { | ||
let name = &met.sig.ident; | ||
let proto: syn::Path = syn::parse_str(m.proto).unwrap(); | ||
|
||
let fn_spec = match FnSpec::parse(&met.sig, &mut met.attrs, false) { | ||
Ok(fn_spec) => fn_spec, | ||
Err(err) => return err.to_compile_error(), | ||
}; | ||
let meth = pymethod::impl_proto_wrap(ty, &fn_spec); | ||
let fn_spec = FnSpec::parse(&met.sig, &mut met.attrs, false)?; | ||
let method = pymethod::impl_proto_wrap(ty, &fn_spec); | ||
let coexist = if m.can_coexist { | ||
// We need METH_COEXIST here to prevent __add__ from overriding __radd__ | ||
quote!(pyo3::ffi::METH_COEXIST) | ||
} else { | ||
quote!(0) | ||
}; | ||
// TODO(kngwyu): doc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :) |
||
py_methods.push(quote! { | ||
impl #proto for #ty | ||
{ | ||
#[inline] | ||
fn #name() -> Option<pyo3::class::methods::PyMethodDef> { | ||
#meth | ||
|
||
Some(pyo3::class::PyMethodDef { | ||
ml_name: stringify!(#name), | ||
ml_meth: pyo3::class::PyMethodType::PyCFunctionWithKeywords(__wrap), | ||
// We need METH_COEXIST here to prevent __add__ from overriding __radd__ | ||
ml_flags: pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS | #coexist, | ||
ml_doc: "" | ||
}) | ||
pyo3::class::PyMethodDefType::Method({ | ||
#method | ||
pyo3::class::PyMethodDef { | ||
ml_name: stringify!(#name), | ||
ml_meth: pyo3::class::PyMethodType::PyCFunctionWithKeywords(__wrap), | ||
ml_flags: pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS | #coexist, | ||
ml_doc: "" | ||
} | ||
} | ||
}) | ||
}); | ||
} | ||
} | ||
} | ||
|
||
quote! { | ||
#tokens | ||
|
||
#(#py_methods)* | ||
if py_methods.is_empty() { | ||
return Ok(quote! { #trait_impls }); | ||
} | ||
let inventory_submission = quote! { | ||
pyo3::inventory::submit! { | ||
#![crate = pyo3] { | ||
type ProtoInventory = <#ty as pyo3::class::methods::PyMethodsImpl>::Methods; | ||
<ProtoInventory as pyo3::class::methods::PyMethodsInventory>::new(&[#(#py_methods),*]) | ||
} | ||
} | ||
}; | ||
Ok(quote! { | ||
#trait_impls | ||
#inventory_submission | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this code block to the last of the page.
It's too long for a user who doesn't have a strong interest in the implementation detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!