Skip to content

Commit 18f0d8a

Browse files
authored
refactor: replace internal util package with ptr (#44)
1 parent 476e28a commit 18f0d8a

File tree

10 files changed

+61
-61
lines changed

10 files changed

+61
-61
lines changed

benchmarks/map_bench_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99

1010
"github.com/reugn/async"
11-
"github.com/reugn/async/internal/util"
11+
"github.com/reugn/async/internal/ptr"
1212
)
1313

1414
var (
@@ -26,15 +26,15 @@ func benchmarkMixedConcurrentLoad(m async.Map[mkey, int]) {
2626
go func() {
2727
defer wg.Done()
2828
for i := from; i < to; i++ {
29-
m.Put(mkey{uint64(i), strconv.Itoa(i)}, util.Ptr(i))
29+
m.Put(mkey{uint64(i), strconv.Itoa(i)}, ptr.Of(i))
3030
}
3131
}()
3232
go func() {
3333
defer wg.Done()
3434
for i := from; i < to; i++ {
3535
_ = m.ComputeIfAbsent(mkey{uint64(i), strconv.Itoa(i)}, func(_ mkey) *int {
3636
time.Sleep(time.Nanosecond)
37-
return util.Ptr(i)
37+
return ptr.Of(i)
3838
})
3939
}
4040
}()
@@ -47,7 +47,7 @@ func benchmarkMixedConcurrentLoad(m async.Map[mkey, int]) {
4747
go func() {
4848
defer wg.Done()
4949
for i := from; i < to; i++ {
50-
_ = m.GetOrDefault(mkey{uint64(i), strconv.Itoa(i)}, util.Ptr(i))
50+
_ = m.GetOrDefault(mkey{uint64(i), strconv.Itoa(i)}, ptr.Of(i))
5151
}
5252
}()
5353
}
@@ -109,8 +109,8 @@ func benchmarkReadConcurrentLoad(m async.Map[mkey, int]) {
109109
go func() {
110110
defer wg.Done()
111111
for i := from; i < to; i++ {
112-
_ = m.GetOrDefault(mkey{uint64(i), strconv.Itoa(i)}, util.Ptr(i))
113-
_ = m.GetOrDefault(mkey{math.MaxUint64, strconv.Itoa(i)}, util.Ptr(i))
112+
_ = m.GetOrDefault(mkey{uint64(i), strconv.Itoa(i)}, ptr.Of(i))
113+
_ = m.GetOrDefault(mkey{math.MaxUint64, strconv.Itoa(i)}, ptr.Of(i))
114114
}
115115
}()
116116
go func() {
@@ -138,7 +138,7 @@ func benchmarkReadConcurrentLoad(m async.Map[mkey, int]) {
138138

139139
func fillMap(m async.Map[mkey, int]) {
140140
for i := 0; i < 100; i++ {
141-
m.Put(mkey{uint64(i), strconv.Itoa(i)}, util.Ptr(i))
141+
m.Put(mkey{uint64(i), strconv.Itoa(i)}, ptr.Of(i))
142142
}
143143
}
144144

@@ -194,15 +194,15 @@ func benchmarkWriteConcurrentLoad(m async.Map[mkey, int]) {
194194
go func() {
195195
defer wg.Done()
196196
for i := from; i < to; i++ {
197-
m.Put(mkey{uint64(i), strconv.Itoa(i)}, util.Ptr(i))
197+
m.Put(mkey{uint64(i), strconv.Itoa(i)}, ptr.Of(i))
198198
}
199199
}()
200200
go func() {
201201
defer wg.Done()
202202
for i := from; i < to; i++ {
203203
_ = m.ComputeIfAbsent(mkey{uint64(i), strconv.Itoa(i)}, func(_ mkey) *int {
204204
time.Sleep(time.Nanosecond)
205-
return util.Ptr(i)
205+
return ptr.Of(i)
206206
})
207207
}
208208
}()

cyclic_barrier_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestCyclicBarrier_ContextCancelRace(t *testing.T) {
9090
go func() {
9191
defer wg.Done()
9292
// skip the first cycle to ensure the barrier is reset
93-
time.Sleep(5 * time.Millisecond)
93+
time.Sleep(10 * time.Millisecond)
9494
// second cycle
9595
assert.IsNil(t, barrier.Await())
9696
}()

future_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/reugn/async/internal/assert"
13-
"github.com/reugn/async/internal/util"
13+
"github.com/reugn/async/internal/ptr"
1414
)
1515

1616
func TestFuture(t *testing.T) {
@@ -31,8 +31,8 @@ func TestFuture_Utils(t *testing.T) {
3131
p2 := NewPromise[*int]()
3232
p3 := NewPromise[*int]()
3333

34-
res1 := util.Ptr(1)
35-
res2 := util.Ptr(2)
34+
res1 := ptr.Of(1)
35+
res2 := ptr.Of(2)
3636
err3 := errors.New("error")
3737

3838
go func() {
@@ -55,7 +55,7 @@ func TestFuture_FirstCompleted(t *testing.T) {
5555
p := NewPromise[*bool]()
5656
go func() {
5757
time.Sleep(100 * time.Millisecond)
58-
p.Success(util.Ptr(true))
58+
p.Success(ptr.Of(true))
5959
}()
6060

6161
timeout := FutureTimer[*bool](10 * time.Millisecond)
@@ -69,7 +69,7 @@ func TestFuture_Transform(t *testing.T) {
6969
p1 := NewPromise[*int]()
7070
go func() {
7171
time.Sleep(100 * time.Millisecond)
72-
p1.Success(util.Ptr(1))
72+
p1.Success(ptr.Of(1))
7373
}()
7474

7575
future := p1.Future().Map(func(v *int) (*int, error) {
@@ -81,7 +81,7 @@ func TestFuture_Transform(t *testing.T) {
8181
p2.Success(&inc)
8282
return p2.Future(), nil
8383
}).Recover(func() (*int, error) {
84-
return util.Ptr(5), nil
84+
return ptr.Of(5), nil
8585
})
8686

8787
res, _ := future.Get(context.Background())
@@ -178,7 +178,7 @@ func TestFuture_GoroutineLeak(t *testing.T) {
178178
go func() {
179179
defer wg.Done()
180180
time.Sleep(100 * time.Millisecond)
181-
promise.Success(util.Ptr("OK"))
181+
promise.Success(ptr.Of("OK"))
182182
}()
183183
wg.Add(1)
184184
go func() {

internal/ptr/ptr.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ptr
2+
3+
// Of returns a pointer to the given value.
4+
func Of[T any](value T) *T {
5+
return &value
6+
}

internal/util/util.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

map_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
"github.com/reugn/async/internal/assert"
12-
"github.com/reugn/async/internal/util"
12+
"github.com/reugn/async/internal/ptr"
1313
)
1414

1515
func TestMap_Clear(t *testing.T) {
@@ -21,7 +21,7 @@ func TestMap_Clear(t *testing.T) {
2121
t.Parallel()
2222
tt.m.Clear()
2323
assert.Equal(t, tt.m.Size(), 0)
24-
tt.m.Put(1, util.Ptr("a"))
24+
tt.m.Put(1, ptr.Of("a"))
2525
assert.Equal(t, tt.m.Size(), 1)
2626
})
2727
}
@@ -36,14 +36,14 @@ func TestMap_ComputeIfAbsent(t *testing.T) {
3636
t.Parallel()
3737
assert.Equal(
3838
t,
39-
tt.m.ComputeIfAbsent(4, func(_ int) *string { return util.Ptr("d") }),
40-
util.Ptr("d"),
39+
tt.m.ComputeIfAbsent(4, func(_ int) *string { return ptr.Of("d") }),
40+
ptr.Of("d"),
4141
)
4242
assert.Equal(t, tt.m.Size(), 4)
4343
assert.Equal(
4444
t,
45-
tt.m.ComputeIfAbsent(4, func(_ int) *string { return util.Ptr("e") }),
46-
util.Ptr("d"),
45+
tt.m.ComputeIfAbsent(4, func(_ int) *string { return ptr.Of("e") }),
46+
ptr.Of("d"),
4747
)
4848
assert.Equal(t, tt.m.Size(), 4)
4949
})
@@ -70,7 +70,7 @@ func TestMap_Get(t *testing.T) {
7070
for _, tt := range tests {
7171
t.Run(tt.name, func(t *testing.T) {
7272
t.Parallel()
73-
assert.Equal(t, tt.m.Get(1), util.Ptr("a"))
73+
assert.Equal(t, tt.m.Get(1), ptr.Of("a"))
7474
assert.IsNil(t, tt.m.Get(4))
7575
})
7676
}
@@ -83,8 +83,8 @@ func TestMap_GetOrDefault(t *testing.T) {
8383
for _, tt := range tests {
8484
t.Run(tt.name, func(t *testing.T) {
8585
t.Parallel()
86-
assert.Equal(t, tt.m.GetOrDefault(1, util.Ptr("e")), util.Ptr("a"))
87-
assert.Equal(t, tt.m.GetOrDefault(5, util.Ptr("e")), util.Ptr("e"))
86+
assert.Equal(t, tt.m.GetOrDefault(1, ptr.Of("e")), ptr.Of("a"))
87+
assert.Equal(t, tt.m.GetOrDefault(5, ptr.Of("e")), ptr.Of("e"))
8888
})
8989
}
9090
}
@@ -111,7 +111,7 @@ func TestMap_KeySet(t *testing.T) {
111111
t.Run(tt.name, func(t *testing.T) {
112112
t.Parallel()
113113
assert.ElementsMatch(t, tt.m.KeySet(), []int{1, 2, 3})
114-
tt.m.Put(4, util.Ptr("d"))
114+
tt.m.Put(4, ptr.Of("d"))
115115
assert.ElementsMatch(t, tt.m.KeySet(), []int{1, 2, 3, 4})
116116
})
117117
}
@@ -125,12 +125,12 @@ func TestMap_Put(t *testing.T) {
125125
t.Run(tt.name, func(t *testing.T) {
126126
t.Parallel()
127127
assert.Equal(t, tt.m.Size(), 3)
128-
tt.m.Put(4, util.Ptr("d"))
128+
tt.m.Put(4, ptr.Of("d"))
129129
assert.Equal(t, tt.m.Size(), 4)
130-
assert.Equal(t, tt.m.Get(4), util.Ptr("d"))
131-
tt.m.Put(4, util.Ptr("e"))
130+
assert.Equal(t, tt.m.Get(4), ptr.Of("d"))
131+
tt.m.Put(4, ptr.Of("e"))
132132
assert.Equal(t, tt.m.Size(), 4)
133-
assert.Equal(t, tt.m.Get(4), util.Ptr("e"))
133+
assert.Equal(t, tt.m.Get(4), ptr.Of("e"))
134134
})
135135
}
136136
}
@@ -142,7 +142,7 @@ func TestMap_Remove(t *testing.T) {
142142
for _, tt := range tests {
143143
t.Run(tt.name, func(t *testing.T) {
144144
t.Parallel()
145-
assert.Equal(t, tt.m.Remove(3), util.Ptr("c"))
145+
assert.Equal(t, tt.m.Remove(3), ptr.Of("c"))
146146
assert.Equal(t, tt.m.Size(), 2)
147147
assert.IsNil(t, tt.m.Remove(5))
148148
assert.Equal(t, tt.m.Size(), 2)
@@ -172,13 +172,13 @@ func TestMap_Values(t *testing.T) {
172172
assert.ElementsMatch(
173173
t,
174174
tt.m.Values(),
175-
[]*string{util.Ptr("a"), util.Ptr("b"), util.Ptr("c")},
175+
[]*string{ptr.Of("a"), ptr.Of("b"), ptr.Of("c")},
176176
)
177-
tt.m.Put(4, util.Ptr("d"))
177+
tt.m.Put(4, ptr.Of("d"))
178178
assert.ElementsMatch(
179179
t,
180180
tt.m.Values(),
181-
[]*string{util.Ptr("a"), util.Ptr("b"), util.Ptr("c"), util.Ptr("d")},
181+
[]*string{ptr.Of("a"), ptr.Of("b"), ptr.Of("c"), ptr.Of("d")},
182182
)
183183
})
184184
}
@@ -196,18 +196,18 @@ func TestMap_All(t *testing.T) {
196196

197197
// verify we got all 3 expected pairs
198198
expected := map[int]*string{
199-
1: util.Ptr("a"),
200-
2: util.Ptr("b"),
201-
3: util.Ptr("c"),
199+
1: ptr.Of("a"),
200+
2: ptr.Of("b"),
201+
3: ptr.Of("c"),
202202
}
203203
assert.Equal(t, collected, expected)
204204

205205
// add a new entry and verify it appears in the iterator
206-
tt.m.Put(4, util.Ptr("d"))
206+
tt.m.Put(4, ptr.Of("d"))
207207
collected = maps.Collect(tt.m.All())
208208

209209
// verify we now have 4 pairs
210-
expected[4] = util.Ptr("d")
210+
expected[4] = ptr.Of("d")
211211
assert.Equal(t, collected, expected)
212212
})
213213
}
@@ -244,7 +244,7 @@ func TestConcurrentMap_MemoryLeaks(t *testing.T) {
244244
go func() {
245245
defer wg.Done()
246246
for i := 0; i < 1000000; i++ {
247-
m.Put(i, util.Ptr(strconv.Itoa(i)))
247+
m.Put(i, ptr.Of(strconv.Itoa(i)))
248248
time.Sleep(time.Nanosecond)
249249
}
250250
}()
@@ -295,9 +295,9 @@ func prepareTestMaps() []testMap {
295295
}
296296

297297
func putValues(m Map[int, string]) {
298-
m.Put(1, util.Ptr("a"))
299-
m.Put(2, util.Ptr("b"))
300-
m.Put(3, util.Ptr("c"))
298+
m.Put(1, ptr.Of("a"))
299+
m.Put(2, ptr.Of("b"))
300+
m.Put(3, ptr.Of("c"))
301301
}
302302

303303
type testMap struct {

once_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77

88
"github.com/reugn/async/internal/assert"
9-
"github.com/reugn/async/internal/util"
9+
"github.com/reugn/async/internal/ptr"
1010
)
1111

1212
func TestOnce(t *testing.T) {
@@ -32,7 +32,7 @@ func TestOnce_Ptr(t *testing.T) {
3232
return count, nil
3333
})
3434
}
35-
assert.Equal(t, util.Ptr(1), count)
35+
assert.Equal(t, ptr.Of(1), count)
3636
}
3737

3838
func TestOnce_Concurrent(t *testing.T) {

task_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77

88
"github.com/reugn/async/internal/assert"
9-
"github.com/reugn/async/internal/util"
9+
"github.com/reugn/async/internal/ptr"
1010
)
1111

1212
func TestTask_Success(t *testing.T) {
@@ -23,7 +23,7 @@ func TestTask_Success(t *testing.T) {
2323
func TestTask_SuccessPtr(t *testing.T) {
2424
task := NewTask(func() (*string, error) {
2525
time.Sleep(10 * time.Millisecond)
26-
return util.Ptr("ok"), nil
26+
return ptr.Of("ok"), nil
2727
})
2828
res, err := task.Call().Join()
2929

value_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55

66
"github.com/reugn/async/internal/assert"
7-
"github.com/reugn/async/internal/util"
7+
"github.com/reugn/async/internal/ptr"
88
)
99

1010
func TestValueCompareAndSwap(t *testing.T) {
@@ -47,12 +47,12 @@ func TestValueCompareAndSwap(t *testing.T) {
4747
assert.Equal(t, swapped, true)
4848
assert.Equal(t, value.Load(), "a")
4949

50-
stringPointer := util.Ptr("b")
50+
stringPointer := ptr.Of("b")
5151
swapped = value.CompareAndSwap("a", stringPointer)
5252
assert.Equal(t, swapped, true)
5353
assert.Same(t, value.Load().(*string), stringPointer)
5454

55-
swapped = value.CompareAndSwap(util.Ptr("b"), "c")
55+
swapped = value.CompareAndSwap(ptr.Of("b"), "c")
5656
assert.Equal(t, swapped, false)
5757
assert.Same(t, value.Load().(*string), stringPointer)
5858

@@ -79,7 +79,7 @@ func TestValueStore(t *testing.T) {
7979
value.Store("a")
8080
assert.Equal(t, value.Load(), "a")
8181

82-
stringPointer := util.Ptr("b")
82+
stringPointer := ptr.Of("b")
8383
value.Store(stringPointer)
8484
assert.Same(t, value.Load().(*string), stringPointer)
8585
}
@@ -94,7 +94,7 @@ func TestValueSwap(t *testing.T) {
9494
old = value.Swap("a")
9595
assert.Equal(t, old, 1)
9696

97-
stringPointer := util.Ptr("b")
97+
stringPointer := ptr.Of("b")
9898
old = value.Swap(stringPointer)
9999
assert.Equal(t, old, "a")
100100
assert.Same(t, value.Load().(*string), stringPointer)

0 commit comments

Comments
 (0)