From 2a755f740d43fb0910e093387562c9028dd5b2b9 Mon Sep 17 00:00:00 2001 From: Szymon Walter Date: Tue, 20 Mar 2018 13:42:52 +0100 Subject: [PATCH] fix `sector::Address::with_block_size` --- src/sector.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/sector.rs b/src/sector.rs index cb84a61..85c4e63 100644 --- a/src/sector.rs +++ b/src/sector.rs @@ -62,14 +62,17 @@ impl Address { pub fn with_block_size( block: usize, - offset: usize, + offset: isize, log_block_size: u32, ) -> Address { + let block = (block as isize + (offset >> log_block_size)) as usize; + let offset = offset.abs() as usize & ((1 << log_block_size) - 1); + let log_diff = log_block_size as isize - S::LOG_SIZE as isize; let top_offset = offset >> S::LOG_SIZE; - let offset = offset & ((1 << log_block_size) - 1); + let offset = offset & ((1 << S::LOG_SIZE) - 1); let sector = block << log_diff | top_offset; - Address::new(sector, offset as isize) + unsafe { Address::new_unchecked(sector, offset) } } pub fn index64(&self) -> u64 { @@ -204,6 +207,14 @@ mod tests { Address::::with_block_size(1, 256, 10).into_index(), Some(1024 + 256) ); + assert_eq!( + Address::::with_block_size(2, 0, 10).into_index(), + Some(2048) + ); + assert_eq!( + Address::::with_block_size(0, 1792, 10).into_index(), + Some(1792) + ); } #[test]