daemon/libnetwork/types: PortBinding.Equal: use non-pointer receiver

Change `PortBinding.Equal` to use a value receiver and parameter, this
allows us to use it directly with `slices.IndexFunc`, `DeleteFunc`,
without having to add a wrapper func.

The only exception currently is the `UnmapPorts` function (stub), which
takes portmapperapi.PortBinding as argument; the portmapperapi.PortBinding
type embeds `types.PortBinding`, and it's the only field that's compared
as part of `UnmapPorts`

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-10 15:41:54 +02:00
parent 7d5312ab56
commit 80452e5d4a
3 changed files with 6 additions and 8 deletions

View File

@@ -116,9 +116,7 @@ func (nw *StubFirewallerNetwork) AddPorts(_ context.Context, pbs []types.PortBin
func (nw *StubFirewallerNetwork) DelPorts(_ context.Context, pbs []types.PortBinding) error {
for _, pb := range pbs {
nw.Ports = slices.DeleteFunc(nw.Ports, func(p types.PortBinding) bool {
return p.Equal(&pb)
})
nw.Ports = slices.DeleteFunc(nw.Ports, pb.Equal)
}
return nil
}
@@ -148,9 +146,7 @@ func (nw *StubFirewallerNetwork) DelLink(_ context.Context, parentIP, childIP ne
}
func (nw *StubFirewallerNetwork) PortExists(pb types.PortBinding) bool {
return slices.ContainsFunc(nw.Ports, func(p types.PortBinding) bool {
return p.Equal(&pb)
})
return slices.ContainsFunc(nw.Ports, pb.Equal)
}
func (nw *StubFirewallerNetwork) LinkExists(parentIP, childIP netip.Addr, ports []types.TransportPort) bool {

View File

@@ -1009,8 +1009,10 @@ func (pm *stubPortMapper) MapPorts(_ context.Context, reqs []portmapperapi.PortB
func (pm *stubPortMapper) UnmapPorts(_ context.Context, reqs []portmapperapi.PortBinding, _ portmapperapi.Firewaller) error {
for _, req := range reqs {
// We're only checking for the PortBinding here, not any other
// property of [portmapperapi.PortBinding].
idx := slices.IndexFunc(pm.mapped, func(pb portmapperapi.PortBinding) bool {
return pb.Equal(&req.PortBinding)
return pb.Equal(req.PortBinding)
})
if idx == -1 {
return fmt.Errorf("stubPortMapper.UnmapPorts: pb doesn't exist %v", req)

View File

@@ -121,7 +121,7 @@ func (p PortBinding) Copy() PortBinding {
}
// Equal returns true if o has the same values as p, else false.
func (p *PortBinding) Equal(o *PortBinding) bool {
func (p PortBinding) Equal(o PortBinding) bool {
return p.Proto == o.Proto &&
p.IP.Equal(o.IP) &&
p.Port == o.Port &&