summaryrefslogtreecommitdiff
path: root/lib/binstruct/size.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-01 22:43:58 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-01 22:43:58 -0700
commitd675f41242c043ddc4c6c1a1fb8aabcfd324aae2 (patch)
tree4f2afbce761eb377ad0b0ab2e4fb2f478ff844f5 /lib/binstruct/size.go
parent9971e38110d5f90d15c7b78f396f2638b3952a96 (diff)
parent6e1a9fbb1e9a943e04902ed3a4958f6821e39456 (diff)
Merge branch 'lukeshu/lint'
Diffstat (limited to 'lib/binstruct/size.go')
-rw-r--r--lib/binstruct/size.go18
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/binstruct/size.go b/lib/binstruct/size.go
index 365da85..d6d70c6 100644
--- a/lib/binstruct/size.go
+++ b/lib/binstruct/size.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -28,8 +28,16 @@ var (
unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
)
+const (
+ sizeof8 = 1
+ sizeof16 = 2
+ sizeof32 = 4
+ sizeof64 = 8
+)
+
func staticSize(typ reflect.Type) (int, error) {
if typ.Implements(staticSizerType) {
+ //nolint:forcetypeassert // Already did a type check via reflection.
return reflect.New(typ).Elem().Interface().(StaticSizer).BinaryStaticSize(), nil
}
if typ.Implements(marshalerType) || typ.Implements(unmarshalerType) {
@@ -42,13 +50,13 @@ func staticSize(typ reflect.Type) (int, error) {
}
switch typ.Kind() {
case reflect.Uint8, reflect.Int8:
- return 1, nil
+ return sizeof8, nil
case reflect.Uint16, reflect.Int16:
- return 2, nil
+ return sizeof16, nil
case reflect.Uint32, reflect.Int32:
- return 4, nil
+ return sizeof32, nil
case reflect.Uint64, reflect.Int64:
- return 8, nil
+ return sizeof64, nil
case reflect.Ptr:
return staticSize(typ.Elem())
case reflect.Array: