comparison rust/hg-core/src/revlog/index.rs @ 49930:e98fd81bb151

rust-clippy: fix most warnings in `hg-core` All of these are simple changes that for the most part are clear improvements and the rest are at most equivalent. The remaining warnings have to be fixed either with a bigger refactor like for the nested "revlog" module, or in the dependency `bytes-cast`, which we own. This will be done sometime in the future.
author Raphaël Gomès <rgomes@octobus.net>
date Mon, 09 Jan 2023 19:18:43 +0100
parents 0780371d6b1e
children 1928b770e3e7
comparison
equal deleted inserted replaced
49929:5f1cd6839c69 49930:e98fd81bb151
19 19
20 /// Corresponds to the high bits of `_format_flags` in python 20 /// Corresponds to the high bits of `_format_flags` in python
21 impl IndexHeaderFlags { 21 impl IndexHeaderFlags {
22 /// Corresponds to FLAG_INLINE_DATA in python 22 /// Corresponds to FLAG_INLINE_DATA in python
23 pub fn is_inline(self) -> bool { 23 pub fn is_inline(self) -> bool {
24 return self.flags & 1 != 0; 24 self.flags & 1 != 0
25 } 25 }
26 /// Corresponds to FLAG_GENERALDELTA in python 26 /// Corresponds to FLAG_GENERALDELTA in python
27 pub fn uses_generaldelta(self) -> bool { 27 pub fn uses_generaldelta(self) -> bool {
28 return self.flags & 2 != 0; 28 self.flags & 2 != 0
29 } 29 }
30 } 30 }
31 31
32 /// Corresponds to the INDEX_HEADER structure, 32 /// Corresponds to the INDEX_HEADER structure,
33 /// which is parsed as a `header` variable in `_loadindex` in `revlog.py` 33 /// which is parsed as a `header` variable in `_loadindex` in `revlog.py`
34 impl IndexHeader { 34 impl IndexHeader {
35 fn format_flags(&self) -> IndexHeaderFlags { 35 fn format_flags(&self) -> IndexHeaderFlags {
36 // No "unknown flags" check here, unlike in python. Maybe there should 36 // No "unknown flags" check here, unlike in python. Maybe there should
37 // be. 37 // be.
38 return IndexHeaderFlags { 38 IndexHeaderFlags {
39 flags: BigEndian::read_u16(&self.header_bytes[0..2]), 39 flags: BigEndian::read_u16(&self.header_bytes[0..2]),
40 }; 40 }
41 } 41 }
42 42
43 /// The only revlog version currently supported by rhg. 43 /// The only revlog version currently supported by rhg.
44 const REVLOGV1: u16 = 1; 44 const REVLOGV1: u16 = 1;
45 45
46 /// Corresponds to `_format_version` in Python. 46 /// Corresponds to `_format_version` in Python.
47 fn format_version(&self) -> u16 { 47 fn format_version(&self) -> u16 {
48 return BigEndian::read_u16(&self.header_bytes[2..4]); 48 BigEndian::read_u16(&self.header_bytes[2..4])
49 } 49 }
50 50
51 const EMPTY_INDEX_HEADER: IndexHeader = IndexHeader { 51 const EMPTY_INDEX_HEADER: IndexHeader = IndexHeader {
52 // We treat an empty file as a valid index with no entries. 52 // We treat an empty file as a valid index with no entries.
53 // Here we make an arbitrary choice of what we assume the format of the 53 // Here we make an arbitrary choice of what we assume the format of the
57 // `revlog.py`, `_loadindex` 57 // `revlog.py`, `_loadindex`
58 header_bytes: [0, 3, 0, 1], 58 header_bytes: [0, 3, 0, 1],
59 }; 59 };
60 60
61 fn parse(index_bytes: &[u8]) -> Result<IndexHeader, HgError> { 61 fn parse(index_bytes: &[u8]) -> Result<IndexHeader, HgError> {
62 if index_bytes.len() == 0 { 62 if index_bytes.is_empty() {
63 return Ok(IndexHeader::EMPTY_INDEX_HEADER); 63 return Ok(IndexHeader::EMPTY_INDEX_HEADER);
64 } 64 }
65 if index_bytes.len() < 4 { 65 if index_bytes.len() < 4 {
66 return Err(HgError::corrupted( 66 return Err(HgError::corrupted(
67 "corrupted revlog: can't read the index format header", 67 "corrupted revlog: can't read the index format header",
68 )); 68 ));
69 } 69 }
70 return Ok(IndexHeader { 70 Ok(IndexHeader {
71 header_bytes: { 71 header_bytes: {
72 let bytes: [u8; 4] = 72 let bytes: [u8; 4] =
73 index_bytes[0..4].try_into().expect("impossible"); 73 index_bytes[0..4].try_into().expect("impossible");
74 bytes 74 bytes
75 }, 75 },
76 }); 76 })
77 } 77 }
78 } 78 }
79 79
80 /// A Revlog index 80 /// A Revlog index
81 pub struct Index { 81 pub struct Index {
125 bytes, 125 bytes,
126 offsets: Some(offsets), 126 offsets: Some(offsets),
127 uses_generaldelta, 127 uses_generaldelta,
128 }) 128 })
129 } else { 129 } else {
130 Err(HgError::corrupted("unexpected inline revlog length") 130 Err(HgError::corrupted("unexpected inline revlog length"))
131 .into())
132 } 131 }
133 } else { 132 } else {
134 Ok(Self { 133 Ok(Self {
135 bytes, 134 bytes,
136 offsets: None, 135 offsets: None,
464 .is_first(true) 463 .is_first(true)
465 .with_general_delta(false) 464 .with_general_delta(false)
466 .with_inline(false) 465 .with_inline(false)
467 .build(); 466 .build();
468 467
469 assert_eq!(is_inline(&bytes), false); 468 assert!(!is_inline(&bytes));
470 assert_eq!(uses_generaldelta(&bytes), false); 469 assert!(!uses_generaldelta(&bytes));
471 } 470 }
472 471
473 #[test] 472 #[test]
474 fn flags_when_inline_flag_test() { 473 fn flags_when_inline_flag_test() {
475 let bytes = IndexEntryBuilder::new() 474 let bytes = IndexEntryBuilder::new()
476 .is_first(true) 475 .is_first(true)
477 .with_general_delta(false) 476 .with_general_delta(false)
478 .with_inline(true) 477 .with_inline(true)
479 .build(); 478 .build();
480 479
481 assert_eq!(is_inline(&bytes), true); 480 assert!(is_inline(&bytes));
482 assert_eq!(uses_generaldelta(&bytes), false); 481 assert!(!uses_generaldelta(&bytes));
483 } 482 }
484 483
485 #[test] 484 #[test]
486 fn flags_when_inline_and_generaldelta_flags_test() { 485 fn flags_when_inline_and_generaldelta_flags_test() {
487 let bytes = IndexEntryBuilder::new() 486 let bytes = IndexEntryBuilder::new()
488 .is_first(true) 487 .is_first(true)
489 .with_general_delta(true) 488 .with_general_delta(true)
490 .with_inline(true) 489 .with_inline(true)
491 .build(); 490 .build();
492 491
493 assert_eq!(is_inline(&bytes), true); 492 assert!(is_inline(&bytes));
494 assert_eq!(uses_generaldelta(&bytes), true); 493 assert!(uses_generaldelta(&bytes));
495 } 494 }
496 495
497 #[test] 496 #[test]
498 fn test_offset() { 497 fn test_offset() {
499 let bytes = IndexEntryBuilder::new().with_offset(1).build(); 498 let bytes = IndexEntryBuilder::new().with_offset(1).build();