diff options
| author | Sébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com> | 2023-10-17 20:50:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-17 20:50:36 +0200 |
| commit | 37a988af83e54c9351eadd2e7f8a1c32441fafb4 (patch) | |
| tree | 49472313db0a35adf4c59bc867793a627597e3a9 | |
| parent | 77b84675e569dec3e1c29d17e9033a1d0c26dc23 (diff) | |
Replace `Vec` with `EcoVec`, removed `Box` (#2420)
| -rw-r--r-- | crates/typst-syntax/src/node.rs | 6 | ||||
| -rw-r--r-- | crates/typst/src/diag.rs | 31 | ||||
| -rw-r--r-- | crates/typst/src/eval/args.rs | 6 | ||||
| -rw-r--r-- | crates/typst/src/eval/mod.rs | 8 | ||||
| -rw-r--r-- | crates/typst/src/model/mod.rs | 7 | ||||
| -rw-r--r-- | tests/src/tests.rs | 2 |
6 files changed, 30 insertions, 30 deletions
diff --git a/crates/typst-syntax/src/node.rs b/crates/typst-syntax/src/node.rs index dd5deab2..9b73dd7f 100644 --- a/crates/typst-syntax/src/node.rs +++ b/crates/typst-syntax/src/node.rs @@ -3,7 +3,7 @@ use std::ops::{Deref, Range}; use std::rc::Rc; use std::sync::Arc; -use ecow::EcoString; +use ecow::{eco_vec, EcoString, EcoVec}; use super::ast::AstNode; use super::{FileId, Span, SyntaxKind}; @@ -616,7 +616,7 @@ impl ErrorNode { error: SyntaxError { span: Span::detached(), message: message.into(), - hints: vec![], + hints: eco_vec![], }, } } @@ -652,7 +652,7 @@ pub struct SyntaxError { pub message: EcoString, /// Additonal hints to the user, indicating how this error could be avoided /// or worked around. - pub hints: Vec<EcoString>, + pub hints: EcoVec<EcoString>, } impl SyntaxError { diff --git a/crates/typst/src/diag.rs b/crates/typst/src/diag.rs index 846ab78f..9342606e 100644 --- a/crates/typst/src/diag.rs +++ b/crates/typst/src/diag.rs @@ -7,6 +7,7 @@ use std::str::Utf8Error; use std::string::FromUtf8Error; use comemo::Tracked; +use ecow::{eco_vec, EcoVec}; use crate::syntax::{PackageSpec, Span, Spanned, SyntaxError}; use crate::{World, WorldExt}; @@ -29,14 +30,14 @@ macro_rules! __bail { }; ($error:expr) => { - return Err(Box::new(vec![$error])) + return Err(::ecow::eco_vec![$error]) }; ($span:expr, $fmt:literal $(, $arg:expr)* $(,)?) => { - return Err(Box::new(vec![$crate::diag::SourceDiagnostic::error( + return Err(::ecow::eco_vec![$crate::diag::SourceDiagnostic::error( $span, $crate::diag::eco_format!($fmt, $($arg),*), - )])) + )]) }; } @@ -75,7 +76,7 @@ macro_rules! __warning { } /// A result that can carry multiple source errors. -pub type SourceResult<T> = Result<T, Box<Vec<SourceDiagnostic>>>; +pub type SourceResult<T> = Result<T, EcoVec<SourceDiagnostic>>; /// An error or warning in a source file. /// @@ -90,10 +91,10 @@ pub struct SourceDiagnostic { /// A diagnostic message describing the problem. pub message: EcoString, /// The trace of function calls leading to the problem. - pub trace: Vec<Spanned<Tracepoint>>, + pub trace: EcoVec<Spanned<Tracepoint>>, /// Additonal hints to the user, indicating how this problem could be avoided /// or worked around. - pub hints: Vec<EcoString>, + pub hints: EcoVec<EcoString>, } /// The severity of a [`SourceDiagnostic`]. @@ -111,9 +112,9 @@ impl SourceDiagnostic { Self { severity: Severity::Error, span, - trace: vec![], + trace: eco_vec![], message: message.into(), - hints: vec![], + hints: eco_vec![], } } @@ -122,9 +123,9 @@ impl SourceDiagnostic { Self { severity: Severity::Warning, span, - trace: vec![], + trace: eco_vec![], message: message.into(), - hints: vec![], + hints: eco_vec![], } } @@ -152,7 +153,7 @@ impl From<SyntaxError> for SourceDiagnostic { severity: Severity::Error, span: error.span, message: error.message, - trace: vec![], + trace: eco_vec![], hints: error.hints, } } @@ -203,7 +204,7 @@ impl<T> Trace<T> for SourceResult<T> { { self.map_err(|mut errors| { let Some(trace_range) = world.range(span) else { return errors }; - for error in errors.iter_mut() { + for error in errors.make_mut().iter_mut() { // Skip traces that surround the error. if let Some(error_range) = world.range(error.span) { if error.span.id() == span.id() @@ -242,7 +243,7 @@ where diagnostic .hint("you can adjust the project root with the --root argument"); } - Box::new(vec![diagnostic]) + eco_vec![diagnostic] }) } } @@ -263,9 +264,7 @@ pub struct HintedString { impl<T> At<T> for Result<T, HintedString> { fn at(self, span: Span) -> SourceResult<T> { self.map_err(|diags| { - Box::new(vec![ - SourceDiagnostic::error(span, diags.message).with_hints(diags.hints) - ]) + eco_vec![SourceDiagnostic::error(span, diags.message).with_hints(diags.hints)] }) } } diff --git a/crates/typst/src/eval/args.rs b/crates/typst/src/eval/args.rs index aad9fda6..c347f527 100644 --- a/crates/typst/src/eval/args.rs +++ b/crates/typst/src/eval/args.rs @@ -1,6 +1,6 @@ use std::fmt::{self, Debug, Formatter}; -use ecow::{eco_format, EcoString, EcoVec}; +use ecow::{eco_format, eco_vec, EcoString, EcoVec}; use super::{func, scope, ty, Array, Dict, FromValue, IntoValue, Repr, Str, Value}; use crate::diag::{bail, At, SourceDiagnostic, SourceResult}; @@ -155,7 +155,7 @@ impl Args { T: FromValue<Spanned<Value>>, { let mut list = vec![]; - let mut errors = vec![]; + let mut errors = eco_vec![]; self.items.retain(|item| { if item.name.is_some() { return true; @@ -169,7 +169,7 @@ impl Args { false }); if !errors.is_empty() { - return Err(Box::new(errors)); + return Err(errors); } Ok(list) } diff --git a/crates/typst/src/eval/mod.rs b/crates/typst/src/eval/mod.rs index b6dbace9..b527bed1 100644 --- a/crates/typst/src/eval/mod.rs +++ b/crates/typst/src/eval/mod.rs @@ -134,7 +134,7 @@ pub fn eval( let root = source.root(); let errors = root.errors(); if !errors.is_empty() && vm.inspected.is_none() { - return Err(Box::new(errors.into_iter().map(Into::into).collect())); + return Err(errors.into_iter().map(Into::into).collect()); } // Evaluate the module. @@ -178,7 +178,7 @@ pub fn eval_string( let errors = root.errors(); if !errors.is_empty() { - return Err(Box::new(errors.into_iter().map(Into::into).collect())); + return Err(errors.into_iter().map(Into::into).collect()); } // Prepare VT. @@ -1786,7 +1786,7 @@ impl Eval for ast::ModuleImport<'_> { } } Some(ast::Imports::Items(items)) => { - let mut errors = vec![]; + let mut errors = eco_vec![]; for item in items.iter() { let original_ident = item.original_name(); if let Some(value) = scope.get(&original_ident) { @@ -1808,7 +1808,7 @@ impl Eval for ast::ModuleImport<'_> { } } if !errors.is_empty() { - return Err(Box::new(errors)); + return Err(errors); } } } diff --git a/crates/typst/src/model/mod.rs b/crates/typst/src/model/mod.rs index 2741f731..dcad8741 100644 --- a/crates/typst/src/model/mod.rs +++ b/crates/typst/src/model/mod.rs @@ -8,6 +8,7 @@ mod realize; mod selector; mod styles; +use ecow::EcoVec; #[doc(inline)] pub use typst_macros::elem; @@ -89,7 +90,7 @@ pub fn typeset( // Promote delayed errors. if !delayed.0.is_empty() { - return Err(Box::new(delayed.0)); + return Err(delayed.0); } Ok(document) @@ -123,7 +124,7 @@ impl Vt<'_> { match f(self) { Ok(value) => value, Err(errors) => { - for error in *errors { + for error in errors { self.delayed.push(error); } T::default() @@ -134,7 +135,7 @@ impl Vt<'_> { /// Holds delayed errors. #[derive(Default, Clone)] -pub struct DelayedErrors(Vec<SourceDiagnostic>); +pub struct DelayedErrors(EcoVec<SourceDiagnostic>); impl DelayedErrors { /// Create an empty list of delayed errors. diff --git a/tests/src/tests.rs b/tests/src/tests.rs index 41fd00e8..bdbfd397 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -545,7 +545,7 @@ fn test_part( Ok(document) => (document.pages, tracer.warnings()), Err(errors) => { let mut warnings = tracer.warnings(); - warnings.extend(*errors); + warnings.extend(errors); (vec![], warnings) } }; |
