Howto implement PyAsPointer for a struct?

In another project I've defined a struct ValueType:

#[derive(Clone, PartialEq, Debug)]
pub enum ValueType {

In the current project I want to extend this type to support into_py. So I've extended it as follows:

struct PyValueType(ValueType);

impl ToPyObject for PyValueType
    fn to_object(&self, py: Python) -> PyObject {
        match self {
            PyValueType(ValueType::Int(v)) => v.into_py(py),
            PyValueType(ValueType::Hex(v)) => v.into_py(py),
            PyValueType(ValueType::Float(v)) => v.into_py(py),
            PyValueType(ValueType::String(v)) => v.into_py(py),

If I want to use it in a pyclass:

    fn foo(slf: PyRef<Self>) -> PyResult<PyObject> {
        let gil = Python::acquire_gil();
        let py = gil.python();

I get the following error:

the method `into_py` exists for struct `PyValueType`, but its trait bounds were not satisfied

method cannot be called on `PyValueType` due to unsatisfied trait bounds

note: the following trait bounds were not satisfied:
      `PyValueType: pyo3::AsPyPointer`
      which is required by `&PyValueType: pyo3::IntoPy<pyo3::Py<pyo3::PyAny>>`

How can I solve this issue?

You're implementing ToPyObject for PyValueType, but the into_py method comes from the IntoPy trait, right? I'm not familiar with pyo3 but that seems like it have might something to do with the problem.

You're right. I've replaced into_py with to_object. Now it works.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.