pub fn add_peer(&mut self, peer_id: PeerId, slot_type: SlotType) -> SlotState {
		if let Some(st) = self.slots.get_mut(&peer_id) {
			if *st == SlotType::Common && slot_type == SlotType::Reserved {
				*st = SlotType::Reserved;
				return SlotState::Upgraded(peer_id);
			} else {
				return SlotState::AlreadyConnected(peer_id);
			}
		}

		if self.slots.len() == self.max_slots {
			if let SlotType::Reserved = slot_type {
				// if we are trying to insert a reserved peer, but we all of our slots are full,
				// we need to remove one of the existing common connections
				let to_remove = self.slots.iter()
					.find(|(_, &slot_type)| slot_type == SlotType::Common)
					.map(|(to_remove, _)| to_remove)
					.cloned();

				if let Some(to_remove) = to_remove {
					self.slots.remove(&to_remove);
					self.slots.insert(peer_id.clone(), slot_type);

					return SlotState::Swaped {
						removed: to_remove,
						added: peer_id,
					};
				}
			}
			return SlotState::MaxConnections(peer_id);
		}

		self.slots.insert(peer_id.clone(), slot_type);
		SlotState::Added(peer_id)
	}
/// Tries to find a slot for a given peer and returns `SlotState`.
Tries to find a slot for a given peer and returns `SlotState`.
Tries to find a slot for a given peer and returns ` SlotState ` .
pub fn add_peer(&mut self, peer_id: PeerId, slot_type: SlotType) -> SlotState {
		if let Some(st) = self.slots.get_mut(&peer_id) {
			if *st == SlotType::Common && slot_type == SlotType::Reserved {
				*st = SlotType::Reserved;
				return SlotState::Upgraded(peer_id);
			} else {
				return SlotState::AlreadyConnected(peer_id);
			}
		}
		if self.slots.len() == self.max_slots {
			if let SlotType::Reserved = slot_type {
				// if we are trying to insert a reserved peer, but we all of our slots are full,
				// we need to remove one of the existing common connections
				let to_remove = self.slots.iter()
					.find(|(_, &slot_type)| slot_type == SlotType::Common)
					.map(|(to_remove, _)| to_remove)
					.cloned();
				if let Some(to_remove) = to_remove {
					self.slots.remove(&to_remove);
					self.slots.insert(peer_id.clone(), slot_type);
					return SlotState::Swaped {
						removed: to_remove,
						added: peer_id,
					};
				}
			}
			return SlotState::MaxConnections(peer_id);
		}
		self.slots.insert(peer_id.clone(), slot_type);
		SlotState::Added(peer_id)
	}
pub fn add_peer ( & mut self , peer_id : PeerId , slot_type : SlotType ) -> SlotState { if let Some ( st ) = self . slots . get_mut ( & peer_id ) { if * st == SlotType :: Common && slot_type == SlotType :: Reserved { * st = SlotType :: Reserved ; return SlotState :: Upgraded ( peer_id ) ; } else { return SlotState :: AlreadyConnected ( peer_id ) ; } } if self . slots . len ( ) == self . max_slots { if let SlotType :: Reserved = slot_type { // if we are trying to insert a reserved peer, but we all of our slots are full, // we need to remove one of the existing common connections let to_remove = self . slots . iter ( ) . find ( | ( _ , & slot_type ) | slot_type == SlotType :: Common ) . map ( | ( to_remove , _ ) | to_remove ) . cloned ( ) ; if let Some ( to_remove ) = to_remove { self . slots . remove ( & to_remove ) ; self . slots . insert ( peer_id . clone ( ) , slot_type ) ; return SlotState :: Swaped { removed : to_remove , added : peer_id , } ; } } return SlotState :: MaxConnections ( peer_id ) ; } self . slots . insert ( peer_id . clone ( ) , slot_type ) ; SlotState :: Added ( peer_id ) }
Tries to find a slot for a given peer and returns `SlotState`.
Tries to find a slot for a given peer and returns ` SlotState ` .
&mut self
peer_id
PeerId
slot_type
SlotType
&mut self
peer_id
PeerId
slot_type
SlotType