Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enforce unreachable_pub lint #3735

Merged
merged 20 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[alias]
# Temporary solution to have clippy config in a single place until https://github.com/rust-lang/rust-clippy/blob/master/doc/roadmap-2021.md#lintstoml-configuration is shipped.
custom-clippy = "clippy --workspace --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -W clippy::used_underscore_binding -D warnings"
custom-clippy = "clippy --workspace --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -W clippy::used_underscore_binding -W unreachable_pub -D warnings"
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod proto {
#![allow(unreachable_pub)]
include!("generated/mod.rs");
pub use self::{
envelope_proto::*, peer_record_proto::mod_PeerRecord::*, peer_record_proto::PeerRecord,
Expand Down
2 changes: 1 addition & 1 deletion core/src/signed_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ mod tests {
use super::*;

#[test]
pub fn test_roundtrip() {
fn test_roundtrip() {
let kp = Keypair::generate_ed25519();
let payload = "some payload".as_bytes();
let domain_separation = "domain separation".to_string();
Expand Down
2 changes: 1 addition & 1 deletion core/src/transport/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::{
};

/// Creates a new [`Boxed`] transport from the given transport.
pub fn boxed<T>(transport: T) -> Boxed<T::Output>
pub(crate) fn boxed<T>(transport: T) -> Boxed<T::Output>
where
T: Transport + Send + Unpin + 'static,
T::Error: Send + Sync,
Expand Down
4 changes: 2 additions & 2 deletions core/src/upgrade/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use log::debug;
use multistream_select::{self, DialerSelectFuture, ListenerSelectFuture};
use std::{iter, mem, pin::Pin, task::Context, task::Poll};

pub use multistream_select::Version;
pub(crate) use multistream_select::Version;
use smallvec::SmallVec;
use std::fmt;

Expand Down Expand Up @@ -275,7 +275,7 @@ impl<N: ProtocolName> AsRef<[u8]> for NameWrap<N> {
}

/// Wrapper for printing a [`ProtocolName`] that is expected to be mostly ASCII
pub(crate) struct DisplayProtocolName<N>(pub N);
pub(crate) struct DisplayProtocolName<N>(pub(crate) N);

impl<N: ProtocolName> fmt::Display for DisplayProtocolName<N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
32 changes: 19 additions & 13 deletions examples/file-sharing/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use std::iter;
/// - The network event stream, e.g. for incoming requests.
///
/// - The network task driving the network itself.
pub async fn new(
pub(crate) async fn new(
secret_key_seed: Option<u8>,
) -> Result<(Client, impl Stream<Item = Event>, EventLoop), Box<dyn Error>> {
// Create a public/private key pair, either random or based on a seed.
Expand Down Expand Up @@ -82,13 +82,16 @@ pub async fn new(
}

#[derive(Clone)]
pub struct Client {
pub(crate) struct Client {
sender: mpsc::Sender<Command>,
}

impl Client {
/// Listen for incoming connections on the given address.
pub async fn start_listening(&mut self, addr: Multiaddr) -> Result<(), Box<dyn Error + Send>> {
pub(crate) async fn start_listening(
&mut self,
addr: Multiaddr,
) -> Result<(), Box<dyn Error + Send>> {
let (sender, receiver) = oneshot::channel();
self.sender
.send(Command::StartListening { addr, sender })
Expand All @@ -98,7 +101,7 @@ impl Client {
}

/// Dial the given peer at the given address.
pub async fn dial(
pub(crate) async fn dial(
&mut self,
peer_id: PeerId,
peer_addr: Multiaddr,
Expand All @@ -116,7 +119,7 @@ impl Client {
}

/// Advertise the local node as the provider of the given file on the DHT.
pub async fn start_providing(&mut self, file_name: String) {
pub(crate) async fn start_providing(&mut self, file_name: String) {
let (sender, receiver) = oneshot::channel();
self.sender
.send(Command::StartProviding { file_name, sender })
Expand All @@ -126,7 +129,7 @@ impl Client {
}

/// Find the providers for the given file on the DHT.
pub async fn get_providers(&mut self, file_name: String) -> HashSet<PeerId> {
pub(crate) async fn get_providers(&mut self, file_name: String) -> HashSet<PeerId> {
let (sender, receiver) = oneshot::channel();
self.sender
.send(Command::GetProviders { file_name, sender })
Expand All @@ -136,7 +139,7 @@ impl Client {
}

/// Request the content of the given file from the given peer.
pub async fn request_file(
pub(crate) async fn request_file(
&mut self,
peer: PeerId,
file_name: String,
Expand All @@ -154,15 +157,19 @@ impl Client {
}

/// Respond with the provided file content to the given request.
pub async fn respond_file(&mut self, file: Vec<u8>, channel: ResponseChannel<FileResponse>) {
pub(crate) async fn respond_file(
&mut self,
file: Vec<u8>,
channel: ResponseChannel<FileResponse>,
) {
self.sender
.send(Command::RespondFile { file, channel })
.await
.expect("Command receiver not to be dropped.");
}
}

pub struct EventLoop {
pub(crate) struct EventLoop {
swarm: Swarm<ComposedBehaviour>,
command_receiver: mpsc::Receiver<Command>,
event_sender: mpsc::Sender<Event>,
Expand Down Expand Up @@ -190,7 +197,7 @@ impl EventLoop {
}
}

pub async fn run(mut self) {
pub(crate) async fn run(mut self) {
loop {
futures::select! {
event = self.swarm.next() => self.handle_event(event.expect("Swarm stream to be infinite.")).await ,
Expand Down Expand Up @@ -452,7 +459,7 @@ enum Command {
}

#[derive(Debug)]
pub enum Event {
pub(crate) enum Event {
InboundRequest {
request: String,
channel: ResponseChannel<FileResponse>,
Expand All @@ -468,8 +475,7 @@ struct FileExchangeCodec();
#[derive(Debug, Clone, PartialEq, Eq)]
struct FileRequest(String);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileResponse(Vec<u8>);

pub(crate) struct FileResponse(Vec<u8>);
impl ProtocolName for FileExchangeProtocol {
fn protocol_name(&self) -> &[u8] {
"/file-exchange/1".as_bytes()
Expand Down
8 changes: 4 additions & 4 deletions examples/metrics/src/http_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::task::{Context, Poll};

const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0";

pub async fn metrics_server(registry: Registry) -> Result<(), std::io::Error> {
pub(crate) async fn metrics_server(registry: Registry) -> Result<(), std::io::Error> {
// Serve on localhost.
let addr = ([127, 0, 0, 1], 0).into();

Expand All @@ -47,7 +47,7 @@ pub async fn metrics_server(registry: Registry) -> Result<(), std::io::Error> {
})
}

pub struct MetricService {
pub(crate) struct MetricService {
reg: Arc<Mutex<Registry>>,
}

Expand Down Expand Up @@ -102,12 +102,12 @@ impl Service<Request<Body>> for MetricService {
}
}

pub struct MakeMetricService {
pub(crate) struct MakeMetricService {
reg: SharedRegistry,
}

impl MakeMetricService {
pub fn new(registry: Registry) -> MakeMetricService {
pub(crate) fn new(registry: Registry) -> MakeMetricService {
MakeMetricService {
reg: Arc::new(Mutex::new(registry)),
}
Expand Down
3 changes: 2 additions & 1 deletion identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
feature = "rsa"
))]
mod proto {
#![allow(unreachable_pub)]
include!("generated/mod.rs");
pub use self::keys_proto::*;
pub(crate) use self::keys_proto::*;
}

#[cfg(feature = "ecdsa")]
Expand Down
4 changes: 2 additions & 2 deletions identity/src/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ struct Asn1RawOid<'a> {

impl<'a> Asn1RawOid<'a> {
/// The underlying OID as byte literal.
pub fn oid(&self) -> &[u8] {
pub(crate) fn oid(&self) -> &[u8] {
self.object.value()
}

/// Writes an OID raw `value` as DER-object to `sink`.
pub fn write<S: Sink>(value: &[u8], sink: &mut S) -> Result<(), Asn1DerError> {
pub(crate) fn write<S: Sink>(value: &[u8], sink: &mut S) -> Result<(), Asn1DerError> {
DerObject::write(Self::TAG, value.len(), &mut value.iter(), sink)
}
}
Expand Down
17 changes: 10 additions & 7 deletions misc/keygen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ use libp2p_identity::PeerId;

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Config {
pub identity: Identity,
pub(crate) struct Config {
pub(crate) identity: Identity,
}

impl Config {
pub fn from_file(path: &Path) -> Result<Self, Box<dyn Error>> {
pub(crate) fn from_file(path: &Path) -> Result<Self, Box<dyn Error>> {
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
}

pub fn from_key_material(peer_id: PeerId, keypair: &Keypair) -> Result<Self, Box<dyn Error>> {
pub(crate) fn from_key_material(
peer_id: PeerId,
keypair: &Keypair,
) -> Result<Self, Box<dyn Error>> {
let priv_key = BASE64_STANDARD.encode(keypair.to_protobuf_encoding()?);
let peer_id = peer_id.to_base58();
Ok(Self {
Expand All @@ -28,10 +31,10 @@ impl Config {

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Identity {
pub(crate) struct Identity {
#[serde(rename = "PeerID")]
pub peer_id: String,
pub priv_key: String,
pub(crate) peer_id: String,
pub(crate) priv_key: String,
}

impl zeroize::Zeroize for Config {
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/dcutr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;

pub struct Metrics {
pub(crate) struct Metrics {
events: Family<EventLabels, Counter>,
}

impl Metrics {
pub fn new(registry: &mut Registry) -> Self {
pub(crate) fn new(registry: &mut Registry) -> Self {
let sub_registry = registry.sub_registry_with_prefix("dcutr");

let events = Family::default();
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/gossipsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
use prometheus_client::metrics::counter::Counter;
use prometheus_client::registry::Registry;

pub struct Metrics {
pub(crate) struct Metrics {
messages: Counter,
}

impl Metrics {
pub fn new(registry: &mut Registry) -> Self {
pub(crate) fn new(registry: &mut Registry) -> Self {
let sub_registry = registry.sub_registry_with_prefix("gossipsub");

let messages = Counter::default();
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::collections::HashMap;
use std::iter;
use std::sync::{Arc, Mutex};

pub struct Metrics {
pub(crate) struct Metrics {
protocols: Protocols,
error: Counter,
pushed: Counter,
Expand All @@ -42,7 +42,7 @@ pub struct Metrics {
}

impl Metrics {
pub fn new(registry: &mut Registry) -> Self {
pub(crate) fn new(registry: &mut Registry) -> Self {
let sub_registry = registry.sub_registry_with_prefix("identify");

let protocols = Protocols::default();
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/kad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
use prometheus_client::registry::{Registry, Unit};

pub struct Metrics {
pub(crate) struct Metrics {
query_result_get_record_ok: Counter,
query_result_get_record_error: Family<GetRecordResult, Counter>,

Expand All @@ -45,7 +45,7 @@ pub struct Metrics {
}

impl Metrics {
pub fn new(registry: &mut Registry) -> Self {
pub(crate) fn new(registry: &mut Registry) -> Self {
let sub_registry = registry.sub_registry_with_prefix("kad");

let query_result_get_record_ok = Counter::default();
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ enum Failure {
Other,
}

pub struct Metrics {
pub(crate) struct Metrics {
rtt: Histogram,
failure: Family<FailureLabels, Counter>,
pong_received: Counter,
}

impl Metrics {
pub fn new(registry: &mut Registry) -> Self {
pub(crate) fn new(registry: &mut Registry) -> Self {
let sub_registry = registry.sub_registry_with_prefix("ping");

let rtt = Histogram::new(exponential_buckets(0.001, 2.0, 12));
Expand Down
2 changes: 1 addition & 1 deletion misc/metrics/src/protocol_stack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use libp2p_core::multiaddr::Multiaddr;

pub fn as_string(ma: &Multiaddr) -> String {
pub(crate) fn as_string(ma: &Multiaddr) -> String {
let len = ma
.protocol_stack()
.fold(0, |acc, proto| acc + proto.len() + 1);
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;

pub struct Metrics {
pub(crate) struct Metrics {
events: Family<EventLabels, Counter>,
}

impl Metrics {
pub fn new(registry: &mut Registry) -> Self {
pub(crate) fn new(registry: &mut Registry) -> Self {
let sub_registry = registry.sub_registry_with_prefix("relay");

let events = Family::default();
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
use prometheus_client::registry::Registry;

pub struct Metrics {
pub(crate) struct Metrics {
connections_incoming: Family<AddressLabels, Counter>,
connections_incoming_error: Family<IncomingConnectionErrorLabels, Counter>,

Expand All @@ -45,7 +45,7 @@ pub struct Metrics {
}

impl Metrics {
pub fn new(registry: &mut Registry) -> Self {
pub(crate) fn new(registry: &mut Registry) -> Self {
let sub_registry = registry.sub_registry_with_prefix("swarm");

let connections_incoming = Family::default();
Expand Down
Loading