rust/bupsplit: minor idiomatic fixes

This fixes a few un-idiomatic bits in Rust bupsplit code, getting rid
of some unchecked casts and an assert statement.

Closes: #1502
Approved by: cgwalters
This commit is contained in:
Luca Bruno 2018-03-16 21:33:11 +00:00 committed by Atomic Bot
parent 4e4436beec
commit 52f7ba1878
1 changed files with 9 additions and 9 deletions

View File

@ -65,8 +65,8 @@ impl Rollsum {
// These formulas are based on rollsum.h in the librsync project. // These formulas are based on rollsum.h in the librsync project.
pub fn add(&mut self, drop: u8, add: u8) -> () { pub fn add(&mut self, drop: u8, add: u8) -> () {
let drop_expanded = drop as u32; let drop_expanded = u32::from(drop);
let add_expanded = add as u32; let add_expanded = u32::from(add);
self.s1 = self.s1.wrapping_add(add_expanded.wrapping_sub(drop_expanded)); self.s1 = self.s1.wrapping_add(add_expanded.wrapping_sub(drop_expanded));
self.s2 = self.s2.wrapping_add(self.s1.wrapping_sub(BUP_WINDOWSIZE * (drop_expanded + ROLLSUM_CHAR_OFFSET))); self.s2 = self.s2.wrapping_add(self.s1.wrapping_sub(BUP_WINDOWSIZE * (drop_expanded + ROLLSUM_CHAR_OFFSET)));
} }
@ -102,11 +102,11 @@ pub extern fn bupsplit_sum(buf: *const u8, ofs: libc::size_t, len: libc::size_t)
pub extern fn bupsplit_find_ofs(buf: *const u8, len: libc::size_t, pub extern fn bupsplit_find_ofs(buf: *const u8, len: libc::size_t,
bits: *mut libc::c_int) -> libc::c_int bits: *mut libc::c_int) -> libc::c_int
{ {
let sbuf = unsafe { if buf.is_null() {
assert!(!buf.is_null()); return 0;
slice::from_raw_parts(buf, len as usize) }
};
let sbuf = unsafe { slice::from_raw_parts(buf, len as usize) };
let mut r = Rollsum::new(); let mut r = Rollsum::new();
for x in sbuf { for x in sbuf {
r.roll(*x); r.roll(*x);
@ -115,8 +115,8 @@ pub extern fn bupsplit_find_ofs(buf: *const u8, len: libc::size_t,
let mut sum = r.digest() >> BUP_BLOBBITS; let mut sum = r.digest() >> BUP_BLOBBITS;
let mut rbits : libc::c_int = BUP_BLOBBITS as i32; let mut rbits : libc::c_int = BUP_BLOBBITS as i32;
while sum & 1 != 0 { while sum & 1 != 0 {
sum = sum >> 1; sum >>= 1;
rbits = rbits + 1; rbits += 1;
} }
unsafe { unsafe {
*bits = rbits; *bits = rbits;