Skip to content

Commit

Permalink
oooooooooooohhhhhhhhhh
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Mar 13, 2024
1 parent e6ac717 commit cd415ff
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions common/atomic/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ type TypedValue[T any] struct {
value atomic.Value
}

// tValue is a struct with determined type to resolve atomic.Value usages with interface types
// typedValue is a struct with determined type to resolve atomic.Value usages with interface types
// https://github.com/golang/go/issues/22550
//
// The intention to have an atomic value store for errors. However, running this code panics:
// panic: sync/atomic: store of inconsistently typed value into Value
// This is because atomic.Value requires that the underlying concrete type be the same (which is a reasonable expectation for its implementation).
// When going through the atomic.Value.Store method call, the fact that both these are of the error interface is lost.
type tValue[T any] struct {
type typedValue[T any] struct {
value T
}

Expand All @@ -26,21 +26,21 @@ func (t *TypedValue[T]) Load() T {
if value == nil {
return common.DefaultValue[T]()
}
return value.(tValue[T]).value
return value.(typedValue[T]).value
}

func (t *TypedValue[T]) Store(value T) {
t.value.Store(tValue[T]{value})
t.value.Store(typedValue[T]{value})
}

func (t *TypedValue[T]) Swap(new T) T {
old := t.value.Swap(tValue[T]{new})
old := t.value.Swap(typedValue[T]{new})
if old == nil {
return common.DefaultValue[T]()
}
return old.(tValue[T]).value
return old.(typedValue[T]).value
}

func (t *TypedValue[T]) CompareAndSwap(old, new T) bool {
return t.value.CompareAndSwap(tValue[T]{old}, tValue[T]{new})
return t.value.CompareAndSwap(typedValue[T]{old}, typedValue[T]{new})
}

0 comments on commit cd415ff

Please sign in to comment.