Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicwrapper.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion duration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicwrapper.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 11 additions & 0 deletions duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ func TestDuration(t *testing.T) {
assert.Equal(t, "42s", NewDuration(42*time.Second).String(),
"String() returned an unexpected value.")
})

t.Run("Alignment", func(t *testing.T) {
type wrapper struct {
_ byte
v Duration
}
s := &wrapper{}
assert.Equal(t, time.Duration(0), s.v.Load())
s.v.Store(42 * time.Second)
assert.Equal(t, 42*time.Second, s.v.Load())
})
}
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicwrapper.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion float32.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicwrapper.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion float64.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicwrapper.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 11 additions & 0 deletions float64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ func TestFloat64(t *testing.T) {
assert.Equal(t, "42.5", NewFloat64(42.5).String(),
"String() returned an unexpected value.")
})

t.Run("Alignment", func(t *testing.T) {
type wrapper struct {
_ byte
v Float64
}
s := &wrapper{}
assert.Equal(t, float64(0), s.v.Load())
s.v.Store(42.5)
assert.Equal(t, 42.5, s.v.Load())
})
}
22 changes: 13 additions & 9 deletions int32.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicint.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -32,27 +32,31 @@ import (
type Int32 struct {
_ nocmp // disallow non-atomic comparison

v int32
v atomic.Int32
}

// NewInt32 creates a new Int32.
func NewInt32(val int32) *Int32 {
return &Int32{v: val}
x := &Int32{}
if val != 0 {
x.Store(val)
}
return x
}

// Load atomically loads the wrapped value.
func (i *Int32) Load() int32 {
return atomic.LoadInt32(&i.v)
return i.v.Load()
}

// Add atomically adds to the wrapped int32 and returns the new value.
func (i *Int32) Add(delta int32) int32 {
return atomic.AddInt32(&i.v, delta)
return i.v.Add(delta)
}

// Sub atomically subtracts from the wrapped int32 and returns the new value.
func (i *Int32) Sub(delta int32) int32 {
return atomic.AddInt32(&i.v, -delta)
return i.v.Add(-delta)
}

// Inc atomically increments the wrapped int32 and returns the new value.
Expand All @@ -74,17 +78,17 @@ func (i *Int32) CAS(old, new int32) (swapped bool) {

// CompareAndSwap is an atomic compare-and-swap.
func (i *Int32) CompareAndSwap(old, new int32) (swapped bool) {
return atomic.CompareAndSwapInt32(&i.v, old, new)
return i.v.CompareAndSwap(old, new)
}

// Store atomically stores the passed value.
func (i *Int32) Store(val int32) {
atomic.StoreInt32(&i.v, val)
i.v.Store(val)
}

// Swap atomically swaps the wrapped int32 and returns the old value.
func (i *Int32) Swap(val int32) (old int32) {
return atomic.SwapInt32(&i.v, val)
return i.v.Swap(val)
}

// MarshalJSON encodes the wrapped int32 into JSON.
Expand Down
22 changes: 13 additions & 9 deletions int64.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicint.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -32,27 +32,31 @@ import (
type Int64 struct {
_ nocmp // disallow non-atomic comparison

v int64
v atomic.Int64
}

// NewInt64 creates a new Int64.
func NewInt64(val int64) *Int64 {
return &Int64{v: val}
x := &Int64{}
if val != 0 {
x.Store(val)
}
return x
}

// Load atomically loads the wrapped value.
func (i *Int64) Load() int64 {
return atomic.LoadInt64(&i.v)
return i.v.Load()
}

// Add atomically adds to the wrapped int64 and returns the new value.
func (i *Int64) Add(delta int64) int64 {
return atomic.AddInt64(&i.v, delta)
return i.v.Add(delta)
}

// Sub atomically subtracts from the wrapped int64 and returns the new value.
func (i *Int64) Sub(delta int64) int64 {
return atomic.AddInt64(&i.v, -delta)
return i.v.Add(-delta)
}

// Inc atomically increments the wrapped int64 and returns the new value.
Expand All @@ -74,17 +78,17 @@ func (i *Int64) CAS(old, new int64) (swapped bool) {

// CompareAndSwap is an atomic compare-and-swap.
func (i *Int64) CompareAndSwap(old, new int64) (swapped bool) {
return atomic.CompareAndSwapInt64(&i.v, old, new)
return i.v.CompareAndSwap(old, new)
}

// Store atomically stores the passed value.
func (i *Int64) Store(val int64) {
atomic.StoreInt64(&i.v, val)
i.v.Store(val)
}

// Swap atomically swaps the wrapped int64 and returns the old value.
func (i *Int64) Swap(val int64) (old int64) {
return atomic.SwapInt64(&i.v, val)
return i.v.Swap(val)
}

// MarshalJSON encodes the wrapped int64 into JSON.
Expand Down
11 changes: 11 additions & 0 deletions int64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,15 @@ func TestInt64(t *testing.T) {
"String() returned an unexpected value.")
})
})

