Skip to content

Commit

Permalink
Reorder new_closure arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dalcde committed Oct 15, 2022
1 parent 6cef339 commit fa10d41
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ impl PyCFunction {
/// let i = args.extract::<(i64,)>()?.0;
/// Ok(i+1)
/// };
/// let add_one = types::PyCFunction::new_closure(add_one, "add_one", "add one to integer", py).unwrap();
/// let add_one = types::PyCFunction::new_closure(py, "add_one", "add one to integer", add_one).unwrap();
/// py_run!(py, add_one, "assert add_one(42) == 43");
/// });
/// ```
pub fn new_closure<'a, F, R>(
f: F,
py: Python<'a>,
name: &'static str,
doc: &'static str,
py: Python<'a>,
f: F,
) -> PyResult<&'a PyCFunction>
where
F: Fn(&types::PyTuple, Option<&types::PyDict>) -> R + Send + 'static,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ fn test_closure() {
Ok(res)
})
};
let closure_py = PyCFunction::new_closure(f, "test_fn", "test_fn doc", py).unwrap();
let closure_py = PyCFunction::new_closure(py, "test_fn", "test_fn doc", f).unwrap();

py_assert!(py, closure_py, "closure_py(42) == [43]");
py_assert!(py, closure_py, "closure_py.__name__ == 'test_fn'");
Expand All @@ -442,7 +442,7 @@ fn test_closure_counter() {
Ok(*counter)
};
let counter_py =
PyCFunction::new_closure(counter_fn, "counter", "counter doc", py).unwrap();
PyCFunction::new_closure(py, "counter", "counter doc", counter_fn).unwrap();

py_assert!(py, counter_py, "counter_py() == 1");
py_assert!(py, counter_py, "counter_py() == 2");
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
println!("This is five: {:?}", ref_.len());
Ok(())
};
PyCFunction::new_closure(closure_fn, "name", "doc", py).unwrap().into()
PyCFunction::new_closure(py, "name", "doc", closure_fn).unwrap().into()
});

Python::with_gil(|py| {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/invalid_closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0597]: `local_data` does not live long enough
7 | let ref_: &[u8] = &local_data;
| ^^^^^^^^^^^ borrowed value does not live long enough
...
13 | PyCFunction::new_closure(closure_fn, "name", "doc", py).unwrap().into()
13 | PyCFunction::new_closure(py, "name", "doc", closure_fn).unwrap().into()
| ------------------------------------------------------- argument requires that `local_data` is borrowed for `'static`
14 | });
| - `local_data` dropped here while still borrowed
Expand All @@ -20,7 +20,7 @@ error[E0373]: closure may outlive the current function, but it borrows `ref_`, w
note: function requires argument type to outlive `'static`
--> tests/ui/invalid_closure.rs:13:9
|
13 | PyCFunction::new_closure(closure_fn, "name", "doc", py).unwrap().into()
13 | PyCFunction::new_closure(py, "name", "doc", closure_fn).unwrap().into()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `ref_` (and any other referenced variables), use the `move` keyword
|
Expand Down

0 comments on commit fa10d41

Please sign in to comment.