1111 primitives:: PodU32 ,
1212 } ,
1313 bytemuck:: Pod ,
14- solana_program_error:: ProgramError ,
1514 std:: {
1615 marker:: PhantomData ,
1716 mem:: { align_of, size_of} ,
@@ -51,17 +50,17 @@ struct Layout {
5150impl < T : Pod , L : PodLength > ListView < T , L > {
5251 /// Calculate the total byte size for a `ListView` holding `num_items`.
5352 /// This includes the length prefix, padding, and data.
54- pub fn size_of ( num_items : usize ) -> Result < usize , ProgramError > {
53+ pub fn size_of ( num_items : usize ) -> Result < usize , PodSliceError > {
5554 let header_padding = Self :: header_padding ( ) ?;
5655 size_of :: < T > ( )
5756 . checked_mul ( num_items)
5857 . and_then ( |curr| curr. checked_add ( size_of :: < L > ( ) ) )
5958 . and_then ( |curr| curr. checked_add ( header_padding) )
60- . ok_or_else ( || PodSliceError :: CalculationFailure . into ( ) )
59+ . ok_or ( PodSliceError :: CalculationFailure )
6160 }
6261
6362 /// Unpack a read-only buffer into a `ListViewReadOnly`
64- pub fn unpack ( buf : & [ u8 ] ) -> Result < ListViewReadOnly < T , L > , ProgramError > {
63+ pub fn unpack ( buf : & [ u8 ] ) -> Result < ListViewReadOnly < T , L > , PodSliceError > {
6564 let layout = Self :: calculate_layout ( buf. len ( ) ) ?;
6665
6766 // Slice the buffer to get the length prefix and the data.
@@ -79,7 +78,7 @@ impl<T: Pod, L: PodLength> ListView<T, L> {
7978 let capacity = data. len ( ) ;
8079
8180 if ( * length) . into ( ) > capacity {
82- return Err ( PodSliceError :: BufferTooSmall . into ( ) ) ;
81+ return Err ( PodSliceError :: BufferTooSmall ) ;
8382 }
8483
8584 Ok ( ListViewReadOnly {
@@ -90,24 +89,24 @@ impl<T: Pod, L: PodLength> ListView<T, L> {
9089 }
9190
9291 /// Unpack the mutable buffer into a mutable `ListViewMut`
93- pub fn unpack_mut ( buf : & mut [ u8 ] ) -> Result < ListViewMut < T , L > , ProgramError > {
92+ pub fn unpack_mut ( buf : & mut [ u8 ] ) -> Result < ListViewMut < T , L > , PodSliceError > {
9493 let view = Self :: build_mut_view ( buf) ?;
9594 if ( * view. length ) . into ( ) > view. capacity {
96- return Err ( PodSliceError :: BufferTooSmall . into ( ) ) ;
95+ return Err ( PodSliceError :: BufferTooSmall ) ;
9796 }
9897 Ok ( view)
9998 }
10099
101100 /// Initialize a buffer: sets `length = 0` and returns a mutable `ListViewMut`.
102- pub fn init ( buf : & mut [ u8 ] ) -> Result < ListViewMut < T , L > , ProgramError > {
101+ pub fn init ( buf : & mut [ u8 ] ) -> Result < ListViewMut < T , L > , PodSliceError > {
103102 let view = Self :: build_mut_view ( buf) ?;
104103 * view. length = L :: try_from ( 0 ) ?;
105104 Ok ( view)
106105 }
107106
108107 /// Internal helper to build a mutable view without validation or initialization.
109108 #[ inline]
110- fn build_mut_view ( buf : & mut [ u8 ] ) -> Result < ListViewMut < T , L > , ProgramError > {
109+ fn build_mut_view ( buf : & mut [ u8 ] ) -> Result < ListViewMut < T , L > , PodSliceError > {
111110 let layout = Self :: calculate_layout ( buf. len ( ) ) ?;
112111
113112 // Split the buffer to get the length prefix and the data.
@@ -133,13 +132,13 @@ impl<T: Pod, L: PodLength> ListView<T, L> {
133132
134133 /// Calculate the byte ranges for the length and data sections of the buffer
135134 #[ inline]
136- fn calculate_layout ( buf_len : usize ) -> Result < Layout , ProgramError > {
135+ fn calculate_layout ( buf_len : usize ) -> Result < Layout , PodSliceError > {
137136 let len_field_end = size_of :: < L > ( ) ;
138137 let header_padding = Self :: header_padding ( ) ?;
139138 let data_start = len_field_end. saturating_add ( header_padding) ;
140139
141140 if buf_len < data_start {
142- return Err ( PodSliceError :: BufferTooSmall . into ( ) ) ;
141+ return Err ( PodSliceError :: BufferTooSmall ) ;
143142 }
144143
145144 Ok ( Layout {
@@ -153,10 +152,10 @@ impl<T: Pod, L: PodLength> ListView<T, L> {
153152 /// The goal is to ensure that the data field `T` starts at a memory offset
154153 /// that is a multiple of its alignment requirement.
155154 #[ inline]
156- fn header_padding ( ) -> Result < usize , ProgramError > {
155+ fn header_padding ( ) -> Result < usize , PodSliceError > {
157156 // Enforce that the length prefix type `L` itself does not have alignment requirements
158157 if align_of :: < L > ( ) != 1 {
159- return Err ( ProgramError :: InvalidArgument ) ;
158+ return Err ( PodSliceError :: InvalidArgument ) ;
160159 }
161160
162161 let length_size = size_of :: < L > ( ) ;
@@ -240,12 +239,12 @@ mod tests {
240239 // Case 1: Multiplication overflows.
241240 // `size_of::<u16>() * usize::MAX` will overflow.
242241 let err = ListView :: < u16 , PodU32 > :: size_of ( usize:: MAX ) . unwrap_err ( ) ;
243- assert_eq ! ( err, PodSliceError :: CalculationFailure . into ( ) ) ;
242+ assert_eq ! ( err, PodSliceError :: CalculationFailure ) ;
244243
245244 // Case 2: Multiplication does not overflow, but subsequent addition does.
246245 // `size_of::<u8>() * usize::MAX` does not overflow, but adding `size_of<L>` will.
247246 let err = ListView :: < u8 , PodU32 > :: size_of ( usize:: MAX ) . unwrap_err ( ) ;
248- assert_eq ! ( err, PodSliceError :: CalculationFailure . into ( ) ) ;
247+ assert_eq ! ( err, PodSliceError :: CalculationFailure ) ;
249248 }
250249
251250 #[ test]
@@ -271,13 +270,13 @@ mod tests {
271270 let mut buf = [ 0u8 ; 100 ] ;
272271
273272 let err_size_of = ListView :: < u8 , TestPodU32 > :: size_of ( 10 ) . unwrap_err ( ) ;
274- assert_eq ! ( err_size_of, ProgramError :: InvalidArgument ) ;
273+ assert_eq ! ( err_size_of, PodSliceError :: InvalidArgument ) ;
275274
276275 let err_unpack = ListView :: < u8 , TestPodU32 > :: unpack ( & buf) . unwrap_err ( ) ;
277- assert_eq ! ( err_unpack, ProgramError :: InvalidArgument ) ;
276+ assert_eq ! ( err_unpack, PodSliceError :: InvalidArgument ) ;
278277
279278 let err_init = ListView :: < u8 , TestPodU32 > :: init ( & mut buf) . unwrap_err ( ) ;
280- assert_eq ! ( err_init, ProgramError :: InvalidArgument ) ;
279+ assert_eq ! ( err_init, PodSliceError :: InvalidArgument ) ;
281280 }
282281
283282 #[ test]
@@ -445,10 +444,10 @@ mod tests {
445444 let mut buf = vec ! [ 0u8 ; header_size - 1 ] ; // 7 bytes
446445
447446 let err = ListView :: < u64 , PodU32 > :: unpack ( & buf) . unwrap_err ( ) ;
448- assert_eq ! ( err, PodSliceError :: BufferTooSmall . into ( ) ) ;
447+ assert_eq ! ( err, PodSliceError :: BufferTooSmall ) ;
449448
450449 let err = ListView :: < u64 , PodU32 > :: unpack_mut ( & mut buf) . unwrap_err ( ) ;
451- assert_eq ! ( err, PodSliceError :: BufferTooSmall . into ( ) ) ;
450+ assert_eq ! ( err, PodSliceError :: BufferTooSmall ) ;
452451 }
453452
454453 #[ test]
@@ -465,10 +464,10 @@ mod tests {
465464 buf[ 0 ..len_size] . copy_from_slice ( bytemuck:: bytes_of ( & pod_len) ) ;
466465
467466 let err = ListView :: < u32 , PodU32 > :: unpack ( & buf) . unwrap_err ( ) ;
468- assert_eq ! ( err, PodSliceError :: BufferTooSmall . into ( ) ) ;
467+ assert_eq ! ( err, PodSliceError :: BufferTooSmall ) ;
469468
470469 let err = ListView :: < u32 , PodU32 > :: unpack_mut ( & mut buf) . unwrap_err ( ) ;
471- assert_eq ! ( err, PodSliceError :: BufferTooSmall . into ( ) ) ;
470+ assert_eq ! ( err, PodSliceError :: BufferTooSmall ) ;
472471 }
473472
474473 #[ test]
@@ -482,20 +481,20 @@ mod tests {
482481 // bytemuck::try_cast_slice returns an alignment error, which we map to InvalidArgument
483482
484483 let err = ListView :: < u32 , PodU32 > :: unpack ( & buf) . unwrap_err ( ) ;
485- assert_eq ! ( err, ProgramError :: InvalidArgument ) ;
484+ assert_eq ! ( err, PodSliceError :: PodCast ) ;
486485
487486 let err = ListView :: < u32 , PodU32 > :: unpack_mut ( & mut buf) . unwrap_err ( ) ;
488- assert_eq ! ( err, ProgramError :: InvalidArgument ) ;
487+ assert_eq ! ( err, PodSliceError :: PodCast ) ;
489488 }
490489
491490 #[ test]
492491 fn test_unpack_empty_buffer ( ) {
493492 let mut buf = [ ] ;
494493 let err = ListView :: < u32 , PodU32 > :: unpack ( & buf) . unwrap_err ( ) ;
495- assert_eq ! ( err, PodSliceError :: BufferTooSmall . into ( ) ) ;
494+ assert_eq ! ( err, PodSliceError :: BufferTooSmall ) ;
496495
497496 let err = ListView :: < u32 , PodU32 > :: unpack_mut ( & mut buf) . unwrap_err ( ) ;
498- assert_eq ! ( err, PodSliceError :: BufferTooSmall . into ( ) ) ;
497+ assert_eq ! ( err, PodSliceError :: BufferTooSmall ) ;
499498 }
500499
501500 #[ test]
@@ -562,12 +561,12 @@ mod tests {
562561 // Header requires 4 bytes (size_of<PodU32>)
563562 let mut buf = vec ! [ 0u8 ; 3 ] ;
564563 let err = ListView :: < u32 , PodU32 > :: init ( & mut buf) . unwrap_err ( ) ;
565- assert_eq ! ( err, PodSliceError :: BufferTooSmall . into ( ) ) ;
564+ assert_eq ! ( err, PodSliceError :: BufferTooSmall ) ;
566565
567566 // With padding, header requires 8 bytes (4 for len, 4 for pad)
568567 let mut buf_padded = vec ! [ 0u8 ; 7 ] ;
569568 let err_padded = ListView :: < u64 , PodU32 > :: init ( & mut buf_padded) . unwrap_err ( ) ;
570- assert_eq ! ( err_padded, PodSliceError :: BufferTooSmall . into ( ) ) ;
569+ assert_eq ! ( err_padded, PodSliceError :: BufferTooSmall ) ;
571570 }
572571
573572 #[ test]
0 commit comments