-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support Bound in pymodule and pyfunction macros Co-authored-by: David Hewitt <[email protected]> * Remove spurious $ character Co-authored-by: Matthew Neeley <[email protected]> * Rework PyCFunction::new_bound signatures This allows us to remove the awkward `PyFunctionArgumentsBound` enum. * Use BoundRef instead of BoundModule * support argument deduction for `wrap_pyfunction_bound!` * support `wrap_pyfunction!` with `Bound` input/output * Fix docs link to `wrap_pyfunction_bound!` * Revert back to wrap_pyfunction! --------- Co-authored-by: David Hewitt <[email protected]> Co-authored-by: Matthew Neeley <[email protected]>
- Loading branch information
1 parent
9370404
commit a3ad28b
Showing
12 changed files
with
203 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,10 +1,68 @@ | ||
use crate::{derive_utils::PyFunctionArguments, types::PyCFunction, PyResult}; | ||
use crate::{ | ||
types::{PyCFunction, PyModule}, | ||
Borrowed, Bound, PyResult, Python, | ||
}; | ||
|
||
pub use crate::impl_::pymethods::PyMethodDef; | ||
|
||
pub fn _wrap_pyfunction<'a>( | ||
method_def: &PyMethodDef, | ||
py_or_module: impl Into<PyFunctionArguments<'a>>, | ||
) -> PyResult<&'a PyCFunction> { | ||
PyCFunction::internal_new(method_def, py_or_module.into()).map(|x| x.into_gil_ref()) | ||
/// Trait to enable the use of `wrap_pyfunction` with both `Python` and `PyModule`, | ||
/// and also to infer the return type of either `&'py PyCFunction` or `Bound<'py, PyCFunction>`. | ||
pub trait WrapPyFunctionArg<'py, T> { | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<T>; | ||
} | ||
|
||
impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for Bound<'py, PyModule> { | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<Bound<'py, PyCFunction>> { | ||
PyCFunction::internal_new_bound(self.py(), method_def, Some(&self)) | ||
} | ||
} | ||
|
||
impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for &'_ Bound<'py, PyModule> { | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<Bound<'py, PyCFunction>> { | ||
PyCFunction::internal_new_bound(self.py(), method_def, Some(self)) | ||
} | ||
} | ||
|
||
impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for Borrowed<'_, 'py, PyModule> { | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<Bound<'py, PyCFunction>> { | ||
PyCFunction::internal_new_bound(self.py(), method_def, Some(&self)) | ||
} | ||
} | ||
|
||
impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for &'_ Borrowed<'_, 'py, PyModule> { | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<Bound<'py, PyCFunction>> { | ||
PyCFunction::internal_new_bound(self.py(), method_def, Some(self)) | ||
} | ||
} | ||
|
||
// For Python<'py>, only the GIL Ref form exists to avoid causing type inference to kick in. | ||
// The `wrap_pyfunction_bound!` macro is needed for the Bound form. | ||
impl<'py> WrapPyFunctionArg<'py, &'py PyCFunction> for Python<'py> { | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<&'py PyCFunction> { | ||
PyCFunction::internal_new(method_def, self.into()).map(Bound::into_gil_ref) | ||
} | ||
} | ||
|
||
impl<'py> WrapPyFunctionArg<'py, &'py PyCFunction> for &'py PyModule { | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<&'py PyCFunction> { | ||
PyCFunction::internal_new(method_def, self.into()).map(Bound::into_gil_ref) | ||
} | ||
} | ||
|
||
/// Helper for `wrap_pyfunction_bound!` to guarantee return type of `Bound<'py, PyCFunction>`. | ||
pub struct OnlyBound<T>(pub T); | ||
|
||
impl<'py, T> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for OnlyBound<T> | ||
where | ||
T: WrapPyFunctionArg<'py, Bound<'py, PyCFunction>>, | ||
{ | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<Bound<'py, PyCFunction>> { | ||
WrapPyFunctionArg::wrap_pyfunction(self.0, method_def) | ||
} | ||
} | ||
|
||
impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for OnlyBound<Python<'py>> { | ||
fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult<Bound<'py, PyCFunction>> { | ||
PyCFunction::internal_new_bound(self.0, method_def, None) | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.