Skip to content

Commit

Permalink
Rename helper trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mejrs committed May 1, 2022
1 parent 924cb28 commit f348c17
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@ impl<'a> FnSpec<'a> {
let rust_call = quote! {
let ret = #rust_name(#self_arg #(#arg_names),*);

use _pyo3::callback::IntoResult;
let ret: _pyo3::PyResult<_> = ret.into_result();
use _pyo3::callback::IntoPyResult;
let ret: _pyo3::PyResult<_> = ret.into_py_result();

_pyo3::callback::convert(#py, ret)
};
Expand Down
13 changes: 7 additions & 6 deletions src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,27 @@ impl PyCallbackOutput for () {
const ERR_VALUE: Self = ();
}

/// A helper trait to result-wrap the return values of functions and methods.
#[doc(hidden)]
pub trait IntoResult<R> {
fn into_result(self) -> R;
pub trait IntoPyResult<R> {
fn into_py_result(self) -> R;
}

impl<T> IntoResult<PyResult<T>> for T
impl<T> IntoPyResult<PyResult<T>> for T
where
T: IntoPy<PyObject>,
{
fn into_result(self) -> PyResult<Self> {
fn into_py_result(self) -> PyResult<Self> {
Ok(self)
}
}

impl<T, E> IntoResult<PyResult<T>> for Result<T, E>
impl<T, E> IntoPyResult<PyResult<T>> for Result<T, E>
where
T: IntoPy<PyObject>,
E: Into<PyErr>,
{
fn into_result(self) -> PyResult<T> {
fn into_py_result(self) -> PyResult<T> {
self.map_err(Into::into)
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/invalid_result_conversion.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error[E0599]: the method `into_result` exists for enum `Result<(), MyError>`, but its trait bounds were not satisfied
error[E0599]: the method `into_py_result` exists for enum `Result<(), MyError>`, but its trait bounds were not satisfied
--> tests/ui/invalid_result_conversion.rs:21:1
|
21 | #[pyfunction]
| ^^^^^^^^^^^^^ method cannot be called on `Result<(), MyError>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`&Result<(), MyError>: IntoPy<Py<PyAny>>`
which is required by `&Result<(), MyError>: IntoResult<Result<&Result<(), MyError>, PyErr>>`
which is required by `&Result<(), MyError>: IntoPyResult<Result<&Result<(), MyError>, PyErr>>`
`&mut Result<(), MyError>: IntoPy<Py<PyAny>>`
which is required by `&mut Result<(), MyError>: IntoResult<Result<&mut Result<(), MyError>, PyErr>>`
which is required by `&mut Result<(), MyError>: IntoPyResult<Result<&mut Result<(), MyError>, PyErr>>`
= note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
12 changes: 6 additions & 6 deletions tests/ui/missing_intopy.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error[E0599]: the method `into_result` exists for struct `Blah`, but its trait bounds were not satisfied
error[E0599]: the method `into_py_result` exists for struct `Blah`, but its trait bounds were not satisfied
--> tests/ui/missing_intopy.rs:3:1
|
1 | struct Blah;
| ------------
| |
| method `into_result` not found for this
| method `into_py_result` not found for this
| doesn't satisfy `Blah: IntoPy<Py<PyAny>>`
| doesn't satisfy `Blah: IntoResult<Result<Blah, PyErr>>`
| doesn't satisfy `Blah: IntoPyResult<Result<Blah, PyErr>>`
2 |
3 | #[pyo3::pyfunction]
| ^^^^^^^^^^^^^^^^^^^ method cannot be called on `Blah` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`Blah: IntoPy<Py<PyAny>>`
which is required by `Blah: IntoResult<Result<Blah, PyErr>>`
which is required by `Blah: IntoPyResult<Result<Blah, PyErr>>`
`&Blah: IntoPy<Py<PyAny>>`
which is required by `&Blah: IntoResult<Result<&Blah, PyErr>>`
which is required by `&Blah: IntoPyResult<Result<&Blah, PyErr>>`
`&mut Blah: IntoPy<Py<PyAny>>`
which is required by `&mut Blah: IntoResult<Result<&mut Blah, PyErr>>`
which is required by `&mut Blah: IntoPyResult<Result<&mut Blah, PyErr>>`
note: the following trait must be implemented
--> src/conversion.rs
|
Expand Down

0 comments on commit f348c17

Please sign in to comment.