github.com/kamalshkeir/kencoding@v0.0.2-0.20230409043843-44b609a0475a/proto/bool.go (about) 1 package proto 2 3 import ( 4 "io" 5 "unsafe" 6 ) 7 8 var boolCodec = codec{ 9 wire: varint, 10 size: sizeOfBool, 11 encode: encodeBool, 12 decode: decodeBool, 13 } 14 15 func sizeOfBool(p unsafe.Pointer, flags flags) int { 16 if p != nil && *(*bool)(p) || flags.has(wantzero) { 17 return 1 18 } 19 return 0 20 } 21 22 func encodeBool(b []byte, p unsafe.Pointer, flags flags) (int, error) { 23 if p != nil && *(*bool)(p) || flags.has(wantzero) { 24 if len(b) == 0 { 25 return 0, io.ErrShortBuffer 26 } 27 b[0] = 1 28 return 1, nil 29 } 30 return 0, nil 31 } 32 33 func decodeBool(b []byte, p unsafe.Pointer, _ flags) (int, error) { 34 if len(b) == 0 { 35 return 0, io.ErrUnexpectedEOF 36 } 37 *(*bool)(p) = b[0] != 0 38 return 1, nil 39 }