Skip to content

Commit 88f6bae

Browse files
committed
Merge #353: Documented features
18f74d5 Clarify what does "less security" mean (Martin Habovstiak) 94c55b4 Fixed typos/grammar mistakes (Martin Habovštiak) 1bf0552 Documented features (Martin Habovstiak) Pull request description: This documents the Cargo features making sure docs.rs shows warning for feature-gated items. They are also explicitly spelled out in the crate documentation. The PR is similar in spirit to rust-bitcoin/rust-bitcoin#633 ACKs for top commit: apoelstra: ACK 18f74d5 Tree-SHA512: 8aac3fc5fd8ee887d6b13606d66b3d11ce44662afb92228c4f8da6169e3f70ac6a005b328f427a91d307f8d36d091dcf24bfe4d17dfc034d02b578258719a90a
2 parents f7baa53 + 18f74d5 commit 88f6bae

File tree

9 files changed

+85
-12
lines changed

9 files changed

+85
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ autoexamples = false # Remove when edition 2018 https://github.com/rust-lang/car
1515
# Should make docs.rs show all functions, even those behind non-default features
1616
[package.metadata.docs.rs]
1717
features = [ "rand", "rand-std", "serde", "recovery" ]
18+
rustdoc-args = ["--cfg", "docsrs"]
1819

1920
[features]
2021
unstable = ["recovery", "rand-std"]

secp256k1-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ links = "rustsecp256k1_v0_4_1"
1717
# Should make docs.rs show all functions, even those behind non-default features
1818
[package.metadata.docs.rs]
1919
features = [ "recovery", "lowmemory" ]
20+
rustdoc-args = ["--cfg", "docsrs"]
2021

2122
[build-dependencies]
2223
cc = "1.0.28"

secp256k1-sys/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#![deny(unused_mut)]
2424

2525
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
26+
#![cfg_attr(docsrs, feature(doc_cfg))]
27+
2628
#[cfg(any(test, feature = "std"))]
2729
extern crate core;
2830

@@ -34,6 +36,7 @@ mod macros;
3436
pub mod types;
3537

3638
#[cfg(feature = "recovery")]
39+
#[cfg_attr(docsrs, doc(cfg(feature = "recovery")))]
3740
pub mod recovery;
3841

3942
use core::{slice, ptr};
@@ -524,6 +527,7 @@ extern "C" {
524527
// In: flags: which parts of the context to initialize.
525528
#[no_mangle]
526529
#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
530+
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))]
527531
pub unsafe extern "C" fn rustsecp256k1_v0_4_1_context_create(flags: c_uint) -> *mut Context {
528532
use core::mem;
529533
use std::alloc;
@@ -543,6 +547,7 @@ pub unsafe extern "C" fn rustsecp256k1_v0_4_1_context_create(flags: c_uint) -> *
543547
}
544548

545549
#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
550+
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))]
546551
pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context {
547552
rustsecp256k1_v0_4_1_context_create(flags)
548553
}
@@ -555,6 +560,7 @@ pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context {
555560
///
556561
#[no_mangle]
557562
#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
563+
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))]
558564
pub unsafe extern "C" fn rustsecp256k1_v0_4_1_context_destroy(ctx: *mut Context) {
559565
use std::alloc;
560566
secp256k1_context_preallocated_destroy(ctx);
@@ -565,6 +571,7 @@ pub unsafe extern "C" fn rustsecp256k1_v0_4_1_context_destroy(ctx: *mut Context)
565571
}
566572

567573
#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
574+
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))]
568575
pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
569576
rustsecp256k1_v0_4_1_context_destroy(ctx)
570577
}

src/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ use Error;
66
use Secp256k1;
77

88
#[cfg(any(feature = "std", feature = "alloc"))]
9+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
910
pub use self::alloc_only::*;
1011

