turbo_tasks_fs/
invalidation.rs

1use std::{
2    fmt::{Display, Formatter},
3    hash::Hash,
4};
5
6use turbo_rcstr::RcStr;
7use turbo_tasks::{FxIndexSet, InvalidationReason, InvalidationReasonKind, util::StaticOrArc};
8
9/// Invalidation was caused by a file change detected by the file watcher
10#[derive(PartialEq, Eq, Hash)]
11pub(crate) struct WatchChange {
12    pub path: String,
13}
14
15impl InvalidationReason for WatchChange {
16    fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
17        Some(StaticOrArc::Static(&WATCH_CHANGE_KIND))
18    }
19}
20
21impl Display for WatchChange {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        write!(f, "{} changed", self.path)
24    }
25}
26
27/// Invalidation kind for [WatchChange]
28#[derive(PartialEq, Eq, Hash)]
29struct WatchChangeKind;
30
31static WATCH_CHANGE_KIND: WatchChangeKind = WatchChangeKind;
32
33impl InvalidationReasonKind for WatchChangeKind {
34    fn fmt(
35        &self,
36        reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
37        f: &mut Formatter<'_>,
38    ) -> std::fmt::Result {
39        write!(
40            f,
41            "{} files changed ({}, ...)",
42            reasons.len(),
43            reasons[0]
44                .as_any()
45                .downcast_ref::<WatchChange>()
46                .unwrap()
47                .path
48        )
49    }
50}
51
52/// Invalidation was caused by a directory starting to watch from which was read
53/// before.
54#[derive(PartialEq, Eq, Hash, Clone)]
55pub(crate) struct WatchStart {
56    pub name: RcStr,
57    pub path: RcStr,
58}
59
60impl InvalidationReason for WatchStart {
61    fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
62        Some(StaticOrArc::Static(&WATCH_START_KIND))
63    }
64}
65
66impl Display for WatchStart {
67    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
68        write!(f, "started watching {} in {}", self.path, self.name)
69    }
70}
71
72/// Invalidation kind for [WatchStart]
73#[derive(PartialEq, Eq, Hash)]
74struct WatchStartKind;
75
76static WATCH_START_KIND: WatchStartKind = WatchStartKind;
77
78impl InvalidationReasonKind for WatchStartKind {
79    fn fmt(
80        &self,
81        reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
82        f: &mut Formatter<'_>,
83    ) -> std::fmt::Result {
84        let example = reasons[0].as_any().downcast_ref::<WatchStart>().unwrap();
85        write!(
86            f,
87            "{} items started watching (e.g. {} in {})",
88            reasons.len(),
89            example.path,
90            example.name
91        )
92    }
93}
94
95/// Invalidation was caused by initialization of a project or filesystem.
96#[derive(PartialEq, Eq, Hash, Clone)]
97pub struct Initialize {
98    pub path: RcStr,
99}
100
101impl InvalidationReason for Initialize {
102    fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
103        Some(StaticOrArc::Static(&INITIALIZE_KIND))
104    }
105}
106
107impl Display for Initialize {
108    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
109        write!(
110            f,
111            "initialized project or filesystem with path {}",
112            self.path
113        )
114    }
115}
116
117/// [Invalidation kind][InvalidationReasonKind] for [`Initialize`].
118#[derive(PartialEq, Eq, Hash)]
119struct InitializeKind;
120
121static INITIALIZE_KIND: InitializeKind = InitializeKind;
122
123impl InvalidationReasonKind for InitializeKind {
124    fn fmt(
125        &self,
126        reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
127        f: &mut Formatter<'_>,
128    ) -> std::fmt::Result {
129        let example = reasons[0].as_any().downcast_ref::<Initialize>().unwrap();
130        write!(
131            f,
132            "{} items invalidated as part of project or filesystem initialization ({}, ...)",
133            reasons.len(),
134            example.path,
135        )
136    }
137}
138
139/// Invalidation was caused by a write operation on the filesystem
140#[derive(PartialEq, Eq, Hash)]
141pub(crate) struct Write {
142    pub path: String,
143}
144
145impl InvalidationReason for Write {
146    fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
147        Some(StaticOrArc::Static(&WRITE_KIND))
148    }
149}
150
151impl Display for Write {
152    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
153        write!(f, "{} written", self.path)
154    }
155}
156
157/// Invalidation kind for [Write]
158#[derive(PartialEq, Eq, Hash)]
159struct WriteKind;
160
161static WRITE_KIND: WriteKind = WriteKind;
162
163impl InvalidationReasonKind for WriteKind {
164    fn fmt(
165        &self,
166        reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
167        f: &mut Formatter<'_>,
168    ) -> std::fmt::Result {
169        write!(
170            f,
171            "{} files written ({}, ...)",
172            reasons.len(),
173            reasons[0].as_any().downcast_ref::<Write>().unwrap().path
174        )
175    }
176}