Convert a Rust struct to a Python tuple #2825
Answered
by
davidhewitt
JackAshwell11
asked this question in
Questions
-
If I have this Rust struct: #[derive(FromPyObject, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct Point {
pub x: isize,
pub y: isize,
} How can it be converted into a Python tuple upon returning it from a function? |
Beta Was this translation helpful? Give feedback.
Answered by
davidhewitt
Dec 23, 2022
Replies: 2 comments 2 replies
-
At the moment we don't have a #[pyfunction]
fn point_to_tuple(point: Point) -> (isize, isize) {
(point.x, point.y)
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
JackAshwell11
-
I have this so far, is it okay? #[derive(FromPyObject, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl IntoPy<PyObject> for Point {
fn into_py(self, py: Python<'_>) -> PyObject {
PyObject::from(PyTuple::new(py, [self.x, self.y]))
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At the moment we don't have a
#[derive]
for the into-Python direction (we might have one eventually), however at the moment this is easy enough by just returning a Rust tuple of the fields: