Skip to main content

rustc_utils/source_map/
filename.rs

1use std::{fmt, path::PathBuf};
2
3use rustc_index::Idx;
4
5#[derive(Debug, Clone, Hash, PartialEq, Eq)]
6pub struct Filename(pub PathBuf);
7
8#[derive(Clone, Copy, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize))]
10#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
11pub struct FilenameIndex {
12  private_use_as_methods_instead: usize,
13}
14
15impl fmt::Debug for FilenameIndex {
16  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17    write!(f, "f{}", self.private_use_as_methods_instead)
18  }
19}
20
21impl Idx for FilenameIndex {
22  fn new(idx: usize) -> Self {
23    FilenameIndex {
24      private_use_as_methods_instead: idx,
25    }
26  }
27
28  fn index(self) -> usize {
29    self.private_use_as_methods_instead
30  }
31}
32
33// NOTE(nightly-2026-05-01): the newtype_index has been commented out below
34// bc it uses some wacky `T is range` type that isn't implemented by serde or ts_rs.
35
36// rustc_index::newtype_index! {
37//   #[cfg_attr(feature = "serde", derive(serde::Serialize))]
38//   #[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
39//   #[debug_format = "f{}"]
40//   pub struct FilenameIndex {}
41// }