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

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