Files
realy/json/unsigned.go
2025-02-08 16:15:13 -01:06

36 lines
845 B
Go

package json
import (
"realy.lol/ints"
)
// Unsigned integers have no possible `-` prefix nor a decimal place.
//
// Initialize these values as follows:
//
// num := &json.Unsigned{}
//
// to get a default value which is zero.
//
// There is a generic NewUnsigned function which will automatically convert any integer type to
// the internal uint64 type, saving the caller from needing to cast it in their code.
type Unsigned struct{ V uint64 }
func NewUnsigned[V int64 | int32 | int16 | int8 | uint64 | uint32 | uint16 |
uint8](i V) *Signed {
return &Signed{int64(i)}
}
func (u *Unsigned) Marshal(dst []byte) (b []byte) { return ints.New(u.V).Marshal(dst) }
func (u *Unsigned) Unmarshal(dst []byte) (rem []byte, err error) {
rem = dst
n := ints.New(u.V)
if rem, err = n.Unmarshal(rem); chk.E(err) {
return
}
u.V = n.N
return
}