mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-30 16:31:57 +00:00
chore: enforce unreachable_pub
lint
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: #3735.
This commit is contained in:
@@ -41,7 +41,7 @@ use std::{
|
||||
/// An mDNS instance for a networking interface. To discover all peers when having multiple
|
||||
/// interfaces an [`InterfaceState`] is required for each interface.
|
||||
#[derive(Debug)]
|
||||
pub struct InterfaceState<U, T> {
|
||||
pub(crate) struct InterfaceState<U, T> {
|
||||
/// Address this instance is bound to.
|
||||
addr: IpAddr,
|
||||
/// Receive socket.
|
||||
@@ -77,7 +77,7 @@ where
|
||||
T: Builder + futures::Stream,
|
||||
{
|
||||
/// Builds a new [`InterfaceState`].
|
||||
pub fn new(addr: IpAddr, config: Config, local_peer_id: PeerId) -> io::Result<Self> {
|
||||
pub(crate) fn new(addr: IpAddr, config: Config, local_peer_id: PeerId) -> io::Result<Self> {
|
||||
log::info!("creating instance on iface {}", addr);
|
||||
let recv_socket = match addr {
|
||||
IpAddr::V4(addr) => {
|
||||
@@ -141,15 +141,15 @@ where
|
||||
})
|
||||
}
|
||||
|
||||
pub fn reset_timer(&mut self) {
|
||||
pub(crate) fn reset_timer(&mut self) {
|
||||
self.timeout = T::interval(self.query_interval);
|
||||
}
|
||||
|
||||
pub fn fire_timer(&mut self) {
|
||||
pub(crate) fn fire_timer(&mut self) {
|
||||
self.timeout = T::interval_at(Instant::now(), self.query_interval);
|
||||
}
|
||||
|
||||
pub fn poll(
|
||||
pub(crate) fn poll(
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
listen_addresses: &ListenAddresses,
|
||||
|
@@ -47,11 +47,10 @@ const MAX_PACKET_SIZE: usize = 9000 - 68;
|
||||
const MAX_RECORDS_PER_PACKET: usize = (MAX_PACKET_SIZE - 100) / MAX_TXT_RECORD_SIZE;
|
||||
|
||||
/// An encoded MDNS packet.
|
||||
pub type MdnsPacket = Vec<u8>;
|
||||
|
||||
pub(crate) type MdnsPacket = Vec<u8>;
|
||||
/// Decodes a `<character-string>` (as defined by RFC1035) into a `Vec` of ASCII characters.
|
||||
// TODO: better error type?
|
||||
pub fn decode_character_string(mut from: &[u8]) -> Result<Cow<'_, [u8]>, ()> {
|
||||
pub(crate) fn decode_character_string(mut from: &[u8]) -> Result<Cow<'_, [u8]>, ()> {
|
||||
if from.is_empty() {
|
||||
return Ok(Cow::Owned(Vec::new()));
|
||||
}
|
||||
@@ -70,7 +69,7 @@ pub fn decode_character_string(mut from: &[u8]) -> Result<Cow<'_, [u8]>, ()> {
|
||||
}
|
||||
|
||||
/// Builds the binary representation of a DNS query to send on the network.
|
||||
pub fn build_query() -> MdnsPacket {
|
||||
pub(crate) fn build_query() -> MdnsPacket {
|
||||
let mut out = Vec::with_capacity(33);
|
||||
|
||||
// Program-generated transaction ID; unused by our implementation.
|
||||
@@ -104,7 +103,7 @@ pub fn build_query() -> MdnsPacket {
|
||||
/// Builds the response to an address discovery DNS query.
|
||||
///
|
||||
/// If there are more than 2^16-1 addresses, ignores the rest.
|
||||
pub fn build_query_response<'a>(
|
||||
pub(crate) fn build_query_response<'a>(
|
||||
id: u16,
|
||||
peer_id: PeerId,
|
||||
addresses: impl ExactSizeIterator<Item = &'a Multiaddr>,
|
||||
@@ -166,7 +165,7 @@ pub fn build_query_response<'a>(
|
||||
}
|
||||
|
||||
/// Builds the response to a service discovery DNS query.
|
||||
pub fn build_service_discovery_response(id: u16, ttl: Duration) -> MdnsPacket {
|
||||
pub(crate) fn build_service_discovery_response(id: u16, ttl: Duration) -> MdnsPacket {
|
||||
// Convert the TTL into seconds.
|
||||
let ttl = duration_to_secs(ttl);
|
||||
|
||||
|
@@ -34,7 +34,7 @@ use trust_dns_proto::{
|
||||
|
||||
/// A valid mDNS packet received by the service.
|
||||
#[derive(Debug)]
|
||||
pub enum MdnsPacket {
|
||||
pub(crate) enum MdnsPacket {
|
||||
/// A query made by a remote.
|
||||
Query(MdnsQuery),
|
||||
/// A response sent by a remote in response to one of our queries.
|
||||
@@ -44,7 +44,7 @@ pub enum MdnsPacket {
|
||||
}
|
||||
|
||||
impl MdnsPacket {
|
||||
pub fn new_from_bytes(
|
||||
pub(crate) fn new_from_bytes(
|
||||
buf: &[u8],
|
||||
from: SocketAddr,
|
||||
) -> Result<Option<MdnsPacket>, trust_dns_proto::error::ProtoError> {
|
||||
@@ -82,7 +82,7 @@ impl MdnsPacket {
|
||||
}
|
||||
|
||||
/// A received mDNS query.
|
||||
pub struct MdnsQuery {
|
||||
pub(crate) struct MdnsQuery {
|
||||
/// Sender of the address.
|
||||
from: SocketAddr,
|
||||
/// Id of the received DNS query. We need to pass this ID back in the results.
|
||||
@@ -91,12 +91,12 @@ pub struct MdnsQuery {
|
||||
|
||||
impl MdnsQuery {
|
||||
/// Source address of the packet.
|
||||
pub fn remote_addr(&self) -> &SocketAddr {
|
||||
pub(crate) fn remote_addr(&self) -> &SocketAddr {
|
||||
&self.from
|
||||
}
|
||||
|
||||
/// Query id of the packet.
|
||||
pub fn query_id(&self) -> u16 {
|
||||
pub(crate) fn query_id(&self) -> u16 {
|
||||
self.query_id
|
||||
}
|
||||
}
|
||||
@@ -111,7 +111,7 @@ impl fmt::Debug for MdnsQuery {
|
||||
}
|
||||
|
||||
/// A received mDNS service discovery query.
|
||||
pub struct MdnsServiceDiscovery {
|
||||
pub(crate) struct MdnsServiceDiscovery {
|
||||
/// Sender of the address.
|
||||
from: SocketAddr,
|
||||
/// Id of the received DNS query. We need to pass this ID back in the results.
|
||||
@@ -120,12 +120,12 @@ pub struct MdnsServiceDiscovery {
|
||||
|
||||
impl MdnsServiceDiscovery {
|
||||
/// Source address of the packet.
|
||||
pub fn remote_addr(&self) -> &SocketAddr {
|
||||
pub(crate) fn remote_addr(&self) -> &SocketAddr {
|
||||
&self.from
|
||||
}
|
||||
|
||||
/// Query id of the packet.
|
||||
pub fn query_id(&self) -> u16 {
|
||||
pub(crate) fn query_id(&self) -> u16 {
|
||||
self.query_id
|
||||
}
|
||||
}
|
||||
@@ -140,14 +140,14 @@ impl fmt::Debug for MdnsServiceDiscovery {
|
||||
}
|
||||
|
||||
/// A received mDNS response.
|
||||
pub struct MdnsResponse {
|
||||
pub(crate) struct MdnsResponse {
|
||||
peers: Vec<MdnsPeer>,
|
||||
from: SocketAddr,
|
||||
}
|
||||
|
||||
impl MdnsResponse {
|
||||
/// Creates a new `MdnsResponse` based on the provided `Packet`.
|
||||
pub fn new(packet: &Message, from: SocketAddr) -> MdnsResponse {
|
||||
pub(crate) fn new(packet: &Message, from: SocketAddr) -> MdnsResponse {
|
||||
let peers = packet
|
||||
.answers()
|
||||
.iter()
|
||||
@@ -168,7 +168,7 @@ impl MdnsResponse {
|
||||
MdnsResponse { peers, from }
|
||||
}
|
||||
|
||||
pub fn extract_discovered(
|
||||
pub(crate) fn extract_discovered(
|
||||
&self,
|
||||
now: Instant,
|
||||
local_peer_id: PeerId,
|
||||
@@ -188,7 +188,7 @@ impl MdnsResponse {
|
||||
}
|
||||
|
||||
/// Source address of the packet.
|
||||
pub fn remote_addr(&self) -> &SocketAddr {
|
||||
pub(crate) fn remote_addr(&self) -> &SocketAddr {
|
||||
&self.from
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ impl fmt::Debug for MdnsResponse {
|
||||
}
|
||||
|
||||
/// A peer discovered by the service.
|
||||
pub struct MdnsPeer {
|
||||
pub(crate) struct MdnsPeer {
|
||||
addrs: Vec<Multiaddr>,
|
||||
/// Id of the peer.
|
||||
peer_id: PeerId,
|
||||
@@ -228,7 +228,7 @@ pub struct MdnsPeer {
|
||||
|
||||
impl MdnsPeer {
|
||||
/// Creates a new `MdnsPeer` based on the provided `Packet`.
|
||||
pub fn new(packet: &Message, record_value: &Name, ttl: u32) -> Option<MdnsPeer> {
|
||||
pub(crate) fn new(packet: &Message, record_value: &Name, ttl: u32) -> Option<MdnsPeer> {
|
||||
let mut my_peer_id: Option<PeerId> = None;
|
||||
let addrs = packet
|
||||
.additionals()
|
||||
@@ -291,20 +291,20 @@ impl MdnsPeer {
|
||||
|
||||
/// Returns the id of the peer.
|
||||
#[inline]
|
||||
pub fn id(&self) -> &PeerId {
|
||||
pub(crate) fn id(&self) -> &PeerId {
|
||||
&self.peer_id
|
||||
}
|
||||
|
||||
/// Returns the requested time-to-live for the record.
|
||||
#[inline]
|
||||
pub fn ttl(&self) -> Duration {
|
||||
pub(crate) fn ttl(&self) -> Duration {
|
||||
Duration::from_secs(u64::from(self.ttl))
|
||||
}
|
||||
|
||||
/// Returns the list of addresses the peer says it is listening on.
|
||||
///
|
||||
/// Filters out invalid addresses.
|
||||
pub fn addresses(&self) -> &Vec<Multiaddr> {
|
||||
pub(crate) fn addresses(&self) -> &Vec<Multiaddr> {
|
||||
&self.addrs
|
||||
}
|
||||
}
|
||||
|
@@ -26,6 +26,7 @@ use std::{
|
||||
};
|
||||
|
||||
/// Interface that must be implemented by the different runtimes to use the [`UdpSocket`] in async mode
|
||||
#[allow(unreachable_pub)] // Users should not depend on this.
|
||||
pub trait AsyncSocket: Unpin + Send + 'static {
|
||||
/// Create the async socket from the [`std::net::UdpSocket`]
|
||||
fn from_std(socket: UdpSocket) -> std::io::Result<Self>
|
||||
@@ -49,14 +50,13 @@ pub trait AsyncSocket: Unpin + Send + 'static {
|
||||
}
|
||||
|
||||
#[cfg(feature = "async-io")]
|
||||
pub mod asio {
|
||||
pub(crate) mod asio {
|
||||
use super::*;
|
||||
use async_io::Async;
|
||||
use futures::FutureExt;
|
||||
|
||||
/// AsyncIo UdpSocket
|
||||
pub type AsyncUdpSocket = Async<UdpSocket>;
|
||||
|
||||
pub(crate) type AsyncUdpSocket = Async<UdpSocket>;
|
||||
impl AsyncSocket for AsyncUdpSocket {
|
||||
fn from_std(socket: UdpSocket) -> std::io::Result<Self> {
|
||||
Async::new(socket)
|
||||
@@ -92,13 +92,12 @@ pub mod asio {
|
||||
}
|
||||
|
||||
#[cfg(feature = "tokio")]
|
||||
pub mod tokio {
|
||||
pub(crate) mod tokio {
|
||||
use super::*;
|
||||
use ::tokio::{io::ReadBuf, net::UdpSocket as TkUdpSocket};
|
||||
|
||||
/// Tokio ASync Socket`
|
||||
pub type TokioUdpSocket = TkUdpSocket;
|
||||
|
||||
pub(crate) type TokioUdpSocket = TkUdpSocket;
|
||||
impl AsyncSocket for TokioUdpSocket {
|
||||
fn from_std(socket: UdpSocket) -> std::io::Result<Self> {
|
||||
socket.set_nonblocking(true)?;
|
||||
|
@@ -31,6 +31,7 @@ pub struct Timer<T> {
|
||||
}
|
||||
|
||||
/// Builder interface to homogenize the different implementations
|
||||
#[allow(unreachable_pub)] // Users should not depend on this.
|
||||
pub trait Builder: Send + Unpin + 'static {
|
||||
/// Creates a timer that emits an event once at the given time instant.
|
||||
fn at(instant: Instant) -> Self;
|
||||
@@ -43,7 +44,7 @@ pub trait Builder: Send + Unpin + 'static {
|
||||
}
|
||||
|
||||
#[cfg(feature = "async-io")]
|
||||
pub mod asio {
|
||||
pub(crate) mod asio {
|
||||
use super::*;
|
||||
use async_io::Timer as AsioTimer;
|
||||
use futures::Stream;
|
||||
@@ -53,8 +54,7 @@ pub mod asio {
|
||||
};
|
||||
|
||||
/// Async Timer
|
||||
pub type AsyncTimer = Timer<AsioTimer>;
|
||||
|
||||
pub(crate) type AsyncTimer = Timer<AsioTimer>;
|
||||
impl Builder for AsyncTimer {
|
||||
fn at(instant: Instant) -> Self {
|
||||
Self {
|
||||
@@ -85,7 +85,7 @@ pub mod asio {
|
||||
}
|
||||
|
||||
#[cfg(feature = "tokio")]
|
||||
pub mod tokio {
|
||||
pub(crate) mod tokio {
|
||||
use super::*;
|
||||
use ::tokio::time::{self, Instant as TokioInstant, Interval, MissedTickBehavior};
|
||||
use futures::Stream;
|
||||
@@ -95,8 +95,7 @@ pub mod tokio {
|
||||
};
|
||||
|
||||
/// Tokio wrapper
|
||||
pub type TokioTimer = Timer<Interval>;
|
||||
|
||||
pub(crate) type TokioTimer = Timer<Interval>;
|
||||
impl Builder for TokioTimer {
|
||||
fn at(instant: Instant) -> Self {
|
||||
// Taken from: https://docs.rs/async-io/1.7.0/src/async_io/lib.rs.html#91
|
||||
|
Reference in New Issue
Block a user