]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix CopyExact to work on a source string
authorMatt Joiner <anacrolix@gmail.com>
Sun, 16 Nov 2014 19:52:37 +0000 (13:52 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 16 Nov 2014 19:52:37 +0000 (13:52 -0600)
util/copy.go
util/copy_test.go

index c7d64cc27538bc241916d5ea8f921b3dae9b97ff..793a99cd951c832922dabd474a83e3c9080dd977 100644 (file)
@@ -15,7 +15,7 @@ func CopyExact(dest interface{}, src interface{}) {
                panic(fmt.Sprintf("dest not addressable: %T", dest))
        }
        if sV.Kind() == reflect.String {
-               sV = sV.Convert(dV.Type())
+               sV = sV.Convert(reflect.SliceOf(dV.Type().Elem()))
        }
        if dV.Len() != sV.Len() {
                panic(fmt.Sprintf("dest len (%d) != src len (%d)", dV.Len(), sV.Len()))
index 450e5c4b5d83cb2322d17baf87aabb4a2a2ecbc5..284840a3ad3982f7b45e69639e483ca698f95406 100644 (file)
@@ -44,3 +44,18 @@ func TestCopyLenMismatch(t *testing.T) {
        }()
        CopyExact(make([]byte, 2), "abc")
 }
+
+func TestCopySrcString(t *testing.T) {
+       dest := make([]byte, 3)
+       CopyExact(dest, "lol")
+       if string(dest) != "lol" {
+               t.FailNow()
+       }
+       defer func() {
+               r := recover()
+               if r == nil {
+                       t.FailNow()
+               }
+       }()
+       CopyExact(dest, "rofl")
+}