Box<[u8]> instead of Vec<u8> for ByteBufOwned
This commit is contained in:
parent
d9ec702f59
commit
deee41cd93
23 changed files with 106 additions and 124 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use anyhow::Context;
|
||||
use buffers::ByteString;
|
||||
use buffers::ByteBufOwned;
|
||||
use dht::{DhtStats, Id20};
|
||||
use futures::Stream;
|
||||
use http::StatusCode;
|
||||
|
|
@ -268,7 +268,7 @@ pub struct ApiAddTorrentResponse {
|
|||
|
||||
fn make_torrent_details(
|
||||
info_hash: &Id20,
|
||||
info: &TorrentMetaV1Info<ByteString>,
|
||||
info: &TorrentMetaV1Info<ByteBufOwned>,
|
||||
only_files: Option<&[usize]>,
|
||||
) -> Result<TorrentDetailsResponse> {
|
||||
let files = info
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ pub struct ChunkTracker {
|
|||
fn compute_chunk_status(lengths: &Lengths, needed_pieces: &BF) -> BF {
|
||||
let required_size = lengths.chunk_bitfield_bytes();
|
||||
let vec = vec![0u8; required_size];
|
||||
let mut chunk_bf = BF::from_vec(vec);
|
||||
let mut chunk_bf = BF::from_boxed_slice(vec.into_boxed_slice());
|
||||
for piece_index in needed_pieces
|
||||
.get(0..lengths.total_pieces() as usize)
|
||||
.unwrap()
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use std::path::Path;
|
|||
|
||||
use anyhow::Context;
|
||||
use bencode::bencode_serialize_to_writer;
|
||||
use buffers::ByteString;
|
||||
use buffers::ByteBufOwned;
|
||||
use librqbit_core::torrent_metainfo::{TorrentMetaV1File, TorrentMetaV1Info, TorrentMetaV1Owned};
|
||||
use librqbit_core::Id20;
|
||||
use sha1w::{ISha1, Sha1};
|
||||
|
|
@ -44,7 +44,7 @@ fn walk_dir_find_paths(dir: &Path, out: &mut Vec<Cow<'_, Path>>) -> anyhow::Resu
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_info_hash(t: &TorrentMetaV1Info<ByteString>) -> anyhow::Result<Id20> {
|
||||
fn compute_info_hash(t: &TorrentMetaV1Info<ByteBufOwned>) -> anyhow::Result<Id20> {
|
||||
struct W {
|
||||
hash: sha1w::Sha1,
|
||||
}
|
||||
|
|
@ -79,7 +79,7 @@ fn osstr_to_bytes(o: &OsStr) -> Vec<u8> {
|
|||
async fn create_torrent_raw<'a>(
|
||||
path: &'a Path,
|
||||
options: CreateTorrentOptions<'a>,
|
||||
) -> anyhow::Result<TorrentMetaV1Info<ByteString>> {
|
||||
) -> anyhow::Result<TorrentMetaV1Info<ByteBufOwned>> {
|
||||
path.try_exists()
|
||||
.with_context(|| format!("path {:?} doesn't exist", path))?;
|
||||
let basename = path
|
||||
|
|
@ -87,7 +87,7 @@ async fn create_torrent_raw<'a>(
|
|||
.ok_or_else(|| anyhow::anyhow!("cannot determine basename of {:?}", path))?;
|
||||
let is_dir = path.is_dir();
|
||||
let single_file_mode = !is_dir;
|
||||
let name: ByteString = match options.name {
|
||||
let name: ByteBufOwned = match options.name {
|
||||
Some(name) => name.as_bytes().into(),
|
||||
None => osstr_to_bytes(basename).into(),
|
||||
};
|
||||
|
|
@ -112,7 +112,7 @@ async fn create_torrent_raw<'a>(
|
|||
let mut remaining_piece_length = piece_length;
|
||||
let mut piece_checksum = sha1w::Sha1::new();
|
||||
let mut piece_hashes = Vec::<u8>::new();
|
||||
let mut output_files: Vec<TorrentMetaV1File<ByteString>> = Vec::new();
|
||||
let mut output_files: Vec<TorrentMetaV1File<ByteBufOwned>> = Vec::new();
|
||||
|
||||
let spawner = BlockingSpawner::default();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::{collections::HashSet, net::SocketAddr};
|
||||
|
||||
use anyhow::Context;
|
||||
use buffers::ByteString;
|
||||
use buffers::ByteBufOwned;
|
||||
use futures::{stream::FuturesUnordered, Stream, StreamExt};
|
||||
use librqbit_core::torrent_metainfo::TorrentMetaV1Info;
|
||||
use tracing::debug;
|
||||
|
|
@ -14,7 +14,7 @@ use librqbit_core::hash_id::Id20;
|
|||
#[derive(Debug)]
|
||||
pub enum ReadMetainfoResult<Rx> {
|
||||
Found {
|
||||
info: TorrentMetaV1Info<ByteString>,
|
||||
info: TorrentMetaV1Info<ByteBufOwned>,
|
||||
rx: Rx,
|
||||
seen: HashSet<SocketAddr>,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use std::{
|
|||
};
|
||||
|
||||
use anyhow::Context;
|
||||
use buffers::ByteString;
|
||||
use buffers::ByteBufOwned;
|
||||
use librqbit_core::{
|
||||
lengths::{ChunkInfo, Lengths, ValidPieceIndex},
|
||||
torrent_metainfo::{FileIteratorName, TorrentMetaV1Info},
|
||||
|
|
@ -55,7 +55,7 @@ pub fn update_hash_from_file<Sha1: ISha1>(
|
|||
}
|
||||
|
||||
pub(crate) struct FileOps<'a, Sha1> {
|
||||
torrent: &'a TorrentMetaV1Info<ByteString>,
|
||||
torrent: &'a TorrentMetaV1Info<ByteBufOwned>,
|
||||
files: &'a [Arc<Mutex<File>>],
|
||||
lengths: &'a Lengths,
|
||||
phantom_data: PhantomData<Sha1>,
|
||||
|
|
@ -63,7 +63,7 @@ pub(crate) struct FileOps<'a, Sha1> {
|
|||
|
||||
impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
|
||||
pub fn new(
|
||||
torrent: &'a TorrentMetaV1Info<ByteString>,
|
||||
torrent: &'a TorrentMetaV1Info<ByteBufOwned>,
|
||||
files: &'a [Arc<Mutex<File>>],
|
||||
lengths: &'a Lengths,
|
||||
) -> Self {
|
||||
|
|
@ -80,8 +80,10 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
|
|||
only_files: Option<&[usize]>,
|
||||
progress: &AtomicU64,
|
||||
) -> anyhow::Result<InitialCheckResults> {
|
||||
let mut needed_pieces = BF::from_vec(vec![0u8; self.lengths.piece_bitfield_bytes()]);
|
||||
let mut have_pieces = BF::from_vec(vec![0u8; self.lengths.piece_bitfield_bytes()]);
|
||||
let mut needed_pieces =
|
||||
BF::from_boxed_slice(vec![0u8; self.lengths.piece_bitfield_bytes()].into());
|
||||
let mut have_pieces =
|
||||
BF::from_boxed_slice(vec![0u8; self.lengths.piece_bitfield_bytes()].into());
|
||||
|
||||
let mut have_bytes = 0u64;
|
||||
let mut needed_bytes = 0u64;
|
||||
|
|
@ -92,7 +94,7 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
|
|||
index: usize,
|
||||
fd: &'a Arc<Mutex<File>>,
|
||||
len: u64,
|
||||
name: FileIteratorName<'a, ByteString>,
|
||||
name: FileIteratorName<'a, ByteBufOwned>,
|
||||
full_file_required: bool,
|
||||
processed_bytes: u64,
|
||||
is_broken: bool,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use std::{
|
|||
};
|
||||
|
||||
use anyhow::{bail, Context};
|
||||
use buffers::{ByteBuf, ByteString};
|
||||
use buffers::{ByteBuf, ByteBufOwned};
|
||||
use clone_to_owned::CloneToOwned;
|
||||
use librqbit_core::{hash_id::Id20, lengths::ChunkInfo, peer_id::try_decode_peer_id};
|
||||
use parking_lot::RwLock;
|
||||
|
|
@ -100,7 +100,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
|
|||
&self,
|
||||
outgoing_chan: tokio::sync::mpsc::UnboundedReceiver<WriterRequest>,
|
||||
read_buf: ReadBuf,
|
||||
handshake: Handshake<ByteString>,
|
||||
handshake: Handshake<ByteBufOwned>,
|
||||
mut conn: tokio::net::TcpStream,
|
||||
) -> anyhow::Result<()> {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
|
@ -220,7 +220,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
|
|||
.read_write_timeout
|
||||
.unwrap_or_else(|| Duration::from_secs(10));
|
||||
|
||||
let extended_handshake: RwLock<Option<ExtendedHandshake<ByteString>>> = RwLock::new(None);
|
||||
let extended_handshake: RwLock<Option<ExtendedHandshake<ByteBufOwned>>> = RwLock::new(None);
|
||||
let extended_handshake_ref = &extended_handshake;
|
||||
let supports_extended = handshake_supports_extended;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
use bencode::from_bytes;
|
||||
use buffers::{ByteBuf, ByteString};
|
||||
use buffers::{ByteBuf, ByteBufOwned};
|
||||
use librqbit_core::{
|
||||
constants::CHUNK_SIZE,
|
||||
hash_id::Id20,
|
||||
|
|
@ -30,9 +30,9 @@ pub(crate) async fn read_metainfo_from_peer(
|
|||
info_hash: Id20,
|
||||
peer_connection_options: Option<PeerConnectionOptions>,
|
||||
spawner: BlockingSpawner,
|
||||
) -> anyhow::Result<TorrentMetaV1Info<ByteString>> {
|
||||
) -> anyhow::Result<TorrentMetaV1Info<ByteBufOwned>> {
|
||||
let (result_tx, result_rx) =
|
||||
tokio::sync::oneshot::channel::<anyhow::Result<TorrentMetaV1Info<ByteString>>>();
|
||||
tokio::sync::oneshot::channel::<anyhow::Result<TorrentMetaV1Info<ByteBufOwned>>>();
|
||||
let (writer_tx, writer_rx) = tokio::sync::mpsc::unbounded_channel::<WriterRequest>();
|
||||
let handler = Handler {
|
||||
addr,
|
||||
|
|
@ -131,8 +131,9 @@ struct Handler {
|
|||
addr: SocketAddr,
|
||||
info_hash: Id20,
|
||||
writer_tx: UnboundedSender<WriterRequest>,
|
||||
result_tx:
|
||||
Mutex<Option<tokio::sync::oneshot::Sender<anyhow::Result<TorrentMetaV1Info<ByteString>>>>>,
|
||||
result_tx: Mutex<
|
||||
Option<tokio::sync::oneshot::Sender<anyhow::Result<TorrentMetaV1Info<ByteBufOwned>>>>,
|
||||
>,
|
||||
locked: RwLock<Option<HandlerLocked>>,
|
||||
}
|
||||
|
||||
|
|
@ -169,7 +170,7 @@ impl PeerConnectionHandler for Handler {
|
|||
.record_piece(piece, &data, self.info_hash)?;
|
||||
if piece_ready {
|
||||
let buf = self.locked.write().take().unwrap().buffer;
|
||||
let info = from_bytes::<TorrentMetaV1Info<ByteString>>(&buf);
|
||||
let info = from_bytes::<TorrentMetaV1Info<ByteBufOwned>>(&buf);
|
||||
self.result_tx
|
||||
.lock()
|
||||
.take()
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ use crate::{
|
|||
};
|
||||
use anyhow::{bail, Context};
|
||||
use bencode::{bencode_serialize_to_writer, BencodeDeserializer};
|
||||
use buffers::{ByteBuf, ByteBufT, ByteString};
|
||||
use buffers::{ByteBuf, ByteBufOwned, ByteBufT};
|
||||
use clone_to_owned::CloneToOwned;
|
||||
use dht::{Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig};
|
||||
use futures::{
|
||||
|
|
@ -126,14 +126,17 @@ struct SerializedTorrent {
|
|||
serialize_with = "serialize_torrent",
|
||||
deserialize_with = "deserialize_torrent"
|
||||
)]
|
||||
info: TorrentMetaV1Info<ByteString>,
|
||||
info: TorrentMetaV1Info<ByteBufOwned>,
|
||||
trackers: HashSet<String>,
|
||||
output_folder: PathBuf,
|
||||
only_files: Option<Vec<usize>>,
|
||||
is_paused: bool,
|
||||
}
|
||||
|
||||
fn serialize_torrent<S>(t: &TorrentMetaV1Info<ByteString>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
fn serialize_torrent<S>(
|
||||
t: &TorrentMetaV1Info<ByteBufOwned>,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
|
|
@ -145,7 +148,7 @@ where
|
|||
s.serialize(serializer)
|
||||
}
|
||||
|
||||
fn deserialize_torrent<'de, D>(deserializer: D) -> Result<TorrentMetaV1Info<ByteString>, D::Error>
|
||||
fn deserialize_torrent<'de, D>(deserializer: D) -> Result<TorrentMetaV1Info<ByteBufOwned>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
|
|
@ -155,7 +158,7 @@ where
|
|||
let b = general_purpose::STANDARD_NO_PAD
|
||||
.decode(s)
|
||||
.map_err(D::Error::custom)?;
|
||||
TorrentMetaV1Info::<ByteString>::deserialize(&mut BencodeDeserializer::new_from_buf(&b))
|
||||
TorrentMetaV1Info::<ByteBufOwned>::deserialize(&mut BencodeDeserializer::new_from_buf(&b))
|
||||
.map_err(D::Error::custom)
|
||||
}
|
||||
|
||||
|
|
@ -216,7 +219,7 @@ fn compute_only_files_regex<ByteBuf: AsRef<[u8]>>(
|
|||
}
|
||||
|
||||
fn compute_only_files(
|
||||
info: &TorrentMetaV1Info<ByteString>,
|
||||
info: &TorrentMetaV1Info<ByteBufOwned>,
|
||||
only_files: Option<Vec<usize>>,
|
||||
only_files_regex: Option<String>,
|
||||
list_only: bool,
|
||||
|
|
@ -303,7 +306,7 @@ pub struct AddTorrentOptions {
|
|||
|
||||
pub struct ListOnlyResponse {
|
||||
pub info_hash: Id20,
|
||||
pub info: TorrentMetaV1Info<ByteString>,
|
||||
pub info: TorrentMetaV1Info<ByteBufOwned>,
|
||||
pub only_files: Option<Vec<usize>>,
|
||||
pub output_folder: PathBuf,
|
||||
pub seen_peers: Vec<SocketAddr>,
|
||||
|
|
@ -426,7 +429,7 @@ pub(crate) struct CheckedIncomingConnection {
|
|||
pub addr: SocketAddr,
|
||||
pub stream: tokio::net::TcpStream,
|
||||
pub read_buf: ReadBuf,
|
||||
pub handshake: Handshake<ByteString>,
|
||||
pub handshake: Handshake<ByteBufOwned>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
|
|
@ -715,10 +718,10 @@ impl Session {
|
|||
serde_json::from_reader(&mut rdr).context("error deserializing session database")?;
|
||||
let mut futures = Vec::new();
|
||||
for (id, storrent) in db.torrents.into_iter() {
|
||||
let trackers: Vec<ByteString> = storrent
|
||||
let trackers: Vec<ByteBufOwned> = storrent
|
||||
.trackers
|
||||
.into_iter()
|
||||
.map(|t| ByteString(t.into_bytes()))
|
||||
.map(|t| ByteBufOwned::from(t.into_bytes()))
|
||||
.collect();
|
||||
let info = TorrentMetaV1Owned {
|
||||
announce: trackers.first().cloned(),
|
||||
|
|
@ -929,7 +932,7 @@ impl Session {
|
|||
|
||||
fn get_default_subfolder_for_torrent(
|
||||
&self,
|
||||
info: &TorrentMetaV1Info<ByteString>,
|
||||
info: &TorrentMetaV1Info<ByteBufOwned>,
|
||||
) -> anyhow::Result<Option<PathBuf>> {
|
||||
let files = info
|
||||
.iter_filenames_and_lengths()?
|
||||
|
|
@ -957,7 +960,7 @@ impl Session {
|
|||
async fn main_torrent_info(
|
||||
&self,
|
||||
info_hash: Id20,
|
||||
info: TorrentMetaV1Info<ByteString>,
|
||||
info: TorrentMetaV1Info<ByteBufOwned>,
|
||||
trackers: Vec<String>,
|
||||
peer_rx: Option<PeerStream>,
|
||||
initial_peers: Vec<SocketAddr>,
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ use std::{
|
|||
|
||||
use anyhow::{bail, Context};
|
||||
use backoff::backoff::Backoff;
|
||||
use buffers::{ByteBuf, ByteString};
|
||||
use buffers::{ByteBuf, ByteBufOwned};
|
||||
use clone_to_owned::CloneToOwned;
|
||||
use futures::{stream::FuturesUnordered, StreamExt};
|
||||
use itertools::Itertools;
|
||||
|
|
@ -130,7 +130,7 @@ fn dummy_file() -> anyhow::Result<std::fs::File> {
|
|||
}
|
||||
|
||||
fn make_piece_bitfield(lengths: &Lengths) -> BF {
|
||||
BF::from_vec(vec![0; lengths.piece_bitfield_bytes()])
|
||||
BF::from_boxed_slice(vec![0; lengths.piece_bitfield_bytes()].into_boxed_slice())
|
||||
}
|
||||
|
||||
pub(crate) struct TorrentStateLocked {
|
||||
|
|
@ -485,7 +485,7 @@ impl TorrentStateLive {
|
|||
&self.meta
|
||||
}
|
||||
|
||||
pub fn info(&self) -> &TorrentMetaV1Info<ByteString> {
|
||||
pub fn info(&self) -> &TorrentMetaV1Info<ByteBufOwned> {
|
||||
&self.meta.info
|
||||
}
|
||||
pub fn info_hash(&self) -> Id20 {
|
||||
|
|
@ -1047,7 +1047,7 @@ impl PeerHandler {
|
|||
self.on_bitfield_notify.notify_waiters();
|
||||
}
|
||||
|
||||
fn on_bitfield(&self, bitfield: ByteString) -> anyhow::Result<()> {
|
||||
fn on_bitfield(&self, bitfield: ByteBufOwned) -> anyhow::Result<()> {
|
||||
if bitfield.len() != self.state.lengths.piece_bitfield_bytes() {
|
||||
anyhow::bail!(
|
||||
"dropping peer as its bitfield has unexpected size. Got {}, expected {}",
|
||||
|
|
|
|||
|
|
@ -198,7 +198,7 @@ impl LivePeerState {
|
|||
LivePeerState {
|
||||
peer_id,
|
||||
peer_interested: false,
|
||||
bitfield: BF::new(),
|
||||
bitfield: BF::default(),
|
||||
inflight_requests: Default::default(),
|
||||
tx,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,9 +81,9 @@ impl PeerStates {
|
|||
prev
|
||||
})
|
||||
}
|
||||
pub fn update_bitfield_from_vec(&self, handle: PeerHandle, bitfield: Vec<u8>) -> Option<()> {
|
||||
pub fn update_bitfield_from_vec(&self, handle: PeerHandle, bitfield: Box<[u8]>) -> Option<()> {
|
||||
self.with_live_mut(handle, "update_bitfield_from_vec", |live| {
|
||||
live.bitfield = BF::from_vec(bitfield);
|
||||
live.bitfield = BF::from_boxed_slice(bitfield);
|
||||
})
|
||||
}
|
||||
pub fn mark_peer_connecting(&self, h: PeerHandle) -> anyhow::Result<(PeerRx, PeerTx)> {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use std::time::Duration;
|
|||
|
||||
use anyhow::bail;
|
||||
use anyhow::Context;
|
||||
use buffers::ByteString;
|
||||
use buffers::ByteBufOwned;
|
||||
use futures::future::BoxFuture;
|
||||
use futures::FutureExt;
|
||||
use librqbit_core::hash_id::Id20;
|
||||
|
|
@ -78,7 +78,7 @@ pub(crate) struct ManagedTorrentOptions {
|
|||
}
|
||||
|
||||
pub struct ManagedTorrentInfo {
|
||||
pub info: TorrentMetaV1Info<ByteString>,
|
||||
pub info: TorrentMetaV1Info<ByteBufOwned>,
|
||||
pub info_hash: Id20,
|
||||
pub out_dir: PathBuf,
|
||||
pub(crate) spawner: BlockingSpawner,
|
||||
|
|
@ -410,7 +410,7 @@ impl ManagedTorrent {
|
|||
}
|
||||
|
||||
pub struct ManagedTorrentBuilder {
|
||||
info: TorrentMetaV1Info<ByteString>,
|
||||
info: TorrentMetaV1Info<ByteBufOwned>,
|
||||
info_hash: Id20,
|
||||
output_folder: PathBuf,
|
||||
force_tracker_interval: Option<Duration>,
|
||||
|
|
@ -425,7 +425,7 @@ pub struct ManagedTorrentBuilder {
|
|||
|
||||
impl ManagedTorrentBuilder {
|
||||
pub fn new<P: AsRef<Path>>(
|
||||
info: TorrentMetaV1Info<ByteString>,
|
||||
info: TorrentMetaV1Info<ByteBufOwned>,
|
||||
info_hash: Id20,
|
||||
output_folder: P,
|
||||
) -> Self {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::net::SocketAddr;
|
|||
|
||||
use futures::stream::BoxStream;
|
||||
|
||||
pub type BF = bitvec::vec::BitVec<u8, bitvec::order::Msb0>;
|
||||
pub type BF = bitvec::boxed::BitBox<u8, bitvec::order::Msb0>;
|
||||
|
||||
pub type PeerHandle = SocketAddr;
|
||||
pub type PeerStream = BoxStream<'static, SocketAddr>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue