mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-26 17:52:25 +01:00
Merge pull request #9345 from ziggie1984/add-copy-method
Add a deep copy generic harness to the internal fn package
This commit is contained in:
commit
8c9de4d605
42
fn/func.go
Normal file
42
fn/func.go
Normal file
@ -0,0 +1,42 @@
|
||||
package fn
|
||||
|
||||
// Copyable is a generic interface for a type that's able to return a deep copy
|
||||
// of itself.
|
||||
type Copyable[T any] interface {
|
||||
Copy() T
|
||||
}
|
||||
|
||||
// CopyAll creates a new slice where each item of the slice is a deep copy of
|
||||
// the elements of the input slice.
|
||||
func CopyAll[T Copyable[T]](xs []T) []T {
|
||||
newItems := make([]T, len(xs))
|
||||
for i := range xs {
|
||||
newItems[i] = xs[i].Copy()
|
||||
}
|
||||
|
||||
return newItems
|
||||
}
|
||||
|
||||
// CopyableErr is a generic interface for a type that's able to return a deep copy
|
||||
// of itself. This is identical to Copyable, but should be used in cases where
|
||||
// the copy method can return an error.
|
||||
type CopyableErr[T any] interface {
|
||||
Copy() (T, error)
|
||||
}
|
||||
|
||||
// CopyAllErr creates a new slice where each item of the slice is a deep copy of
|
||||
// the elements of the input slice. This is identical to CopyAll, but should be
|
||||
// used in cases where the copy method can return an error.
|
||||
func CopyAllErr[T CopyableErr[T]](xs []T) ([]T, error) {
|
||||
var err error
|
||||
|
||||
newItems := make([]T, len(xs))
|
||||
for i := range xs {
|
||||
newItems[i], err = xs[i].Copy()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return newItems, nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user