1

I am trying to implement the trait PythonObject for a custom type.

impl PythonObject for PyTerm {
    #[inline]
    fn as_object(&self) -> &PyObject {
        //???
    }

    #[inline]
    fn into_object(self) -> PyObject {
        //???
    }

    #[inline]
    unsafe fn unchecked_downcast_from(o: PyObject) -> PyTerm {
        std::mem::transmute(o)
    }

    #[inline]
    unsafe fn unchecked_downcast_borrow_from(o: &PyObject) -> &PyTerm {
        std::mem::transmute(o)
    }
}

PyTerm is defined as a custom struct:

struct PyTerm {
    // not important fields
}
3
  • What's the definition of PyTerm, first of all? Commented Dec 30, 2020 at 16:41
  • I've added code for the struct Commented Dec 30, 2020 at 16:58
  • Tangentially related, but PyO3 is considered to be a go-to crate for Python interop. Commented Dec 30, 2020 at 18:00

1 Answer 1

1

You may be interested in cpython::pyobject_newtype! (sources).

It looks like the macros is a blessed way to define PythonObjects.

Sign up to request clarification or add additional context in comments.

1 Comment

This didn't work because it complained that the field 0 is not defined. I suspect that they only used it for something internal. I've migrated to using PyO3, because it seems to work there pyo3.rs/v0.2.7/class.html and because it seems to be more usable and up-to-date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.