Skip to content

Implementing getter for vector of class instances (without copy) #3223

Discussion options

You must be logged in to vote
#[pyclass]
#[derive(Clone)]
pub struct Node {
    pub type_: String,
    pub children_: Vec<Py<Node>>,
}

#[pymethods]
impl Node {
    #[getter]
    fn children(&self) -> Vec<Py<Node>> {
        self.children_.clone()
    }

    fn add_child(&mut self, type_: String, py: Python<'_>) {
        let child = Self {
            type_,
            children_: Vec::new(),
        };

        self.children_.push(Py::new(py, child).unwrap());
    }
}

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@chrisjsewell
Comment options

@adamreichold
Comment options

Answer selected by chrisjsewell
@chrisjsewell
Comment options

@chrisjsewell
Comment options

@adamreichold
Comment options

@chrisjsewell
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants