github.com/segmentio/encoding@v0.4.0/proto/int.go (about)

     1  package proto
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  var intCodec = codec{
     8  	wire:   varint,
     9  	size:   sizeOfInt,
    10  	encode: encodeInt,
    11  	decode: decodeInt,
    12  }
    13  
    14  func sizeOfInt(p unsafe.Pointer, flags flags) int {
    15  	if p != nil {
    16  		if v := *(*int)(p); v != 0 || flags.has(wantzero) {
    17  			return sizeOfVarint(flags.uint64(int64(v)))
    18  		}
    19  	}
    20  	return 0
    21  }
    22  
    23  func encodeInt(b []byte, p unsafe.Pointer, flags flags) (int, error) {
    24  	if p != nil {
    25  		if v := *(*int)(p); v != 0 || flags.has(wantzero) {
    26  			return encodeVarint(b, flags.uint64(int64(v)))
    27  		}
    28  	}
    29  	return 0, nil
    30  }
    31  
    32  func decodeInt(b []byte, p unsafe.Pointer, flags flags) (int, error) {
    33  	v, n, err := decodeVarint(b)
    34  	*(*int)(p) = int(flags.int64(v))
    35  	return n, err
    36  }