codeberg.org/gruf/go-errors/v2@v2.3.1/once.go (about) 1 package errors 2 3 import ( 4 "sync/atomic" 5 ) 6 7 // OnceError is an error structure that supports safe multi 8 // threaded usage and setting only once (until reset). 9 type OnceError struct{ ptr atomic.Pointer[error] } 10 11 // Store will safely set the OnceError to value, no-op if nil. 12 func (e *OnceError) Store(err error) bool { 13 if err == nil { 14 return false 15 } 16 return e.ptr.CompareAndSwap(nil, &err) 17 } 18 19 // Load will load the currently stored error. 20 func (e *OnceError) Load() error { 21 if ptr := e.ptr.Load(); ptr != nil { 22 return *ptr 23 } 24 return nil 25 } 26 27 // IsSet returns whether OnceError has been set. 28 func (e *OnceError) IsSet() bool { return (e.ptr.Load() != nil) } 29 30 // Reset will reset the OnceError value. 31 func (e *OnceError) Reset() { e.ptr.Store(nil) }