Skip to content

Commit 5f1f0e1

Browse files
Rollup merge of rust-lang#38334 - frewsxcv:BuildHasherDefault, r=GuillaumeGomez
Rewrite, improve documentation for `core::hash::BuildHasherDefault`. Fixes rust-lang#31242.
2 parents 269978e + ce1fbad commit 5f1f0e1

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/libcore/hash/mod.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,44 @@ pub trait BuildHasher {
255255
fn build_hasher(&self) -> Self::Hasher;
256256
}
257257

258-
/// A structure which implements `BuildHasher` for all `Hasher` types which also
259-
/// implement `Default`.
258+
/// The `BuildHasherDefault` structure is used in scenarios where one has a
259+
/// type that implements [`Hasher`] and [`Default`], but needs that type to
260+
/// implement [`BuildHasher`].
260261
///
261-
/// This struct is 0-sized and does not need construction.
262+
/// This structure is zero-sized and does not need construction.
263+
///
264+
/// # Examples
265+
///
266+
/// Using `BuildHasherDefault` to specify a custom [`BuildHasher`] for
267+
/// [`HashMap`]:
268+
///
269+
/// ```
270+
/// use std::collections::HashMap;
271+
/// use std::hash::{BuildHasherDefault, Hasher};
272+
///
273+
/// #[derive(Default)]
274+
/// struct MyHasher;
275+
///
276+
/// impl Hasher for MyHasher {
277+
/// fn write(&mut self, bytes: &[u8]) {
278+
/// // Your hashing algorithm goes here!
279+
/// unimplemented!()
280+
/// }
281+
///
282+
/// fn finish(&self) -> u64 {
283+
/// // Your hashing algorithm goes here!
284+
/// unimplemented!()
285+
/// }
286+
/// }
287+
///
288+
/// type MyBuildHasher = BuildHasherDefault<SomeHasher>;
289+
///
290+
/// let hash_map = HashMap::<u32, u32, SomeBuidHasher>::default();
291+
/// ```
292+
///
293+
/// [`BuildHasher`]: trait.BuildHasher.html
294+
/// [`Default`]: ../default/trait.Default.html
295+
/// [`Hasher`]: trait.Hasher.html
262296
#[stable(since = "1.7.0", feature = "build_hasher")]
263297
pub struct BuildHasherDefault<H>(marker::PhantomData<H>);
264298

0 commit comments

Comments
 (0)