Restore Msb0 as Lsb0 is bugged - BEP 003 uses MSB

This commit is contained in:
Igor Katson 2024-08-20 22:19:37 +01:00
parent ea39ec787a
commit cd4d812aca
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
4 changed files with 21 additions and 21 deletions

View file

@ -3,15 +3,15 @@ use std::fs::File;
use anyhow::Context; use anyhow::Context;
use bitvec::{ use bitvec::{
boxed::BitBox, boxed::BitBox,
order::Lsb0, order::Msb0,
slice::BitSlice, slice::BitSlice,
vec::BitVec, vec::BitVec,
view::{AsBits, AsMutBits}, view::{AsBits, AsMutBits},
}; };
pub trait BitV: Send + Sync { pub trait BitV: Send + Sync {
fn as_slice(&self) -> &BitSlice<u8, Lsb0>; fn as_slice(&self) -> &BitSlice<u8, Msb0>;
fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Lsb0>; fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Msb0>;
fn into_dyn(self) -> Box<dyn BitV>; fn into_dyn(self) -> Box<dyn BitV>;
fn as_bytes(&self) -> &[u8]; fn as_bytes(&self) -> &[u8];
fn flush(&mut self) -> anyhow::Result<()>; fn flush(&mut self) -> anyhow::Result<()>;
@ -33,12 +33,12 @@ impl MmapBitV {
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl BitV for BitVec<u8, Lsb0> { impl BitV for BitVec<u8, Msb0> {
fn as_slice(&self) -> &BitSlice<u8, Lsb0> { fn as_slice(&self) -> &BitSlice<u8, Msb0> {
self.as_bitslice() self.as_bitslice()
} }
fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Lsb0> { fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Msb0> {
self.as_mut_bitslice() self.as_mut_bitslice()
} }
@ -56,12 +56,12 @@ impl BitV for BitVec<u8, Lsb0> {
} }
#[async_trait::async_trait] #[async_trait::async_trait]
impl BitV for BitBox<u8, Lsb0> { impl BitV for BitBox<u8, Msb0> {
fn as_slice(&self) -> &BitSlice<u8, Lsb0> { fn as_slice(&self) -> &BitSlice<u8, Msb0> {
self.as_bitslice() self.as_bitslice()
} }
fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Lsb0> { fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Msb0> {
self.as_mut_bitslice() self.as_mut_bitslice()
} }
@ -79,11 +79,11 @@ impl BitV for BitBox<u8, Lsb0> {
} }
impl BitV for MmapBitV { impl BitV for MmapBitV {
fn as_slice(&self) -> &BitSlice<u8, Lsb0> { fn as_slice(&self) -> &BitSlice<u8, Msb0> {
self.mmap.as_bits() self.mmap.as_bits()
} }
fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Lsb0> { fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Msb0> {
self.mmap.as_mut_bits() self.mmap.as_mut_bits()
} }
@ -101,11 +101,11 @@ impl BitV for MmapBitV {
} }
impl BitV for Box<dyn BitV> { impl BitV for Box<dyn BitV> {
fn as_slice(&self) -> &BitSlice<u8, Lsb0> { fn as_slice(&self) -> &BitSlice<u8, Msb0> {
(**self).as_slice() (**self).as_slice()
} }
fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Lsb0> { fn as_slice_mut(&mut self) -> &mut BitSlice<u8, Msb0> {
(**self).as_slice_mut() (**self).as_slice_mut()
} }

View file

@ -425,10 +425,8 @@ impl ChunkTracker {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::HashSet;
use bitvec::{order::Lsb0, vec::BitVec};
use librqbit_core::{constants::CHUNK_SIZE, lengths::Lengths}; use librqbit_core::{constants::CHUNK_SIZE, lengths::Lengths};
use std::collections::HashSet;
use crate::{bitv::BitV, chunk_tracker::HaveNeededSelected, type_aliases::BF}; use crate::{bitv::BitV, chunk_tracker::HaveNeededSelected, type_aliases::BF};
@ -548,7 +546,7 @@ mod tests {
]; ];
let bf_len = l.piece_bitfield_bytes(); let bf_len = l.piece_bitfield_bytes();
let initial_have: BitVec<u8, Lsb0> = 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()); let initial_selected = BF::from_boxed_slice(vec![u8::MAX; bf_len].into_boxed_slice());
// Initially, we need all files and all pieces. // Initially, we need all files and all pieces.

View file

@ -4,8 +4,10 @@ use futures::stream::BoxStream;
use crate::{file_info::FileInfo, storage::TorrentStorage}; use crate::{file_info::FileInfo, storage::TorrentStorage};
pub type BS = bitvec::slice::BitSlice<u8, bitvec::order::Lsb0>; // NOTE: Msb0 is used because that's what bittorrent protocol uses for bitfield.
pub type BF = bitvec::boxed::BitBox<u8, bitvec::order::Lsb0>; // Don't change to Lsb0 even though it might be a bit faster (in theory) on LE architectures.
pub type BS = bitvec::slice::BitSlice<u8, bitvec::order::Msb0>;
pub type BF = bitvec::boxed::BitBox<u8, bitvec::order::Msb0>;
pub type PeerHandle = SocketAddr; pub type PeerHandle = SocketAddr;
pub type PeerStream = BoxStream<'static, SocketAddr>; pub type PeerStream = BoxStream<'static, SocketAddr>;

View file

@ -199,8 +199,8 @@ pub enum Message<ByteBuf: std::hash::Hash + Eq> {
pub type MessageBorrowed<'a> = Message<ByteBuf<'a>>; pub type MessageBorrowed<'a> = Message<ByteBuf<'a>>;
pub type MessageOwned = Message<ByteBufOwned>; pub type MessageOwned = Message<ByteBufOwned>;
pub type BitfieldBorrowed<'a> = &'a bitvec::slice::BitSlice<u8, bitvec::order::Lsb0>; pub type BitfieldBorrowed<'a> = &'a bitvec::slice::BitSlice<u8, bitvec::order::Msb0>;
pub type BitfieldOwned = bitvec::vec::BitVec<u8, bitvec::order::Lsb0>; pub type BitfieldOwned = bitvec::vec::BitVec<u8, bitvec::order::Msb0>;
pub struct Bitfield<'a> { pub struct Bitfield<'a> {
pub data: BitfieldBorrowed<'a>, pub data: BitfieldBorrowed<'a>,