Skip to content

Commit

Permalink
fixup! Fix issues reported by golangsci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 3, 2024
1 parent 474eb12 commit a533934
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion net/blockwise/blockwise.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func DecodeBlockOption(blockVal uint32) (szx SZX, blockNumber int64, moreBlocksF
return
}

szx = SZX(blockVal & szxMask) // masking for the SZX
szx = math.CastTo[SZX](blockVal & szxMask) // masking for the SZX
if (blockVal & moreBlocksFollowingMask) != 0 { // masking for the "M"
moreBlocksFollowing = true
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/math/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"golang.org/x/exp/constraints"
)

// Max returns maximal value for given integer type
func Max[T constraints.Integer]() T {
size := unsafe.Sizeof(T(0))
switch reflect.TypeOf((*T)(nil)).Elem().Kind() {
Expand All @@ -21,6 +22,7 @@ func Max[T constraints.Integer]() T {
}
}

// Min returns minimal value for given integer type
func Min[T constraints.Integer]() T {
size := unsafe.Sizeof(T(0))
switch reflect.TypeOf((*T)(nil)).Elem().Kind() {
Expand All @@ -33,6 +35,7 @@ func Min[T constraints.Integer]() T {
}
}

// CastTo casts one integer type to another with bounds checking and returns error in case of overflow
func SafeCastTo[T, F constraints.Integer](from F) (T, error) {
if from > 0 && uint64(Max[T]()) < uint64(from) {
return T(0), fmt.Errorf("value(%v) exceeds the maximum value for type(%v)", from, Max[T]())
Expand All @@ -43,10 +46,12 @@ func SafeCastTo[T, F constraints.Integer](from F) (T, error) {
return T(from), nil
}

// CastTo casts one integer type to another without bounds checking
func CastTo[T, F constraints.Integer](from F) T {
return T(from)
}

// MustSafeCastTo casts one integer type to another with bounds checking and panics in case of overflow
func MustSafeCastTo[T, F constraints.Integer](from F) T {
to, err := SafeCastTo[T](from)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions tcp/coder/coder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func (c *Coder) Size(m message.Message) (int, error) {

func getHeader(messageLength int) (uint8, []byte) {
if messageLength < MessageLength13Base {
return uint8(messageLength), nil
return math.CastTo[uint8](messageLength), nil
}
if messageLength < MessageLength14Base {
extLen := messageLength - MessageLength13Base
extLenBytes := []byte{uint8(extLen)}
extLenBytes := []byte{math.CastTo[uint8](extLen)}
return 13, extLenBytes
}
if messageLength < MessageLength15Base {
Expand Down Expand Up @@ -115,7 +115,7 @@ func (c *Coder) Encode(m message.Message, buf []byte) (int, error) {
}

// Length and TKL nibbles.
hdr[hdrOff] = uint8(0xf&len(m.Token)) | (lenNib << 4)
hdr[hdrOff] = math.CastTo[uint8](len(m.Token)) | (lenNib << 4)
hdrOff++

// Extended length, if present.
Expand Down

0 comments on commit a533934

Please sign in to comment.