#help with pyo3

13 messages · Page 1 of 1 (latest)

sterile rune
#

so i have the following code to wrap my rust code in a python lib: rs #[pyfunction] fn optimize_func( objective_raw: Bound<'_, PyAny>, ... ) -> PyResult<Vec<usize>> { let objective = |v: &Vec<f64>| { objective_raw .call1(v) .unwrap() .extract() .expect("objective returned wrong type, must retur f64") }; ... } which fails to compile with the error: ```error[E0277]: the trait bound Vec<f64>: IntoPy<Py<PyTuple>> is not satisfied
--> src/lib.rs:35:20
|
35 | .call1(v.clone())
| ----- ^^^^^^^^^ the trait IntoPy<Py<PyTuple>> is not implemented for Vec<f64>
| |
| required by a bound introduced by this call
|
= help: the trait IntoPy<Py<PyAny>> is implemented for Vec<f64>
= help: for that trait implementation, expected PyAny, found PyTuple
note: required by a bound in call1
--> /home/fbwdw/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pyo3-0.21.2/src/types/any.rs:1335:32
|
1335 | fn call1(&self, args: impl IntoPy<Py<PyTuple>>) -> PyResult<Bound<'py, PyAny>>;
| ^^^^^^^^^^^^^^^^^^^ required by this bound in PyAnyMethods::call1

harsh cedar
#

you want to pass (v,)

#

this creates a one element tuple, just like in python

sterile rune
# harsh cedar this creates a one element tuple, just like in python

tysm! if i clone it works great, but if i don't it gives: ```error[E0277]: the trait bound Vec<f64>: AsRef<PyAny> is not satisfied
--> src/lib.rs:35:21
|
35 | .call1((v,))
| ----- ^ the trait AsRef<PyAny> is not implemented for Vec<f64>, which is required by (&Vec<f64>,): IntoPy<Py<PyTuple>>
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait AsRef<T>:
<Vec<T, A> as AsRef<Vec<T, A>>>
<Vec<T, A> as AsRef<[T]>>
= note: required for &Vec<f64> to implement IntoPy<Py<PyAny>>
= note: 1 redundant requirement hidden
= note: required for (&Vec<f64>,) to implement IntoPy<Py<PyTuple>>
note: required by a bound in call1
--> /home/fbwdw/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pyo3-0.21.2/src/types/any.rs:1335:32
|
1335 | fn call1(&self, args: impl IntoPy<Py<PyTuple>>) -> PyResult<Bound<'py, PyAny>>;
| ^^^^^^^^^^^^^^^^^^^ required by this bound in PyAnyMethods::call1

#

is there any way to not clone

#

?

harsh cedar
#

what if you coerce to a slice first

sterile rune
harsh cedar
#

a reference to a slice, to be precise

sterile rune
#

something like this: ```rs
.call1((&(v.as_slice()),))

sterile rune
harsh cedar
#

weird

#

this should work