t.Run("Alignment", func(t *testing.T) {
type wrapper struct {
_ byte
v Int64
}
s := &wrapper{}
assert.Equal(t, int64(0), s.v.Load())
s.v.Store(42)
assert.Equal(t, int64(42), s.v.Load())
})
}
20 changes: 12 additions & 8 deletions internal/gen-atomicint/wrapper.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,31 @@ import (
type {{ .Name }} struct {
_ nocmp // disallow non-atomic comparison

v {{ .Wrapped }}
v atomic.{{ .Name }}
}

// New{{ .Name }} creates a new {{ .Name }}.
func New{{ .Name }}(val {{ .Wrapped }}) *{{ .Name }} {
return &{{ .Name }}{v: val}
x := &{{ .Name }}{}
if val != 0 {
x.Store(val)
}
return x
}

// Load atomically loads the wrapped value.
func (i *{{ .Name }}) Load() {{ .Wrapped }} {
return atomic.Load{{ .Name }}(&i.v)
return i.v.Load()
}

// Add atomically adds to the wrapped {{ .Wrapped }} and returns the new value.
func (i *{{ .Name }}) Add(delta {{ .Wrapped }}) {{ .Wrapped }} {
return atomic.Add{{ .Name }}(&i.v, delta)
return i.v.Add(delta)
}

// Sub atomically subtracts from the wrapped {{ .Wrapped }} and returns the new value.
func (i *{{ .Name }}) Sub(delta {{ .Wrapped }}) {{ .Wrapped }} {
return atomic.Add{{ .Name }}(&i.v,
return i.v.Add(
{{- if .Unsigned -}}
^(delta - 1)
{{- else -}}
Expand Down Expand Up @@ -78,17 +82,17 @@ func (i *{{ .Name }}) CAS(old, new {{ .Wrapped }}) (swapped bool) {

// CompareAndSwap is an atomic compare-and-swap.
func (i *{{ .Name }}) CompareAndSwap(old, new {{ .Wrapped }}) (swapped bool) {
return atomic.CompareAndSwap{{ .Name }}(&i.v, old, new)
return i.v.CompareAndSwap(old, new)
}

// Store atomically stores the passed value.
func (i *{{ .Name }}) Store(val {{ .Wrapped }}) {
atomic.Store{{ .Name }}(&i.v, val)
i.v.Store(val)
}

// Swap atomically swaps the wrapped {{ .Wrapped }} and returns the old value.
func (i *{{ .Name }}) Swap(val {{ .Wrapped }}) (old {{ .Wrapped }}) {
return atomic.Swap{{ .Name }}(&i.v, val)
return i.v.Swap(val)
}

// MarshalJSON encodes the wrapped {{ .Wrapped }} into JSON.
Expand Down
2 changes: 1 addition & 1 deletion string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicwrapper.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion time.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicwrapper.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
22 changes: 13 additions & 9 deletions uint32.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @generated Code generated by gen-atomicint.

// Copyright (c) 2020-2023 Uber Technologies, Inc.
// Copyright (c) 2020-2026 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -32,27 +32,31 @@ import (
type Uint32 struct {
_ nocmp // disallow non-atomic comparison

v uint32
v atomic.Uint32
}

// NewUint32 creates a new Uint32.
func NewUint32(val uint32) *Uint32 {
return &Uint32{v: val}
x := &Uint32{}
if val != 0 {
x.Store(val)
}
return x
}

// Load atomically loads the wrapped value.
func (i *Uint32) Load() uint32 {
return atomic.LoadUint32(&i.v)
return i.v.Load()
}

// Add atomically adds to the wrapped uint32 and returns the new value.
func (i *Uint32) Add(delta uint32) uint32 {
return atomic.AddUint32(&i.v, delta)
return i.v.Add(delta)
}

// Sub atomically subtracts from the wrapped uint32 and returns the new value.
func (i *Uint32) Sub(delta uint32) uint32 {
return atomic.AddUint32(&i.v, ^(delta - 1))
return i.v.Add(^(delta - 1))
}

// Inc atomically increments the wrapped uint32 and returns the new value.
Expand All @@ -74,17 +78,17 @@ func (i *Uint32) CAS(old, new uint32) (swapped bool) {

// CompareAndSwap is an atomic compare-and-swap.
func (i *Uint32) CompareAndSwap(old, new uint32) (swapped bool) {
return atomic.CompareAndSwapUint32(&i.v, old, new)
return i.v.CompareAndSwap(old, new)
}

// Store atomically stores the passed value.
func (i *Uint32) Store(val uint32) {
atomic.StoreUint32(&i.v, val)
i.v.Store(val)
}

// Swap atomically swaps the wrapped uint32 and returns the old value.
func (i *Uint32) Swap(val uint32) (old uint32) {
return atomic.SwapUint32(&i.v, val)
return i.v.Swap(val)
}

// MarshalJSON encodes the wrapped uint32 into JSON.
Expand Down
Loading