Skip to content

Commit

Permalink
chore: Use generic function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Oct 19, 2022
1 parent d750252 commit 9574f0a
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions pkg/chezmoi/boltpersistentstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"go.etcd.io/bbolt"
"golang.org/x/exp/slices"
)

// A BoltPersistentStateMode is a mode for opening a PersistentState.
Expand Down Expand Up @@ -86,7 +87,7 @@ func (b *BoltPersistentState) CopyTo(p PersistentState) error {
return b.db.View(func(tx *bbolt.Tx) error {
return tx.ForEach(func(bucket []byte, b *bbolt.Bucket) error {
return b.ForEach(func(key, value []byte) error {
return p.Set(copyByteSlice(bucket), copyByteSlice(key), copyByteSlice(value))
return p.Set(slices.Clone(bucket), slices.Clone(key), slices.Clone(value))
})
})
})
Expand Down Expand Up @@ -170,7 +171,7 @@ func (b *BoltPersistentState) ForEach(bucket []byte, fn func(k, v []byte) error)
return nil
}
return b.ForEach(func(k, v []byte) error {
return fn(copyByteSlice(k), copyByteSlice(v))
return fn(slices.Clone(k), slices.Clone(v))
})
})
}
Expand All @@ -190,7 +191,7 @@ func (b *BoltPersistentState) Get(bucket, key []byte) ([]byte, error) {
if b == nil {
return nil
}
value = copyByteSlice(b.Get(key))
value = slices.Clone(b.Get(key))
return nil
}); err != nil {
return nil, err
Expand Down Expand Up @@ -230,13 +231,3 @@ func (b *BoltPersistentState) open() error {
b.db = db
return nil
}

// copyByteSlice returns a copy of value.
func copyByteSlice(value []byte) []byte {
if value == nil {
return nil
}
result := make([]byte, len(value))
copy(result, value)
return result
}

0 comments on commit 9574f0a

Please sign in to comment.