go-car icon indicating copy to clipboard operation
go-car copied to clipboard

v2: use a byte array for the characteristics bitfield

Open mvdan opened this issue 2 years ago • 1 comments

Right now, characteristics is as follows:

type Characteristics struct {
	Hi uint64
	Lo uint64
}

And, as per the code, it encodes and decodes as little-endian.

If Go had a uint128 type, I'd definitely agree with this approach. For better or worse it doesn't, so using encoding/binary.LittleEndian doesn't save us complexity - we still have to choose between Hi and Lo.

I think it would be easier to use [16]byte, as proposed by @rvagg here: https://github.com/ipld/go-car/pull/239#discussion_r711861809

Then the logic should also become fairly straightforward to follow. For example, a "get bit" or "set bit" method on Characteristics would use pos/8 to select an array index, and pos%8 to select the bit within its byte.

mvdan avatar Sep 20 '21 12:09 mvdan