Skip to content

Commit

Permalink
Merge pull request #103 from ikatson/small-refactor-attempt-2
Browse files Browse the repository at this point in the history
A few small tweaks and cleanups
  • Loading branch information
ikatson authored Mar 29, 2024
2 parents 3cdf6d4 + c481f77 commit 051e450
Show file tree
Hide file tree
Showing 24 changed files with 138 additions and 133 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ release-all: release-windows release-linux release-macos-universal
cp ./target/x86_64-pc-windows-gnu/release-github/rqbit.exe /tmp/rqbit-release
cp ./target/x86_64-apple-darwin/release-github/rqbit-osx-universal /tmp/rqbit-release
cp ./target/x86_64-unknown-linux-gnu/release-github/rqbit /tmp/rqbit-release/rqbit-linux-x86_64
echo "The release was built in /tmp/rqbit-release"
echo "The release was built in /tmp/rqbit-release"
4 changes: 2 additions & 2 deletions crates/bencode/src/bencode_value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, marker::PhantomData};

use buffers::{ByteBuf, ByteString};
use buffers::{ByteBuf, ByteBufOwned};
use clone_to_owned::CloneToOwned;
use serde::Deserializer;

Expand Down Expand Up @@ -133,7 +133,7 @@ where
}

pub type BencodeValueBorrowed<'a> = BencodeValue<ByteBuf<'a>>;
pub type BencodeValueOwned = BencodeValue<ByteString>;
pub type BencodeValueOwned = BencodeValue<ByteBufOwned>;

#[cfg(test)]
mod tests {
Expand Down
2 changes: 1 addition & 1 deletion crates/bencode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pub use bencode_value::*;
pub use serde_bencode_de::*;
pub use serde_bencode_ser::*;

pub use buffers::{ByteBuf, ByteString};
pub use buffers::{ByteBuf, ByteBufOwned};
14 changes: 7 additions & 7 deletions crates/bencode/src/serde_bencode_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;

use serde::{ser::Impossible, Serialize, Serializer};

use buffers::ByteString;
use buffers::ByteBufOwned;

#[derive(Debug)]
pub enum SerErrorKind {
Expand Down Expand Up @@ -136,8 +136,8 @@ impl<'ser, W: std::io::Write> serde::ser::SerializeTuple for SerializeTuple<'ser

struct SerializeMap<'ser, W: std::io::Write> {
ser: &'ser mut BencodeSerializer<W>,
tmp: BTreeMap<ByteString, ByteString>,
last_key: Option<ByteString>,
tmp: BTreeMap<ByteBufOwned, ByteBufOwned>,
last_key: Option<ByteBufOwned>,
}
impl<'ser, W: std::io::Write> serde::ser::SerializeMap for SerializeMap<'ser, W> {
type Ok = ();
Expand All @@ -152,7 +152,7 @@ impl<'ser, W: std::io::Write> serde::ser::SerializeMap for SerializeMap<'ser, W>
let mut ser = BencodeSerializer::new(&mut buf);
ser.hack_no_bytestring_prefix = true;
key.serialize(&mut ser)?;
self.last_key.replace(ByteString::from(buf));
self.last_key.replace(ByteBufOwned::from(buf));
Ok(())
// key.serialize(&mut *self.ser);
}
Expand All @@ -165,7 +165,7 @@ impl<'ser, W: std::io::Write> serde::ser::SerializeMap for SerializeMap<'ser, W>
let mut ser = BencodeSerializer::new(&mut buf);
value.serialize(&mut ser)?;
self.tmp
.insert(self.last_key.take().unwrap(), ByteString::from(buf));
.insert(self.last_key.take().unwrap(), ByteBufOwned::from(buf));
Ok(())
}

Expand All @@ -180,7 +180,7 @@ impl<'ser, W: std::io::Write> serde::ser::SerializeMap for SerializeMap<'ser, W>

struct SerializeStruct<'ser, W: std::io::Write> {
ser: &'ser mut BencodeSerializer<W>,
tmp: BTreeMap<&'static str, ByteString>,
tmp: BTreeMap<&'static str, ByteBufOwned>,
}
impl<'ser, W: std::io::Write> serde::ser::SerializeStruct for SerializeStruct<'ser, W> {
type Ok = ();
Expand All @@ -198,7 +198,7 @@ impl<'ser, W: std::io::Write> serde::ser::SerializeStruct for SerializeStruct<'s
let mut buf = Vec::new();
let mut ser = BencodeSerializer::new(&mut buf);
value.serialize(&mut ser)?;
self.tmp.insert(key, ByteString::from(buf));
self.tmp.insert(key, ByteBufOwned::from(buf));
Ok(())
}

Expand Down
38 changes: 19 additions & 19 deletions crates/buffers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Deserializer};
use clone_to_owned::CloneToOwned;

#[derive(Default, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)]
pub struct ByteString(pub Vec<u8>);
pub struct ByteBufOwned(pub Box<[u8]>);

