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

Properly set rustdoc attr. Closing #2950 #2975

Closed
Closed
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
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ rsa = ["libp2p-core/rsa"]
ecdsa = ["libp2p-core/ecdsa"]
serde = ["libp2p-core/serde", "libp2p-kad?/serde", "libp2p-gossipsub?/serde"]

[package.metadata.docs.rs]
all-features = true

[dependencies]
bytes = "1"
futures = "0.3.1"
Expand Down Expand Up @@ -201,3 +198,8 @@ required-features = ["full"]
[[example]]
name = "distributed-key-value-store"
required-features = ["full"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a newline so this icon goes away? 😇

5 changes: 5 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ serde = ["multihash/serde-codec", "dep:serde"]
[[bench]]
name = "peer_id"
harness = false

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
17 changes: 15 additions & 2 deletions core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
//! All key types have functions to enable conversion to/from their binary representations.

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
Comment on lines 35 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether it is worth it using a macro to remove this duplication. I can see a couple of downsides, not sure how critical they actually are:

  • Less flexibility in specifying the rendered feature combination.
  • Increased compile-time because the macro needs to run before all our other crates.
  • Newcomers will be unfamiliar with the solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just found doc_auto_cfg which seems to be useful to automate all of this. The magic line seems to be:

#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

See https://github.com/tokio-rs/valuable/pull/80/files for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try that, although if we decide to go with this, it would be much easier for me to do that in a new branch (and pull request).

pub mod ecdsa;
pub mod ed25519;
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "rsa", not(target_arch = "wasm32")))))]
pub mod rsa;
#[cfg(feature = "secp256k1")]
#[cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]
pub mod secp256k1;

pub mod error;
Expand Down Expand Up @@ -70,12 +73,15 @@ pub enum Keypair {
Ed25519(ed25519::Keypair),
/// An RSA keypair.
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "rsa", not(target_arch = "wasm32")))))]
Rsa(rsa::Keypair),
/// A Secp256k1 keypair.
#[cfg(feature = "secp256k1")]
#[cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]
Secp256k1(secp256k1::Keypair),
/// An ECDSA keypair.
#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
Ecdsa(ecdsa::Keypair),
}

