Skip to content

Commit

Permalink
chore: enforce unreachable_pub lint
Browse files Browse the repository at this point in the history
The `unreachable_pub` lint makes us aware of uses of `pub` that are not actually reachable from the crate root. This is considered good because it means reading a `pub` somewhere means it is actually public API. Some of our crates are quite large and keeping their entire API surface in your head is difficult.

We should strive for most items being `pub(crate)`. This lint helps us enforce that.

Pull-Request: libp2p#3735.
  • Loading branch information
thomaseizinger authored Apr 26, 2023
1 parent 72bccb1 commit 1aa2c58
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
43 changes: 19 additions & 24 deletions src/behaviour/as_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,21 @@ pub enum OutboundProbeEvent {
}

/// View over [`super::Behaviour`] in a client role.
pub struct AsClient<'a> {
pub inner: &'a mut request_response::Behaviour<AutoNatCodec>,
pub local_peer_id: PeerId,
pub config: &'a Config,
pub connected: &'a HashMap<PeerId, HashMap<ConnectionId, Option<Multiaddr>>>,
pub probe_id: &'a mut ProbeId,

pub servers: &'a HashSet<PeerId>,
pub throttled_servers: &'a mut Vec<(PeerId, Instant)>,

pub nat_status: &'a mut NatStatus,
pub confidence: &'a mut usize,

pub ongoing_outbound: &'a mut HashMap<RequestId, ProbeId>,

pub last_probe: &'a mut Option<Instant>,
pub schedule_probe: &'a mut Delay,

pub listen_addresses: &'a ListenAddresses,
pub external_addresses: &'a ExternalAddresses,
pub(crate) struct AsClient<'a> {
pub(crate) inner: &'a mut request_response::Behaviour<AutoNatCodec>,
pub(crate) local_peer_id: PeerId,
pub(crate) config: &'a Config,
pub(crate) connected: &'a HashMap<PeerId, HashMap<ConnectionId, Option<Multiaddr>>>,
pub(crate) probe_id: &'a mut ProbeId,
pub(crate) servers: &'a HashSet<PeerId>,
pub(crate) throttled_servers: &'a mut Vec<(PeerId, Instant)>,
pub(crate) nat_status: &'a mut NatStatus,
pub(crate) confidence: &'a mut usize,
pub(crate) ongoing_outbound: &'a mut HashMap<RequestId, ProbeId>,
pub(crate) last_probe: &'a mut Option<Instant>,
pub(crate) schedule_probe: &'a mut Delay,
pub(crate) listen_addresses: &'a ListenAddresses,
pub(crate) external_addresses: &'a ExternalAddresses,
}

impl<'a> HandleInnerEvent for AsClient<'a> {
Expand Down Expand Up @@ -200,7 +195,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
}

impl<'a> AsClient<'a> {
pub fn poll_auto_probe(&mut self, cx: &mut Context<'_>) -> Poll<OutboundProbeEvent> {
pub(crate) fn poll_auto_probe(&mut self, cx: &mut Context<'_>) -> Poll<OutboundProbeEvent> {
match self.schedule_probe.poll_unpin(cx) {
Poll::Ready(()) => {
self.schedule_probe.reset(self.config.retry_interval);
Expand Down Expand Up @@ -231,7 +226,7 @@ impl<'a> AsClient<'a> {
}

// An inbound connection can indicate that we are public; adjust the delay to the next probe.
pub fn on_inbound_connection(&mut self) {
pub(crate) fn on_inbound_connection(&mut self) {
if *self.confidence == self.config.confidence_max {
if self.nat_status.is_public() {
self.schedule_next_probe(self.config.refresh_interval * 2);
Expand All @@ -241,7 +236,7 @@ impl<'a> AsClient<'a> {
}
}

pub fn on_new_address(&mut self) {
pub(crate) fn on_new_address(&mut self) {
if !self.nat_status.is_public() {
// New address could be publicly reachable, trigger retry.
if *self.confidence > 0 {
Expand All @@ -251,7 +246,7 @@ impl<'a> AsClient<'a> {
}
}

pub fn on_expired_address(&mut self, addr: &Multiaddr) {
pub(crate) fn on_expired_address(&mut self, addr: &Multiaddr) {
if let NatStatus::Public(public_address) = self.nat_status {
if public_address == addr {
*self.confidence = 0;
Expand Down
20 changes: 9 additions & 11 deletions src/behaviour/as_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,14 @@ pub enum InboundProbeEvent {
}

/// View over [`super::Behaviour`] in a server role.
pub struct AsServer<'a> {
pub inner: &'a mut request_response::Behaviour<AutoNatCodec>,
pub config: &'a Config,
pub connected: &'a HashMap<PeerId, HashMap<ConnectionId, Option<Multiaddr>>>,
pub probe_id: &'a mut ProbeId,

pub throttled_clients: &'a mut Vec<(PeerId, Instant)>,

pub(crate) struct AsServer<'a> {
pub(crate) inner: &'a mut request_response::Behaviour<AutoNatCodec>,
pub(crate) config: &'a Config,
pub(crate) connected: &'a HashMap<PeerId, HashMap<ConnectionId, Option<Multiaddr>>>,
pub(crate) probe_id: &'a mut ProbeId,
pub(crate) throttled_clients: &'a mut Vec<(PeerId, Instant)>,
#[allow(clippy::type_complexity)]
pub ongoing_inbound: &'a mut HashMap<
pub(crate) ongoing_inbound: &'a mut HashMap<
PeerId,
(
ProbeId,
Expand Down Expand Up @@ -197,7 +195,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
}

impl<'a> AsServer<'a> {
pub fn on_outbound_connection(
pub(crate) fn on_outbound_connection(
&mut self,
peer: &PeerId,
address: &Multiaddr,
Expand Down Expand Up @@ -229,7 +227,7 @@ impl<'a> AsServer<'a> {
})
}

pub fn on_outbound_dial_error(
pub(crate) fn on_outbound_dial_error(
&mut self,
peer: Option<PeerId>,
error: &DialError,
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use self::{
pub use libp2p_request_response::{InboundFailure, OutboundFailure};

mod proto {
#![allow(unreachable_pub)]
include!("generated/mod.rs");
pub use self::structs::{mod_Message::*, Message};
pub(crate) use self::structs::{mod_Message::*, Message};
}

0 comments on commit 1aa2c58

Please sign in to comment.