Skip to content

Commit

Permalink
feat: impl Extend for BatchedErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Feb 25, 2024
1 parent c86b552 commit e22acce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
20 changes: 15 additions & 5 deletions crates/rolldown/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ use smallvec::SmallVec;
pub struct BatchedErrors(SmallVec<[BuildError; 1]>);

impl BatchedErrors {
// TODO(hyf): using `trait Extend` would be more proper.
pub fn merge(&mut self, mut other: Self) {
self.0.append(&mut other.0);
}

pub fn with_error(err: BuildError) -> Self {
Self(smallvec::smallvec![err])
}
Expand Down Expand Up @@ -50,6 +45,21 @@ pub fn into_batched_result<T>(value: Vec<Result<T, BuildError>>) -> BatchedResul
}
}

impl Extend<BuildError> for BatchedErrors {
fn extend<T: IntoIterator<Item = BuildError>>(&mut self, iter: T) {
self.0.extend(iter.into_iter());
}
}

impl IntoIterator for BatchedErrors {
type Item = BuildError;
type IntoIter = smallvec::IntoIter<[BuildError; 1]>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

pub type BatchedResult<T> = Result<T, BatchedErrors>;

impl From<BuildError> for BatchedErrors {
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<T: FileSystem + 'static + Default> ModuleLoader<T> {
runtime_brief = Some(runtime);
}
Msg::Errors(errs) => {
errors.merge(errs);
errors.extend(errs);
}
}
self.remaining -= 1;
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/module_loader/normal_module_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
ret.push(item);
}
Err(e) => {
errors.merge(e);
errors.extend(e);
}
});
debug_assert!(errors.is_empty() && ret.len() == dependencies.len(), "{dependencies:#?}");
Expand Down

0 comments on commit e22acce

Please sign in to comment.