Skip to content

Commit a3f7eaa

Browse files
committed
Return unwritten bytes instead of the length of written bytes
1 parent c6c1898 commit a3f7eaa

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/diff/algorithm.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,8 @@ pub fn apply_diff_copy(original: &[u8], diffset: &DiffSet<'_>) -> Result<Vec<u8>
243243
/// - destination is assumed to be zero-initialized. That automatically holds true for freshly
244244
/// allocated Solana account data. The function does NOT validate this assumption for performance reason.
245245
/// Returns:
246-
/// - Ok(n) where n is number of bytes written to destination.
247-
/// - if n < destination.len(), then the last (destination.len() - n) bytes are not written by this function
248-
/// and are assumed to be already zero-initialized. Callers may write to those bytes starting at index `n`.
249-
/// - else n == destination.len().
246+
/// - Ok(&mut [u8]) where the slice contains the trailing unwritten bytes in destination and are
247+
/// assumed to be already zero-initialized. Callers may write to those bytes or validate it.
250248
/// Notes:
251249
/// - Merge consists of:
252250
/// - bytes covered by diff segments are written from diffset.
@@ -255,11 +253,11 @@ pub fn apply_diff_copy(original: &[u8], diffset: &DiffSet<'_>) -> Result<Vec<u8>
255253
/// - In expansion case, any remaining bytes beyond both the diff coverage
256254
/// and original.len() stay unwritten and are assumed to be zero-initialized.
257255
///
258-
pub fn merge_diff_copy(
259-
destination: &mut [u8],
256+
pub fn merge_diff_copy<'a>(
257+
destination: &'a mut [u8],
260258
original: &[u8],
261259
diffset: &DiffSet<'_>,
262-
) -> Result<usize, ProgramError> {
260+
) -> Result<&'a mut [u8], ProgramError> {
263261
if destination.len() != diffset.changed_len() {
264262
return Err(DlpError::MergeDiffError.into());
265263
}
@@ -338,7 +336,9 @@ pub fn merge_diff_copy(
338336
}
339337
};
340338

341-
Ok(num_bytes_written)
339+
let (_, unwritten_bytes) = destination.split_at_mut(num_bytes_written);
340+
341+
Ok(unwritten_bytes)
342342
}
343343

344344
// private function that does the actual work.
@@ -530,11 +530,11 @@ mod tests {
530530

531531
let expected_changed = {
532532
let mut destination = vec![255; CHANGED_LEN];
533-
let written = merge_diff_copy(&mut destination, &original, &actual_diffset).unwrap();
533+
let unwritten = merge_diff_copy(&mut destination, &original, &actual_diffset).unwrap();
534534

535-
// TODO (snawaz): written == 120, is because currently the expanded bytes are part of the diff.
536-
// Once compute_diff is optimized further, written must be 100.
537-
assert_eq!(written, 120);
535+
// TODO (snawaz): unwritten == &mut [], is because currently the expanded bytes are part of the diff.
536+
// Once compute_diff is optimized further, written must be &mut [0; 20].
537+
assert_eq!(unwritten, &mut []);
538538

539539
destination
540540
};

0 commit comments

Comments
 (0)