Skip to content

Commit b37503b

Browse files
committed
refactor: replace LazyTaskGraph enum with struct + Option to remove unreachable!
Restructure LazyTaskGraph from a two-variant enum (Uninitialized/Initialized) to a struct with an Option<IndexedTaskGraph>. This eliminates the unreachable! in the double-match pattern that was needed after mutating self from Uninitialized to Initialized. The remaining unreachable! calls in the codebase (fspy_preload_windows buffer size invariant, vite_path strip_prefix mathematical invariant) are genuine logical invariants that cannot be expressed in the type system.
1 parent 6e53425 commit b37503b

File tree

1 file changed

+14
-20
lines changed
  • crates/vite_task/src/session

1 file changed

+14
-20
lines changed

crates/vite_task/src/session/mod.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,23 @@ use vite_workspace::{WorkspaceRoot, find_workspace_root};
2929
use crate::cli::{CacheSubcommand, Command, RunCommand, RunFlags};
3030

3131
#[derive(Debug)]
32-
enum LazyTaskGraph<'a> {
33-
Uninitialized { workspace_root: WorkspaceRoot, config_loader: &'a dyn UserConfigLoader },
34-
Initialized(IndexedTaskGraph),
32+
struct LazyTaskGraph<'a> {
33+
workspace_root: WorkspaceRoot,
34+
config_loader: &'a dyn UserConfigLoader,
35+
graph: Option<IndexedTaskGraph>,
3536
}
3637

3738
#[async_trait::async_trait(?Send)]
3839
impl TaskGraphLoader for LazyTaskGraph<'_> {
3940
async fn load_task_graph(
4041
&mut self,
4142
) -> Result<&vite_task_graph::IndexedTaskGraph, TaskGraphLoadError> {
42-
Ok(match self {
43-
Self::Uninitialized { workspace_root, config_loader } => {
44-
let graph = IndexedTaskGraph::load(workspace_root, *config_loader).await?;
45-
*self = Self::Initialized(graph);
46-
match self {
47-
Self::Initialized(graph) => &*graph,
48-
Self::Uninitialized { .. } => unreachable!(),
49-
}
50-
}
51-
Self::Initialized(graph) => &*graph,
52-
})
43+
if self.graph.is_none() {
44+
let graph = IndexedTaskGraph::load(&self.workspace_root, self.config_loader).await?;
45+
self.graph = Some(graph);
46+
}
47+
// Unwrap is safe: the block above ensures `self.graph` is `Some`.
48+
Ok(self.graph.as_ref().unwrap())
5349
}
5450
}
5551

@@ -200,9 +196,10 @@ impl<'a> Session<'a> {
200196
// Cache is lazily initialized on first access to avoid SQLite race conditions
201197
Ok(Self {
202198
workspace_path: Arc::clone(&workspace_root.path),
203-
lazy_task_graph: LazyTaskGraph::Uninitialized {
199+
lazy_task_graph: LazyTaskGraph {
204200
workspace_root,
205201
config_loader: callbacks.user_config_loader,
202+
graph: None,
206203
},
207204
envs: Arc::new(envs),
208205
cwd,
@@ -414,11 +411,8 @@ impl<'a> Session<'a> {
414411
Arc::clone(&self.workspace_path)
415412
}
416413

417-
pub const fn task_graph(&self) -> Option<&TaskGraph> {
418-
match &self.lazy_task_graph {
419-
LazyTaskGraph::Initialized(graph) => Some(graph.task_graph()),
420-
LazyTaskGraph::Uninitialized { .. } => None,
421-
}
414+
pub fn task_graph(&self) -> Option<&TaskGraph> {
415+
self.lazy_task_graph.graph.as_ref().map(IndexedTaskGraph::task_graph)
422416
}
423417

424418
pub const fn envs(&self) -> &Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>> {

0 commit comments

Comments
 (0)