]> Sergey Matveev's repositories - btrtrc.git/commitdiff
util: Support CopyExact with pointer source
authorMatt Joiner <anacrolix@gmail.com>
Wed, 18 Mar 2015 07:09:54 +0000 (18:09 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 18 Mar 2015 07:09:54 +0000 (18:09 +1100)
Nice to avoid copying large value types.

util/copy.go
util/copy_test.go

index fda4cffc39906c68bf0881a076d33ae8f73a3faf..94d98255b22e15fd0439474c66064b5699f4711f 100644 (file)
@@ -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()))
        }
index 18c9e2d6b80da87d3c7ea8b326ad649c443fea4b..452987101ff16a5bf40aac3af076a6a55fc71a3a 100644 (file)
@@ -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()
+       }
+}