Skip to content

Commit df5a5c3

Browse files
authored
Add Swap for Float64 (#94)
Float64 wraps a Uint64, and since Uint64 supports Swap, Float64 can also support Swap. Enable the Swap method in the generated code, and add tests. This also adds a note for why String doesn't support Swap (though it will be possible after Go 1.17+).
1 parent 38b6e7f commit df5a5c3

5 files changed

Lines changed: 16 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
### Added
9+
- Add `Float64.Swap` to match int atomic operations.
10+
711
## [1.8.0] - 2021-06-09
812
### Added
913
- Add `atomic.Uintptr` type for atomic operations on `uintptr` values.

float64.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ func (x *Float64) Store(v float64) {
5555
x.v.Store(math.Float64bits(v))
5656
}
5757

58+
// Swap atomically stores the given float64 and returns the old
59+
// value.
60+
func (x *Float64) Swap(o float64) float64 {
61+
return math.Float64frombits(x.v.Swap(math.Float64bits(o)))
62+
}
63+
5864
// MarshalJSON encodes the wrapped float64 into JSON.
5965
func (x *Float64) MarshalJSON() ([]byte, error) {
6066
return json.Marshal(x.Load())

float64_ext.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"strconv"
2626
)
2727

28-
//go:generate bin/gen-atomicwrapper -name=Float64 -type=float64 -wrapped=Uint64 -pack=math.Float64bits -unpack=math.Float64frombits -json -imports math -file=float64.go
28+
//go:generate bin/gen-atomicwrapper -name=Float64 -type=float64 -wrapped=Uint64 -pack=math.Float64bits -unpack=math.Float64frombits -swap -json -imports math -file=float64.go
2929

3030
// Add atomically adds to the wrapped float64 and returns the new value.
3131
func (f *Float64) Add(s float64) float64 {

float64_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func TestFloat64(t *testing.T) {
4242
require.Equal(t, float64(42.5), atom.Add(0.5), "Add didn't work.")
4343
require.Equal(t, float64(42.0), atom.Sub(0.5), "Sub didn't work.")
4444

45+
require.Equal(t, float64(42.0), atom.Swap(45.0), "Swap didn't return the old value.")
46+
require.Equal(t, float64(45.0), atom.Load(), "Swap didn't set the correct value.")
47+
4548
t.Run("JSON/Marshal", func(t *testing.T) {
4649
atom.Store(42.5)
4750
bytes, err := json.Marshal(atom)

string_ext.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
package atomic
2222

2323
//go:generate bin/gen-atomicwrapper -name=String -type=string -wrapped=Value -file=string.go
24+
// Note: No Swap as String wraps Value, which wraps the stdlib sync/atomic.Value which
25+
// only supports Swap as of go1.17: https://github.com/golang/go/issues/39351
2426

2527
// String returns the wrapped value.
2628
func (s *String) String() string {

0 commit comments

Comments
 (0)