mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-21 04:52:35 +02:00

* chore: replace gopkg.in/yaml.v3 vs goccy/go-yaml; replace encoding/json with bytedance/sonic * fix: yaml unmarshal panic * feat: custom json marshaler implementation * chore: fix import and err marshal handling --------- Co-authored-by: yusing <yusing@6uo.me>
35 lines
502 B
Go
35 lines
502 B
Go
package atomic
|
|
|
|
import (
|
|
"sync/atomic"
|
|
|
|
"github.com/yusing/go-proxy/pkg/json"
|
|
)
|
|
|
|
type Value[T any] struct {
|
|
atomic.Value
|
|
}
|
|
|
|
func (a *Value[T]) Load() T {
|
|
if v := a.Value.Load(); v != nil {
|
|
return v.(T)
|
|
}
|
|
var zero T
|
|
return zero
|
|
}
|
|
|
|
func (a *Value[T]) Store(v T) {
|
|
a.Value.Store(v)
|
|
}
|
|
|
|
func (a *Value[T]) Swap(v T) T {
|
|
if v := a.Value.Swap(v); v != nil {
|
|
return v.(T)
|
|
}
|
|
var zero T
|
|
return zero
|
|
}
|
|
|
|
func (a *Value[T]) MarshalJSONTo(buf []byte) []byte {
|
|
return json.MarshalTo(a.Load(), buf)
|
|
}
|