Skip to content

Commit 538fb69

Browse files
committed
all: use a few more newer std APIs
Noticed in passing at different times and git stashed. Now that I have a handful, send them out as a single patch. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I257175f2b404d0d6890aa90e16f723ec55a7f6ac Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1209880 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent d73e690 commit 538fb69

File tree

5 files changed

+9
-18
lines changed

5 files changed

+9
-18
lines changed

cmd/cue/cmd/get_go.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,13 +433,12 @@ func (e *extractor) addPackage(p *packages.Package) {
433433

434434
func (e *extractor) recordTypeInfo(p *packages.Package) {
435435
for _, f := range p.Syntax {
436-
ast.Inspect(f, func(n ast.Node) bool {
436+
for n := range ast.Preorder(f) {
437437
switch n := n.(type) {
438438
case *ast.StructType:
439439
e.orig[p.TypesInfo.TypeOf(n)] = n
440440
}
441-
return true
442-
})
441+
}
443442
}
444443
}
445444

cue/interpreter/wasm/runtime.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package wasm
1616

1717
import (
18+
"bytes"
1819
"context"
1920
"fmt"
2021
"os"
@@ -183,11 +184,11 @@ func (m *memory) Bytes() []byte {
183184
m.i.mu.Lock()
184185
defer m.i.mu.Unlock()
185186

186-
bytes, ok := m.i.instance.Memory().Read(m.ptr, m.len)
187+
p, ok := m.i.instance.Memory().Read(m.ptr, m.len)
187188
if !ok {
188189
panic(fmt.Sprintf("can't read %d bytes from Wasm address %#x", m.len, m.ptr))
189190
}
190-
return append([]byte{}, bytes...)
191+
return bytes.Clone(p)
191192
}
192193

193194
// WriteAt writes p at the given relative offset within m.

internal/mod/mvs/mvs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func Upgrade[V comparable](target V, reqs UpgradeReqs[V], upgrade ...V) ([]V, er
297297
for _, m := range list {
298298
pathInList[reqs.Path(m)] = true
299299
}
300-
list = append([]V(nil), list...)
300+
list = slices.Clone(list)
301301

302302
upgradeTo := make(map[string]string, len(upgrade))
303303
for _, u := range upgrade {

pkg/gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ var (
314314

315315
func (g *generator) genFunc(fn *types.Func) {
316316
g.nonConcrete = false
317-
sign := fn.Type().(*types.Signature)
317+
sign := fn.Signature()
318318
if sign.Recv() != nil {
319319
return
320320
}

pkg/list/list.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,7 @@ func Repeat(x []cue.Value, count int) ([]cue.Value, error) {
141141
if count < 0 {
142142
return nil, fmt.Errorf("negative count")
143143
}
144-
var a []cue.Value
145-
for range count {
146-
a = append(a, x...)
147-
}
148-
return a, nil
144+
return slices.Repeat(x, count), nil
149145
}
150146

151147
// Concat takes a list of lists and concatenates them.
@@ -319,12 +315,7 @@ outer:
319315
// Contains reports whether v is contained in a. The value must be a
320316
// comparable value.
321317
func Contains(a []cue.Value, v cue.Value) bool {
322-
for _, w := range a {
323-
if v.Equals(w) {
324-
return true
325-
}
326-
}
327-
return false
318+
return slices.ContainsFunc(a, v.Equals)
328319
}
329320

330321
// MatchN is a validator that checks that the number of elements in the given

0 commit comments

Comments
 (0)