You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add top-level CompareAndSwap and CompareAndDelete functions
Summary
sync.Map gained CompareAndSwap and CompareAndDelete in Go 1.20. Both require the value type to be comparable. SyncMap[K comparable, V any] allows non-comparable V (slices, maps, functions), so these cannot be added as methods without a breaking constraint change. Per the v1.0.0 requirements doc and the user's architectural decision recorded in the plan, expose them as top-level generic functions with a tighter V comparable constraint — mirroring the stdlib pattern for constraint mismatches. The SyncMap type constraints stay unchanged.
Requirements
Add in syncmap.go (or a new cas.go at the implementer's discretion — prefer syncmap.go to keep single-source rule):
grep -nE 'func CompareAndSwap\[' syncmap.go returns exactly one match.
grep -nE 'func CompareAndDelete\[' syncmap.go returns exactly one match.
Both functions compile against SyncMap[string, int] (comparable V) in the test file.
A deliberate test-compile check with SyncMap[string, []byte] (non-comparable V) must produce a compile error — demonstrated by a comment in the test file referencing the Go playground or by including a commented-out line in a file-level comment block documenting that this is the expected failure mode. This is documentation, not a running test — it proves the constraint is load-bearing.
go doc github.com/axonops/syncmap CompareAndSwap and … CompareAndDelete print non-empty godoc.
go test -race -run 'TestCompareAndSwap|TestCompareAndDelete' -count=10 ./... passes stably.
make coverage reports ≥95% including the new functions.
feat: add top-level CompareAndSwap and CompareAndDelete functions
Summary
sync.MapgainedCompareAndSwapandCompareAndDeletein Go 1.20. Both require the value type to becomparable.SyncMap[K comparable, V any]allows non-comparable V (slices, maps, functions), so these cannot be added as methods without a breaking constraint change. Per the v1.0.0 requirements doc and the user's architectural decision recorded in the plan, expose them as top-level generic functions with a tighterV comparableconstraint — mirroring the stdlib pattern for constraint mismatches. TheSyncMaptype constraints stay unchanged.Requirements
syncmap.go(or a newcas.goat the implementer's discretion — prefersyncmap.goto keep single-source rule):sync.Mapmethod.V comparableconstraint explicitly and naming the reason (sync.Maprequires it).syncmap_test.go:TestCompareAndSwap— subtests:match_swaps_returns_true,mismatch_no_swap_returns_false,missing_key_returns_false,zero_V_match_works.TestCompareAndDelete— subtests:match_deletes_returns_true,mismatch_no_delete_returns_false,missing_key_returns_false,zero_V_match_works.TestCompareAndSwapContention— 100 goroutines all attempting CAS on same key; exactly one succeeds per round.Acceptance Criteria
grep -nE 'func CompareAndSwap\[' syncmap.goreturns exactly one match.grep -nE 'func CompareAndDelete\[' syncmap.goreturns exactly one match.SyncMap[string, int](comparable V) in the test file.SyncMap[string, []byte](non-comparable V) must produce a compile error — demonstrated by a comment in the test file referencing the Go playground or by including a commented-out line in a file-level comment block documenting that this is the expected failure mode. This is documentation, not a running test — it proves the constraint is load-bearing.go doc github.com/axonops/syncmap CompareAndSwapand… CompareAndDeleteprint non-empty godoc.go test -race -run 'TestCompareAndSwap|TestCompareAndDelete' -count=10 ./...passes stably.make coveragereports ≥95% including the new functions.make checkpasses end-to-end.Testing Requirements
-count=10repetition on the contention test.make checkgreen; CI green.Documentation Requirements
V comparableconstraint carve-out.Dependencies
Labels
enhancementP0