2023-01-16 06:38:55 +00:00
|
|
|
package safety
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
type Slice[T any] struct {
|
2023-02-17 15:36:54 +00:00
|
|
|
*Var[[]T]
|
2023-01-16 06:38:55 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSlice[T any](a []T) *Slice[T] {
|
|
|
|
return &Slice[T]{
|
|
|
|
NewVar(a),
|
|
|
|
sync.Mutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Slice[T]) Append(t ...T) {
|
|
|
|
r.mu.Lock()
|
2023-11-12 13:39:04 +00:00
|
|
|
defer r.mu.Unlock()
|
2023-01-16 06:38:55 +00:00
|
|
|
ts := append(r.Load(), t...)
|
|
|
|
r.Store(ts)
|
2023-11-12 13:39:04 +00:00
|
|
|
|
2023-01-16 06:38:55 +00:00
|
|
|
}
|