oci-spec-rs icon indicating copy to clipboard operation
oci-spec-rs copied to clipboard

The mutability of the API 0.6.4, is it by design?

Open Forsworns opened this issue 1 year ago • 4 comments

I found that the LinuxMemory is without setter, is this by design?

BTW, the current API returns an immutable reference to the struct fields, making it hard to revise the spec in place. Why not simply return a mutable reference, so that we can use get_or_insert to modify it.

For example, I have a mutable referenced spec s and want to only modify its cpu quota in palce. With current API (v0.6.4), I have to write the following code, right?

let mut linux = s.linux().clone().unwrap_or_default();
let mut resources = linux.resource().clone().unwrap_or_default();
let mut cpu = resources.cpu().clone().unwrap_or_default();
cpu.set_quota(Some(new_cpus));
resources.set_cpu(Some(cpu));
linux.set_resources(Some(resources));
s.set_linux(Some(linux));

So complicated :(

Forsworns avatar Dec 19 '23 08:12 Forsworns

Friendly ping @utam0k

Forsworns avatar Dec 24 '23 03:12 Forsworns

@Forsworns Why don't you use a builder pattern like that? https://github.com/containers/youki/blob/9e5516346b7a0ed6b59967a164ecabe5ca1a38ee/tests/integration_test/src/tests/example/hello_world.rs#L9-L20

utam0k avatar Dec 24 '23 05:12 utam0k

@utam0k Thanks, but I only want to modify a single field of existing spec, as described above, the spec.linux.resource.cpu.quota. It is nested so deep that it seems not a good choice to use the builder pattern, I have to copy each attribute of thespec to a new builder. Do I misunderstand something?

SpecBuilder::default()
        .root(s.root().unwrap_or_default())
        .process(
            s.process().unwrap_or_default()
        )
        .linux(
               LinuxBuilder::default()
                      .resources(
                                 LinuxResourceBuilder::default()
                                         .cpu(...)
                                 ...
                      )
                      ...
               ).build()?
        )
        ...
        .build()
        .context("failed to create spec")

Forsworns avatar Dec 24 '23 08:12 Forsworns

I guess simply appending a get_mut="pub" to the current getset attributes is enough. I can take this if you think it's worthwhile to do so. @utam0k

Forsworns avatar Dec 25 '23 01:12 Forsworns