mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-24 12:08:19 +02:00
31 lines
531 B
Go
31 lines
531 B
Go
package slices
|
|
|
|
import "sync"
|
|
|
|
// SafeSlice is a thread safe slice.
|
|
type SafeSlice[E any] struct {
|
|
mu sync.RWMutex
|
|
slice []E
|
|
}
|
|
|
|
// NewSafeSlice creates a new SafeSlice.
|
|
func NewSafeSlice[E any]() *SafeSlice[E] {
|
|
return &SafeSlice[E]{}
|
|
}
|
|
|
|
// Append appends e to the slice.
|
|
func (s *SafeSlice[E]) Append(e E) {
|
|
s.mu.Lock()
|
|
s.slice = append(s.slice, e)
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// Get gets the slice.
|
|
func (s *SafeSlice[E]) Get() []E {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
c := make([]E, len(s.slice))
|
|
copy(c, s.slice)
|
|
return c
|
|
}
|