diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 3392bbf..bb69635 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -205,13 +205,13 @@ impl HttpApi { let (info, content) = match added { crate::AddTorrentResponse::AlreadyManaged(_, handle) => ( handle.info().info.clone(), - handle.info().torrent_bytes.clone().0, + handle.info().torrent_bytes.clone(), ), crate::AddTorrentResponse::ListOnly(ListOnlyResponse { info, torrent_bytes, .. - }) => (info, torrent_bytes.0), + }) => (info, torrent_bytes), crate::AddTorrentResponse::Added(_, _) => { return Err(ApiError::new_from_text( StatusCode::INTERNAL_SERVER_ERROR, @@ -226,7 +226,7 @@ impl HttpApi { ); if let Some(name) = info.name.as_ref() { - if let Ok(name) = std::str::from_utf8(&name) { + if let Ok(name) = std::str::from_utf8(name) { if let Ok(h) = HeaderValue::from_str(&format!("attachment; filename=\"{}.torrent\"", name)) { diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index 2ae66f5..bc9d940 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -29,6 +29,7 @@ use crate::{ use anyhow::{bail, Context}; use bencode::{bencode_serialize_to_writer, BencodeDeserializer}; use buffers::{ByteBuf, ByteBufOwned, ByteBufT}; +use bytes::Bytes; use clone_to_owned::CloneToOwned; use dht::{Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig}; use futures::{ @@ -144,9 +145,9 @@ struct SerializedTorrent { #[serde( serialize_with = "serialize_torrent_bytes", deserialize_with = "deserialize_torrent_bytes", - default = "empty_bytes" + default )] - torrent_bytes: ByteBufOwned, + torrent_bytes: Bytes, trackers: HashSet, output_folder: PathBuf, only_files: Option>, @@ -182,16 +183,16 @@ where .map_err(D::Error::custom) } -fn serialize_torrent_bytes(t: &ByteBufOwned, serializer: S) -> Result +fn serialize_torrent_bytes(t: &Bytes, serializer: S) -> Result where S: Serializer, { use base64::{engine::general_purpose, Engine as _}; - let s = general_purpose::STANDARD_NO_PAD.encode(&t.0); + let s = general_purpose::STANDARD_NO_PAD.encode(t); s.serialize(serializer) } -fn deserialize_torrent_bytes<'de, D>(deserializer: D) -> Result +fn deserialize_torrent_bytes<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { @@ -204,10 +205,6 @@ where Ok(b.into()) } -fn empty_bytes() -> ByteBufOwned { - ByteBufOwned(Vec::new().into_boxed_slice()) -} - #[derive(Serialize, Deserialize)] struct SerializedSessionDatabase { torrents: HashMap, @@ -380,7 +377,7 @@ pub struct ListOnlyResponse { pub only_files: Option>, pub output_folder: PathBuf, pub seen_peers: Vec, - pub torrent_bytes: ByteBufOwned, + pub torrent_bytes: Bytes, } #[allow(clippy::large_enum_variant)] @@ -515,7 +512,7 @@ pub(crate) struct CheckedIncomingConnection { struct InternalAddResult { info_hash: Id20, info: TorrentMetaV1Info, - torrent_bytes: ByteBufOwned, + torrent_bytes: Bytes, trackers: Vec, peer_rx: Option, initial_peers: Vec, @@ -1000,7 +997,7 @@ impl Session { InternalAddResult { info_hash, info, - torrent_bytes: bytes, + torrent_bytes: Bytes::from(bytes.0), trackers: magnet.trackers.into_iter().unique().collect(), peer_rx: Some(rx), initial_peers: seen.into_iter().collect(), @@ -1064,7 +1061,7 @@ impl Session { InternalAddResult { info_hash: torrent.info_hash, info: torrent.info, - torrent_bytes: bytes, + torrent_bytes: Bytes::from(bytes.0), trackers, peer_rx, initial_peers: opts diff --git a/crates/librqbit/src/torrent_state/mod.rs b/crates/librqbit/src/torrent_state/mod.rs index f653316..d4d935b 100644 --- a/crates/librqbit/src/torrent_state/mod.rs +++ b/crates/librqbit/src/torrent_state/mod.rs @@ -14,6 +14,7 @@ use std::time::Duration; use anyhow::bail; use anyhow::Context; use buffers::ByteBufOwned; +use bytes::Bytes; use futures::future::BoxFuture; use futures::FutureExt; use librqbit_core::hash_id::Id20; @@ -99,7 +100,7 @@ pub(crate) struct ManagedTorrentOptions { pub struct ManagedTorrentInfo { pub info: TorrentMetaV1Info, - pub torrent_bytes: ByteBufOwned, + pub torrent_bytes: Bytes, pub info_hash: Id20, pub(crate) spawner: BlockingSpawner, pub trackers: HashSet, @@ -502,7 +503,7 @@ pub(crate) struct ManagedTorrentBuilder { info: TorrentMetaV1Info, output_folder: PathBuf, info_hash: Id20, - torrent_bytes: ByteBufOwned, + torrent_bytes: Bytes, force_tracker_interval: Option, peer_connect_timeout: Option, peer_read_write_timeout: Option, @@ -520,7 +521,7 @@ impl ManagedTorrentBuilder { pub fn new( info: TorrentMetaV1Info, info_hash: Id20, - torrent_bytes: ByteBufOwned, + torrent_bytes: Bytes, output_folder: PathBuf, storage_factory: BoxStorageFactory, ) -> Self {