github.com/kamalshkeir/kencoding@v0.0.2-0.20230409043843-44b609a0475a/proto/uint.go (about)

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