mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 04:36:43 +00:00
Other: Better cookies test
This commit is contained in:
46
internal/safe/set.go
Normal file
46
internal/safe/set.go
Normal file
@ -0,0 +1,46 @@
|
||||
package safe
|
||||
|
||||
import "golang.org/x/exp/maps"
|
||||
|
||||
type Set[Val comparable] Map[Val, struct{}]
|
||||
|
||||
func NewSet[Val comparable](vals ...Val) *Set[Val] {
|
||||
set := (*Set[Val])(NewMap[Val, struct{}](nil))
|
||||
|
||||
for _, val := range vals {
|
||||
set.Insert(val)
|
||||
}
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func (m *Set[Val]) Has(key Val) bool {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
_, ok := m.data[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m *Set[Val]) Insert(key Val) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
m.data[key] = struct{}{}
|
||||
}
|
||||
|
||||
func (m *Set[Val]) Iter(fn func(key Val)) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
for key := range m.data {
|
||||
fn(key)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Set[Val]) Values(fn func(vals []Val)) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
fn(maps.Keys(m.data))
|
||||
}
|
||||
Reference in New Issue
Block a user