1112
#[cfg(feature = "global-context-less-secure")]
13+
#[cfg_attr(docsrs, doc(cfg(feature = "global-context-less-secure")))]
1214
/// Module implementing a singleton pattern for a global `Secp256k1` context
1315
pub mod global {
1416
#[cfg(feature = "global-context")]
@@ -94,6 +96,7 @@ mod private {
9496
}
9597

9698
#[cfg(any(feature = "std", feature = "alloc"))]
99+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
97100
mod alloc_only {
98101
#[cfg(feature = "std")]
99102
use std::alloc;
@@ -108,12 +111,15 @@ mod alloc_only {
108111
const ALIGN_TO: usize = ::core::mem::align_of::<AlignedType>();
109112

110113
/// Represents the set of capabilities needed for signing.
114+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
111115
pub enum SignOnly {}
112116

113117
/// Represents the set of capabilities needed for verification.
118+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
114119
pub enum VerifyOnly {}
115120

116121
/// Represents the set of all capabilities.
122+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
117123
pub enum All {}
118124

119125
impl Signing for SignOnly {}

src/ecdsa/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ffi::CPtr;
99
mod recovery;
1010

1111
#[cfg(feature = "recovery")]
12+
#[cfg_attr(docsrs, doc(cfg(feature = "recovery")))]
1213
pub use self::recovery::{RecoveryId, RecoverableSignature};
1314

1415
/// An ECDSA signature
@@ -279,6 +280,7 @@ impl From<ffi::Signature> for Signature {
279280
}
280281

281282
#[cfg(feature = "serde")]
283+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
282284
impl ::serde::Serialize for Signature {
283285
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
284286
if s.is_human_readable() {
@@ -290,6 +292,7 @@ impl ::serde::Serialize for Signature {
290292
}
291293

292294
#[cfg(feature = "serde")]
295+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
293296
impl<'de> ::serde::Deserialize<'de> for Signature {
294297
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
295298
if d.is_human_readable() {

src/key.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ fn random_32_bytes<R: Rng + ?Sized>(rng: &mut R) -> [u8; 32] {
9595
}
9696

9797
impl SecretKey {
98-
/// Creates a new random secret key. Requires compilation with the "rand" feature.
98+
/// Generates a new random secret key.
9999
#[inline]
100100
#[cfg(any(test, feature = "rand"))]
101+
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
101102
pub fn new<R: Rng + ?Sized>(rng: &mut R) -> SecretKey {
102103
let mut data = random_32_bytes(rng);
103104
unsafe {
@@ -221,6 +222,7 @@ impl SecretKey {
221222
}
222223

223224
#[cfg(feature = "serde")]
225+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
224226
impl ::serde::Serialize for SecretKey {
225227
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
226228
if s.is_human_readable() {
@@ -233,6 +235,7 @@ impl ::serde::Serialize for SecretKey {
233235
}
234236

235237
#[cfg(feature = "serde")]
238+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
236239
impl<'de> ::serde::Deserialize<'de> for SecretKey {
237240
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
238241
if d.is_human_readable() {
@@ -467,6 +470,7 @@ impl From<ffi::PublicKey> for PublicKey {
467470
}
468471

469472
#[cfg(feature = "serde")]
473+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
470474
impl ::serde::Serialize for PublicKey {
471475
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
472476
if s.is_human_readable() {
@@ -478,6 +482,7 @@ impl ::serde::Serialize for PublicKey {
478482
}
479483

480484
#[cfg(feature = "serde")]
485+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
481486
impl<'de> ::serde::Deserialize<'de> for PublicKey {
482487
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error> {
483488
if d.is_human_readable() {
@@ -586,9 +591,10 @@ impl KeyPair {
586591
}
587592
}
588593

589-
/// Creates a new random secret key. Requires compilation with the "rand" feature.
594+
/// Generates a new random secret key.
590595
#[inline]
591596
#[cfg(any(test, feature = "rand"))]
597+
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
592598
pub fn new<R: ::rand::Rng + ?Sized, C: Signing>(secp: &Secp256k1<C>, rng: &mut R) -> KeyPair {
593599
let mut random_32_bytes = || {
594600
let mut ret = [0u8; 32];
@@ -685,6 +691,7 @@ impl str::FromStr for KeyPair {
685691
}
686692

687693
#[cfg(feature = "serde")]
694+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
688695
impl ::serde::Serialize for KeyPair {
689696
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
690697
if s.is_human_readable() {
@@ -698,6 +705,7 @@ impl ::serde::Serialize for KeyPair {
698705
}
699706

700707
#[cfg(feature = "serde")]
708+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
701709
impl<'de> ::serde::Deserialize<'de> for KeyPair {
702710
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
703711
if d.is_human_readable() {
@@ -954,6 +962,7 @@ impl From<::key::PublicKey> for XOnlyPublicKey {
954962
}
955963

956964
#[cfg(feature = "serde")]
965+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
957966
impl ::serde::Serialize for XOnlyPublicKey {
958967
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
959968
if s.is_human_readable() {
@@ -965,6 +974,7 @@ impl ::serde::Serialize for XOnlyPublicKey {
965974
}
966975

967976
#[cfg(feature = "serde")]
977+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
968978
impl<'de> ::serde::Deserialize<'de> for XOnlyPublicKey {
969979
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
970980
if d.is_human_readable() {

src/lib.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@
110110
//! Observe that the same code using, say [`signing_only`](struct.Secp256k1.html#method.signing_only)
111111
//! to generate a context would simply not compile.
112112
//!
113+
//! ## Crate features/optional dependencies
114+
//!
115+
//! This crate provides the following opt-in Cargo features:
116+
//!
117+
//! * `std` - use standard Rust library, enabled by default.
118+
//! * `alloc` - use the `alloc` standard Rust library to provide heap allocations.
119+
//! * `rand` - use `rand` library to provide random generator (e.g. to generate keys).
120+
//! * `rand-std` - use `rand` library with its `std` feature enabled. (Implies `rand`.)
121+
//! * `recovery` - enable functions that can compute the public key from signature.
122+
//! * `lowmemory` - optimize the library for low-memory environments.
123+
//! * `global-context` - enable use of global secp256k1 context. (Implies `std`, `rand-std` and
124+
//! `global-context-less-secure`.)
125+
//! * `global-context-less-secure` - enables global context without extra sidechannel protection.
126+
//! * `serde` - implements serialization and deserialization for types in this crate using `serde`.
127+
//! * `bitcoin_hashes` - enables interaction with the `bitcoin-hashes` crate (e.g. conversions).
113128
114129
// Coding conventions
115130
#![deny(non_upper_case_globals)]
@@ -121,21 +136,35 @@
121136

122137
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
123138
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
139+
#![cfg_attr(docsrs, feature(doc_cfg))]
124140

125141
#[macro_use]
126142
pub extern crate secp256k1_sys;
127143
pub use secp256k1_sys as ffi;
128144

129-
#[cfg(feature = "bitcoin_hashes")] pub extern crate bitcoin_hashes as hashes;
130-
#[cfg(all(test, feature = "unstable"))] extern crate test;
131-
#[cfg(any(test, feature = "rand"))] pub extern crate rand;
132-
#[cfg(any(test))] extern crate rand_core;
133-
#[cfg(feature = "serde")] pub extern crate serde;
134-
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
135-
#[cfg(any(test, feature = "rand"))] use rand::Rng;
136-
#[cfg(any(test, feature = "std"))] extern crate core;
137-
#[cfg(all(test, target_arch = "wasm32"))] extern crate wasm_bindgen_test;
138-
#[cfg(feature = "alloc")] extern crate alloc;
145+
#[cfg(feature = "bitcoin_hashes")]
146+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
147+
pub extern crate bitcoin_hashes as hashes;
148+
#[cfg(all(test, feature = "unstable"))]
149+
extern crate test;
150+
#[cfg(any(test, feature = "rand"))]
151+
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
152+
pub extern crate rand;
153+
#[cfg(any(test))]
154+
extern crate rand_core;
155+
#[cfg(feature = "serde")]
156+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
157+
pub extern crate serde;
158+
#[cfg(all(test, feature = "serde"))]
159+
extern crate serde_test;
160+
#[cfg(any(test, feature = "rand"))]
161+
use rand::Rng;
162+
#[cfg(any(test, feature = "std"))]
163+
extern crate core;
164+
#[cfg(all(test, target_arch = "wasm32"))]
165+
extern crate wasm_bindgen_test;
166+
#[cfg(feature = "alloc")]
167+
extern crate alloc;
139168

140169

141170
#[macro_use]
@@ -163,6 +192,7 @@ use core::{mem, fmt, str};
163192
use ffi::{CPtr, types::AlignedType};
164193

165194
#[cfg(feature = "global-context-less-secure")]
195+
#[cfg_attr(docsrs, doc(cfg(feature = "global-context-less-secure")))]
166196
pub use context::global::SECP256K1;
167197

168198
#[cfg(feature = "bitcoin_hashes")]
@@ -196,20 +226,23 @@ pub trait ThirtyTwoByteHash {
196226
}
197227

198228
#[cfg(feature = "bitcoin_hashes")]
229+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
199230
impl ThirtyTwoByteHash for hashes::sha256::Hash {
200231
fn into_32(self) -> [u8; 32] {
201232
self.into_inner()
202233
}
203234
}
204235

205236
#[cfg(feature = "bitcoin_hashes")]
237+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
206238
impl ThirtyTwoByteHash for hashes::sha256d::Hash {
207239
fn into_32(self) -> [u8; 32] {
208240
self.into_inner()
209241
}
210242
}
211243

212244
#[cfg(feature = "bitcoin_hashes")]
245+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
213246
impl<T: hashes::sha256t::Tag> ThirtyTwoByteHash for hashes::sha256t::Hash<T> {
214247
fn into_32(self) -> [u8; 32] {
215248
self.into_inner()
@@ -256,6 +289,7 @@ impl Message {
256289
/// assert_eq!(m1, m2);
257290
/// ```
258291
#[cfg(feature = "bitcoin_hashes")]
292+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
259293
pub fn from_hashed_data<H: ThirtyTwoByteHash + hashes::Hash>(data: &[u8]) -> Self {
260294
<H as hashes::Hash>::hash(data).into()
261295
}
@@ -316,6 +350,7 @@ impl fmt::Display for Error {
316350
}
317351

318352
#[cfg(feature = "std")]
353+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
319354
impl std::error::Error for Error {}
320355

321356

@@ -374,6 +409,7 @@ impl<C: Context> Secp256k1<C> {
374409
/// see comment in libsecp256k1 commit d2275795f by Gregory Maxwell. Requires
375410
/// compilation with "rand" feature.
376411
#[cfg(any(test, feature = "rand"))]
412+
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
377413
pub fn randomize<R: Rng + ?Sized>(&mut self, rng: &mut R) {
378414
let mut seed = [0u8; 32];
379415
rng.fill_bytes(&mut seed);
@@ -406,6 +442,7 @@ impl<C: Signing> Secp256k1<C> {
406442
/// with the "rand" feature.
407443
#[inline]
408444
#[cfg(any(test, feature = "rand"))]
445+
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
409446
pub fn generate_keypair<R: Rng + ?Sized>(&self, rng: &mut R)
410447
-> (key::SecretKey, key::PublicKey) {
411448
let sk = key::SecretKey::new(rng);

0 commit comments

Comments
 (0)