diff --git a/bool.go b/bool.go index a1bf1bd..2205f9b 100644 --- a/bool.go +++ b/bool.go @@ -49,9 +49,9 @@ func (ab *AtomicBool) SetTo(yes bool) { } } -// Toggle negates boolean atomically and returns a new AtomicBool object which holds previous boolean value. -func (ab *AtomicBool) Toggle() *AtomicBool { - return NewBool(atomic.AddInt32((*int32)(ab), 1)&1 == 0) +// Toggle inverts the boolean then returns the value before inverting. +func (ab *AtomicBool) Toggle() bool { + return atomic.AddInt32((*int32)(ab), 1)&1 == 0 } // SetToIf sets the Boolean to new only if the Boolean matches the old diff --git a/bool_test.go b/bool_test.go index bc16f9e..c35aa16 100644 --- a/bool_test.go +++ b/bool_test.go @@ -64,7 +64,7 @@ func TestBool(t *testing.T) { } prev := v.Toggle() - if v.IsSet() == prev.IsSet() { + if v.IsSet() == prev { t.Fatal("AtomicBool.Toggle() to false failed") } } @@ -73,7 +73,7 @@ func TestToogleMultipleTimes(t *testing.T) { t.Parallel() v := New() - pre := NewBool(!v.IsSet()) + pre := !v.IsSet() for i := 0; i < 100; i++ { v.SetTo(false) for j := 0; j < i; j++ { @@ -85,8 +85,8 @@ func TestToogleMultipleTimes(t *testing.T) { t.Fatalf("AtomicBool.Toogle() doesn't work after %d calls, expected: %v, got %v", i, expected, v.IsSet()) } - if pre.IsSet() == v.IsSet() { - t.Fatalf("AtomicBool.Toogle() returned wrong value at the %dth calls, expected: %v, got %v", i, !v.IsSet(), pre.IsSet()) + if pre == v.IsSet() { + t.Fatalf("AtomicBool.Toogle() returned wrong value at the %dth calls, expected: %v, got %v", i, !v.IsSet(), pre) } } }