Skip to content

Commit 78a3b8e

Browse files
Implement String method for pointers (#122)
* Implement String method for pointers By implementing this method ourselves using atomic loading and fmt.Sprint, we can avoid possible unguarded accesses to the embedded pointer if someone attempts to fmt.Print the atomic.Pointer . * Use Pointer.Load, not wrapped.Load Co-authored-by: Abhinav Gupta <[email protected]>
1 parent 159e329 commit 78a3b8e

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ 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 `String` method to `atomic.Pointer[T]` type allowing users to safely print
10+
underlying values of pointers.
11+
712
## [1.10.0] - 2022-08-11
813
### Added
914
- Add `atomic.Float32` type for atomic operations on `float32`.

pointer_go118.go

+6-35
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,14 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
//go:build go1.18 && !go1.19
22-
// +build go1.18,!go1.19
21+
//go:build go1.18
22+
// +build go1.18
2323

2424
package atomic
2525

26-
import "unsafe"
26+
import "fmt"
2727

28-
type Pointer[T any] struct {
29-
_ nocmp // disallow non-atomic comparison
30-
p UnsafePointer
31-
}
32-
33-
// NewPointer creates a new Pointer.
34-
func NewPointer[T any](v *T) *Pointer[T] {
35-
var p Pointer[T]
36-
if v != nil {
37-
p.p.Store(unsafe.Pointer(v))
38-
}
39-
return &p
40-
}
41-
42-
// Load atomically loads the wrapped value.
43-
func (p *Pointer[T]) Load() *T {
44-
return (*T)(p.p.Load())
45-
}
46-
47-
// Store atomically stores the passed value.
48-
func (p *Pointer[T]) Store(val *T) {
49-
p.p.Store(unsafe.Pointer(val))
50-
}
51-
52-
// Swap atomically swaps the wrapped pointer and returns the old value.
53-
func (p *Pointer[T]) Swap(val *T) (old *T) {
54-
return (*T)(p.p.Swap(unsafe.Pointer(val)))
55-
}
56-
57-
// CompareAndSwap is an atomic compare-and-swap.
58-
func (p *Pointer[T]) CompareAndSwap(old, new *T) (swapped bool) {
59-
return p.p.CompareAndSwap(unsafe.Pointer(old), unsafe.Pointer(new))
28+
// String returns a human readable representation of a Pointer's underlying value.
29+
func (p *Pointer[T]) String() string {
30+
return fmt.Sprint(p.Load())
6031
}

pointer_go118_pre119.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2022 Uber Technologies, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
//go:build go1.18 && !go1.19
22+
// +build go1.18,!go1.19
23+
24+
package atomic
25+
26+
import "unsafe"
27+
28+
type Pointer[T any] struct {
29+
_ nocmp // disallow non-atomic comparison
30+
p UnsafePointer
31+
}
32+
33+
// NewPointer creates a new Pointer.
34+
func NewPointer[T any](v *T) *Pointer[T] {
35+
var p Pointer[T]
36+
if v != nil {
37+
p.p.Store(unsafe.Pointer(v))
38+
}
39+
return &p
40+
}
41+
42+
// Load atomically loads the wrapped value.
43+
func (p *Pointer[T]) Load() *T {
44+
return (*T)(p.p.Load())
45+
}
46+
47+
// Store atomically stores the passed value.
48+
func (p *Pointer[T]) Store(val *T) {
49+
p.p.Store(unsafe.Pointer(val))
50+
}
51+
52+
// Swap atomically swaps the wrapped pointer and returns the old value.
53+
func (p *Pointer[T]) Swap(val *T) (old *T) {
54+
return (*T)(p.p.Swap(unsafe.Pointer(val)))
55+
}
56+
57+
// CompareAndSwap is an atomic compare-and-swap.
58+
func (p *Pointer[T]) CompareAndSwap(old, new *T) (swapped bool) {
59+
return p.p.CompareAndSwap(unsafe.Pointer(old), unsafe.Pointer(new))
60+
}

pointer_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package atomic
2525

2626
import (
27+
"fmt"
2728
"testing"
2829

2930
"github.com/stretchr/testify/require"
@@ -89,6 +90,10 @@ func TestPointer(t *testing.T) {
8990
atom.Store(&i)
9091
require.Equal(t, &i, atom.Load(), "Store didn't set the correct value.")
9192
})
93+
t.Run("String", func(t *testing.T) {
94+
atom := tt.newAtomic()
95+
require.Equal(t, fmt.Sprint(tt.initial), atom.String(), "String did not return the correct value.")
96+
})
9297
})
9398
}
9499
}

0 commit comments

Comments
 (0)