Skip to content

Commit 6ceab06

Browse files
committed
update: switch to generics for mkPtr logic
This is much easier to read and removes the need for explicit per-type helper functions. Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
1 parent 8df7573 commit 6ceab06

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

update.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import (
1717
"github.com/urfave/cli"
1818
)
1919

20-
func i64Ptr(i int64) *int64 { return &i }
21-
func u64Ptr(i uint64) *uint64 { return &i }
22-
func u16Ptr(i uint16) *uint16 { return &i }
23-
func boolPtr(b bool) *bool { return &b }
20+
func mkPtr[T any](v T) *T { return &v }
2421

2522
var updateCommand = cli.Command{
2623
Name: "update",
@@ -147,9 +144,9 @@ other options are ignored.
147144
}
148145

149146
r := specs.LinuxResources{
150-
// nil and u64Ptr(0) are not interchangeable
147+
// nil and mkPtr(0) are not interchangeable
151148
Memory: &specs.LinuxMemory{
152-
CheckBeforeUpdate: boolPtr(false), // constant
149+
CheckBeforeUpdate: mkPtr(false), // constant
153150
},
154151
CPU: &specs.LinuxCPU{},
155152
BlockIO: &specs.LinuxBlockIO{},
@@ -179,7 +176,7 @@ other options are ignored.
179176
}
180177
} else {
181178
if val := context.Int("blkio-weight"); val != 0 {
182-
r.BlockIO.Weight = u16Ptr(uint16(val))
179+
r.BlockIO.Weight = mkPtr(uint16(val))
183180
}
184181
if val := context.String("cpuset-cpus"); val != "" {
185182
r.CPU.Cpus = val
@@ -192,7 +189,7 @@ other options are ignored.
192189
if err != nil {
193190
return fmt.Errorf("invalid value for cpu-idle: %w", err)
194191
}
195-
r.CPU.Idle = i64Ptr(idle)
192+
r.CPU.Idle = mkPtr(idle)
196193
}
197194

198195
for _, pair := range []struct {
@@ -253,18 +250,18 @@ other options are ignored.
253250
}
254251

255252
if context.IsSet("pids-limit") {
256-
r.Pids.Limit = i64Ptr(int64(context.Int("pids-limit")))
253+
r.Pids.Limit = mkPtr(int64(context.Int("pids-limit")))
257254
}
258255
}
259256

260257
// Fix up values
261258
if r.Memory.Limit != nil && *r.Memory.Limit == -1 && r.Memory.Swap == nil {
262259
// To avoid error "unable to set swap limit without memory limit"
263-
r.Memory.Swap = i64Ptr(0)
260+
r.Memory.Swap = mkPtr[int64](0)
264261
}
265262
if r.CPU.Idle != nil && r.CPU.Shares == nil {
266263
// To avoid error "failed to write \"4\": write /sys/fs/cgroup/runc-cgroups-integration-test/test-cgroup-7341/cpu.weight: invalid argument"
267-
r.CPU.Shares = u64Ptr(0)
264+
r.CPU.Shares = mkPtr[uint64](0)
268265
}
269266

270267
if (r.Memory.Kernel != nil) || (r.Memory.KernelTCP != nil) { //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.

0 commit comments

Comments
 (0)