mirror of
https://github.com/soypat/lneto.git
synced 2026-08-31 03:49:06 +00:00
do not rely on unspecified Go behaviour; fix TCP ring buffer bug (#16)
* do not rely on unspecified Go behaviour; fix TCP ring buffer bug * even more stricter error handling on tcp connections * stricter lock copying in tcp.Conn, even though likely not a problem * add listener and tcp pool logging * forgot reqAddr unused * add logging to TCPConn and friends
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package internal
|
||||
|
||||
// IsZeroed returns true if all arguments are set to their zero value.
|
||||
func IsZeroed[T comparable](a ...T) bool {
|
||||
var z T
|
||||
for i := range a {
|
||||
if a[i] != z {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// DeleteZeroed deletes zero values in-place contained within the
|
||||
// slice and returns the modified slice without zero values.
|
||||
// Does not modify capacity.
|
||||
func DeleteZeroed[T comparable](a []T) []T {
|
||||
var z T
|
||||
off := 0
|
||||
deleted := false
|
||||
for i := 0; i < len(a); i++ {
|
||||
if a[i] != z {
|
||||
if deleted {
|
||||
a[off] = a[i]
|
||||
}
|
||||
off++
|
||||
} else if !deleted {
|
||||
deleted = true
|
||||
}
|
||||
}
|
||||
return a[:off]
|
||||
}
|
||||
|
||||
// SliceReuse prepares a slice for reuse with capacity at least n.
|
||||
// After calling SliceReuse, the slice will have:
|
||||
// - length = 0
|
||||
// - capacity >= n (exactly n if a new allocation was needed)
|
||||
//
|
||||
// This function provides specified behavior unlike [slices.Grow] which
|
||||
// has unspecified capacity growth behavior that differs between Go and TinyGo.
|
||||
// Use this when the exact capacity matters for subsequent logic.
|
||||
func SliceReuse[T any](buf *[]T, n int) {
|
||||
if cap(*buf) < n {
|
||||
*buf = make([]T, 0, n)
|
||||
} else {
|
||||
*buf = (*buf)[:0]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user