@@ -481,6 +481,50 @@ pub trait BuildHasher {
481
481
/// ```
482
482
#[ stable( since = "1.7.0" , feature = "build_hasher" ) ]
483
483
fn build_hasher ( & self ) -> Self :: Hasher ;
484
+
485
+ /// Calculates the hash of a single value.
486
+ ///
487
+ /// This is intended as a convenience for code which *consumes* hashes, such
488
+ /// as the implementation of a hash table or in unit tests that check
489
+ /// whether a custom [`Hash`] implementation behaves as expected.
490
+ ///
491
+ /// This must not be used in any code which *creates* hashes, such as in an
492
+ /// implementation of [`Hash`]. The way to create a combined hash of
493
+ /// multiple values is to call [`Hash::hash`] multiple times using the same
494
+ /// [`Hasher`], not to call this method repeatedly and combine the results.
495
+ ///
496
+ /// # Example
497
+ ///
498
+ /// ```
499
+ /// #![feature(build_hasher_simple_hash_one)]
500
+ ///
501
+ /// use std::cmp::{max, min};
502
+ /// use std::hash::{BuildHasher, Hash, Hasher};
503
+ /// struct OrderAmbivalentPair<T: Ord>(T, T);
504
+ /// impl<T: Ord + Hash> Hash for OrderAmbivalentPair<T> {
505
+ /// fn hash<H: Hasher>(&self, hasher: &mut H) {
506
+ /// min(&self.0, &self.1).hash(hasher);
507
+ /// max(&self.0, &self.1).hash(hasher);
508
+ /// }
509
+ /// }
510
+ ///
511
+ /// // Then later, in a `#[test]` for the type...
512
+ /// let bh = std::collections::hash_map::RandomState::new();
513
+ /// assert_eq!(
514
+ /// bh.hash_one(OrderAmbivalentPair(1, 2)),
515
+ /// bh.hash_one(OrderAmbivalentPair(2, 1))
516
+ /// );
517
+ /// assert_eq!(
518
+ /// bh.hash_one(OrderAmbivalentPair(10, 2)),
519
+ /// bh.hash_one(&OrderAmbivalentPair(2, 10))
520
+ /// );
521
+ /// ```
522
+ #[ unstable( feature = "build_hasher_simple_hash_one" , issue = "86161" ) ]
523
+ fn hash_one < T : Hash > ( & self , x : T ) -> u64 {
524
+ let mut hasher = self . build_hasher ( ) ;
525
+ x. hash ( & mut hasher) ;
526
+ hasher. finish ( )
527
+ }
484
528
}
485
529
486
530
/// Used to create a default [`BuildHasher`] instance for types that implement
0 commit comments