Search code examples
pythonrustgarbage-collectionpyo3

Garbage collection for vector of object in PyO3


I have 2 pyclasses Block and BlockGroup.

#[pyclass]
struct Block {
    start: i32,
    stop: i32,
}

#[pyclass]
struct BlockGroup {
    blocks: Vec<Block>
}

I'm new to PyO3 and I have read the documentation about garbage collection but I don't completely grok it.

If your type owns references to other python objects, you will need to integrate with Python's garbage collector so that the GC is aware of those references.

Given that BlockGroup owns concrete Block objects, do I need to implement custom garbage collection?


Solution

  • In this case, Block and Vec<Block> respectively are part of rust's memory and not python's memory, so you don't need to worry about garbage collection. A object in python's memory would e.g. be Py<Block>.