-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pycfunction: add PyCFunction::new_closure rust defined closures
- Loading branch information
1 parent
3b94f4b
commit 2042906
Showing
6 changed files
with
209 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::types::{PyCFunction, PyDict, PyTuple}; | ||
|
||
fn main() { | ||
let fun: Py<PyCFunction> = Python::with_gil(|py| { | ||
let local_data = vec![0, 1, 2, 3, 4]; | ||
let ref_: &[u8] = &local_data; | ||
|
||
let closure_fn = |_args: &PyTuple, _kwargs: Option<&PyDict>| -> PyResult<()> { | ||
println!("This is five: {:?}", ref_.len()); | ||
Ok(()) | ||
}; | ||
PyCFunction::new_closure(closure_fn, py).unwrap().into() | ||
}); | ||
|
||
Python::with_gil(|py| { | ||
fun.call0(py).unwrap(); | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error[E0597]: `local_data` does not live long enough | ||
--> tests/ui/invalid_closure.rs:7:27 | ||
| | ||
7 | let ref_: &[u8] = &local_data; | ||
| ^^^^^^^^^^^ borrowed value does not live long enough | ||
... | ||
13 | PyCFunction::new_closure(closure_fn, py).unwrap().into() | ||
| ---------------------------------------- argument requires that `local_data` is borrowed for `'static` | ||
14 | }); | ||
| - `local_data` dropped here while still borrowed | ||
|
||
error[E0373]: closure may outlive the current function, but it borrows `ref_`, which is owned by the current function | ||
--> tests/ui/invalid_closure.rs:9:26 | ||
| | ||
9 | let closure_fn = |_args: &PyTuple, _kwargs: Option<&PyDict>| -> PyResult<()> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ may outlive borrowed value `ref_` | ||
10 | println!("This is five: {:?}", ref_.len()); | ||
| ---- `ref_` is borrowed here | ||
| | ||
note: function requires argument type to outlive `'static` | ||
--> tests/ui/invalid_closure.rs:13:9 | ||
| | ||
13 | PyCFunction::new_closure(closure_fn, py).unwrap().into() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: to force the closure to take ownership of `ref_` (and any other referenced variables), use the `move` keyword | ||
| | ||
9 | let closure_fn = move |_args: &PyTuple, _kwargs: Option<&PyDict>| -> PyResult<()> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |