@@ -43,6 +43,7 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
4343use rustc_serialize:: opaque:: { FileEncoder , MemDecoder } ;
4444use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
4545use tracing:: debug;
46+ use xxhash_rust:: xxh3;
4647
4748mod caching_source_map_view;
4849pub mod source_map;
@@ -1458,14 +1459,25 @@ pub enum SourceFileHashAlgorithm {
14581459 Md5 ,
14591460 Sha1 ,
14601461 Sha256 ,
1462+ XxHash ,
1463+ }
1464+
1465+ impl SourceFileHashAlgorithm {
1466+ pub fn supported_in_cargo ( & self ) -> bool {
1467+ match self {
1468+ Self :: Md5 | Self :: Sha1 => false ,
1469+ Self :: Sha256 | Self :: XxHash => true ,
1470+ }
1471+ }
14611472}
14621473
14631474impl Display for SourceFileHashAlgorithm {
14641475 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
14651476 f. write_str ( match self {
1466- SourceFileHashAlgorithm :: Md5 => "md5" ,
1467- SourceFileHashAlgorithm :: Sha1 => "sha1" ,
1468- SourceFileHashAlgorithm :: Sha256 => "sha256" ,
1477+ Self :: Md5 => "md5" ,
1478+ Self :: Sha1 => "sha1" ,
1479+ Self :: Sha256 => "sha256" ,
1480+ Self :: XxHash => "xxhash" ,
14691481 } )
14701482 }
14711483}
@@ -1478,6 +1490,7 @@ impl FromStr for SourceFileHashAlgorithm {
14781490 "md5" => Ok ( SourceFileHashAlgorithm :: Md5 ) ,
14791491 "sha1" => Ok ( SourceFileHashAlgorithm :: Sha1 ) ,
14801492 "sha256" => Ok ( SourceFileHashAlgorithm :: Sha256 ) ,
1493+ "xxhash" => Ok ( SourceFileHashAlgorithm :: XxHash ) ,
14811494 _ => Err ( ( ) ) ,
14821495 }
14831496 }
@@ -1517,6 +1530,9 @@ impl SourceFileHash {
15171530 SourceFileHashAlgorithm :: Sha256 => {
15181531 value. copy_from_slice ( & Sha256 :: digest ( data) ) ;
15191532 }
1533+ SourceFileHashAlgorithm :: XxHash => {
1534+ value. copy_from_slice ( & xxh3:: xxh3_128 ( data) . to_be_bytes ( ) ) ;
1535+ }
15201536 } ;
15211537 hash
15221538 }
@@ -1551,6 +1567,16 @@ impl SourceFileHash {
15511567 }
15521568
15531569 match kind {
1570+ SourceFileHashAlgorithm :: XxHash => {
1571+ digest (
1572+ xxh3:: Xxh3 :: new ( ) ,
1573+ |h, b| h. update ( b) ,
1574+ |h, out| out. copy_from_slice ( & h. digest128 ( ) . to_be_bytes ( ) ) ,
1575+ src,
1576+ & mut buf,
1577+ value,
1578+ ) ?;
1579+ }
15541580 SourceFileHashAlgorithm :: Sha256 => {
15551581 digest (
15561582 Sha256 :: new ( ) ,
@@ -1607,6 +1633,7 @@ impl SourceFileHash {
16071633 SourceFileHashAlgorithm :: Md5 => 16 ,
16081634 SourceFileHashAlgorithm :: Sha1 => 20 ,
16091635 SourceFileHashAlgorithm :: Sha256 => 32 ,
1636+ SourceFileHashAlgorithm :: XxHash => 16 ,
16101637 }
16111638 }
16121639}
0 commit comments