mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-25 11:26:43 +00:00
GODT-1657: Stable sync (still needs more tests)
This commit is contained in:
97
internal/safe/map.go
Normal file
97
internal/safe/map.go
Normal file
@ -0,0 +1,97 @@
|
||||
package safe
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
type Map[Key comparable, Val any] struct {
|
||||
data map[Key]Val
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMap[Key comparable, Val any](from map[Key]Val) *Map[Key, Val] {
|
||||
m := &Map[Key, Val]{
|
||||
data: make(map[Key]Val),
|
||||
}
|
||||
|
||||
for key, val := range from {
|
||||
m.Set(key, val)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Map[Key, Val]) Get(key Key, fn func(val Val)) bool {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
val, ok := m.data[key]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
fn(val)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *Map[Key, Val]) GetErr(key Key, fn func(val Val) error) (bool, error) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
val, ok := m.data[key]
|
||||
if !ok {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, fn(val)
|
||||
}
|
||||
|
||||
func (m *Map[Key, Val]) Set(key Key, val Val) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
m.data[key] = val
|
||||
}
|
||||
|
||||
func (m *Map[Key, Val]) Keys(fn func(keys []Key)) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
fn(maps.Keys(m.data))
|
||||
}
|
||||
|
||||
func (m *Map[Key, Val]) Values(fn func(vals []Val)) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
fn(maps.Values(m.data))
|
||||
}
|
||||
|
||||
func GetMap[Key comparable, Val, Ret any](m *Map[Key, Val], key Key, fn func(val Val) Ret) (Ret, bool) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
val, ok := m.data[key]
|
||||
if !ok {
|
||||
return *new(Ret), false
|
||||
}
|
||||
|
||||
return fn(val), true
|
||||
}
|
||||
|
||||
func GetMapErr[Key comparable, Val, Ret any](m *Map[Key, Val], key Key, fn func(val Val) (Ret, error)) (Ret, bool, error) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
val, ok := m.data[key]
|
||||
if !ok {
|
||||
return *new(Ret), false, nil
|
||||
}
|
||||
|
||||
ret, err := fn(val)
|
||||
|
||||
return ret, true, err
|
||||
}
|
||||
53
internal/safe/slice.go
Normal file
53
internal/safe/slice.go
Normal file
@ -0,0 +1,53 @@
|
||||
package safe
|
||||
|
||||
import "sync"
|
||||
|
||||
type Slice[Val any] struct {
|
||||
data []Val
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewSlice[Val any](from []Val) *Slice[Val] {
|
||||
s := &Slice[Val]{
|
||||
data: make([]Val, len(from)),
|
||||
}
|
||||
|
||||
copy(s.data, from)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Slice[Val]) Get(fn func(data []Val)) {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
fn(s.data)
|
||||
}
|
||||
|
||||
func (s *Slice[Val]) GetErr(fn func(data []Val) error) error {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
return fn(s.data)
|
||||
}
|
||||
|
||||
func (s *Slice[Val]) Set(data []Val) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
s.data = data
|
||||
}
|
||||
|
||||
func GetSlice[Val, Ret any](s *Slice[Val], fn func(data []Val) Ret) Ret {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
return fn(s.data)
|
||||
}
|
||||
|
||||
func GetSliceErr[Val, Ret any](s *Slice[Val], fn func(data []Val) (Ret, error)) (Ret, error) {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
return fn(s.data)
|
||||
}
|
||||
49
internal/safe/type.go
Normal file
49
internal/safe/type.go
Normal file
@ -0,0 +1,49 @@
|
||||
package safe
|
||||
|
||||
import "sync"
|
||||
|
||||
type Type[T any] struct {
|
||||
data T
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewType[T any](data T) *Type[T] {
|
||||
return &Type[T]{
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Type[T]) Get(fn func(data T)) {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
fn(s.data)
|
||||
}
|
||||
|
||||
func (s *Type[T]) GetErr(fn func(data T) error) error {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
return fn(s.data)
|
||||
}
|
||||
|
||||
func (s *Type[T]) Set(data T) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
s.data = data
|
||||
}
|
||||
|
||||
func GetType[T, Ret any](s *Type[T], fn func(data T) Ret) Ret {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
return fn(s.data)
|
||||
}
|
||||
|
||||
func GetTypeErr[T, Ret any](s *Type[T], fn func(data T) (Ret, error)) (Ret, error) {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
return fn(s.data)
|
||||
}
|
||||
Reference in New Issue
Block a user