mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-29 10:36:31 +00:00
32 lines
752 B
Go
32 lines
752 B
Go
package errors
|
|
|
|
import (
|
|
"sync/atomic"
|
|
)
|
|
|
|
// OnceError is an error structure that supports safe multi
|
|
// threaded usage and setting only once (until reset).
|
|
type OnceError struct{ ptr atomic.Pointer[error] }
|
|
|
|
// Store will safely set the OnceError to value, no-op if nil.
|
|
func (e *OnceError) Store(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
return e.ptr.CompareAndSwap(nil, &err)
|
|
}
|
|
|
|
// Load will load the currently stored error.
|
|
func (e *OnceError) Load() error {
|
|
if ptr := e.ptr.Load(); ptr != nil {
|
|
return *ptr
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IsSet returns whether OnceError has been set.
|
|
func (e *OnceError) IsSet() bool { return (e.ptr.Load() != nil) }
|
|
|
|
// Reset will reset the OnceError value.
|
|
func (e *OnceError) Reset() { e.ptr.Store(nil) }
|