Skip to content

Commit fae9c82

Browse files
committed
cue: make use of Go 1.20+ APIs
Now that we require Go 1.20 or later, we can use reflect.Value.SetZero and bytes.Clone, which are clearer and faster. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I6bee04ec9e42aaaa4d15e1bc8e1aa90d64b4134b Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1167629 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 3165a5e commit fae9c82

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

cue/decode.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func incompleteError(v Value) errors.Error {
7070

7171
func (d *decoder) clear(x reflect.Value) {
7272
if x.CanSet() {
73-
x.Set(reflect.Zero(x.Type()))
73+
x.SetZero()
7474
}
7575
}
7676

@@ -373,11 +373,10 @@ func (d *decoder) convertMap(x reflect.Value, v Value) {
373373
}
374374
}
375375

376-
elemType := t.Elem()
377376
if !mapElem.IsValid() {
378-
mapElem = reflect.New(elemType).Elem()
377+
mapElem = reflect.New(t.Elem()).Elem()
379378
} else {
380-
mapElem.Set(reflect.Zero(elemType))
379+
mapElem.SetZero()
381380
}
382381
d.decode(mapElem, iter.Value(), false)
383382

cue/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ func (v Value) Bytes() ([]byte, error) {
13321332
ctx := v.ctx()
13331333
switch x := v.eval(ctx).(type) {
13341334
case *adt.Bytes:
1335-
return append([]byte(nil), x.B...), nil
1335+
return bytes.Clone(x.B), nil
13361336
case *adt.String:
13371337
return []byte(x.Str), nil
13381338
}

0 commit comments

Comments
 (0)