Expand All @@ -87,12 +93,14 @@ impl Keypair {

/// Generate a new Secp256k1 keypair.
#[cfg(feature = "secp256k1")]
#[cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]
pub fn generate_secp256k1() -> Keypair {
Keypair::Secp256k1(secp256k1::Keypair::generate())
}

/// Generate a new ECDSA keypair.
#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
pub fn generate_ecdsa() -> Keypair {
Keypair::Ecdsa(ecdsa::Keypair::generate())
}
Expand All @@ -102,6 +110,7 @@ impl Keypair {
///
/// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "rsa", not(target_arch = "wasm32")))))]
pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result<Keypair, DecodingError> {
rsa::Keypair::from_pkcs8(pkcs8_der).map(Keypair::Rsa)
}
Expand All @@ -111,6 +120,7 @@ impl Keypair {
///
/// [RFC5915]: https://tools.ietf.org/html/rfc5915
#[cfg(feature = "secp256k1")]
#[cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]
pub fn secp256k1_from_der(der: &mut [u8]) -> Result<Keypair, DecodingError> {
secp256k1::SecretKey::from_der(der)
.map(|sk| Keypair::Secp256k1(secp256k1::Keypair::from(sk)))
Expand Down Expand Up @@ -218,14 +228,17 @@ impl zeroize::Zeroize for keys_proto::PrivateKey {
pub enum PublicKey {
/// A public Ed25519 key.
Ed25519(ed25519::PublicKey),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
/// A public RSA key.
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "rsa", not(target_arch = "wasm32")))))]
Rsa(rsa::PublicKey),
#[cfg(feature = "secp256k1")]
/// A public Secp256k1 key.
#[cfg(feature = "secp256k1")]
#[cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]
Secp256k1(secp256k1::PublicKey),
/// A public ECDSA key.
#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
Ecdsa(ecdsa::PublicKey),
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
//! define how to upgrade each individual substream to use a protocol.
//! See the `upgrade` module.

#![cfg_attr(docsrs, feature(doc_cfg))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seems like it is a left over from another attempt at solving this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't think so. This turns the required feature gate on. I.e. #[doc(cfg(...))].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, my bad! Thanks for clarifying!


#[allow(clippy::derive_partial_eq_without_eq)]
mod keys_proto {
include!(concat!(env!("OUT_DIR"), "/keys_proto.rs"));
Expand Down
2 changes: 2 additions & 0 deletions core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl From<PeerId> for Vec<u8> {
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for PeerId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -194,6 +195,7 @@ impl Serialize for PeerId {
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for PeerId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
5 changes: 5 additions & 0 deletions misc/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ futures = "0.3.1"
libp2p = { path = "../..", features = ["full"] }
hyper = { version="0.14", features = ["server", "tcp", "http1"] }
tokio = { version = "1", features = ["rt-multi-thread"] }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
18 changes: 14 additions & 4 deletions misc/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
//!
//! See `examples` directory for more.

#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(feature = "dcutr")]
mod dcutr;
#[cfg(feature = "gossipsub")]
Expand Down Expand Up @@ -97,42 +99,50 @@ pub trait Recorder<Event> {
}

#[cfg(feature = "dcutr")]
#[cfg_attr(docsrs, doc(cfg(feature = "dcutr")))]
impl Recorder<libp2p_dcutr::behaviour::Event> for Metrics {
fn record(&self, event: &libp2p_dcutr::behaviour::Event) {
self.dcutr.record(event)
}
}

#[cfg(feature = "gossipsub")]
#[cfg(not(target_os = "unknown"))]
#[cfg(all(feature = "gossipsub", not(target_os = "unknown")))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "gossipsub", not(target_os = "unknown"))))
)]
impl Recorder<libp2p_gossipsub::GossipsubEvent> for Metrics {
fn record(&self, event: &libp2p_gossipsub::GossipsubEvent) {
self.gossipsub.record(event)
}
}

#[cfg(feature = "identify")]
#[cfg_attr(docsrs, doc(cfg(feature = "identify")))]
impl Recorder<libp2p_identify::Event> for Metrics {
fn record(&self, event: &libp2p_identify::Event) {
self.identify.record(event)
}
}

#[cfg(feature = "kad")]
#[cfg_attr(docsrs, doc(cfg(feature = "kad")))]
impl Recorder<libp2p_kad::KademliaEvent> for Metrics {
fn record(&self, event: &libp2p_kad::KademliaEvent) {
self.kad.record(event)
}
}

#[cfg(feature = "ping")]
#[cfg(any(feature = "ping", docsrs))]
#[cfg_attr(docsrs, doc(cfg(feature = "ping")))]
impl Recorder<libp2p_ping::Event> for Metrics {
fn record(&self, event: &libp2p_ping::Event) {
self.ping.record(event)
}
}

#[cfg(feature = "relay")]
#[cfg(any(feature = "relay", docsrs))]
#[cfg_attr(docsrs, doc(cfg(feature = "relay")))]
impl Recorder<libp2p_relay::v2::relay::Event> for Metrics {
fn record(&self, event: &libp2p_relay::v2::relay::Event) {
self.relay.record(event)
Expand Down
1 change: 1 addition & 0 deletions muxers/yamux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ parking_lot = "0.12"
thiserror = "1.0"
yamux = "0.10.0"
log = "0.4"

3 changes: 3 additions & 0 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ derive_builder = "0.11.1"

[build-dependencies]
prost-build = "0.11"

[package.metadata.docs.rs]
all-features = true
3 changes: 3 additions & 0 deletions protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ prost-build = "0.11"

[features]
serde = ["dep:serde", "bytes/serde"]

[package.metadata.docs.rs]
all-features = true
4 changes: 4 additions & 0 deletions protocols/mdns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ required-features = ["async-io"]
name = "use-tokio"
required-features = ["tokio"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
Comment on lines +49 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be worthwhile to just copy-paste this into all manifests? Otherwise, if we add features later we may forget about this.

We can include a link to this documentation to make this a bit easier to understand.

4 changes: 4 additions & 0 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,21 @@ use std::collections::hash_map::{Entry, HashMap};
use std::{cmp, fmt, io, net::IpAddr, pin::Pin, task::Context, task::Poll, time::Instant};

#[cfg(feature = "async-io")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-io")))]
use crate::behaviour::{socket::asio::AsyncUdpSocket, timer::asio::AsyncTimer};

/// The type of a [`GenMdns`] using the `async-io` implementation.
#[cfg(feature = "async-io")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-io")))]
pub type Mdns = GenMdns<AsyncUdpSocket, AsyncTimer>;

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
use crate::behaviour::{socket::tokio::TokioUdpSocket, timer::tokio::TokioTimer};

/// The type of a [`GenMdns`] using the `tokio` implementation.
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub type TokioMdns = GenMdns<TokioUdpSocket, TokioTimer>;

/// A `NetworkBehaviour` for mDNS. Automatically discovers peers on the local network and adds
Expand Down
2 changes: 2 additions & 0 deletions protocols/mdns/src/behaviour/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub trait AsyncSocket: Unpin + Send + 'static {
}

#[cfg(feature = "async-io")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-io")))]
pub mod asio {
use super::*;
use async_io::Async;
Expand Down Expand Up @@ -92,6 +93,7 @@ pub mod asio {
}

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub mod tokio {
use super::*;
use ::tokio::{io::ReadBuf, net::UdpSocket as TkUdpSocket};
Expand Down
2 changes: 2 additions & 0 deletions protocols/mdns/src/behaviour/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub trait Builder: Send + Unpin + 'static {
}

#[cfg(feature = "async-io")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-io")))]
pub mod asio {
use super::*;
use async_io::Timer as AsioTimer;
Expand Down Expand Up @@ -82,6 +83,7 @@ pub mod asio {
}

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub mod tokio {
use super::*;
use ::tokio::time::{self, Instant as TokioInstant, Interval, MissedTickBehavior};
Expand Down
5 changes: 5 additions & 0 deletions protocols/mdns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
//! implements the `NetworkBehaviour` trait. This struct will automatically discover other
//! libp2p nodes on the local network.
//!

#![cfg_attr(docsrs, feature(doc_cfg))]

use lazy_static::lazy_static;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::time::Duration;
Expand All @@ -38,9 +41,11 @@ mod behaviour;
pub use crate::behaviour::{GenMdns, MdnsEvent};

#[cfg(feature = "async-io")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-io")))]
pub use crate::behaviour::Mdns;

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub use crate::behaviour::TokioMdns;

/// The DNS service name for all libp2p peers used to query for addresses.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#![doc(html_logo_url = "https://libp2p.io/img/logo_small.png")]
#![doc(html_favicon_url = "https://libp2p.io/img/favicon.png")]
#![cfg_attr(docsrs, feature(doc_cfg))]

pub use bytes;
pub use futures;
Expand Down
4 changes: 4 additions & 0 deletions swarm-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ libp2p = { path = "..", features = ["full"] }
either = "1.6.0"
futures = "0.3.1"
void = "1"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
5 changes: 5 additions & 0 deletions swarm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ async-std = { version = "1.6.2", features = ["attributes"] }
env_logger = "0.9"
libp2p = { path = "..", features = ["full"] }
quickcheck = { package = "quickcheck-ext", path = "../misc/quickcheck-ext" }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
5 changes: 5 additions & 0 deletions transports/dns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ tokio = ["trust-dns-resolver/tokio-runtime"]
# available for `tokio`.
tokio-dns-over-rustls = ["tokio", "trust-dns-resolver/dns-over-rustls"]
tokio-dns-over-https-rustls = ["tokio", "trust-dns-resolver/dns-over-https-rustls"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
6 changes: 6 additions & 0 deletions transports/dns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
//!
//![trust-dns-resolver]: https://docs.rs/trust-dns-resolver/latest/trust_dns_resolver/#dns-over-tls-and-dns-over-https

#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(feature = "async-std")]
use async_std_resolver::{AsyncStdConnection, AsyncStdConnectionProvider};
use futures::{future::BoxFuture, prelude::*};
Expand Down Expand Up @@ -107,11 +109,13 @@ const MAX_TXT_RECORDS: usize = 16;
/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses
/// using `async-std` for all async I/O.
#[cfg(feature = "async-std")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std")))]
pub type DnsConfig<T> = GenDnsConfig<T, AsyncStdConnection, AsyncStdConnectionProvider>;

/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses
/// using `tokio` for all async I/O.
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub type TokioDnsConfig<T> = GenDnsConfig<T, TokioConnection, TokioConnectionProvider>;

/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses.
Expand All @@ -127,6 +131,7 @@ where
}

#[cfg(feature = "async-std")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std")))]
impl<T> DnsConfig<T> {
/// Creates a new [`DnsConfig`] from the OS's DNS configuration and defaults.
pub async fn system(inner: T) -> Result<DnsConfig<T>, io::Error> {
Expand All @@ -148,6 +153,7 @@ impl<T> DnsConfig<T> {
}

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl<T> TokioDnsConfig<T> {
/// Creates a new [`TokioDnsConfig`] from the OS's DNS configuration and defaults.
pub fn system(inner: T) -> Result<TokioDnsConfig<T>, io::Error> {
Expand Down
5 changes: 5 additions & 0 deletions transports/tcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ async-io = ["async-io-crate"]
async-std = { version = "1.6.5", features = ["attributes"] }
tokio-crate = { package = "tokio", version = "1.0.1", default-features = false, features = ["net", "rt", "macros"] }
env_logger = "0.9.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
Loading