Skip to content

Commit

Permalink
cue: make use of Go 1.20+ APIs
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mvdan committed Sep 1, 2023
1 parent 3165a5e commit fae9c82
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 3 additions & 4 deletions cue/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func incompleteError(v Value) errors.Error {

func (d *decoder) clear(x reflect.Value) {
if x.CanSet() {
x.Set(reflect.Zero(x.Type()))
x.SetZero()
}
}

Expand Down Expand Up @@ -373,11 +373,10 @@ func (d *decoder) convertMap(x reflect.Value, v Value) {
}
}

elemType := t.Elem()
if !mapElem.IsValid() {
mapElem = reflect.New(elemType).Elem()
mapElem = reflect.New(t.Elem()).Elem()
} else {
mapElem.Set(reflect.Zero(elemType))
mapElem.SetZero()
}
d.decode(mapElem, iter.Value(), false)

Expand Down
2 changes: 1 addition & 1 deletion cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ func (v Value) Bytes() ([]byte, error) {
ctx := v.ctx()
switch x := v.eval(ctx).(type) {
case *adt.Bytes:
return append([]byte(nil), x.B...), nil
return bytes.Clone(x.B), nil
case *adt.String:
return []byte(x.Str), nil
}
Expand Down

0 comments on commit fae9c82

Please sign in to comment.