0
votes

How do I edit Header.a via Packet.Header.a?

#![allow(dead_code)]
use pyo3::prelude::*;

#[pyclass]
#[derive(Clone)]
pub struct Header {
    #[pyo3(get, set)]
    a: u32,
    #[pyo3(get, set)]
    b: u32,
}
#[pymethods]
impl Header {
    #[new]
    fn new(a: u32, b: u32) -> Self {
        Header { a, b }
    }
}

#[pyclass]
/// Structure used to hold an ordered list of headers
pub struct Packet {
    #[pyo3(get, set)]
    pub h: Header,
}
#[pymethods]
impl Packet {
    #[new]
    fn new() -> Self {
        Packet {
            h: Header { a: 0, b: 0 },
        }
    }
}

#[pymodule]
fn pyo3test(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_class::<Header>()?;
    m.add_class::<Packet>()?;
    Ok(())
}

After running "maturin develop", within python

from pyo3test import *
p = Packet()
print(p.h.a) # prints 0
h = p.h
h.a = 1
print(h.a) -> # prints 1
print(p.h.a) -> # still prints 0
p.h.a = 1
print(p.h.a) # still prints 0

This seems against python semantics. h is a reference to p.h. An update to h should have updated p.h. How do I implement the get trait to return a reference to Packet.Header?