From: Matt Joiner Date: Wed, 18 Mar 2015 07:09:54 +0000 (+1100) Subject: util: Support CopyExact with pointer source X-Git-Tag: v1.0.0~1278 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=0f7c4c5bda4616d55f62fa0c64da28e9272ef0ce;p=btrtrc.git util: Support CopyExact with pointer source Nice to avoid copying large value types. --- diff --git a/util/copy.go b/util/copy.go index fda4cffc..94d98255 100644 --- a/util/copy.go +++ b/util/copy.go @@ -14,6 +14,9 @@ func CopyExact(dest interface{}, src interface{}) { if dV.Kind() == reflect.Array && !dV.CanAddr() { panic(fmt.Sprintf("dest not addressable: %T", dest)) } + if sV.Kind() == reflect.Ptr { + sV = sV.Elem() + } if sV.Kind() == reflect.String { sV = sV.Convert(reflect.SliceOf(dV.Type().Elem())) } diff --git a/util/copy_test.go b/util/copy_test.go index 18c9e2d6..45298710 100644 --- a/util/copy_test.go +++ b/util/copy_test.go @@ -78,3 +78,12 @@ func TestCopySrcNilInterface(t *testing.T) { }() CopyExact(&arr, nil) } + +func TestCopySrcPtr(t *testing.T) { + var bigDst [1024]byte + var bigSrc [1024]byte = [1024]byte{'h', 'i'} + CopyExact(&bigDst, &bigSrc) + if !bytes.Equal(bigDst[:], bigSrc[:]) { + t.FailNow() + } +}