tlv: add in new BigSizeT type

This type is useful when one wants to encode an integer as an underlying BigSize record. It wraps any integer, then handles the transformation into and out of the BigSize encoding on disk.
This commit is contained in:
Olaoluwa Osuntokun 2024-03-30 17:24:38 -07:00 committed by Oliver Gugger
parent 5b2869b0ec
commit 956b00b599
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

View File

@ -159,3 +159,30 @@ func ZeroRecordT[T TlvType, V any]() RecordT[T, V] {
Val: v,
}
}
// BigSizeT is a high-order type that represents a TLV record that encodes an
// integer as a BigSize value in the stream.
type BigSizeT[T constraints.Integer] struct {
// We'll store the base value in the struct as a uin64, but then expose
// a public method to cast to the specified type.
v uint64
}
// NewBigSizeT creates a new BigSizeT type from a given integer type.
func NewBigSizeT[T constraints.Integer](val T) BigSizeT[T] {
return BigSizeT[T]{
v: uint64(val),
}
}
// Int returns the underlying integer value of the BigSize record.
func (b BigSizeT[T]) Int() T {
return T(b.v)
}
// Record returns the underlying record interface for the record type.
func (t *BigSizeT[T]) Record() Record {
// We use a zero value for the type here as this should be used with
// the higher order RecordT type.
return MakeBigSizeRecord(0, &t.v)
}