summaryrefslogtreecommitdiffhomepage
path: root/tools
diff options
context:
space:
mode:
authorAyush Ranjan <ayushranjan@google.com>2021-11-08 00:37:24 -0800
committergVisor bot <gvisor-bot@google.com>2021-11-08 00:39:46 -0800
commit4622e17bccc7c40a2698e8314d29bbde87090cec (patch)
treeebf9e83a9c9275bfb90367f301902072d8526028 /tools
parentce4f4283badb6b07baf9f8e6d99e7a5fd15c92db (diff)
Simplify {Un}MarshalUnsafeSlice method signatures.
Earlier this function was returning (int, error) much like the Copy{In/Out} methods. The returned error was always nil. The returned int was never used. Instead make it returned the shifted buffer which is more useful. Updates #6450 PiperOrigin-RevId: 408268327
Diffstat (limited to 'tools')
-rw-r--r--tools/go_marshal/gomarshal/generator_interfaces_primitive_newtype.go20
-rw-r--r--tools/go_marshal/gomarshal/generator_interfaces_struct.go33
2 files changed, 26 insertions, 27 deletions
diff --git a/tools/go_marshal/gomarshal/generator_interfaces_primitive_newtype.go b/tools/go_marshal/gomarshal/generator_interfaces_primitive_newtype.go
index e2387f032..86c2dbdd5 100644
--- a/tools/go_marshal/gomarshal/generator_interfaces_primitive_newtype.go
+++ b/tools/go_marshal/gomarshal/generator_interfaces_primitive_newtype.go
@@ -264,36 +264,36 @@ func (g *interfaceGenerator) emitMarshallableSliceForPrimitiveNewtype(nt *ast.Id
g.emit("}\n\n")
g.emit("// MarshalUnsafe%s is like %s.MarshalUnsafe, but for a []%s.\n", slice.ident, g.typeName(), g.typeName())
- g.emit("func MarshalUnsafe%s(src []%s, dst []byte) (int, error) {\n", slice.ident, g.typeName())
+ g.emit("func MarshalUnsafe%s(src []%s, dst []byte) []byte {\n", slice.ident, g.typeName())
g.inIndent(func() {
g.emit("count := len(src)\n")
g.emit("if count == 0 {\n")
g.inIndent(func() {
- g.emit("return 0, nil\n")
+ g.emit("return dst\n")
})
g.emit("}\n")
g.emit("size := (*%s)(nil).SizeBytes()\n\n", g.typeName())
- g.emit("dst = dst[:size*count]\n")
- g.emit("gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(&src[0]), uintptr(len(dst)))\n")
- g.emit("return size*count, nil\n")
+ g.emit("buf := dst[:size*count]\n")
+ g.emit("gohacks.Memmove(unsafe.Pointer(&buf[0]), unsafe.Pointer(&src[0]), uintptr(len(buf)))\n")
+ g.emit("return dst[size*count:]\n")
})
g.emit("}\n\n")
g.emit("// UnmarshalUnsafe%s is like %s.UnmarshalUnsafe, but for a []%s.\n", slice.ident, g.typeName(), g.typeName())
- g.emit("func UnmarshalUnsafe%s(dst []%s, src []byte) (int, error) {\n", slice.ident, g.typeName())
+ g.emit("func UnmarshalUnsafe%s(dst []%s, src []byte) []byte {\n", slice.ident, g.typeName())
g.inIndent(func() {
g.emit("count := len(dst)\n")
g.emit("if count == 0 {\n")
g.inIndent(func() {
- g.emit("return 0, nil\n")
+ g.emit("return src\n")
})
g.emit("}\n")
g.emit("size := (*%s)(nil).SizeBytes()\n\n", g.typeName())
- g.emit("src = src[:(size*count)]\n")
- g.emit("gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(&src[0]), uintptr(len(src)))\n")
- g.emit("return size*count, nil\n")
+ g.emit("buf := src[:size*count]\n")
+ g.emit("gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(&buf[0]), uintptr(len(buf)))\n")
+ g.emit("return src[size*count:]\n")
})
g.emit("}\n\n")
}
diff --git a/tools/go_marshal/gomarshal/generator_interfaces_struct.go b/tools/go_marshal/gomarshal/generator_interfaces_struct.go
index 21177d39c..e7e4aef76 100644
--- a/tools/go_marshal/gomarshal/generator_interfaces_struct.go
+++ b/tools/go_marshal/gomarshal/generator_interfaces_struct.go
@@ -545,15 +545,14 @@ func (g *interfaceGenerator) emitMarshallableSliceForStruct(st *ast.StructType,
g.emit("}\n\n")
g.emit("// MarshalUnsafe%s is like %s.MarshalUnsafe, but for a []%s.\n", slice.ident, g.typeName(), g.typeName())
- g.emit("func MarshalUnsafe%s(src []%s, dst []byte) (int, error) {\n", slice.ident, g.typeName())
+ g.emit("func MarshalUnsafe%s(src []%s, dst []byte) []byte {\n", slice.ident, g.typeName())
g.inIndent(func() {
g.emit("count := len(src)\n")
g.emit("if count == 0 {\n")
g.inIndent(func() {
- g.emit("return 0, nil\n")
+ g.emit("return dst\n")
})
- g.emit("}\n")
- g.emit("size := (*%s)(nil).SizeBytes()\n\n", g.typeName())
+ g.emit("}\n\n")
fallback := func() {
g.emit("// Type %s doesn't have a packed layout in memory, fall back to MarshalBytes.\n", g.typeName())
@@ -562,7 +561,7 @@ func (g *interfaceGenerator) emitMarshallableSliceForStruct(st *ast.StructType,
g.emit("dst = src[idx].MarshalBytes(dst)\n")
})
g.emit("}\n")
- g.emit("return size * count, nil\n")
+ g.emit("return dst\n")
}
if thisPacked {
g.recordUsedImport("reflect")
@@ -574,9 +573,10 @@ func (g *interfaceGenerator) emitMarshallableSliceForStruct(st *ast.StructType,
g.inIndent(fallback)
g.emit("}\n\n")
}
- g.emit("dst = dst[:size*count]\n")
- g.emit("gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(&src[0]), uintptr(len(dst)))\n")
- g.emit("return size * count, nil\n")
+ g.emit("size := (*%s)(nil).SizeBytes()\n", g.typeName())
+ g.emit("buf := dst[:size*count]\n")
+ g.emit("gohacks.Memmove(unsafe.Pointer(&buf[0]), unsafe.Pointer(&src[0]), uintptr(len(buf)))\n")
+ g.emit("return dst[size*count:]\n")
} else {
fallback()
}
@@ -584,15 +584,14 @@ func (g *interfaceGenerator) emitMarshallableSliceForStruct(st *ast.StructType,
g.emit("}\n\n")
g.emit("// UnmarshalUnsafe%s is like %s.UnmarshalUnsafe, but for a []%s.\n", slice.ident, g.typeName(), g.typeName())
- g.emit("func UnmarshalUnsafe%s(dst []%s, src []byte) (int, error) {\n", slice.ident, g.typeName())
+ g.emit("func UnmarshalUnsafe%s(dst []%s, src []byte) []byte {\n", slice.ident, g.typeName())
g.inIndent(func() {
g.emit("count := len(dst)\n")
g.emit("if count == 0 {\n")
g.inIndent(func() {
- g.emit("return 0, nil\n")
+ g.emit("return src\n")
})
- g.emit("}\n")
- g.emit("size := (*%s)(nil).SizeBytes()\n\n", g.typeName())
+ g.emit("}\n\n")
fallback := func() {
g.emit("// Type %s doesn't have a packed layout in memory, fall back to UnmarshalBytes.\n", g.typeName())
@@ -601,7 +600,7 @@ func (g *interfaceGenerator) emitMarshallableSliceForStruct(st *ast.StructType,
g.emit("src = dst[idx].UnmarshalBytes(src)\n")
})
g.emit("}\n")
- g.emit("return size * count, nil\n")
+ g.emit("return src\n")
}
if thisPacked {
g.recordUsedImport("gohacks")
@@ -613,10 +612,10 @@ func (g *interfaceGenerator) emitMarshallableSliceForStruct(st *ast.StructType,
g.emit("}\n\n")
}
- g.emit("src = src[:(size*count)]\n")
- g.emit("gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(&src[0]), uintptr(len(src)))\n")
-
- g.emit("return count*size, nil\n")
+ g.emit("size := (*%s)(nil).SizeBytes()\n", g.typeName())
+ g.emit("buf := src[:size*count]\n")
+ g.emit("gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(&buf[0]), uintptr(len(buf)))\n")
+ g.emit("return src[size*count:]\n")
} else {
fallback()
}