@@ -10,16 +10,13 @@ import (
1010 "slices"
1111)
1212
13- // TODO(adonovan): when https://go.dev/issue/32816 is accepted, all of
14- // these functions should be annotated (provisionally with "//go:fix
15- // inline") so that tools can safely and automatically replace calls
16- // to exp/slices with calls to std slices by inlining them.
17-
1813// Equal reports whether two slices are equal: the same length and all
1914// elements equal. If the lengths are different, Equal returns false.
2015// Otherwise, the elements are compared in increasing index order, and the
2116// comparison stops at the first unequal pair.
2217// Floating point NaNs are not considered equal.
18+ //
19+ //go:fix inline
2320func Equal [S ~ []E , E comparable ](s1 , s2 S ) bool {
2421 return slices .Equal (s1 , s2 )
2522}
@@ -29,6 +26,8 @@ func Equal[S ~[]E, E comparable](s1, s2 S) bool {
2926// EqualFunc returns false. Otherwise, the elements are compared in
3027// increasing index order, and the comparison stops at the first index
3128// for which eq returns false.
29+ //
30+ //go:fix inline
3231func EqualFunc [S1 ~ []E1 , S2 ~ []E2 , E1 , E2 any ](s1 S1 , s2 S2 , eq func (E1 , E2 ) bool ) bool {
3332 return slices .EqualFunc (s1 , s2 , eq )
3433}
@@ -40,6 +39,8 @@ func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) boo
4039// If both slices are equal until one of them ends, the shorter slice is
4140// considered less than the longer one.
4241// The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2.
42+ //
43+ //go:fix inline
4344func Compare [S ~ []E , E cmp.Ordered ](s1 , s2 S ) int {
4445 return slices .Compare (s1 , s2 )
4546}
@@ -49,29 +50,39 @@ func Compare[S ~[]E, E cmp.Ordered](s1, s2 S) int {
4950// The result is the first non-zero result of cmp; if cmp always
5051// returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2),
5152// and +1 if len(s1) > len(s2).
53+ //
54+ //go:fix inline
5255func CompareFunc [S1 ~ []E1 , S2 ~ []E2 , E1 , E2 any ](s1 S1 , s2 S2 , cmp func (E1 , E2 ) int ) int {
5356 return slices .CompareFunc (s1 , s2 , cmp )
5457}
5558
5659// Index returns the index of the first occurrence of v in s,
5760// or -1 if not present.
61+ //
62+ //go:fix inline
5863func Index [S ~ []E , E comparable ](s S , v E ) int {
5964 return slices .Index (s , v )
6065}
6166
6267// IndexFunc returns the first index i satisfying f(s[i]),
6368// or -1 if none do.
69+ //
70+ //go:fix inline
6471func IndexFunc [S ~ []E , E any ](s S , f func (E ) bool ) int {
6572 return slices .IndexFunc (s , f )
6673}
6774
6875// Contains reports whether v is present in s.
76+ //
77+ //go:fix inline
6978func Contains [S ~ []E , E comparable ](s S , v E ) bool {
7079 return slices .Contains (s , v )
7180}
7281
7382// ContainsFunc reports whether at least one
7483// element e of s satisfies f(e).
84+ //
85+ //go:fix inline
7586func ContainsFunc [S ~ []E , E any ](s S , f func (E ) bool ) bool {
7687 return slices .ContainsFunc (s , f )
7788}
@@ -83,6 +94,8 @@ func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool {
8394// and r[i+len(v)] == value originally at r[i].
8495// Insert panics if i is out of range.
8596// This function is O(len(s) + len(v)).
97+ //
98+ //go:fix inline
8699func Insert [S ~ []E , E any ](s S , i int , v ... E ) S {
87100 return slices .Insert (s , i , v ... )
88101}
@@ -92,26 +105,34 @@ func Insert[S ~[]E, E any](s S, i int, v ...E) S {
92105// Delete is O(len(s)-i), so if many items must be deleted, it is better to
93106// make a single call deleting them all together than to delete one at a time.
94107// Delete zeroes the elements s[len(s)-(j-i):len(s)].
108+ //
109+ //go:fix inline
95110func Delete [S ~ []E , E any ](s S , i , j int ) S {
96111 return slices .Delete (s , i , j )
97112}
98113
99114// DeleteFunc removes any elements from s for which del returns true,
100115// returning the modified slice.
101116// DeleteFunc zeroes the elements between the new length and the original length.
117+ //
118+ //go:fix inline
102119func DeleteFunc [S ~ []E , E any ](s S , del func (E ) bool ) S {
103120 return slices .DeleteFunc (s , del )
104121}
105122
106123// Replace replaces the elements s[i:j] by the given v, and returns the
107124// modified slice. Replace panics if s[i:j] is not a valid slice of s.
108125// When len(v) < (j-i), Replace zeroes the elements between the new length and the original length.
126+ //
127+ //go:fix inline
109128func Replace [S ~ []E , E any ](s S , i , j int , v ... E ) S {
110129 return slices .Replace (s , i , j , v ... )
111130}
112131
113132// Clone returns a copy of the slice.
114133// The elements are copied using assignment, so this is a shallow clone.
134+ //
135+ //go:fix inline
115136func Clone [S ~ []E , E any ](s S ) S {
116137 return slices .Clone (s )
117138}
@@ -121,13 +142,17 @@ func Clone[S ~[]E, E any](s S) S {
121142// Compact modifies the contents of the slice s and returns the modified slice,
122143// which may have a smaller length.
123144// Compact zeroes the elements between the new length and the original length.
145+ //
146+ //go:fix inline
124147func Compact [S ~ []E , E comparable ](s S ) S {
125148 return slices .Compact (s )
126149}
127150
128151// CompactFunc is like [Compact] but uses an equality function to compare elements.
129152// For runs of elements that compare equal, CompactFunc keeps the first one.
130153// CompactFunc zeroes the elements between the new length and the original length.
154+ //
155+ //go:fix inline
131156func CompactFunc [S ~ []E , E any ](s S , eq func (E , E ) bool ) S {
132157 return slices .CompactFunc (s , eq )
133158}
@@ -136,16 +161,22 @@ func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
136161// another n elements. After Grow(n), at least n elements can be appended
137162// to the slice without another allocation. If n is negative or too large to
138163// allocate the memory, Grow panics.
164+ //
165+ //go:fix inline
139166func Grow [S ~ []E , E any ](s S , n int ) S {
140167 return slices .Grow (s , n )
141168}
142169
143170// Clip removes unused capacity from the slice, returning s[:len(s):len(s)].
171+ //
172+ //go:fix inline
144173func Clip [S ~ []E , E any ](s S ) S {
145174 return slices .Clip (s )
146175}
147176
148177// Reverse reverses the elements of the slice in place.
178+ //
179+ //go:fix inline
149180func Reverse [S ~ []E , E any ](s S ) {
150181 slices .Reverse (s )
151182}
0 commit comments