Skip to main content

pedant_core/resolution/rust/snapshot/
unit.rs

1//! The resolution-unit view and the Cargo edges that selected it.
2
3use std::fmt;
4use std::sync::Arc;
5
6use crate::resolution::rust::dependency::{CargoDependencyKind, DependencyActivation};
7use crate::resolution::rust::identity::{PackageId, TargetId};
8use crate::resolution::rust::target::CargoTargetKind;
9
10use super::module::RustModuleInstance;
11
12/// Opaque identity of one resolution unit inside a single snapshot.
13///
14/// Unit identities are snapshot-local: they index the snapshot that issued
15/// them and mean nothing outside it.
16#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct RustSnapshotUnitId {
18    index: u32,
19}
20
21impl RustSnapshotUnitId {
22    /// Issue an identity for the unit at `index`.
23    pub(super) fn new(index: u32) -> Self {
24        Self { index }
25    }
26
27    /// The snapshot-local index this identity selects.
28    pub(in crate::resolution::rust) fn index(&self) -> u32 {
29        self.index
30    }
31}
32
33impl fmt::Debug for RustSnapshotUnitId {
34    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(formatter, "RustSnapshotUnitId({})", self.index)
36    }
37}
38
39/// One Cargo target instance a resolution snapshot resolves names inside.
40#[derive(Debug)]
41pub struct RustResolutionUnit {
42    pub(super) id: RustSnapshotUnitId,
43    pub(super) package: PackageId,
44    pub(super) target: TargetId,
45    pub(super) name: Arc<str>,
46    pub(super) manifest: Arc<str>,
47    pub(super) kind: CargoTargetKind,
48    pub(super) activation: DependencyActivation,
49    pub(super) crate_root: Arc<str>,
50    pub(super) modules: Box<[RustModuleInstance]>,
51    pub(super) sources: Box<[Arc<str>]>,
52}
53
54impl RustResolutionUnit {
55    /// This unit's snapshot-local identity.
56    pub fn id(&self) -> RustSnapshotUnitId {
57        self.id
58    }
59
60    /// The package that declares this unit's Cargo target.
61    pub fn package(&self) -> PackageId {
62        self.package
63    }
64
65    /// The Cargo target this unit compiles.
66    pub fn target(&self) -> TargetId {
67        self.target
68    }
69
70    /// The Cargo target name this unit compiles under.
71    pub fn name(&self) -> &str {
72        &self.name
73    }
74
75    /// The repository-relative manifest that declares this unit's target.
76    pub fn manifest_path(&self) -> &str {
77        &self.manifest
78    }
79
80    /// Which kind of Cargo target this unit compiles.
81    pub fn kind(&self) -> CargoTargetKind {
82        self.kind
83    }
84
85    /// When this unit is active. `Always` when any root-to-unit path is
86    /// unconditional; otherwise the combined, unevaluated predicate.
87    pub fn activation(&self) -> &DependencyActivation {
88        &self.activation
89    }
90
91    /// The repository-relative entry point this unit's module tree starts at.
92    pub fn crate_root(&self) -> &str {
93        &self.crate_root
94    }
95
96    /// Every module instance inside this unit, root first.
97    pub fn modules(&self) -> &[RustModuleInstance] {
98        &self.modules
99    }
100
101    /// The sorted repository-relative sources this unit's modules occupy.
102    pub fn sources(&self) -> &[Arc<str>] {
103        &self.sources
104    }
105}
106
107/// One Cargo namespace edge between two units of a resolution snapshot.
108///
109/// Manifest dependencies retain their declared kind and alias. Cargo's
110/// implicit same-package library edge is unconditional `Normal` and uses the
111/// library crate name.
112#[derive(Debug)]
113pub struct RustSnapshotEdge {
114    pub(super) source: RustSnapshotUnitId,
115    pub(super) target: RustSnapshotUnitId,
116    pub(super) name: Arc<str>,
117    pub(super) kind: CargoDependencyKind,
118    pub(super) activation: DependencyActivation,
119}
120
121impl RustSnapshotEdge {
122    /// The unit that declares this edge.
123    pub fn source(&self) -> RustSnapshotUnitId {
124        self.source
125    }
126
127    /// The unit this edge selects.
128    pub fn target(&self) -> RustSnapshotUnitId {
129        self.target
130    }
131
132    /// The namespace-local dependency or library crate name.
133    pub fn name(&self) -> &str {
134        &self.name
135    }
136
137    /// Which dependency table declared this edge.
138    pub fn kind(&self) -> CargoDependencyKind {
139        self.kind
140    }
141
142    /// When this edge is exposed to the source unit.
143    pub fn activation(&self) -> &DependencyActivation {
144        &self.activation
145    }
146}