@@ -142,8 +142,10 @@ impl InlineView {
142
142
143
143
/// The length of the data, in bytes
144
144
#[ inline( always) ]
145
- pub fn len ( & self ) -> u32 {
146
- self . 0 as u32
145
+ pub fn len ( & self ) -> usize {
146
+ // take first 4 bytes
147
+ let len = self . 0 as u32 ;
148
+ len as usize
147
149
}
148
150
149
151
/// Access the value of the data, as bytes
@@ -152,8 +154,7 @@ impl InlineView {
152
154
/// If the length is greater than 12 (aka if this view is invalid)
153
155
#[ inline( always) ]
154
156
pub fn as_bytes ( & self ) -> & [ u8 ] {
155
- let len = self . len ( ) as usize ;
156
- & self . 0 . to_byte_slice ( ) [ 4 ..4 + len]
157
+ & self . 0 . to_byte_slice ( ) [ 4 ..4 + self . len ( ) ]
157
158
}
158
159
159
160
/// Is the view zero bytes?
@@ -227,6 +228,36 @@ impl ByteView {
227
228
pub fn as_u128 ( self ) -> u128 {
228
229
self . into_u128 ( )
229
230
}
231
+
232
+ /// The length of the data, in bytes
233
+ #[ inline( always) ]
234
+ pub fn len ( & self ) -> usize {
235
+ self . length as usize
236
+ }
237
+
238
+ /// If the view is zero bytes (always false)
239
+ #[ inline( always) ]
240
+ pub fn is_empty ( & self ) -> bool {
241
+ false
242
+ }
243
+
244
+ /// The buffer index
245
+ #[ inline( always) ]
246
+ pub fn buffer_index ( & self ) -> u32 {
247
+ self . buffer_index
248
+ }
249
+
250
+ /// The offset into the buffer
251
+ #[ inline( always) ]
252
+ pub fn offset ( & self ) -> usize {
253
+ self . offset as usize
254
+ }
255
+
256
+ /// The prefix of the data (always 4 bytes)
257
+ #[ inline( always) ]
258
+ pub fn prefix_as_bytes ( & self ) -> & [ u8 ] {
259
+ self . prefix . to_byte_slice ( )
260
+ }
230
261
}
231
262
232
263
impl From < u128 > for ByteView {
@@ -358,7 +389,7 @@ mod tests {
358
389
fn access_small_invalid ( ) {
359
390
// use invalid length 20
360
391
// (7 bytes 0 padding, "hello", 15)
361
- let v = 0x00000000_0000006f_6c6c6568_0000000Fu128 ;
392
+ let v = 0x00000000_0000006f_6c6c6568_0000000fu128 ;
362
393
let inline = InlineView ( v) ;
363
394
inline. as_bytes ( ) ;
364
395
}
@@ -379,6 +410,16 @@ mod tests {
379
410
assert_eq ! ( view, View :: from( "hello world here I am" ) )
380
411
}
381
412
413
+ #[ test]
414
+ fn access_large ( ) {
415
+ let View :: Byte ( bytes) = View :: from ( "hello world here I am" ) else {
416
+ panic ! ( "unexpected view" ) ;
417
+ } ;
418
+
419
+ assert_eq ! ( bytes. len( ) , 21 ) ;
420
+ assert_eq ! ( bytes. prefix_as_bytes( ) , "hell" . as_bytes( ) ) ;
421
+ }
422
+
382
423
// test round trip through u128
383
424
384
425
// Test
0 commit comments