summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-05-13 19:54:34 +0200
committerGitHub <noreply@github.com>2024-05-13 17:54:34 +0000
commit2d32ac73b63c81ea2754916feb7758c042f4ed3d (patch)
treec9770897cbb1134187132d6835702a1869a74a0a
parent95cd6adf24cb14ede8896fbb0c610432960f4f3a (diff)
Replace all `Prehashed` with `LazyHash` (#4127)
-rw-r--r--Cargo.lock2
-rw-r--r--crates/typst-cli/src/world.rs14
-rw-r--r--crates/typst-ide/src/lib.rs14
-rw-r--r--crates/typst-syntax/Cargo.toml2
-rw-r--r--crates/typst-syntax/src/source.rs16
-rw-r--r--crates/typst/src/lib.rs11
-rw-r--r--crates/typst/src/visualize/pattern.rs7
-rw-r--r--docs/src/html.rs6
-rw-r--r--docs/src/lib.rs10
-rw-r--r--tests/fuzz/src/compile.rs14
-rw-r--r--tests/src/world.rs14
11 files changed, 54 insertions, 56 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 8ea562e3..d7edaf31 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2759,10 +2759,10 @@ dependencies = [
name = "typst-syntax"
version = "0.11.0"
dependencies = [
- "comemo",
"ecow",
"once_cell",
"serde",
+ "typst-utils",
"unicode-ident",
"unicode-math-class",
"unicode-script",
diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs
index 57aecd42..81fece2a 100644
--- a/crates/typst-cli/src/world.rs
+++ b/crates/typst-cli/src/world.rs
@@ -5,7 +5,6 @@ use std::sync::OnceLock;
use std::{fmt, fs, io, mem};
use chrono::{DateTime, Datelike, FixedOffset, Local, Utc};
-use comemo::Prehashed;
use ecow::{eco_format, EcoString};
use once_cell::sync::Lazy;
use parking_lot::Mutex;
@@ -13,6 +12,7 @@ use typst::diag::{FileError, FileResult};
use typst::foundations::{Bytes, Datetime, Dict, IntoValue};
use typst::syntax::{FileId, Source, VirtualPath};
use typst::text::{Font, FontBook};
+use typst::utils::LazyHash;
use typst::{Library, World};
use typst_timing::{timed, TimingScope};
@@ -34,9 +34,9 @@ pub struct SystemWorld {
/// The input path.
main: FileId,
/// Typst's standard library.
- library: Prehashed<Library>,
+ library: LazyHash<Library>,
/// Metadata about discovered fonts.
- book: Prehashed<FontBook>,
+ book: LazyHash<FontBook>,
/// Locations of and storage for lazily loaded fonts.
fonts: Vec<FontSlot>,
/// Maps file ids to source files and buffers.
@@ -114,8 +114,8 @@ impl SystemWorld {
workdir: std::env::current_dir().ok(),
root,
main,
- library: Prehashed::new(library),
- book: Prehashed::new(searcher.book),
+ library: LazyHash::new(library),
+ book: LazyHash::new(searcher.book),
fonts: searcher.fonts,
slots: Mutex::new(HashMap::new()),
now,
@@ -170,11 +170,11 @@ impl SystemWorld {
}
impl World for SystemWorld {
- fn library(&self) -> &Prehashed<Library> {
+ fn library(&self) -> &LazyHash<Library> {
&self.library
}
- fn book(&self) -> &Prehashed<FontBook> {
+ fn book(&self) -> &LazyHash<FontBook> {
&self.book
}
diff --git a/crates/typst-ide/src/lib.rs b/crates/typst-ide/src/lib.rs
index 3967aaad..117e75ab 100644
--- a/crates/typst-ide/src/lib.rs
+++ b/crates/typst-ide/src/lib.rs
@@ -93,12 +93,12 @@ fn summarize_font_family<'a>(variants: impl Iterator<Item = &'a FontInfo>) -> Ec
#[cfg(test)]
mod tests {
- use comemo::Prehashed;
use once_cell::sync::Lazy;
use typst::diag::{FileError, FileResult};
use typst::foundations::{Bytes, Datetime};
use typst::syntax::{FileId, Source};
use typst::text::{Font, FontBook};
+ use typst::utils::LazyHash;
use typst::{Library, World};
/// A world for IDE testing.
@@ -120,11 +120,11 @@ mod tests {
}
impl World for TestWorld {
- fn library(&self) -> &Prehashed<Library> {
+ fn library(&self) -> &LazyHash<Library> {
&self.base.library
}
- fn book(&self) -> &Prehashed<FontBook> {
+ fn book(&self) -> &LazyHash<FontBook> {
&self.base.book
}
@@ -155,8 +155,8 @@ mod tests {
/// Shared foundation of all test worlds.
struct TestBase {
- library: Prehashed<Library>,
- book: Prehashed<FontBook>,
+ library: LazyHash<Library>,
+ book: LazyHash<FontBook>,
fonts: Vec<Font>,
}
@@ -168,8 +168,8 @@ mod tests {
.collect();
Self {
- library: Prehashed::new(Library::default()),
- book: Prehashed::new(FontBook::from_fonts(&fonts)),
+ library: LazyHash::new(Library::default()),
+ book: LazyHash::new(FontBook::from_fonts(&fonts)),
fonts,
}
}
diff --git a/crates/typst-syntax/Cargo.toml b/crates/typst-syntax/Cargo.toml
index 001d405c..816f0d34 100644
--- a/crates/typst-syntax/Cargo.toml
+++ b/crates/typst-syntax/Cargo.toml
@@ -13,7 +13,7 @@ keywords = { workspace = true }
readme = { workspace = true }
[dependencies]
-comemo = { workspace = true }
+typst-utils = { workspace = true }
ecow = { workspace = true }
once_cell = { workspace = true }
serde = { workspace = true }
diff --git a/crates/typst-syntax/src/source.rs b/crates/typst-syntax/src/source.rs
index a68a53da..a2ccb5bb 100644
--- a/crates/typst-syntax/src/source.rs
+++ b/crates/typst-syntax/src/source.rs
@@ -6,7 +6,7 @@ use std::iter::zip;
use std::ops::Range;
use std::sync::Arc;
-use comemo::Prehashed;
+use typst_utils::LazyHash;
use crate::reparser::reparse;
use crate::{is_newline, parse, FileId, LinkedNode, Span, SyntaxNode, VirtualPath};
@@ -24,8 +24,8 @@ pub struct Source(Arc<Repr>);
#[derive(Clone)]
struct Repr {
id: FileId,
- text: Prehashed<String>,
- root: Prehashed<SyntaxNode>,
+ text: LazyHash<String>,
+ root: LazyHash<SyntaxNode>,
lines: Vec<Line>,
}
@@ -37,8 +37,8 @@ impl Source {
Self(Arc::new(Repr {
id,
lines: lines(&text),
- text: Prehashed::new(text),
- root: Prehashed::new(root),
+ text: LazyHash::new(text),
+ root: LazyHash::new(root),
}))
}
@@ -117,7 +117,7 @@ impl Source {
let inner = Arc::make_mut(&mut self.0);
// Update the text itself.
- inner.text.update(|text| text.replace_range(replace.clone(), with));
+ inner.text.replace_range(replace.clone(), with);
// Remove invalidated line starts.
inner.lines.truncate(line + 1);
@@ -135,9 +135,7 @@ impl Source {
));
// Incrementally reparse the replaced range.
- inner
- .root
- .update(|root| reparse(root, &inner.text, replace, with.len()))
+ reparse(&mut inner.root, &inner.text, replace, with.len())
}
/// Get the length of the file in UTF-8 encoded bytes.
diff --git a/crates/typst/src/lib.rs b/crates/typst/src/lib.rs
index bb869bfd..aad474a1 100644
--- a/crates/typst/src/lib.rs
+++ b/crates/typst/src/lib.rs
@@ -59,7 +59,7 @@ pub use typst_utils as utils;
use std::collections::HashSet;
use std::ops::{Deref, Range};
-use comemo::{Prehashed, Track, Tracked, Validate};
+use comemo::{Track, Tracked, Validate};
use ecow::{EcoString, EcoVec};
use typst_timing::{timed, TimingScope};
@@ -75,6 +75,7 @@ use crate::model::Document;
use crate::syntax::package::PackageSpec;
use crate::syntax::{FileId, Source, Span};
use crate::text::{Font, FontBook};
+use crate::utils::LazyHash;
use crate::visualize::Color;
/// Compile a source file into a fully layouted document.
@@ -194,10 +195,10 @@ pub trait World {
/// The standard library.
///
/// Can be created through `Library::build()`.
- fn library(&self) -> &Prehashed<Library>;
+ fn library(&self) -> &LazyHash<Library>;
/// Metadata about all known fonts.
- fn book(&self) -> &Prehashed<FontBook>;
+ fn book(&self) -> &LazyHash<FontBook>;
/// Access the main source file.
fn main(&self) -> Source;
@@ -234,11 +235,11 @@ pub trait World {
macro_rules! delegate_for_ptr {
($W:ident for $ptr:ty) => {
impl<$W: World> World for $ptr {
- fn library(&self) -> &Prehashed<Library> {
+ fn library(&self) -> &LazyHash<Library> {
self.deref().library()
}
- fn book(&self) -> &Prehashed<FontBook> {
+ fn book(&self) -> &LazyHash<FontBook> {
self.deref().book()
}
diff --git a/crates/typst/src/visualize/pattern.rs b/crates/typst/src/visualize/pattern.rs
index ed4d25ac..0f12b5d5 100644
--- a/crates/typst/src/visualize/pattern.rs
+++ b/crates/typst/src/visualize/pattern.rs
@@ -1,7 +1,6 @@
use std::hash::Hash;
use std::sync::Arc;
-use comemo::Prehashed;
use ecow::{eco_format, EcoString};
use crate::diag::{bail, SourceResult};
@@ -9,7 +8,7 @@ use crate::engine::Engine;
use crate::foundations::{func, repr, scope, ty, Content, Smart, StyleChain};
use crate::layout::{Abs, Axes, Frame, LayoutMultiple, Length, Regions, Size};
use crate::syntax::{Span, Spanned};
-use crate::utils::Numeric;
+use crate::utils::{LazyHash, Numeric};
use crate::visualize::RelativeTo;
use crate::World;
@@ -102,7 +101,7 @@ pub struct Pattern(Arc<Repr>);
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
struct Repr {
/// The pattern's rendered content.
- frame: Prehashed<Frame>,
+ frame: LazyHash<Frame>,
/// The pattern's tile size.
size: Size,
/// The pattern's tile spacing.
@@ -209,7 +208,7 @@ impl Pattern {
Ok(Self(Arc::new(Repr {
size: frame.size(),
- frame: Prehashed::new(frame),
+ frame: LazyHash::new(frame),
spacing: spacing.v.map(|l| l.abs),
relative,
})))
diff --git a/docs/src/html.rs b/docs/src/html.rs
index e88f0d3d..b5c83895 100644
--- a/docs/src/html.rs
+++ b/docs/src/html.rs
@@ -1,7 +1,6 @@
use std::fmt::{self, Debug, Formatter};
use std::ops::Range;
-use comemo::Prehashed;
use ecow::EcoString;
use heck::{ToKebabCase, ToTitleCase};
use pulldown_cmark as md;
@@ -13,6 +12,7 @@ use typst::foundations::{Bytes, Datetime};
use typst::layout::{Abs, Point, Size};
use typst::syntax::{FileId, Source, VirtualPath};
use typst::text::{Font, FontBook};
+use typst::utils::LazyHash;
use typst::{Library, World};
use unscanny::Scanner;
use yaml_front_matter::YamlFrontMatter;
@@ -457,11 +457,11 @@ fn nest_heading(level: &mut md::HeadingLevel, nesting: usize) {
struct DocWorld(Source);
impl World for DocWorld {
- fn library(&self) -> &Prehashed<Library> {
+ fn library(&self) -> &LazyHash<Library> {
&LIBRARY
}
- fn book(&self) -> &Prehashed<FontBook> {
+ fn book(&self) -> &LazyHash<FontBook> {
&FONTS.0
}
diff --git a/docs/src/lib.rs b/docs/src/lib.rs
index 765e7c7c..66242cbb 100644
--- a/docs/src/lib.rs
+++ b/docs/src/lib.rs
@@ -9,7 +9,6 @@ pub use self::contribs::*;
pub use self::html::*;
pub use self::model::*;
-use comemo::Prehashed;
use ecow::{eco_format, EcoString};
use once_cell::sync::Lazy;
use serde::Deserialize;
@@ -28,6 +27,7 @@ use typst::model::Document;
use typst::model::MODEL;
use typst::symbols::SYMBOLS;
use typst::text::{Font, FontBook, TEXT};
+use typst::utils::LazyHash;
use typst::visualize::VISUALIZE;
use typst::Library;
@@ -54,7 +54,7 @@ static GROUPS: Lazy<Vec<GroupData>> = Lazy::new(|| {
groups
});
-static LIBRARY: Lazy<Prehashed<Library>> = Lazy::new(|| {
+static LIBRARY: Lazy<LazyHash<Library>> = Lazy::new(|| {
let mut lib = Library::default();
lib.styles
.set(PageElem::set_width(Smart::Custom(Abs::pt(240.0).into())));
@@ -62,16 +62,16 @@ static LIBRARY: Lazy<Prehashed<Library>> = Lazy::new(|| {
lib.styles.set(PageElem::set_margin(Margin::splat(Some(Smart::Custom(
Abs::pt(15.0).into(),
)))));
- Prehashed::new(lib)
+ LazyHash::new(lib)
});
-static FONTS: Lazy<(Prehashed<FontBook>, Vec<Font>)> = Lazy::new(|| {
+static FONTS: Lazy<(LazyHash<FontBook>, Vec<Font>)> = Lazy::new(|| {
let fonts: Vec<_> = typst_assets::fonts()
.chain(typst_dev_assets::fonts())
.flat_map(|data| Font::iter(Bytes::from_static(data)))
.collect();
let book = FontBook::from_fonts(&fonts);
- (Prehashed::new(book), fonts)
+ (LazyHash::new(book), fonts)
});
/// Build documentation pages.
diff --git a/tests/fuzz/src/compile.rs b/tests/fuzz/src/compile.rs
index 2d445bf5..4dbea410 100644
--- a/tests/fuzz/src/compile.rs
+++ b/tests/fuzz/src/compile.rs
@@ -1,18 +1,18 @@
#![no_main]
-use comemo::Prehashed;
use libfuzzer_sys::fuzz_target;
use typst::diag::{FileError, FileResult};
use typst::eval::Tracer;
use typst::foundations::{Bytes, Datetime};
use typst::syntax::{FileId, Source};
use typst::text::{Font, FontBook};
+use typst::utils::LazyHash;
use typst::visualize::Color;
use typst::{Library, World};
struct FuzzWorld {
- library: Prehashed<Library>,
- book: Prehashed<FontBook>,
+ library: LazyHash<Library>,
+ book: LazyHash<FontBook>,
font: Font,
source: Source,
}
@@ -23,8 +23,8 @@ impl FuzzWorld {
let font = Font::new(Bytes::from_static(data), 0).unwrap();
let book = FontBook::from_fonts([&font]);
Self {
- library: Prehashed::new(Library::default()),
- book: Prehashed::new(book),
+ library: LazyHash::new(Library::default()),
+ book: LazyHash::new(book),
font,
source: Source::detached(text),
}
@@ -32,11 +32,11 @@ impl FuzzWorld {
}
impl World for FuzzWorld {
- fn library(&self) -> &Prehashed<Library> {
+ fn library(&self) -> &LazyHash<Library> {
&self.library
}
- fn book(&self) -> &Prehashed<FontBook> {
+ fn book(&self) -> &LazyHash<FontBook> {
&self.book
}
diff --git a/tests/src/world.rs b/tests/src/world.rs
index 86ee8da6..ad925a53 100644
--- a/tests/src/world.rs
+++ b/tests/src/world.rs
@@ -5,7 +5,6 @@ use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
-use comemo::Prehashed;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use typst::diag::{bail, FileError, FileResult, StrResult};
@@ -13,6 +12,7 @@ use typst::foundations::{func, Bytes, Datetime, NoneValue, Repr, Smart, Value};
use typst::layout::{Abs, Margin, PageElem};
use typst::syntax::{FileId, Source};
use typst::text::{Font, FontBook, TextElem, TextSize};
+use typst::utils::LazyHash;
use typst::visualize::Color;
use typst::{Library, World};
@@ -35,11 +35,11 @@ impl TestWorld {
}
impl World for TestWorld {
- fn library(&self) -> &Prehashed<Library> {
+ fn library(&self) -> &LazyHash<Library> {
&self.base.library
}
- fn book(&self) -> &Prehashed<FontBook> {
+ fn book(&self) -> &LazyHash<FontBook> {
&self.base.book
}
@@ -81,8 +81,8 @@ impl TestWorld {
/// Shared foundation of all test worlds.
struct TestBase {
- library: Prehashed<Library>,
- book: Prehashed<FontBook>,
+ library: LazyHash<Library>,
+ book: LazyHash<FontBook>,
fonts: Vec<Font>,
slots: Mutex<HashMap<FileId, FileSlot>>,
}
@@ -95,8 +95,8 @@ impl Default for TestBase {
.collect();
Self {
- library: Prehashed::new(library()),
- book: Prehashed::new(FontBook::from_fonts(&fonts)),
+ library: LazyHash::new(library()),
+ book: LazyHash::new(FontBook::from_fonts(&fonts)),
fonts,
slots: Mutex::new(HashMap::new()),
}