mirror of https://github.com/tevino/abool.git
Add IsNotSet()
This commit is contained in:
parent
3a33536c80
commit
8ae5c93531
|
@ -17,6 +17,7 @@ cond := abool.New() // default to false
|
|||
cond.Set() // Sets to true
|
||||
cond.IsSet() // Returns true
|
||||
cond.UnSet() // Sets to false
|
||||
cond.IsNotSet() // Returns true
|
||||
cond.SetTo(any) // Sets to whatever you want
|
||||
cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
|
||||
cond.Toggle() // Inverts the boolean then returns the value before inverting
|
||||
|
|
5
bool.go
5
bool.go
|
@ -38,6 +38,11 @@ func (ab *AtomicBool) IsSet() bool {
|
|||
return atomic.LoadInt32((*int32)(ab))&1 == 1
|
||||
}
|
||||
|
||||
// IsNotSet returns whether the Boolean is false.
|
||||
func (ab *AtomicBool) IsNotSet() bool {
|
||||
return !ab.IsSet()
|
||||
}
|
||||
|
||||
// SetTo sets the boolean with given Boolean.
|
||||
func (ab *AtomicBool) SetTo(yes bool) {
|
||||
if yes {
|
||||
|
|
22
bool_test.go
22
bool_test.go
|
@ -25,6 +25,10 @@ func TestBool(t *testing.T) {
|
|||
t.Fatal("Empty value of AtomicBool should be false")
|
||||
}
|
||||
|
||||
if v.IsSet() == v.IsNotSet() {
|
||||
t.Fatal("AtomicBool.IsNotSet() should be the opposite of IsSet()")
|
||||
}
|
||||
|
||||
v.Set()
|
||||
if !v.IsSet() {
|
||||
t.Fatal("AtomicBool.Set() failed")
|
||||
|
@ -164,12 +168,18 @@ func TestRace(t *testing.T) {
|
|||
}
|
||||
|
||||
func ExampleAtomicBool() {
|
||||
cond := New() // default to false
|
||||
cond.Set() // set to true
|
||||
cond.IsSet() // returns true
|
||||
cond.UnSet() // set to false
|
||||
cond.SetTo(true) // set to whatever you want
|
||||
cond.Toggle() // toggles the boolean value
|
||||
cond := New() // default to false
|
||||
any := true
|
||||
old := any
|
||||
new := !any
|
||||
|
||||
cond.Set() // Sets to true
|
||||
cond.IsSet() // Returns true
|
||||
cond.UnSet() // Sets to false
|
||||
cond.IsNotSet() // Returns true
|
||||
cond.SetTo(any) // Sets to whatever you want
|
||||
cond.SetToIf(new, old) // Sets to `new` only if the Boolean matches the `old`, returns whether succeeded
|
||||
cond.Toggle() // Inverts the boolean then returns the value before inverting
|
||||
}
|
||||
|
||||
// Benchmark Read
|
||||
|
|
Loading…
Reference in New Issue