diff --git a/bitswap/src/behaviour.rs b/bitswap/src/behaviour.rs index 75f355f3..581b3a5a 100644 --- a/bitswap/src/behaviour.rs +++ b/bitswap/src/behaviour.rs @@ -6,7 +6,7 @@ //! The `Bitswap` struct implements the `NetworkBehaviour` trait. When used, it //! will allow providing and reciving IPFS blocks. use crate::block::Block; -use crate::ledger::{Ledger, Message, Priority, I, O}; +use crate::ledger::{Ledger, Message, Priority}; use crate::protocol::BitswapConfig; use crate::strategy::{Strategy, StrategyEvent}; use fnv::FnvHashSet; @@ -23,7 +23,7 @@ use std::collections::{HashMap, VecDeque}; /// Network behaviour that handles sending and receiving IPFS blocks. pub struct Bitswap { /// Queue of events to report to the user. - events: VecDeque, ()>>, + events: VecDeque>, /// List of peers to send messages to. target_peers: FnvHashSet, /// Ledger @@ -185,7 +185,7 @@ impl Bitswap { } impl NetworkBehaviour for Bitswap { - type ProtocolsHandler = OneShotHandler, InnerMessage>; + type ProtocolsHandler = OneShotHandler; type OutEvent = (); fn new_handler(&mut self) -> Self::ProtocolsHandler { @@ -315,14 +315,14 @@ impl NetworkBehaviour for Bitswap { #[derive(Debug)] pub enum InnerMessage { /// We received a `Message` from a remote. - Rx(Message), + Rx(Message), /// We successfully sent a `Message`. Tx, } -impl From> for InnerMessage { +impl From for InnerMessage { #[inline] - fn from(message: Message) -> InnerMessage { + fn from(message: Message) -> InnerMessage { InnerMessage::Rx(message) } } diff --git a/bitswap/src/ledger.rs b/bitswap/src/ledger.rs index 5042c42e..25033df6 100644 --- a/bitswap/src/ledger.rs +++ b/bitswap/src/ledger.rs @@ -3,7 +3,6 @@ use crate::block::Block; use crate::error::BitswapError; use crate::prefix::Prefix; use core::convert::TryFrom; -use core::marker::PhantomData; use libipld::cid::Cid; use prost::Message as ProstMessage; use std::collections::HashMap; @@ -36,21 +35,21 @@ impl Ledger { Self::default() } - pub fn send_block(&mut self, block: Block) -> Message { - let mut message = Message::new(); + pub fn send_block(&mut self, block: Block) -> Message { + let mut message = Message::default(); message.add_block(block); message } - pub fn want_block(&mut self, cid: &Cid, priority: Priority) -> Message { - let mut message = Message::new(); + pub fn want_block(&mut self, cid: &Cid, priority: Priority) -> Message { + let mut message = Message::default(); message.want_block(cid, priority); message } - pub fn cancel_block(&mut self, cid: &Cid) -> Option> { + pub fn cancel_block(&mut self, cid: &Cid) -> Option { if self.sent_want_list.contains_key(cid) { - let mut message = Message::new(); + let mut message = Message::default(); message.cancel_block(cid); Some(message) } else { @@ -58,7 +57,7 @@ impl Ledger { } } - pub fn update_outgoing_stats(&mut self, message: &Message) { + pub fn update_outgoing_stats(&mut self, message: &Message) { self.sent_blocks += message.blocks.len() as u64; for cid in message.cancel() { self.sent_want_list.remove(cid); @@ -68,7 +67,7 @@ impl Ledger { } } - pub fn update_incoming_stats(&mut self, message: &Message) { + pub fn update_incoming_stats(&mut self, message: &Message) { for cid in message.cancel() { self.received_want_list.remove(cid); } @@ -96,16 +95,9 @@ impl Ledger { } } -#[derive(Debug, Clone, PartialEq)] -pub struct I; -#[derive(Debug, Clone, PartialEq)] -pub struct O; - /// A bitswap message. -#[derive(Clone, PartialEq)] -pub struct Message { - /// Message tag - _phantom_data: PhantomData, +#[derive(Clone, PartialEq, Default)] +pub struct Message { /// List of wanted blocks. want: HashMap, /// List of blocks to cancel. @@ -116,23 +108,7 @@ pub struct Message { blocks: Vec, } -impl Default for Message { - fn default() -> Self { - Message { - _phantom_data: PhantomData, - want: HashMap::new(), - cancel: Vec::new(), - full: false, - blocks: Vec::new(), - } - } -} - -impl Message { - pub fn new() -> Self { - Self::default() - } - +impl Message { /// Returns the list of blocks. pub fn blocks(&self) -> &[Block] { &self.blocks @@ -175,7 +151,7 @@ impl Message { } } -impl Into> for &Message { +impl Into> for &Message { fn into(self) -> Vec { let mut proto = bitswap_pb::Message::default(); let mut wantlist = bitswap_pb::message::Wantlist::default(); @@ -208,18 +184,23 @@ impl Into> for &Message { } } -impl Message { +impl Message { /// Turns this `Message` into a message that can be sent to a substream. pub fn to_bytes(&self) -> Vec { self.into() } + + /// Creates a `Message` from bytes that were received from a substream. + pub fn from_bytes(bytes: &[u8]) -> Result { + Self::try_from(bytes) + } } -impl TryFrom<&[u8]> for Message { +impl TryFrom<&[u8]> for Message { type Error = BitswapError; fn try_from(bytes: &[u8]) -> Result { let proto: bitswap_pb::Message = bitswap_pb::Message::decode(bytes)?; - let mut message = Message::new(); + let mut message = Message::default(); for entry in proto.wantlist.unwrap_or_default().entries { let cid = Cid::try_from(entry.block)?; if entry.cancel { @@ -241,14 +222,7 @@ impl TryFrom<&[u8]> for Message { } } -impl Message { - /// Creates a `Message` from bytes that were received from a substream. - pub fn from_bytes(bytes: &[u8]) -> Result { - Self::try_from(bytes) - } -} - -impl std::fmt::Debug for Message { +impl std::fmt::Debug for Message { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { for (cid, priority) in self.want() { writeln!(fmt, "want: {} {}", cid.to_string(), priority)?; diff --git a/bitswap/src/protocol.rs b/bitswap/src/protocol.rs index aa51df80..e33b1d89 100644 --- a/bitswap/src/protocol.rs +++ b/bitswap/src/protocol.rs @@ -4,7 +4,7 @@ use crate::error::BitswapError; /// The protocol works the following way: /// /// - TODO -use crate::ledger::{Message, I, O}; +use crate::ledger::Message; use core::future::Future; use core::iter; use core::pin::Pin; @@ -33,7 +33,7 @@ impl InboundUpgrade for BitswapConfig where TSocket: AsyncRead + AsyncWrite + Send + Unpin + 'static, { - type Output = Message; + type Output = Message; type Error = BitswapError; #[allow(clippy::type_complexity)] type Future = Pin> + Send>>; @@ -50,7 +50,7 @@ where } } -impl UpgradeInfo for Message { +impl UpgradeInfo for Message { type Info = &'static [u8]; type InfoIter = iter::Once; @@ -60,7 +60,7 @@ impl UpgradeInfo for Message { } } -impl OutboundUpgrade for Message +impl OutboundUpgrade for Message where TSocket: AsyncRead + AsyncWrite + Send + Unpin + 'static, {