Mercurial > hg
comparison rust/hg-core/src/revlog/index.rs @ 50979:4c5f6e95df84
rust: make `Revision` a newtype
This change is the one we've been building towards during this series.
The aim is to make `Revision` mean more than a simple integer, holding
the information that it is valid for a given revlog index.
While this still allows for programmer error, since creating a revision
directly and querying a different index with a "checked" revision are
still possible, the friction created by the newtype will hopefully make
us think twice about which type to use.
Enough of the Rust ecosystem relies on the newtype pattern to be
efficiently optimized away (even compiler in codegen testsĀ¹), so I'm not
worried about this being a fundamental problem.
[1] https://github.com/rust-lang/rust/blob/7a70647f195f6b0a0f1ebd72b1542ba91a32f43a/tests/codegen/vec-in-place.rs#L47
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Fri, 18 Aug 2023 14:34:29 +0200 |
parents | 27e773aa607d |
children | 1ef4a36a934d |
comparison
equal
deleted
inserted
replaced
50978:27e773aa607d | 50979:4c5f6e95df84 |
---|---|
213 fn get_entry_inline( | 213 fn get_entry_inline( |
214 &self, | 214 &self, |
215 rev: Revision, | 215 rev: Revision, |
216 offsets: &[usize], | 216 offsets: &[usize], |
217 ) -> IndexEntry { | 217 ) -> IndexEntry { |
218 let start = offsets[rev as usize]; | 218 let start = offsets[rev.0 as usize]; |
219 let end = start + INDEX_ENTRY_SIZE; | 219 let end = start + INDEX_ENTRY_SIZE; |
220 let bytes = &self.bytes[start..end]; | 220 let bytes = &self.bytes[start..end]; |
221 | 221 |
222 // See IndexEntry for an explanation of this override. | 222 // See IndexEntry for an explanation of this override. |
223 let offset_override = Some(end); | 223 let offset_override = Some(end); |
227 offset_override, | 227 offset_override, |
228 } | 228 } |
229 } | 229 } |
230 | 230 |
231 fn get_entry_separated(&self, rev: Revision) -> IndexEntry { | 231 fn get_entry_separated(&self, rev: Revision) -> IndexEntry { |
232 let start = rev as usize * INDEX_ENTRY_SIZE; | 232 let start = rev.0 as usize * INDEX_ENTRY_SIZE; |
233 let end = start + INDEX_ENTRY_SIZE; | 233 let end = start + INDEX_ENTRY_SIZE; |
234 let bytes = &self.bytes[start..end]; | 234 let bytes = &self.bytes[start..end]; |
235 | 235 |
236 // Override the offset of the first revision as its bytes are used | 236 // Override the offset of the first revision as its bytes are used |
237 // for the index's metadata (saving space because it is always 0) | 237 // for the index's metadata (saving space because it is always 0) |
238 let offset_override = if rev == 0 { Some(0) } else { None }; | 238 let offset_override = if rev == Revision(0) { Some(0) } else { None }; |
239 | 239 |
240 IndexEntry { | 240 IndexEntry { |
241 bytes, | 241 bytes, |
242 offset_override, | 242 offset_override, |
243 } | 243 } |
357 is_general_delta: true, | 357 is_general_delta: true, |
358 version: 1, | 358 version: 1, |
359 offset: 0, | 359 offset: 0, |
360 compressed_len: 0, | 360 compressed_len: 0, |
361 uncompressed_len: 0, | 361 uncompressed_len: 0, |
362 base_revision_or_base_of_delta_chain: 0, | 362 base_revision_or_base_of_delta_chain: Revision(0), |
363 link_revision: 0, | 363 link_revision: Revision(0), |
364 p1: NULL_REVISION, | 364 p1: NULL_REVISION, |
365 p2: NULL_REVISION, | 365 p2: NULL_REVISION, |
366 node: NULL_NODE, | 366 node: NULL_NODE, |
367 } | 367 } |
368 } | 368 } |
448 } | 448 } |
449 bytes.extend(&[0u8; 2]); // Revision flags. | 449 bytes.extend(&[0u8; 2]); // Revision flags. |
450 bytes.extend(&(self.compressed_len as u32).to_be_bytes()); | 450 bytes.extend(&(self.compressed_len as u32).to_be_bytes()); |
451 bytes.extend(&(self.uncompressed_len as u32).to_be_bytes()); | 451 bytes.extend(&(self.uncompressed_len as u32).to_be_bytes()); |
452 bytes.extend( | 452 bytes.extend( |
453 &self.base_revision_or_base_of_delta_chain.to_be_bytes(), | 453 &self.base_revision_or_base_of_delta_chain.0.to_be_bytes(), |
454 ); | 454 ); |
455 bytes.extend(&self.link_revision.to_be_bytes()); | 455 bytes.extend(&self.link_revision.0.to_be_bytes()); |
456 bytes.extend(&self.p1.to_be_bytes()); | 456 bytes.extend(&self.p1.0.to_be_bytes()); |
457 bytes.extend(&self.p2.to_be_bytes()); | 457 bytes.extend(&self.p2.0.to_be_bytes()); |
458 bytes.extend(self.node.as_bytes()); | 458 bytes.extend(self.node.as_bytes()); |
459 bytes.extend(vec![0u8; 12]); | 459 bytes.extend(vec![0u8; 12]); |
460 bytes | 460 bytes |
461 } | 461 } |
462 } | 462 } |
562 } | 562 } |
563 | 563 |
564 #[test] | 564 #[test] |
565 fn test_base_revision_or_base_of_delta_chain() { | 565 fn test_base_revision_or_base_of_delta_chain() { |
566 let bytes = IndexEntryBuilder::new() | 566 let bytes = IndexEntryBuilder::new() |
567 .with_base_revision_or_base_of_delta_chain(1) | 567 .with_base_revision_or_base_of_delta_chain(Revision(1)) |
568 .build(); | 568 .build(); |
569 let entry = IndexEntry { | 569 let entry = IndexEntry { |
570 bytes: &bytes, | 570 bytes: &bytes, |
571 offset_override: None, | 571 offset_override: None, |
572 }; | 572 }; |
574 assert_eq!(entry.base_revision_or_base_of_delta_chain(), 1.into()) | 574 assert_eq!(entry.base_revision_or_base_of_delta_chain(), 1.into()) |
575 } | 575 } |
576 | 576 |
577 #[test] | 577 #[test] |
578 fn link_revision_test() { | 578 fn link_revision_test() { |
579 let bytes = IndexEntryBuilder::new().with_link_revision(123).build(); | 579 let bytes = IndexEntryBuilder::new() |
580 .with_link_revision(Revision(123)) | |
581 .build(); | |
580 | 582 |
581 let entry = IndexEntry { | 583 let entry = IndexEntry { |
582 bytes: &bytes, | 584 bytes: &bytes, |
583 offset_override: None, | 585 offset_override: None, |
584 }; | 586 }; |
586 assert_eq!(entry.link_revision(), 123.into()); | 588 assert_eq!(entry.link_revision(), 123.into()); |
587 } | 589 } |
588 | 590 |
589 #[test] | 591 #[test] |
590 fn p1_test() { | 592 fn p1_test() { |
591 let bytes = IndexEntryBuilder::new().with_p1(123).build(); | 593 let bytes = IndexEntryBuilder::new().with_p1(Revision(123)).build(); |
592 | 594 |
593 let entry = IndexEntry { | 595 let entry = IndexEntry { |
594 bytes: &bytes, | 596 bytes: &bytes, |
595 offset_override: None, | 597 offset_override: None, |
596 }; | 598 }; |
598 assert_eq!(entry.p1(), 123.into()); | 600 assert_eq!(entry.p1(), 123.into()); |
599 } | 601 } |
600 | 602 |
601 #[test] | 603 #[test] |
602 fn p2_test() { | 604 fn p2_test() { |
603 let bytes = IndexEntryBuilder::new().with_p2(123).build(); | 605 let bytes = IndexEntryBuilder::new().with_p2(Revision(123)).build(); |
604 | 606 |
605 let entry = IndexEntry { | 607 let entry = IndexEntry { |
606 bytes: &bytes, | 608 bytes: &bytes, |
607 offset_override: None, | 609 offset_override: None, |
608 }; | 610 }; |