all: use slices.Contains #29459 (#1177)

This commit is contained in:
JukLee0ira 2025-07-01 17:49:42 +08:00 committed by GitHub
parent bf093348b7
commit 1dc8158ef0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 12 deletions

View file

@ -25,6 +25,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"sync"
@ -288,16 +289,6 @@ func (n *Node) openEndpoints() error {
return err
}
// containsLifecycle checks if 'lfs' contains 'l'.
func containsLifecycle(lfs []Lifecycle, l Lifecycle) bool {
for _, obj := range lfs {
if obj == l {
return true
}
}
return false
}
// stopServices terminates running services, RPC and p2p networking.
// It is the inverse of Start.
func (n *Node) stopServices(running []Lifecycle) error {
@ -575,7 +566,7 @@ func (n *Node) RegisterLifecycle(lifecycle Lifecycle) {
if n.state != initializingState {
panic("can't register lifecycle on running/stopped node")
}
if containsLifecycle(n.lifecycles, lifecycle) {
if slices.Contains(n.lifecycles, lifecycle) {
panic(fmt.Sprintf("attempt to register lifecycle %T more than once", lifecycle))
}
n.lifecycles = append(n.lifecycles, lifecycle)

View file

@ -23,6 +23,7 @@ import (
"net"
"net/http"
"reflect"
"slices"
"strings"
"testing"
@ -116,7 +117,7 @@ func TestLifecycleRegistry_Successful(t *testing.T) {
noop := NewNoop()
stack.RegisterLifecycle(noop)
if !containsLifecycle(stack.lifecycles, noop) {
if !slices.Contains(stack.lifecycles, Lifecycle(noop)) {
t.Fatalf("lifecycle was not properly registered on the node, %v", err)
}
}