youki-dev / oci-spec-rs

OCI Runtime, Image and Distribution Spec in Rust
https://crates.io/crates/oci-spec
Apache License 2.0
218 stars 52 forks source link

Add mutable getters to the structures in the image module #218

Closed ariel-miculas closed 2 months ago

ariel-miculas commented 2 months ago

I would like to update fields such as manifests and annotations, for example by appending elements to them.

For a specific example, I have the following function that adds a manifest to the image index. I have to first clone the entire manifests vector, add an element to it and then use set_manifests to set the manifests vector to the new value.

pub struct Index(pub ImageIndex);
impl Index {
    pub fn add_manifest(&mut self, desc: Descriptor) {
        let mut manifests = self.0.manifests().to_vec();
        manifests.push(desc);
        self.0.set_manifests(manifests);
    }
}

From what I can tell, if we derived MutGetters on the structures and then use #[getset(get_mut)] on the fields, then I would be able to get a mutable reference to these fields and update them accordingly. I'm planning on opening a PR, I just wanted to make sure there are no objections to this change and check if I missed some other way of achieving this.

ariel-miculas commented 2 months ago

Thanks for the quick resolution, @saschagrunert