Add documentation for raw_pycfuntion#1174
Conversation
|
(Just a note to say thanks very much for writing these docs; I've been very busy lately, hoping to find time to read through these and review hopefully Monday.) |
|
I think the doc comment of |
davidhewitt
left a comment
There was a problem hiding this comment.
Sorry for the delay, just got round to reading it.
Thanks for taking the time to write this. It's some great documentation. I agree with @kngwyu that the example on the bottom is probably better to go as API documentation rather than letting the guide get too long.
guide/src/function.md
Outdated
| The `wrap_pyfunction` macro can be used to directly get a `PyCFunction` given a | ||
| `#[pyfunction]` and a `PyModule`: `wrap_pyfunction!(rust_fun, module)`. In the single-argument | ||
| variant, the macro only takes the identifier of the Rust function and returns a function that | ||
| can be called with either `Python` or `PyModule` and returns a `PyCFunction`. No newline at end of file |
There was a problem hiding this comment.
I think this paragraph may go into more detail than needed. (I'd rather not talk about the single-argument variant of wrap_pyfunction any more so that the ecosystem naturally migrates to the two-argument form.)
guide/src/function.md
Outdated
| E.g.: | ||
|
|
||
| ```rust | ||
| use pyo3::prelude::*; | ||
| use pyo3::types::PyCFunction; | ||
| use pyo3::raw_pycfunction; | ||
|
|
||
| #[pyfunction] | ||
| fn some_fun() -> PyResult<()> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[pymodule] | ||
| fn module(_py: Python, module: &PyModule) -> PyResult<()> { | ||
| let ffi_wrapper_fun = raw_pycfunction!(some_fun); | ||
| let docs = "Some documentation string with null-termination\0"; | ||
| let py_cfunction = | ||
| PyCFunction::new_with_keywords(ffi_wrapper_fun, "function_name", docs, module.into())?; | ||
| module.add_function(py_cfunction) | ||
| } | ||
|
|
||
| # fn main() {} | ||
| ``` |
There was a problem hiding this comment.
I think @kngwyu is correct; this document is great to go as a docstring on raw_pycfunction or maybe even `PyCFunction::new_with_keywords.
And then in the paragraph above can just have a link e.g. `(See the docs for raw_pycfunction).
I can't decide whether it's better for raw_pycfunction or PyCFunction::new_with_keywords though - happy for you to decide!
33dcc72 to
0a346df
Compare
|
Thanks! |
This adds instructions on how to use the
raw_pycfunction!()macro to the guide. I forgot to include this in #1163.It doesn't touch any
.rsfile, so this could probably be merged post release, too.