From 950e0cd50e5b257df1b58c8fbdc385357496b315 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Sat, 30 Mar 2024 12:00:40 +0000 Subject: [PATCH] Remove custom ceil_div_u64 --- crates/librqbit/src/peer_info_reader/mod.rs | 4 ++-- crates/librqbit_core/src/lengths.rs | 14 +++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/crates/librqbit/src/peer_info_reader/mod.rs b/crates/librqbit/src/peer_info_reader/mod.rs index d141f73..2680488 100644 --- a/crates/librqbit/src/peer_info_reader/mod.rs +++ b/crates/librqbit/src/peer_info_reader/mod.rs @@ -5,7 +5,7 @@ use buffers::{ByteBuf, ByteBufOwned}; use librqbit_core::{ constants::CHUNK_SIZE, hash_id::Id20, - lengths::{ceil_div_u64, last_element_size_u64, ChunkInfo}, + lengths::{last_element_size_u64, ChunkInfo}, torrent_metainfo::TorrentMetaV1Info, }; use parking_lot::{Mutex, RwLock}; @@ -76,7 +76,7 @@ impl HandlerLocked { anyhow::bail!("metadata size {} is too big", metadata_size); } let buffer = vec![0u8; metadata_size as usize]; - let total_pieces = ceil_div_u64(metadata_size as u64, CHUNK_SIZE as u64); + let total_pieces = (metadata_size as u64).div_ceil(CHUNK_SIZE as u64); let received_pieces = vec![false; total_pieces as usize]; Ok(Self { metadata_size, diff --git a/crates/librqbit_core/src/lengths.rs b/crates/librqbit_core/src/lengths.rs index 0cb8909..97c510b 100644 --- a/crates/librqbit_core/src/lengths.rs +++ b/crates/librqbit_core/src/lengths.rs @@ -6,10 +6,6 @@ const fn is_power_of_two(x: u64) -> bool { (x != 0) && ((x & (x - 1)) == 0) } -pub const fn ceil_div_u64(a: u64, b: u64) -> u64 { - (a + b - 1) / b -} - pub const fn last_element_size_u64(total: u64, chunk_size: u64) -> u64 { let rem = total % chunk_size; if rem == 0 { @@ -100,21 +96,21 @@ impl Lengths { if total_length == 0 { anyhow::bail!("torrent with 0 length") } - let total_pieces = ceil_div_u64(total_length, piece_length as u64) as u32; + let total_pieces = total_length.div_ceil(piece_length as u64) as u32; Ok(Self { chunk_length, piece_length, total_length, - max_chunks_per_piece: ceil_div_u64(piece_length as u64, chunk_length as u64) as u32, + max_chunks_per_piece: (piece_length as u64).div_ceil(chunk_length as u64) as u32, last_piece_id: total_pieces - 1, last_piece_length: last_element_size_u64(total_length, piece_length as u64) as u32, }) } pub const fn piece_bitfield_bytes(&self) -> usize { - ceil_div_u64(self.total_pieces() as u64, 8) as usize + self.total_pieces().div_ceil(8) as usize } pub const fn chunk_bitfield_bytes(&self) -> usize { - ceil_div_u64(self.total_chunks() as u64, 8) as usize + self.total_chunks().div_ceil(8) as usize } pub const fn total_length(&self) -> u64 { self.total_length @@ -139,7 +135,7 @@ impl Lengths { self.max_chunks_per_piece } pub const fn total_chunks(&self) -> u32 { - ceil_div_u64(self.total_length, self.chunk_length as u64) as u32 + self.total_length.div_ceil(self.chunk_length as u64) as u32 } pub const fn last_piece_id(&self) -> ValidPieceIndex { ValidPieceIndex(self.last_piece_id)