Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit b8d135b

Browse files
committed
Merge #145: Docs improvements
ddfd846 Audit rustdocs (Tobin Harding) 2d4a89e Improve module level docs (Tobin Harding) Pull request description: Do crate wide docs improvements. ACKs for top commit: apoelstra: ACK ddfd846 Tree-SHA512: 606bd5efae32472666c19c73e95f8cdefcf8ee55ab20b0dc44297f3de0da304c3b4285d20673268707af7f9b6b3718a9840f72489f287e5a6366b838ec7f4659
2 parents 7abde5b + ddfd846 commit b8d135b

File tree

16 files changed

+118
-99
lines changed

16 files changed

+118
-99
lines changed

src/cmp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/// volatile read. This should remain stable across compiler upgrades, but is much slower.
1212
///
1313
/// As of rust 1.31.0 disassembly looks completely within reason for this, see
14-
/// https://godbolt.org/z/mMbGQv
14+
/// <https://godbolt.org/z/mMbGQv>.
1515
pub fn fixed_time_eq(a: &[u8], b: &[u8]) -> bool {
1616
assert!(a.len() == b.len());
1717
let count = a.len();

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15-
//! # Error Type
15+
//! Crate error type.
1616
//!
1717
1818
use core::fmt;
1919

20-
/// [bitcoin_hashes] error.
20+
/// Crate error type.
2121
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2222
pub enum Error {
2323
/// Tried to create a fixed-length hash from a slice with the wrong size (expected, got).

src/hash160.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
// was written entirely by Andrew Poelstra, who is re-licensing its
1818
// contents here as CC0.
1919

20-
//! # HASH160 (SHA256 then RIPEMD160)
20+
//! HASH160 (SHA256 then RIPEMD160) implementation.
21+
//!
2122
2223
use core::str;
2324
use core::ops::Index;
@@ -28,7 +29,7 @@ use ripemd160;
2829
use Hash as HashTrait;
2930
use Error;
3031

31-
/// Output of the Bitcoin HASH160 hash function
32+
/// Output of the Bitcoin HASH160 hash function.
3233
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
3334
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3435
#[repr(transparent)]

src/hex.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15-
//! # Hex encoding and decoding
15+
//! Hex encoding and decoding.
1616
//!
1717
1818
#[cfg(any(feature = "std", feature = "alloc"))]
@@ -28,14 +28,14 @@ use core2::io;
2828
use core::{fmt, str};
2929
use Hash;
3030

31-
/// Hex decoding error
31+
/// Hex decoding error.
3232
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3333
pub enum Error {
34-
/// non-hexadecimal character
34+
/// Non-hexadecimal character.
3535
InvalidChar(u8),
36-
/// purported hex string had odd length
36+
/// Purported hex string had odd length.
3737
OddLengthString(usize),
38-
/// tried to parse fixed-length hash from a string with the wrong type (expected, got)
38+
/// Tried to parse fixed-length hash from a string with the wrong type (expected, got).
3939
InvalidLength(usize, usize),
4040
}
4141

@@ -49,29 +49,29 @@ impl fmt::Display for Error {
4949
}
5050
}
5151

52-
/// Trait for objects that can be serialized as hex strings
52+
/// Trait for objects that can be serialized as hex strings.
5353
#[cfg(any(test, feature = "std", feature = "alloc"))]
5454
pub trait ToHex {
55-
/// Hex representation of the object
55+
/// Converts to a hexadecimal representation of the object.
5656
fn to_hex(&self) -> String;
5757
}
5858

59-
/// Trait for objects that can be deserialized from hex strings
59+
/// Trait for objects that can be deserialized from hex strings.
6060
pub trait FromHex: Sized {
61-
/// Produce an object from a byte iterator
61+
/// Produces an object from a byte iterator.
6262
fn from_byte_iter<I>(iter: I) -> Result<Self, Error>
6363
where
6464
I: Iterator<Item = Result<u8, Error>> + ExactSizeIterator + DoubleEndedIterator;
6565

66-
/// Produce an object from a hex string
66+
/// Produces an object from a hex string.
6767
fn from_hex(s: &str) -> Result<Self, Error> {
6868
Self::from_byte_iter(HexIterator::new(s)?)
6969
}
7070
}
7171

7272
#[cfg(any(test, feature = "std", feature = "alloc"))]
7373
impl<T: fmt::LowerHex> ToHex for T {
74-
/// Outputs the hash in hexadecimal form
74+
/// Outputs the hash in hexadecimal form.
7575
fn to_hex(&self) -> String {
7676
format!("{:x}", self)
7777
}
@@ -100,8 +100,11 @@ pub struct HexIterator<'a> {
100100
}
101101

102102
impl<'a> HexIterator<'a> {
103-
/// Constructs a new `HexIterator` from a string slice. If the string is of
104-
/// odd length it returns an error.
103+
/// Constructs a new `HexIterator` from a string slice.
104+
///
105+
/// # Errors
106+
///
107+
/// If the input string is of odd length.
105108
pub fn new(s: &'a str) -> Result<HexIterator<'a>, Error> {
106109
if s.len() % 2 != 0 {
107110
Err(Error::OddLengthString(s.len()))
@@ -165,8 +168,9 @@ impl<'a> DoubleEndedIterator for HexIterator<'a> {
165168

166169
impl<'a> ExactSizeIterator for HexIterator<'a> {}
167170

168-
/// Output hex into an object implementing `fmt::Write`, which is usually more
169-
/// efficient than going through a `String` using `ToHex`.
171+
/// Outputs hex into an object implementing `fmt::Write`.
172+
///
173+
/// This is usually more efficient than going through a `String` using [`ToHex`].
170174
pub fn format_hex(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
171175
let prec = f.precision().unwrap_or(2 * data.len());
172176
let width = f.width().unwrap_or(2 * data.len());
@@ -182,8 +186,9 @@ pub fn format_hex(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
182186
Ok(())
183187
}
184188

185-
/// Output hex in reverse order; used for Sha256dHash whose standard hex encoding
186-
/// has the bytes reversed.
189+
/// Outputs hex in reverse order.
190+
///
191+
/// Used for `sha256d::Hash` whose standard hex encoding has the bytes reversed.
187192
pub fn format_hex_reverse(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
188193
let prec = f.precision().unwrap_or(2 * data.len());
189194
let width = f.width().unwrap_or(2 * data.len());

src/hmac.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
// was written entirely by Andrew Poelstra, who is re-licensing its
1818
// contents here as CC0.
1919

20-
//! # HMAC support
20+
//! Hash-based Message Authentication Code (HMAC).
21+
//!
2122
2223
use core::{borrow, fmt, ops, str};
2324
#[cfg(feature = "serde")]
@@ -41,16 +42,15 @@ impl<T: HashTrait + str::FromStr> str::FromStr for Hmac<T> {
4142
}
4243
}
4344

44-
/// Pair of underlying hash midstates which represent the current state
45-
/// of an `HmacEngine`
45+
/// Pair of underlying hash midstates which represent the current state of an `HmacEngine`.
4646
pub struct HmacMidState<T: HashTrait> {
4747
/// Midstate of the inner hash engine
4848
pub inner: <T::Engine as EngineTrait>::MidState,
4949
/// Midstate of the outer hash engine
5050
pub outer: <T::Engine as EngineTrait>::MidState,
5151
}
5252

53-
/// Pair of underyling hash engines, used for the inner and outer hash of HMAC
53+
/// Pair of underyling hash engines, used for the inner and outer hash of HMAC.
5454
#[derive(Clone)]
5555
pub struct HmacEngine<T: HashTrait> {
5656
iengine: T::Engine,
@@ -64,8 +64,13 @@ impl<T: HashTrait> Default for HmacEngine<T> {
6464
}
6565

6666
impl<T: HashTrait> HmacEngine<T> {
67-
/// Construct a new keyed HMAC with the given key. We only support underlying hashes
68-
/// whose block sizes are ≤ 128 bytes; larger hashes will result in panics.
67+
/// Constructs a new keyed HMAC from `key`.
68+
///
69+
/// We only support underlying hashes whose block sizes are ≤ 128 bytes.
70+
///
71+
/// # Panics
72+
///
73+
/// Larger hashes will result in a panic.
6974
pub fn new(key: &[u8]) -> HmacEngine<T> {
7075
debug_assert!(T::Engine::BLOCK_SIZE <= 128);
7176

@@ -98,8 +103,7 @@ impl<T: HashTrait> HmacEngine<T> {
98103
ret
99104
}
100105

101-
/// A special constructor giving direct access to the underlying
102-
/// "inner" and "outer" engines.
106+
/// A special constructor giving direct access to the underlying "inner" and "outer" engines.
103107
pub fn from_inner_engines(iengine: T::Engine, oengine: T::Engine) -> HmacEngine<T> {
104108
HmacEngine {
105109
iengine: iengine,

src/impls.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15-
//! `std` / `core2` Impls
15+
//! `std` / `core2` Impls.
16+
//!
17+
//! Implementations of traits defined in `std` / `core2` and not in `core`.
1618
//!
17-
//! impls of traits defined in `std` / `core2` and not in `core`
1819
1920
#[cfg(feature = "std")]
2021
use std::{error, io};

src/lib.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15-
//! # Rust Hashes Library
15+
//! Rust hashes library.
1616
//!
1717
//! This is a simple, no-dependency library which implements the hash functions
1818
//! needed by Bitcoin. These are SHA256, SHA256d, and RIPEMD160. As an ancillary
@@ -75,26 +75,26 @@ use core::{borrow, fmt, hash, ops};
7575
pub use hmac::{Hmac, HmacEngine};
7676
pub use error::Error;
7777

78-
/// A hashing engine which bytes can be serialized into
78+
/// A hashing engine which bytes can be serialized into.
7979
pub trait HashEngine: Clone + Default {
80-
/// Byte array representing the internal state of the hash engine
80+
/// Byte array representing the internal state of the hash engine.
8181
type MidState;
8282

8383
/// Outputs the midstate of the hash engine. This function should not be
8484
/// used directly unless you really know what you're doing.
8585
fn midstate(&self) -> Self::MidState;
8686

87-
/// Length of the hash's internal block size, in bytes
87+
/// Length of the hash's internal block size, in bytes.
8888
const BLOCK_SIZE: usize;
8989

90-
/// Add data to the hash engine
90+
/// Add data to the hash engine.
9191
fn input(&mut self, data: &[u8]);
9292

93-
/// Return the number of bytes already n_bytes_hashed(inputted)
93+
/// Return the number of bytes already n_bytes_hashed(inputted).
9494
fn n_bytes_hashed(&self) -> usize;
9595
}
9696

97-
/// Trait which applies to hashes of all types
97+
/// Trait which applies to hashes of all types.
9898
pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
9999
hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex +
100100
ops::Index<ops::RangeFull, Output = [u8]> +
@@ -109,24 +109,24 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
109109
/// any conditions.
110110
type Engine: HashEngine;
111111

112-
/// The byte array that represents the hash internally
112+
/// The byte array that represents the hash internally.
113113
type Inner: hex::FromHex;
114114

115-
/// Construct a new engine
115+
/// Constructs a new engine.
116116
fn engine() -> Self::Engine {
117117
Self::Engine::default()
118118
}
119119

120-
/// Produce a hash from the current state of a given engine
120+
/// Produces a hash from the current state of a given engine.
121121
fn from_engine(e: Self::Engine) -> Self;
122122

123-
/// Length of the hash, in bytes
123+
/// Length of the hash, in bytes.
124124
const LEN: usize;
125125

126-
/// Copies a byte slice into a hash object
126+
/// Copies a byte slice into a hash object.
127127
fn from_slice(sl: &[u8]) -> Result<Self, Error>;
128128

129-
/// Hashes some bytes
129+
/// Hashes some bytes.
130130
fn hash(data: &[u8]) -> Self {
131131
let mut engine = Self::engine();
132132
engine.input(data);
@@ -138,13 +138,13 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
138138
/// true for `Sha256dHash`, so here we are.
139139
const DISPLAY_BACKWARD: bool = false;
140140

141-
/// Unwraps the hash and returns the underlying byte array
141+
/// Unwraps the hash and returns the underlying byte array.
142142
fn into_inner(self) -> Self::Inner;
143143

144-
/// Unwraps the hash and returns a reference to the underlying byte array
144+
/// Unwraps the hash and returns a reference to the underlying byte array.
145145
fn as_inner(&self) -> &Self::Inner;
146146

147-
/// Constructs a hash from the underlying byte array
147+
/// Constructs a hash from the underlying byte array.
148148
fn from_inner(inner: Self::Inner) -> Self;
149149
}
150150

src/ripemd160.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
// was written entirely by Andrew Poelstra, who is re-licensing its
1818
// contents here as CC0.
1919

20-
//! # RIPEMD160
20+
//! RIPEMD160 implementation.
21+
//!
2122
2223
use core::{cmp, str};
2324
use core::ops::Index;
@@ -30,7 +31,7 @@ use util;
3031

3132
const BLOCK_SIZE: usize = 64;
3233

33-
/// Engine to compute RIPEMD160 hash function
34+
/// Engine to compute RIPEMD160 hash function.
3435
#[derive(Clone)]
3536
pub struct HashEngine {
3637
buffer: [u8; BLOCK_SIZE],
@@ -76,7 +77,7 @@ impl EngineTrait for HashEngine {
7677
engine_input_impl!();
7778
}
7879

79-
/// Output of the RIPEMD160 hash function
80+
/// Output of the RIPEMD160 hash function.
8081
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
8182
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
8283
#[repr(transparent)]

src/serde_macros.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15-
//! Macros for serde trait impls, and supporting code
15+
//! Macros for serde trait implementations, and supporting code.
16+
//!
1617
1718
#[cfg(feature = "serde")]
18-
/// Functions used by serde impls of all hashes
19+
/// Functions used by serde impls of all hashes.
1920
pub mod serde_details {
2021
use Error;
2122

@@ -82,7 +83,7 @@ pub mod serde_details {
8283
}
8384
}
8485

85-
/// Default serialization/deserialization methods
86+
/// Default serialization/deserialization methods.
8687
pub trait SerdeHash
8788
where
8889
Self: Sized
@@ -92,13 +93,13 @@ pub mod serde_details {
9293
+ ops::Index<ops::RangeFull, Output = [u8]>,
9394
<Self as FromStr>::Err: fmt::Display,
9495
{
95-
/// Size, in bits, of the hash
96+
/// Size, in bits, of the hash.
9697
const N: usize;
9798

98-
/// helper function to turn a deserialized slice into the correct hash type
99+
/// Helper function to turn a deserialized slice into the correct hash type.
99100
fn from_slice_delegated(sl: &[u8]) -> Result<Self, Error>;
100101

101-
/// serde serialization
102+
/// Do serde serialization.
102103
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
103104
if s.is_human_readable() {
104105
s.collect_str(self)
@@ -107,7 +108,7 @@ pub mod serde_details {
107108
}
108109
}
109110

110-
/// serde deserialization
111+
/// Do serde deserialization.
111112
fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
112113
if d.is_human_readable() {
113114
d.deserialize_str(HexVisitor::<Self>(PhantomData))
@@ -121,7 +122,7 @@ pub mod serde_details {
121122
#[macro_export]
122123
#[cfg(feature = "serde")]
123124
/// Implements `Serialize` and `Deserialize` for a type `$t` which
124-
/// represents a newtype over a byte-slice over length `$len`
125+
/// represents a newtype over a byte-slice over length `$len`.
125126
macro_rules! serde_impl(
126127
($t:ident, $len:expr) => (
127128
impl $crate::serde_macros::serde_details::SerdeHash for $t {
@@ -144,7 +145,7 @@ macro_rules! serde_impl(
144145
}
145146
));
146147

147-
/// Does an "empty" serde implementation for the configuration without serde feature
148+
/// Does an "empty" serde implementation for the configuration without serde feature.
148149
#[macro_export]
149150
#[cfg(not(feature = "serde"))]
150151
macro_rules! serde_impl(

0 commit comments

Comments
 (0)