mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 09:48:49 +02:00
simplify task package implementation
This commit is contained in:
30
internal/utils/atomic/atomic_value.go
Normal file
30
internal/utils/atomic/atomic_value.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package atomic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type Value[T any] struct {
|
||||
atomic.Value
|
||||
}
|
||||
|
||||
func (a *Value[T]) Load() T {
|
||||
return a.Value.Load().(T)
|
||||
}
|
||||
|
||||
func (a *Value[T]) Store(v T) {
|
||||
a.Value.Store(v)
|
||||
}
|
||||
|
||||
func (a *Value[T]) Swap(v T) T {
|
||||
return a.Value.Swap(v).(T)
|
||||
}
|
||||
|
||||
func (a *Value[T]) CompareAndSwap(oldV, newV T) bool {
|
||||
return a.Value.CompareAndSwap(oldV, newV)
|
||||
}
|
||||
|
||||
func (a *Value[T]) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(a.Load())
|
||||
}
|
||||
Reference in New Issue
Block a user