From 46826fc4e5dcfb472f7ae0c13d2ca65110441f0b Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Wed, 16 Mar 2022 16:40:24 -0700 Subject: all: use any in place of interface{} Enabled by using Go 1.18. A bit less verbose. Signed-off-by: Josh Bleecher Snyder --- device/logger.go | 8 ++++---- device/pools.go | 12 ++++++------ device/pools_test.go | 4 ++-- device/uapi.go | 6 +++--- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/device/logger.go b/device/logger.go index c79c971..29073e9 100644 --- a/device/logger.go +++ b/device/logger.go @@ -16,8 +16,8 @@ import ( // They do not require a trailing newline in the format. // If nil, that level of logging will be silent. type Logger struct { - Verbosef func(format string, args ...interface{}) - Errorf func(format string, args ...interface{}) + Verbosef func(format string, args ...any) + Errorf func(format string, args ...any) } // Log levels for use with NewLogger. @@ -28,14 +28,14 @@ const ( ) // Function for use in Logger for discarding logged lines. -func DiscardLogf(format string, args ...interface{}) {} +func DiscardLogf(format string, args ...any) {} // NewLogger constructs a Logger that writes to stdout. // It logs at the specified log level and above. // It decorates log lines with the log level, date, time, and prepend. func NewLogger(level int, prepend string) *Logger { logger := &Logger{DiscardLogf, DiscardLogf} - logf := func(prefix string) func(string, ...interface{}) { + logf := func(prefix string) func(string, ...any) { return log.New(os.Stdout, prefix+": "+prepend, log.Ldate|log.Ltime).Printf } if level >= LogLevelVerbose { diff --git a/device/pools.go b/device/pools.go index f1d1fa0..f40477b 100644 --- a/device/pools.go +++ b/device/pools.go @@ -18,13 +18,13 @@ type WaitPool struct { max uint32 } -func NewWaitPool(max uint32, new func() interface{}) *WaitPool { +func NewWaitPool(max uint32, new func() any) *WaitPool { p := &WaitPool{pool: sync.Pool{New: new}, max: max} p.cond = sync.Cond{L: &p.lock} return p } -func (p *WaitPool) Get() interface{} { +func (p *WaitPool) Get() any { if p.max != 0 { p.lock.Lock() for atomic.LoadUint32(&p.count) >= p.max { @@ -36,7 +36,7 @@ func (p *WaitPool) Get() interface{} { return p.pool.Get() } -func (p *WaitPool) Put(x interface{}) { +func (p *WaitPool) Put(x any) { p.pool.Put(x) if p.max == 0 { return @@ -46,13 +46,13 @@ func (p *WaitPool) Put(x interface{}) { } func (device *Device) PopulatePools() { - device.pool.messageBuffers = NewWaitPool(PreallocatedBuffersPerPool, func() interface{} { + device.pool.messageBuffers = NewWaitPool(PreallocatedBuffersPerPool, func() any { return new([MaxMessageSize]byte) }) - device.pool.inboundElements = NewWaitPool(PreallocatedBuffersPerPool, func() interface{} { + device.pool.inboundElements = NewWaitPool(PreallocatedBuffersPerPool, func() any { return new(QueueInboundElement) }) - device.pool.outboundElements = NewWaitPool(PreallocatedBuffersPerPool, func() interface{} { + device.pool.outboundElements = NewWaitPool(PreallocatedBuffersPerPool, func() any { return new(QueueOutboundElement) }) } diff --git a/device/pools_test.go b/device/pools_test.go index b0840e1..17e2298 100644 --- a/device/pools_test.go +++ b/device/pools_test.go @@ -26,7 +26,7 @@ func TestWaitPool(t *testing.T) { if workers-4 <= 0 { t.Skip("Not enough cores") } - p := NewWaitPool(uint32(workers-4), func() interface{} { return make([]byte, 16) }) + p := NewWaitPool(uint32(workers-4), func() any { return make([]byte, 16) }) wg.Add(workers) max := uint32(0) updateMax := func() { @@ -71,7 +71,7 @@ func BenchmarkWaitPool(b *testing.B) { if workers-4 <= 0 { b.Skip("Not enough cores") } - p := NewWaitPool(uint32(workers-4), func() interface{} { return make([]byte, 16) }) + p := NewWaitPool(uint32(workers-4), func() any { return make([]byte, 16) }) wg.Add(workers) b.ResetTimer() for i := 0; i < workers; i++ { diff --git a/device/uapi.go b/device/uapi.go index 746cf29..30dd97e 100644 --- a/device/uapi.go +++ b/device/uapi.go @@ -39,12 +39,12 @@ func (s IPCError) ErrorCode() int64 { return s.code } -func ipcErrorf(code int64, msg string, args ...interface{}) *IPCError { +func ipcErrorf(code int64, msg string, args ...any) *IPCError { return &IPCError{code: code, err: fmt.Errorf(msg, args...)} } var byteBufferPool = &sync.Pool{ - New: func() interface{} { return new(bytes.Buffer) }, + New: func() any { return new(bytes.Buffer) }, } // IpcGetOperation implements the WireGuard configuration protocol "get" operation. @@ -56,7 +56,7 @@ func (device *Device) IpcGetOperation(w io.Writer) error { buf := byteBufferPool.Get().(*bytes.Buffer) buf.Reset() defer byteBufferPool.Put(buf) - sendf := func(format string, args ...interface{}) { + sendf := func(format string, args ...any) { fmt.Fprintf(buf, format, args...) buf.WriteByte('\n') } -- cgit v1.2.3