From cd4d812aca1f8a278f4ab694d97c1b81fa60ae06 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Tue, 20 Aug 2024 22:19:37 +0100 Subject: [PATCH] Restore Msb0 as Lsb0 is bugged - BEP 003 uses MSB --- crates/librqbit/src/bitv.rs | 26 +++++++++++++------------- crates/librqbit/src/chunk_tracker.rs | 6 ++---- crates/librqbit/src/type_aliases.rs | 6 ++++-- crates/peer_binary_protocol/src/lib.rs | 4 ++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/librqbit/src/bitv.rs b/crates/librqbit/src/bitv.rs index dedc6e7..f3c0129 100644 --- a/crates/librqbit/src/bitv.rs +++ b/crates/librqbit/src/bitv.rs @@ -3,15 +3,15 @@ use std::fs::File; use anyhow::Context; use bitvec::{ boxed::BitBox, - order::Lsb0, + order::Msb0, slice::BitSlice, vec::BitVec, view::{AsBits, AsMutBits}, }; pub trait BitV: Send + Sync { - fn as_slice(&self) -> &BitSlice; - fn as_slice_mut(&mut self) -> &mut BitSlice; + fn as_slice(&self) -> &BitSlice; + fn as_slice_mut(&mut self) -> &mut BitSlice; fn into_dyn(self) -> Box; fn as_bytes(&self) -> &[u8]; fn flush(&mut self) -> anyhow::Result<()>; @@ -33,12 +33,12 @@ impl MmapBitV { } #[async_trait::async_trait] -impl BitV for BitVec { - fn as_slice(&self) -> &BitSlice { +impl BitV for BitVec { + fn as_slice(&self) -> &BitSlice { self.as_bitslice() } - fn as_slice_mut(&mut self) -> &mut BitSlice { + fn as_slice_mut(&mut self) -> &mut BitSlice { self.as_mut_bitslice() } @@ -56,12 +56,12 @@ impl BitV for BitVec { } #[async_trait::async_trait] -impl BitV for BitBox { - fn as_slice(&self) -> &BitSlice { +impl BitV for BitBox { + fn as_slice(&self) -> &BitSlice { self.as_bitslice() } - fn as_slice_mut(&mut self) -> &mut BitSlice { + fn as_slice_mut(&mut self) -> &mut BitSlice { self.as_mut_bitslice() } @@ -79,11 +79,11 @@ impl BitV for BitBox { } impl BitV for MmapBitV { - fn as_slice(&self) -> &BitSlice { + fn as_slice(&self) -> &BitSlice { self.mmap.as_bits() } - fn as_slice_mut(&mut self) -> &mut BitSlice { + fn as_slice_mut(&mut self) -> &mut BitSlice { self.mmap.as_mut_bits() } @@ -101,11 +101,11 @@ impl BitV for MmapBitV { } impl BitV for Box { - fn as_slice(&self) -> &BitSlice { + fn as_slice(&self) -> &BitSlice { (**self).as_slice() } - fn as_slice_mut(&mut self) -> &mut BitSlice { + fn as_slice_mut(&mut self) -> &mut BitSlice { (**self).as_slice_mut() } diff --git a/crates/librqbit/src/chunk_tracker.rs b/crates/librqbit/src/chunk_tracker.rs index e113943..7c25458 100644 --- a/crates/librqbit/src/chunk_tracker.rs +++ b/crates/librqbit/src/chunk_tracker.rs @@ -425,10 +425,8 @@ impl ChunkTracker { #[cfg(test)] mod tests { - use std::collections::HashSet; - - use bitvec::{order::Lsb0, vec::BitVec}; use librqbit_core::{constants::CHUNK_SIZE, lengths::Lengths}; + use std::collections::HashSet; use crate::{bitv::BitV, chunk_tracker::HaveNeededSelected, type_aliases::BF}; @@ -548,7 +546,7 @@ mod tests { ]; let bf_len = l.piece_bitfield_bytes(); - let initial_have: BitVec = BitVec::from_vec(vec![0u8; bf_len]); + let initial_have = BF::from_boxed_slice(vec![0u8; bf_len].into_boxed_slice()); let initial_selected = BF::from_boxed_slice(vec![u8::MAX; bf_len].into_boxed_slice()); // Initially, we need all files and all pieces. diff --git a/crates/librqbit/src/type_aliases.rs b/crates/librqbit/src/type_aliases.rs index 2f104e3..337ce8c 100644 --- a/crates/librqbit/src/type_aliases.rs +++ b/crates/librqbit/src/type_aliases.rs @@ -4,8 +4,10 @@ use futures::stream::BoxStream; use crate::{file_info::FileInfo, storage::TorrentStorage}; -pub type BS = bitvec::slice::BitSlice; -pub type BF = bitvec::boxed::BitBox; +// NOTE: Msb0 is used because that's what bittorrent protocol uses for bitfield. +// Don't change to Lsb0 even though it might be a bit faster (in theory) on LE architectures. +pub type BS = bitvec::slice::BitSlice; +pub type BF = bitvec::boxed::BitBox; pub type PeerHandle = SocketAddr; pub type PeerStream = BoxStream<'static, SocketAddr>; diff --git a/crates/peer_binary_protocol/src/lib.rs b/crates/peer_binary_protocol/src/lib.rs index a6b2e7a..bd2d1a8 100644 --- a/crates/peer_binary_protocol/src/lib.rs +++ b/crates/peer_binary_protocol/src/lib.rs @@ -199,8 +199,8 @@ pub enum Message { pub type MessageBorrowed<'a> = Message>; pub type MessageOwned = Message; -pub type BitfieldBorrowed<'a> = &'a bitvec::slice::BitSlice; -pub type BitfieldOwned = bitvec::vec::BitVec; +pub type BitfieldBorrowed<'a> = &'a bitvec::slice::BitSlice; +pub type BitfieldOwned = bitvec::vec::BitVec; pub struct Bitfield<'a> { pub data: BitfieldBorrowed<'a>,