Skip to content

Commit 956b00b

Browse files
Roasbeefguggero
authored andcommitted
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.
1 parent 5b2869b commit 956b00b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tlv/record_type.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,30 @@ func ZeroRecordT[T TlvType, V any]() RecordT[T, V] {
159159
Val: v,
160160
}
161161
}
162+
163+
// BigSizeT is a high-order type that represents a TLV record that encodes an
164+
// integer as a BigSize value in the stream.
165+
type BigSizeT[T constraints.Integer] struct {
166+
// We'll store the base value in the struct as a uin64, but then expose
167+
// a public method to cast to the specified type.
168+
v uint64
169+
}
170+
171+
// NewBigSizeT creates a new BigSizeT type from a given integer type.
172+
func NewBigSizeT[T constraints.Integer](val T) BigSizeT[T] {
173+
return BigSizeT[T]{
174+
v: uint64(val),
175+
}
176+
}
177+
178+
// Int returns the underlying integer value of the BigSize record.
179+
func (b BigSizeT[T]) Int() T {
180+
return T(b.v)
181+
}
182+
183+
// Record returns the underlying record interface for the record type.
184+
func (t *BigSizeT[T]) Record() Record {
185+
// We use a zero value for the type here as this should be used with
186+
// the higher order RecordT type.
187+
return MakeBigSizeRecord(0, &t.v)
188+
}

0 commit comments

Comments
 (0)