Skip to content

Commit

Permalink
refactor(networking): remove circular vec error
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Apr 11, 2024
1 parent cef9855 commit 7c5d931
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
25 changes: 12 additions & 13 deletions sn_networking/src/circular_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,34 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::error::NetworkError;

/// Based on https://users.rust-lang.org/t/the-best-ring-buffer-library/58489/7
/// A circular buffer implemented with a VecDeque.
#[derive(Debug)]
pub struct CircularVec<T> {
pub(crate) struct CircularVec<T> {
inner: std::collections::VecDeque<T>,
}

impl<T> CircularVec<T> {
/// Creates a new CircularVec with the given capacity.
pub fn new(capacity: usize) -> Self {
///
/// Capacity is normally rounded up to the nearest power of 2, minus one. E.g. 15, 31, 63, 127, 255, etc.
pub(crate) fn new(capacity: usize) -> Self {
Self {
inner: std::collections::VecDeque::with_capacity(capacity),
}
}

/// Pushes an item into the CircularVec. If the CircularVec is full, the oldest item is removed.
pub fn push(&mut self, item: T) -> Result<(), NetworkError> {
pub(crate) fn push(&mut self, item: T) {
if self.inner.len() == self.inner.capacity() {
self.inner
.pop_front()
.ok_or(NetworkError::CircularVecPopFrontError)?;
let _ = self.inner.pop_front();
}
self.inner.push_back(item);
Ok(())
}

/// Checks if the CircularVec contains the given item.
pub fn contains(&self, item: &T) -> bool
pub(crate) fn contains(&self, item: &T) -> bool
where
T: PartialEq,
{
Expand All @@ -51,14 +48,16 @@ mod tests {
#[test]
fn test_push_and_contains() {
let mut cv = CircularVec::new(2);
assert!(cv.push(1).is_ok());
assert!(cv.push(2).is_ok());
cv.push(1);
cv.push(2);
assert!(cv.contains(&1));
assert!(cv.contains(&2));

assert!(cv.push(3).is_ok());
cv.push(3);
assert!(!cv.contains(&1));
assert!(cv.contains(&2));
assert!(cv.contains(&3));

assert!(cv.inner.len() == 2);
}
}
3 changes: 0 additions & 3 deletions sn_networking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ pub enum NetworkError {
#[error("Close group size must be a non-zero usize")]
InvalidCloseGroupSize,

#[error("Failed to pop from front of CircularVec")]
CircularVecPopFrontError,

#[error("Node Listen Address was not provided during construction")]
ListenAddressNotProvided,

Expand Down
8 changes: 2 additions & 6 deletions sn_networking/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ impl SwarmDriver {

if !kbucket_full {
info!(%peer_id, ?addrs, "received identify info from undialed peer for not full kbucket {ilog2:?}, dial back to confirm external accessible");
self.dialed_peers
.push(peer_id)
.map_err(|_| NetworkError::CircularVecPopFrontError)?;
self.dialed_peers.push(peer_id);
if let Err(err) = self.swarm.dial(
DialOpts::peer_id(peer_id)
.condition(PeerCondition::NotDialing)
Expand Down Expand Up @@ -444,9 +442,7 @@ impl SwarmDriver {
);

if endpoint.is_dialer() {
self.dialed_peers
.push(peer_id)
.map_err(|_| NetworkError::CircularVecPopFrontError)?;
self.dialed_peers.push(peer_id);
}
}
SwarmEvent::ConnectionClosed {
Expand Down

0 comments on commit 7c5d931

Please sign in to comment.