Skip to content

Commit 3da61fa

Browse files
committed
Add comments explaining the declare_id macro.
1 parent 0bbac12 commit 3da61fa

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/sync.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use rustc_index::vec::{Idx, IndexVec};
66

77
use crate::*;
88

9+
/// We cannot use the `newtype_index!` macro because we have to use 0 as a
10+
/// sentinel value meaning that the identifier is not assigned. This is because
11+
/// the pthreads static initializers initialize memory with zeros (see the
12+
/// `src/shims/sync.rs` file).
913
macro_rules! declare_id {
1014
($name: ident) => {
1115
/// 0 is used to indicate that the id was not yet assigned and,
@@ -22,9 +26,13 @@ macro_rules! declare_id {
2226

2327
impl Idx for $name {
2428
fn new(idx: usize) -> Self {
29+
// We use 0 as a sentinel value (see the comment above) and,
30+
// therefore, need to shift by one when converting from an index
31+
// into a vector.
2532
$name(NonZeroU32::new(u32::try_from(idx).unwrap() + 1).unwrap())
2633
}
2734
fn index(self) -> usize {
35+
// See the comment in `Self::new`.
2836
usize::try_from(self.0.get() - 1).unwrap()
2937
}
3038
}

0 commit comments

Comments
 (0)