#[derive(Default, Deserialize, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)]
#[serde(transparent)]
Expand All @@ -18,7 +18,7 @@ pub trait ByteBufT {
fn as_slice(&self) -> &[u8];
}

impl ByteBufT for ByteString {
impl ByteBufT for ByteBufOwned {
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
Expand Down Expand Up @@ -78,31 +78,31 @@ impl<'a> std::fmt::Display for ByteBuf<'a> {
}
}

impl std::fmt::Debug for ByteString {
impl std::fmt::Debug for ByteBufOwned {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
debug_bytes(&self.0, f, true)
}
}

impl std::fmt::Display for ByteString {
impl std::fmt::Display for ByteBufOwned {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
debug_bytes(&self.0, f, false)
}
}

impl<'a> CloneToOwned for ByteBuf<'a> {
type Target = ByteString;
type Target = ByteBufOwned;

fn clone_to_owned(&self) -> Self::Target {
ByteString(self.as_slice().to_owned())
ByteBufOwned(self.as_slice().to_owned().into_boxed_slice())
}
}

impl CloneToOwned for ByteString {
type Target = ByteString;
impl CloneToOwned for ByteBufOwned {
type Target = ByteBufOwned;

fn clone_to_owned(&self) -> Self::Target {
ByteString(self.as_slice().to_owned())
ByteBufOwned(self.0.clone())
}
}

Expand All @@ -112,7 +112,7 @@ impl<'a> std::convert::AsRef<[u8]> for ByteBuf<'a> {
}
}

impl std::convert::AsRef<[u8]> for ByteString {
impl std::convert::AsRef<[u8]> for ByteBufOwned {
fn as_ref(&self) -> &[u8] {
&self.0
}
Expand All @@ -126,7 +126,7 @@ impl<'a> std::ops::Deref for ByteBuf<'a> {
}
}

impl std::ops::Deref for ByteString {
impl std::ops::Deref for ByteBufOwned {
type Target = [u8];

fn deref(&self) -> &Self::Target {
Expand All @@ -140,15 +140,15 @@ impl<'a> From<&'a [u8]> for ByteBuf<'a> {
}
}

impl<'a> From<&'a [u8]> for ByteString {
impl<'a> From<&'a [u8]> for ByteBufOwned {
fn from(b: &'a [u8]) -> Self {
Self(b.into())
}
}

impl From<Vec<u8>> for ByteString {
impl From<Vec<u8>> for ByteBufOwned {
fn from(b: Vec<u8>) -> Self {
Self(b)
Self(b.into_boxed_slice())
}
}

Expand All @@ -161,7 +161,7 @@ impl<'a> serde::ser::Serialize for ByteBuf<'a> {
}
}

impl serde::ser::Serialize for ByteString {
impl serde::ser::Serialize for ByteBufOwned {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
Expand All @@ -170,15 +170,15 @@ impl serde::ser::Serialize for ByteString {
}
}

impl<'de> serde::de::Deserialize<'de> for ByteString {
impl<'de> serde::de::Deserialize<'de> for ByteBufOwned {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct Visitor;

impl<'de> serde::de::Visitor<'de> for Visitor {
type Value = Vec<u8>;
type Value = ByteBufOwned;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("byte string")
Expand All @@ -187,9 +187,9 @@ impl<'de> serde::de::Deserialize<'de> for ByteString {
where
E: serde::de::Error,
{
Ok(v.to_owned())
Ok(v.to_owned().into())
}
}
Ok(ByteString(deserializer.deserialize_byte_buf(Visitor {})?))
Ok(deserializer.deserialize_byte_buf(Visitor {})?)
}
}
4 changes: 2 additions & 2 deletions crates/dht/src/bprotocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
net::{Ipv4Addr, SocketAddrV4},
};

use bencode::{ByteBuf, ByteString};
use bencode::{ByteBuf, ByteBufOwned};
use clone_to_owned::CloneToOwned;
use librqbit_core::hash_id::Id20;
use serde::{
Expand Down Expand Up @@ -356,7 +356,7 @@ pub struct Message<BufT> {
pub ip: Option<SocketAddrV4>,
}

impl Message<ByteString> {
impl Message<ByteBufOwned> {
// This implies that the transaction id was generated by us.
pub fn get_our_transaction_id(&self) -> Option<u16> {
if self.transaction_id.len() != 2 {
Expand Down
30 changes: 15 additions & 15 deletions crates/dht/src/dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
};
use anyhow::{bail, Context};
use backoff::{backoff::Backoff, ExponentialBackoffBuilder};
use bencode::ByteString;
use bencode::ByteBufOwned;
use dashmap::DashMap;
use futures::{
future::BoxFuture, stream::FuturesUnordered, FutureExt, Stream, StreamExt, TryFutureExt,
Expand Down Expand Up @@ -59,7 +59,7 @@ struct OutstandingRequest {
pub struct WorkerSendRequest {
// If this is set, we are tracking the response in inflight_by_transaction_id
our_tid: Option<u16>,
message: Message<ByteString>,
message: Message<ByteBufOwned>,
addr: SocketAddr,
}

Expand Down Expand Up @@ -607,13 +607,13 @@ impl DhtState {
}
}

fn create_request(&self, request: Request) -> (u16, Message<ByteString>) {
fn create_request(&self, request: Request) -> (u16, Message<ByteBufOwned>) {
let transaction_id = self.next_transaction_id.fetch_add(1, Ordering::Relaxed);
let transaction_id_buf = [(transaction_id >> 8) as u8, (transaction_id & 0xff) as u8];

let message = match request {
Request::GetPeers(info_hash) => Message {
transaction_id: ByteString::from(transaction_id_buf.as_ref()),
transaction_id: ByteBufOwned::from(transaction_id_buf.as_ref()),
version: None,
ip: None,
kind: MessageKind::GetPeersRequest(GetPeersRequest {
Expand All @@ -622,7 +622,7 @@ impl DhtState {
}),
},
Request::FindNode(target) => Message {
transaction_id: ByteString::from(transaction_id_buf.as_ref()),
transaction_id: ByteBufOwned::from(transaction_id_buf.as_ref()),
version: None,
ip: None,
kind: MessageKind::FindNodeRequest(FindNodeRequest {
Expand All @@ -631,7 +631,7 @@ impl DhtState {
}),
},
Request::Ping => Message {
transaction_id: ByteString::from(transaction_id_buf.as_ref()),
transaction_id: ByteBufOwned::from(transaction_id_buf.as_ref()),
version: None,
ip: None,
kind: MessageKind::PingRequest(PingRequest { id: self.id }),
Expand All @@ -648,7 +648,7 @@ impl DhtState {
port,
token,
}),
transaction_id: ByteString::from(transaction_id_buf.as_ref()),
transaction_id: ByteBufOwned::from(transaction_id_buf.as_ref()),
version: None,
ip: None,
},
Expand All @@ -658,7 +658,7 @@ impl DhtState {

fn on_received_message(
self: &Arc<Self>,
msg: Message<ByteString>,
msg: Message<ByteBufOwned>,
addr: SocketAddr,
) -> anyhow::Result<()> {
let generate_compact_nodes = |target| {
Expand Down Expand Up @@ -770,8 +770,8 @@ impl DhtState {
id: self.id,
nodes: Some(compact_node_info),
values: Some(compact_peer_info),
token: Some(ByteString(
self.peer_store.gen_token_for(req.id, addr).to_vec(),
token: Some(ByteBufOwned::from(
&self.peer_store.gen_token_for(req.id, addr)[..],
)),
}),
};
Expand Down Expand Up @@ -821,15 +821,15 @@ enum Request {
FindNode(Id20),
Announce {
info_hash: Id20,
token: ByteString,
token: ByteBufOwned,
port: u16,
},
Ping,
}

enum ResponseOrError {
Response(Response<ByteString>),
Error(ErrorDescription<ByteString>),
Response(Response<ByteBufOwned>),
Error(ErrorDescription<ByteBufOwned>),
}

impl core::fmt::Debug for ResponseOrError {
Expand Down Expand Up @@ -1010,7 +1010,7 @@ impl DhtWorker {
&self,
socket: &UdpSocket,
mut input_rx: UnboundedReceiver<WorkerSendRequest>,
output_tx: Sender<(Message<ByteString>, SocketAddr)>,
output_tx: Sender<(Message<ByteBufOwned>, SocketAddr)>,
) -> anyhow::Result<()> {
let writer = async {
let mut buf = Vec::new();
Expand Down Expand Up @@ -1050,7 +1050,7 @@ impl DhtWorker {
.recv_from(&mut buf)
.await
.context("error reading from UDP socket")?;
match bprotocol::deserialize_message::<ByteString>(&buf[..size]) {
match bprotocol::deserialize_message::<ByteBufOwned>(&buf[..size]) {
Ok(msg) => match output_tx.send((msg, addr)).await {
Ok(_) => {}
Err(_) => break,
Expand Down
4 changes: 2 additions & 2 deletions crates/dht/src/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
sync::atomic::AtomicU32,
};

use bencode::ByteString;
use bencode::ByteBufOwned;
use chrono::{DateTime, Utc};
use librqbit_core::hash_id::Id20;
use parking_lot::RwLock;
Expand Down Expand Up @@ -134,7 +134,7 @@ impl PeerStore {
token
}

pub fn store_peer(&self, announce: &AnnouncePeer<ByteString>, addr: SocketAddr) -> bool {
pub fn store_peer(&self, announce: &AnnouncePeer<ByteBufOwned>, addr: SocketAddr) -> bool {
// If the info_hash in announce is too far away from us, don't store it.
// If the token doesn't match, don't store it.
// If we are out of capacity, don't store it.
Expand Down
4 changes: 2 additions & 2 deletions crates/librqbit/src/api.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/src/chunk_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading

0 comments on commit 051e450

Please sign in to comment.