@@ -3,14 +3,11 @@ use std::cmp::{min, Ordering};
33use pinocchio:: error:: ProgramError ;
44use rkyv:: util:: AlignedVec ;
55
6- use crate :: error:: DlpError ;
7-
86use super :: {
9- DiffSet , OffsetInData , SizeChanged , SIZE_OF_CHANGED_LEN , SIZE_OF_NUM_OFFSET_PAIRS ,
10- SIZE_OF_SINGLE_OFFSET_PAIR ,
7+ DiffSet , OffsetInData , SizeChanged , SIZE_OF_CHANGED_LEN ,
8+ SIZE_OF_NUM_OFFSET_PAIRS , SIZE_OF_SINGLE_OFFSET_PAIR ,
119} ;
12-
13- use crate :: { require_eq, require_le} ;
10+ use crate :: { error:: DlpError , require_eq, require_le} ;
1411
1512///
1613/// Compute diff between original and changed.
@@ -191,7 +188,10 @@ pub fn compute_diff(original: &[u8], changed: &[u8]) -> AlignedVec {
191188/// - None means NO change
192189/// - Some(size_changed) means the data size has changed and size_changed indicates
193190/// whether it has expanded or shrunk.
194- pub fn detect_size_change ( original : & [ u8 ] , diffset : & DiffSet < ' _ > ) -> Option < SizeChanged > {
191+ pub fn detect_size_change (
192+ original : & [ u8 ] ,
193+ diffset : & DiffSet < ' _ > ,
194+ ) -> Option < SizeChanged > {
195195 match diffset. changed_len ( ) . cmp ( & original. len ( ) ) {
196196 Ordering :: Less => Some ( SizeChanged :: Shrunk ( diffset. changed_len ( ) ) ) ,
197197 Ordering :: Greater => Some ( SizeChanged :: Expanded ( diffset. changed_len ( ) ) ) ,
@@ -203,7 +203,10 @@ pub fn detect_size_change(original: &[u8], diffset: &DiffSet<'_>) -> Option<Size
203203///
204204/// Precondition:
205205/// - original.len() must be equal to the length encoded in the diff.
206- pub fn apply_diff_in_place ( original : & mut [ u8 ] , diffset : & DiffSet < ' _ > ) -> Result < ( ) , ProgramError > {
206+ pub fn apply_diff_in_place (
207+ original : & mut [ u8 ] ,
208+ diffset : & DiffSet < ' _ > ,
209+ ) -> Result < ( ) , ProgramError > {
207210 if let Some ( _layout) = detect_size_change ( original, diffset) {
208211 return Err ( ProgramError :: InvalidInstructionData ) ;
209212 }
@@ -212,7 +215,10 @@ pub fn apply_diff_in_place(original: &mut [u8], diffset: &DiffSet<'_>) -> Result
212215
213216/// This function creates a copy of original, possibly extending or shrinking it,
214217/// and then applies the diff to it, before returning it.
215- pub fn apply_diff_copy ( original : & [ u8 ] , diffset : & DiffSet < ' _ > ) -> Result < Vec < u8 > , ProgramError > {
218+ pub fn apply_diff_copy (
219+ original : & [ u8 ] ,
220+ diffset : & DiffSet < ' _ > ,
221+ ) -> Result < Vec < u8 > , ProgramError > {
216222 Ok ( match detect_size_change ( original, diffset) {
217223 Some ( SizeChanged :: Expanded ( new_size) ) => {
218224 let mut applied = Vec :: with_capacity ( new_size) ;
@@ -273,7 +279,8 @@ pub fn merge_diff_copy<'a>(
273279 if write_index < start {
274280 require_le ! ( start, original. len( ) , DlpError :: InvalidDiff ) ;
275281 // copy the unchanged bytes
276- destination[ write_index..start] . copy_from_slice ( & original[ write_index..start] ) ;
282+ destination[ write_index..start]
283+ . copy_from_slice ( & original[ write_index..start] ) ;
277284 }
278285
279286 destination[ start..end] . copy_from_slice ( diff_segment) ;
@@ -319,12 +326,14 @@ pub fn merge_diff_copy<'a>(
319326 if destination. len ( ) <= original. len ( ) {
320327 // case: all n bytes come from original
321328 let dest_len = destination. len ( ) ;
322- destination[ write_index..] . copy_from_slice ( & original[ write_index..dest_len] ) ;
329+ destination[ write_index..]
330+ . copy_from_slice ( & original[ write_index..dest_len] ) ;
323331 dest_len
324332 } else if write_index < original. len ( ) {
325333 // case: some bytes come from original and the rest are "assumed" to be
326334 // zero-initialized (by the caller).
327- destination[ write_index..original. len ( ) ] . copy_from_slice ( & original[ write_index..] ) ;
335+ destination[ write_index..original. len ( ) ]
336+ . copy_from_slice ( & original[ write_index..] ) ;
328337 original. len ( )
329338 } else {
330339 // case: all N bytes are "assumed" to be zero-initialized (by the caller).
@@ -344,7 +353,10 @@ pub fn merge_diff_copy<'a>(
344353}
345354
346355// private function that does the actual work.
347- fn apply_diff_impl ( original : & mut [ u8 ] , diffset : & DiffSet < ' _ > ) -> Result < ( ) , ProgramError > {
356+ fn apply_diff_impl (
357+ original : & mut [ u8 ] ,
358+ diffset : & DiffSet < ' _ > ,
359+ ) -> Result < ( ) , ProgramError > {
348360 for item in diffset. iter ( ) {
349361 let ( diff_segment, offset_range) = item?;
350362 original[ offset_range] . copy_from_slice ( diff_segment) ;
@@ -361,7 +373,10 @@ mod tests {
361373 Rng , RngCore , SeedableRng ,
362374 } ;
363375
364- use crate :: { apply_diff_copy, apply_diff_in_place, compute_diff, merge_diff_copy, DiffSet } ;
376+ use crate :: {
377+ apply_diff_copy, apply_diff_in_place, compute_diff, merge_diff_copy,
378+ DiffSet ,
379+ } ;
365380
366381 #[ test]
367382 fn test_no_change ( ) {
@@ -395,8 +410,9 @@ mod tests {
395410 // 2 (u32)
396411 expected_diff. extend_from_slice ( & 2u32 . to_le_bytes ( ) ) ;
397412 } else {
398- expected_diff
399- . extend_from_slice ( & ( 2u32 + additional_changes. len ( ) as u32 ) . to_le_bytes ( ) ) ;
413+ expected_diff. extend_from_slice (
414+ & ( 2u32 + additional_changes. len ( ) as u32 ) . to_le_bytes ( ) ,
415+ ) ;
400416 }
401417
402418 // -- offsets
@@ -450,13 +466,15 @@ mod tests {
450466 assert_eq ! ( actual_diff. len( ) , 4 + 4 + 8 + 8 + ( 4 + 8 ) ) ;
451467 assert_eq ! ( actual_diff. as_slice( ) , expected_diff. as_slice( ) ) ;
452468
453- let expected_changed = apply_diff_copy ( & original, & actual_diffset) . unwrap ( ) ;
469+ let expected_changed =
470+ apply_diff_copy ( & original, & actual_diffset) . unwrap ( ) ;
454471
455472 assert_eq ! ( changed. as_slice( ) , expected_changed. as_slice( ) ) ;
456473
457474 let expected_changed = {
458475 let mut destination = vec ! [ 255 ; original. len( ) ] ;
459- merge_diff_copy ( & mut destination, & original, & actual_diffset) . unwrap ( ) ;
476+ merge_diff_copy ( & mut destination, & original, & actual_diffset)
477+ . unwrap ( ) ;
460478 destination
461479 } ;
462480
@@ -492,7 +510,8 @@ mod tests {
492510
493511 let expected_changed = {
494512 let mut destination = vec ! [ 255 ; CHANGED_LEN ] ;
495- merge_diff_copy ( & mut destination, & original, & actual_diffset) . unwrap ( ) ;
513+ merge_diff_copy ( & mut destination, & original, & actual_diffset)
514+ . unwrap ( ) ;
496515 destination
497516 } ;
498517
@@ -525,14 +544,17 @@ mod tests {
525544 //
526545 // TODO (snawaz): we could optimize compute_diff to not include the zero bytes which are
527546 // part of the expansion.
528- let expected_diff = get_example_expected_diff ( CHANGED_LEN , vec ! [ ( 100 , & [ 0 ; 20 ] ) ] ) ;
547+ let expected_diff =
548+ get_example_expected_diff ( CHANGED_LEN , vec ! [ ( 100 , & [ 0 ; 20 ] ) ] ) ;
529549
530550 assert_eq ! ( actual_diff. len( ) , 4 + 4 + ( 8 + 8 ) + ( 4 + 8 ) + ( 4 + 4 + 20 ) ) ;
531551 assert_eq ! ( actual_diff. as_slice( ) , expected_diff. as_slice( ) ) ;
532552
533553 let expected_changed = {
534554 let mut destination = vec ! [ 255 ; CHANGED_LEN ] ;
535- let unwritten = merge_diff_copy ( & mut destination, & original, & actual_diffset) . unwrap ( ) ;
555+ let unwritten =
556+ merge_diff_copy ( & mut destination, & original, & actual_diffset)
557+ . unwrap ( ) ;
536558
537559 // TODO (snawaz): unwritten == &mut [], is because currently the expanded bytes are part of the diff.
538560 // Once compute_diff is optimized further, written must be &mut [0; 20].
@@ -587,7 +609,8 @@ mod tests {
587609 }
588610 }
589611 // println!("{diff_offset}, {diff_end} => {diff_len}");
590- slices. push ( ( diff_offset, copy[ diff_offset..diff_end] . to_vec ( ) ) ) ;
612+ slices
613+ . push ( ( diff_offset, copy[ diff_offset..diff_end] . to_vec ( ) ) ) ;
591614
592615 offset_range = ( diff_end + 8 ) ..( diff_end + 8 + slab) ;
593616 }
@@ -606,7 +629,9 @@ mod tests {
606629 let mut offset_in_diff = 0u32 ;
607630 for ( offset_in_account, slice) in slices. iter ( ) {
608631 diff. extend_from_slice ( & offset_in_diff. to_le_bytes ( ) ) ;
609- diff. extend_from_slice ( & ( * offset_in_account as u32 ) . to_le_bytes ( ) ) ;
632+ diff. extend_from_slice (
633+ & ( * offset_in_account as u32 ) . to_le_bytes ( ) ,
634+ ) ;
610635 offset_in_diff += slice. len ( ) as u32 ;
611636 }
612637
@@ -634,7 +659,8 @@ mod tests {
634659
635660 let expected_changed = {
636661 let mut destination = vec ! [ 255 ; original. len( ) ] ;
637- merge_diff_copy ( & mut destination, & original, & actual_diffset) . unwrap ( ) ;
662+ merge_diff_copy ( & mut destination, & original, & actual_diffset)
663+ . unwrap ( ) ;
638664 destination
639665 } ;
640666
0 commit comments