Ring reference structs

I am working on porting CPython to Rust language, but I had some problem in implement Python's type object system.

It looks like the following:

struct PyObject {
    ob_type: Box<PyTypeObject>,
}

struct PyVarObject {
    ob_base: PyObject,
    ob_size: usize
}

struct PyTypeObject {
    ob_base: PyVarObject,
}

These codes works well, but it is hard for me to create a PyType_Type global variable which has a reference to itself.

static PyType_Type: PyTypeObject = {
    ob_base: PyVarObject {
        ob_base: PyObject { ob_type: &PyType_Type }, ob_size: 0
    }
}

Can anybody help me with the ring reference structs?

1 Like

It sounds like you're trying to create a self-referential struct which isn't really allowed because of the way the borrow system works :disappointed:

There are a couple ways to work around this though, one method would be to use some form of reference-counted pointer (Rc or Arc) or you can fall back to using raw pointers instead of references. Usually I'd recommend against writing self-referential structs because cleanup can become non-trivial (i.e. leaks due to a reference cycle or segfaults due to stuffing up a pointer operation), but in your case I don't think you have a choice.

1 Like

If the data elements are static, it is possible to form cyclic data structures without boxing. Maybe you can declare a hierarchy of different data types taking into account the lifetimes as done for Seq Static, cyclic, infinite lists with Seq