@@ -104,15 +104,15 @@ type OptionalRecordT[T TlvType, V any] struct {
104104// TlvType returns the type of the record. This is the value used to identify
105105// this type on the wire. This value is bound to the specified TlvType type
106106// param.
107- func (t * OptionalRecordT [T , V ]) TlvType () Type {
107+ func (o * OptionalRecordT [T , V ]) TlvType () Type {
108108 zeroRecord := ZeroRecordT [T , V ]()
109109 return zeroRecord .TlvType ()
110110}
111111
112112// WhenSomeV executes the given function if the optional record is present.
113113// This operates on the inner most type, V, which is the value of the record.
114- func (t * OptionalRecordT [T , V ]) WhenSomeV (f func (V )) {
115- t .Option .WhenSome (func (r RecordT [T , V ]) {
114+ func (o * OptionalRecordT [T , V ]) WhenSomeV (f func (V )) {
115+ o .Option .WhenSome (func (r RecordT [T , V ]) {
116116 f (r .Val )
117117 })
118118}
@@ -126,7 +126,7 @@ func (o *OptionalRecordT[T, V]) UnwrapOrFailV(t *testing.T) V {
126126 return inner .Val
127127}
128128
129- // UnwrapOrErr is used to extract a value from an option, if the option is
129+ // UnwrapOrErrV is used to extract a value from an option, if the option is
130130// empty, then the specified error is returned directly. This gives the
131131// underlying value of the record, instead of the record itself.
132132func (o * OptionalRecordT [T , V ]) UnwrapOrErrV (err error ) (V , error ) {
@@ -141,10 +141,19 @@ func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) {
141141}
142142
143143// Zero returns a zero value of the record type.
144- func (t * OptionalRecordT [T , V ]) Zero () RecordT [T , V ] {
144+ func (o * OptionalRecordT [T , V ]) Zero () RecordT [T , V ] {
145145 return ZeroRecordT [T , V ]()
146146}
147147
148+ // ValOpt returns an Option of the underlying value. This can be used to chain
149+ // other option related methods to avoid needing to first go through the outer
150+ // record.
151+ func (o * OptionalRecordT [T , V ]) ValOpt () fn.Option [V ] {
152+ return fn .MapOption (func (record RecordT [T , V ]) V {
153+ return record .Val
154+ })(o .Option )
155+ }
156+
148157// SomeRecordT creates a new OptionalRecordT type from a given RecordT type.
149158func SomeRecordT [T TlvType , V any ](record RecordT [T , V ]) OptionalRecordT [T , V ] {
150159 return OptionalRecordT [T , V ]{
@@ -159,3 +168,30 @@ func ZeroRecordT[T TlvType, V any]() RecordT[T, V] {
159168 Val : v ,
160169 }
161170}
171+
172+ // BigSizeT is a high-order type that represents a TLV record that encodes an
173+ // integer as a BigSize value in the stream.
174+ type BigSizeT [T constraints.Integer ] struct {
175+ // We'll store the base value in the struct as a uin64, but then expose
176+ // a public method to cast to the specified type.
177+ v uint64
178+ }
179+
180+ // NewBigSizeT creates a new BigSizeT type from a given integer type.
181+ func NewBigSizeT [T constraints.Integer ](val T ) BigSizeT [T ] {
182+ return BigSizeT [T ]{
183+ v : uint64 (val ),
184+ }
185+ }
186+
187+ // Int returns the underlying integer value of the BigSize record.
188+ func (b BigSizeT [T ]) Int () T {
189+ return T (b .v )
190+ }
191+
192+ // Record returns the underlying record interface for the record type.
193+ func (b * BigSizeT [T ]) Record () Record {
194+ // We use a zero value for the type here as this should be used with
195+ // the higher order RecordT type.
196+ return MakeBigSizeRecord (0 , & b .v )
197+ }
0 commit comments