From b969c01b282287b44c3e131f8c0c53dcbb304e30 Mon Sep 17 00:00:00 2001 From: Abdul-Rahman Sibahi Date: Thu, 31 Oct 2024 14:52:11 +0300 Subject: [PATCH] Replace `once_cell`'s `Lazy` as much as possible (#4617) Co-authored-by: Laurenz --- Cargo.lock | 8 -------- crates/typst-cli/Cargo.toml | 1 - crates/typst-cli/src/main.rs | 4 ++-- crates/typst-cli/src/world.rs | 7 +++---- crates/typst-layout/Cargo.toml | 1 - crates/typst-layout/src/flow/block.rs | 11 ++++++----- crates/typst-layout/src/flow/collect.rs | 5 ++--- crates/typst-layout/src/inline/box.rs | 7 ++++--- crates/typst-layout/src/inline/linebreak.rs | 10 +++++----- crates/typst-layout/src/transforms.rs | 5 +++-- crates/typst-library/Cargo.toml | 1 - crates/typst-library/src/foundations/element.rs | 6 +++--- crates/typst-library/src/foundations/func.rs | 9 ++++----- crates/typst-library/src/foundations/mod.rs | 1 - crates/typst-library/src/foundations/ty.rs | 6 +++--- crates/typst-library/src/model/bibliography.rs | 7 +++---- crates/typst-library/src/text/mod.rs | 4 ++-- crates/typst-library/src/text/raw.rs | 13 ++++++------- crates/typst-library/src/visualize/color.rs | 11 ++++++----- crates/typst-macros/src/elem.rs | 8 ++++---- crates/typst-macros/src/func.rs | 6 +++--- crates/typst-macros/src/ty.rs | 4 ++-- crates/typst-pdf/Cargo.toml | 1 - crates/typst-pdf/src/color.rs | 11 ++++++----- crates/typst-realize/Cargo.toml | 1 - crates/typst-realize/src/lib.rs | 6 +++--- crates/typst-syntax/Cargo.toml | 1 - crates/typst-syntax/src/file.rs | 9 ++++----- crates/typst-utils/src/macros.rs | 3 +-- crates/typst-utils/src/pico.rs | 9 ++++----- docs/Cargo.toml | 1 - docs/src/lib.rs | 8 ++++---- tests/Cargo.toml | 1 - tests/src/collect.rs | 4 ++-- tests/src/tests.rs | 4 ++-- 35 files changed, 87 insertions(+), 107 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4fbf6bd76..4709fb5b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2690,7 +2690,6 @@ dependencies = [ "fs_extra", "native-tls", "notify", - "once_cell", "open", "parking_lot", "pathdiff", @@ -2730,7 +2729,6 @@ dependencies = [ "clap", "ecow", "heck", - "once_cell", "pulldown-cmark", "serde", "serde_json", @@ -2828,7 +2826,6 @@ dependencies = [ "icu_provider_blob", "icu_segmenter", "kurbo", - "once_cell", "rustybuzz", "smallvec", "ttf-parser", @@ -2867,7 +2864,6 @@ dependencies = [ "kamadak-exif", "kurbo", "lipsum", - "once_cell", "palette", "phf", "png", @@ -2924,7 +2920,6 @@ dependencies = [ "image", "indexmap 2.6.0", "miniz_oxide", - "once_cell", "pdf-writer", "serde", "subsetter", @@ -2947,7 +2942,6 @@ dependencies = [ "bumpalo", "comemo", "ecow", - "once_cell", "regex", "typst-library", "typst-macros", @@ -2994,7 +2988,6 @@ name = "typst-syntax" version = "0.12.0" dependencies = [ "ecow", - "once_cell", "serde", "toml", "typst-timing", @@ -3013,7 +3006,6 @@ dependencies = [ "clap", "comemo", "ecow", - "once_cell", "oxipng", "parking_lot", "rayon", diff --git a/crates/typst-cli/Cargo.toml b/crates/typst-cli/Cargo.toml index 855d7beeb..2ecade545 100644 --- a/crates/typst-cli/Cargo.toml +++ b/crates/typst-cli/Cargo.toml @@ -36,7 +36,6 @@ ecow = { workspace = true } fs_extra = { workspace = true } native-tls = { workspace = true } notify = { workspace = true } -once_cell = { workspace = true } open = { workspace = true } parking_lot = { workspace = true } pathdiff = { workspace = true } diff --git a/crates/typst-cli/src/main.rs b/crates/typst-cli/src/main.rs index 464ad6240..631befe56 100644 --- a/crates/typst-cli/src/main.rs +++ b/crates/typst-cli/src/main.rs @@ -16,12 +16,12 @@ mod world; use std::cell::Cell; use std::io::{self, Write}; use std::process::ExitCode; +use std::sync::LazyLock; use clap::error::ErrorKind; use clap::Parser; use codespan_reporting::term; use codespan_reporting::term::termcolor::WriteColor; -use once_cell::sync::Lazy; use typst::diag::HintedStrResult; use crate::args::{CliArguments, Command}; @@ -33,7 +33,7 @@ thread_local! { } /// The parsed command line arguments. -static ARGS: Lazy = Lazy::new(|| { +static ARGS: LazyLock = LazyLock::new(|| { CliArguments::try_parse().unwrap_or_else(|error| { if error.kind() == ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand { crate::greet::greet(); diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs index b790f41af..f35975442 100644 --- a/crates/typst-cli/src/world.rs +++ b/crates/typst-cli/src/world.rs @@ -1,12 +1,11 @@ use std::collections::HashMap; use std::io::Read; use std::path::{Path, PathBuf}; -use std::sync::OnceLock; +use std::sync::{LazyLock, OnceLock}; use std::{fmt, fs, io, mem}; use chrono::{DateTime, Datelike, FixedOffset, Local, Utc}; use ecow::{eco_format, EcoString}; -use once_cell::sync::Lazy; use parking_lot::Mutex; use typst::diag::{FileError, FileResult}; use typst::foundations::{Bytes, Datetime, Dict, IntoValue}; @@ -25,8 +24,8 @@ use crate::package; /// Static `FileId` allocated for stdin. /// This is to ensure that a file is read in the correct way. -static STDIN_ID: Lazy = - Lazy::new(|| FileId::new_fake(VirtualPath::new(""))); +static STDIN_ID: LazyLock = + LazyLock::new(|| FileId::new_fake(VirtualPath::new(""))); /// A world that provides access to the operating system. pub struct SystemWorld { diff --git a/crates/typst-layout/Cargo.toml b/crates/typst-layout/Cargo.toml index 4c133a4ec..438e09e43 100644 --- a/crates/typst-layout/Cargo.toml +++ b/crates/typst-layout/Cargo.toml @@ -30,7 +30,6 @@ icu_provider_adapters = { workspace = true } icu_provider_blob = { workspace = true } icu_segmenter = { workspace = true } kurbo = { workspace = true } -once_cell = { workspace = true } rustybuzz = { workspace = true } smallvec = { workspace = true } ttf-parser = { workspace = true } diff --git a/crates/typst-layout/src/flow/block.rs b/crates/typst-layout/src/flow/block.rs index 48ca1fba7..71eacc1ce 100644 --- a/crates/typst-layout/src/flow/block.rs +++ b/crates/typst-layout/src/flow/block.rs @@ -1,4 +1,5 @@ -use once_cell::unsync::Lazy; +use std::cell::LazyCell; + use smallvec::SmallVec; use typst_library::diag::SourceResult; use typst_library::engine::Engine; @@ -79,8 +80,8 @@ pub fn layout_single_block( .map(|s| s.map(Stroke::unwrap_or_default)); // Only fetch these if necessary (for clipping or filling/stroking). - let outset = Lazy::new(|| elem.outset(styles).unwrap_or_default()); - let radius = Lazy::new(|| elem.radius(styles).unwrap_or_default()); + let outset = LazyCell::new(|| elem.outset(styles).unwrap_or_default()); + let radius = LazyCell::new(|| elem.radius(styles).unwrap_or_default()); // Clip the contents, if requested. if elem.clip(styles) { @@ -194,8 +195,8 @@ pub fn layout_multi_block( .map(|s| s.map(Stroke::unwrap_or_default)); // Only fetch these if necessary (for clipping or filling/stroking). - let outset = Lazy::new(|| elem.outset(styles).unwrap_or_default()); - let radius = Lazy::new(|| elem.radius(styles).unwrap_or_default()); + let outset = LazyCell::new(|| elem.outset(styles).unwrap_or_default()); + let radius = LazyCell::new(|| elem.radius(styles).unwrap_or_default()); // Fetch/compute these outside of the loop. let clip = elem.clip(styles); diff --git a/crates/typst-layout/src/flow/collect.rs b/crates/typst-layout/src/flow/collect.rs index aee5d5081..d39743491 100644 --- a/crates/typst-layout/src/flow/collect.rs +++ b/crates/typst-layout/src/flow/collect.rs @@ -1,11 +1,10 @@ -use std::cell::RefCell; +use std::cell::{LazyCell, RefCell}; use std::fmt::{self, Debug, Formatter}; use std::hash::Hash; use bumpalo::boxed::Box as BumpBox; use bumpalo::Bump; use comemo::{Track, Tracked, TrackedMut}; -use once_cell::unsync::Lazy; use typst_library::diag::{bail, SourceResult}; use typst_library::engine::{Engine, Route, Sink, Traced}; use typst_library::foundations::{Packed, Resolve, Smart, StyleChain}; @@ -182,7 +181,7 @@ impl<'a> Collector<'a, '_, '_> { _ => None, }; - let fallback = Lazy::new(|| ParElem::spacing_in(styles)); + let fallback = LazyCell::new(|| ParElem::spacing_in(styles)); let spacing = |amount| match amount { Smart::Auto => Child::Rel((*fallback).into(), 4), Smart::Custom(Spacing::Rel(rel)) => Child::Rel(rel.resolve(styles), 3), diff --git a/crates/typst-layout/src/inline/box.rs b/crates/typst-layout/src/inline/box.rs index 62054bead..6dfbc9696 100644 --- a/crates/typst-layout/src/inline/box.rs +++ b/crates/typst-layout/src/inline/box.rs @@ -1,4 +1,5 @@ -use once_cell::unsync::Lazy; +use std::cell::LazyCell; + use typst_library::diag::SourceResult; use typst_library::engine::Engine; use typst_library::foundations::{Packed, StyleChain}; @@ -56,8 +57,8 @@ pub fn layout_box( .map(|s| s.map(Stroke::unwrap_or_default)); // Only fetch these if necessary (for clipping or filling/stroking). - let outset = Lazy::new(|| elem.outset(styles).unwrap_or_default()); - let radius = Lazy::new(|| elem.radius(styles).unwrap_or_default()); + let outset = LazyCell::new(|| elem.outset(styles).unwrap_or_default()); + let radius = LazyCell::new(|| elem.radius(styles).unwrap_or_default()); // Clip the contents, if requested. if elem.clip(styles) { diff --git a/crates/typst-layout/src/inline/linebreak.rs b/crates/typst-layout/src/inline/linebreak.rs index 7fc8b3683..26621cd7b 100644 --- a/crates/typst-layout/src/inline/linebreak.rs +++ b/crates/typst-layout/src/inline/linebreak.rs @@ -1,4 +1,5 @@ use std::ops::{Add, Sub}; +use std::sync::LazyLock; use az::SaturatingAs; use icu_properties::maps::{CodePointMapData, CodePointMapDataBorrowed}; @@ -7,7 +8,6 @@ use icu_provider::AsDeserializingBufferProvider; use icu_provider_adapters::fork::ForkByKeyProvider; use icu_provider_blob::BlobDataProvider; use icu_segmenter::LineSegmenter; -use once_cell::sync::Lazy; use typst_library::engine::Engine; use typst_library::layout::{Abs, Em}; use typst_library::model::Linebreaks; @@ -40,11 +40,11 @@ fn blob() -> BlobDataProvider { } /// The general line break segmenter. -static SEGMENTER: Lazy = - Lazy::new(|| LineSegmenter::try_new_lstm_with_buffer_provider(&blob()).unwrap()); +static SEGMENTER: LazyLock = + LazyLock::new(|| LineSegmenter::try_new_lstm_with_buffer_provider(&blob()).unwrap()); /// The line break segmenter for Chinese/Japanese text. -static CJ_SEGMENTER: Lazy = Lazy::new(|| { +static CJ_SEGMENTER: LazyLock = LazyLock::new(|| { let cj_blob = BlobDataProvider::try_new_from_static_blob(typst_assets::icu::ICU_CJ_SEGMENT) .unwrap(); @@ -53,7 +53,7 @@ static CJ_SEGMENTER: Lazy = Lazy::new(|| { }); /// The Unicode line break properties for each code point. -static LINEBREAK_DATA: Lazy> = Lazy::new(|| { +static LINEBREAK_DATA: LazyLock> = LazyLock::new(|| { icu_properties::maps::load_line_break(&blob().as_deserializing()).unwrap() }); diff --git a/crates/typst-layout/src/transforms.rs b/crates/typst-layout/src/transforms.rs index 5ac9f7776..e0f29c4c2 100644 --- a/crates/typst-layout/src/transforms.rs +++ b/crates/typst-layout/src/transforms.rs @@ -1,4 +1,5 @@ -use once_cell::unsync::Lazy; +use std::cell::LazyCell; + use typst_library::diag::{bail, SourceResult}; use typst_library::engine::Engine; use typst_library::foundations::{Content, Packed, Resolve, Smart, StyleChain}; @@ -113,7 +114,7 @@ fn resolve_scale( }) } - let size = Lazy::new(|| { + let size = LazyCell::new(|| { let pod = Region::new(container, Axes::splat(false)); let frame = crate::layout_frame(engine, &elem.body, locator, styles, pod)?; SourceResult::Ok(frame.size()) diff --git a/crates/typst-library/Cargo.toml b/crates/typst-library/Cargo.toml index de28f0010..a1fb1033f 100644 --- a/crates/typst-library/Cargo.toml +++ b/crates/typst-library/Cargo.toml @@ -37,7 +37,6 @@ indexmap = { workspace = true } kamadak-exif = { workspace = true } kurbo = { workspace = true } lipsum = { workspace = true } -once_cell = { workspace = true } palette = { workspace = true } phf = { workspace = true } png = { workspace = true } diff --git a/crates/typst-library/src/foundations/element.rs b/crates/typst-library/src/foundations/element.rs index 8da71965e..7ff00b9df 100644 --- a/crates/typst-library/src/foundations/element.rs +++ b/crates/typst-library/src/foundations/element.rs @@ -3,9 +3,9 @@ use std::cmp::Ordering; use std::fmt::{self, Debug}; use std::hash::Hash; use std::ptr::NonNull; +use std::sync::LazyLock; use ecow::EcoString; -use once_cell::sync::Lazy; use smallvec::SmallVec; #[doc(inline)] pub use typst_macros::elem; @@ -292,9 +292,9 @@ pub struct NativeElementData { pub field_from_styles: fn(u8, StyleChain) -> Result, /// Gets the localized name for this element (see [`LocalName`][crate::text::LocalName]). pub local_name: Option) -> &'static str>, - pub scope: Lazy, + pub scope: LazyLock, /// A list of parameter information for each field. - pub params: Lazy>, + pub params: LazyLock>, } impl From<&'static NativeElementData> for Element { diff --git a/crates/typst-library/src/foundations/func.rs b/crates/typst-library/src/foundations/func.rs index 1b40714b2..e34f48a17 100644 --- a/crates/typst-library/src/foundations/func.rs +++ b/crates/typst-library/src/foundations/func.rs @@ -2,11 +2,10 @@ pub use typst_macros::func; use std::fmt::{self, Debug, Formatter}; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use comemo::{Tracked, TrackedMut}; use ecow::{eco_format, EcoString}; -use once_cell::sync::Lazy; use typst_syntax::{ast, Span, SyntaxNode}; use typst_utils::{singleton, LazyHash, Static}; @@ -463,11 +462,11 @@ pub struct NativeFuncData { pub keywords: &'static [&'static str], /// Whether this function makes use of context. pub contextual: bool, - pub scope: Lazy, + pub scope: LazyLock, /// A list of parameter information for each parameter. - pub params: Lazy>, + pub params: LazyLock>, /// Information about the return value of this function. - pub returns: Lazy, + pub returns: LazyLock, } impl From<&'static NativeFuncData> for Func { diff --git a/crates/typst-library/src/foundations/mod.rs b/crates/typst-library/src/foundations/mod.rs index a6d6c2539..9259a7d16 100644 --- a/crates/typst-library/src/foundations/mod.rs +++ b/crates/typst-library/src/foundations/mod.rs @@ -71,7 +71,6 @@ pub use typst_macros::{scope, ty}; pub use { ecow::{eco_format, eco_vec}, indexmap::IndexMap, - once_cell::sync::Lazy, }; use ecow::EcoString; diff --git a/crates/typst-library/src/foundations/ty.rs b/crates/typst-library/src/foundations/ty.rs index 70845dd24..680c4f6a1 100644 --- a/crates/typst-library/src/foundations/ty.rs +++ b/crates/typst-library/src/foundations/ty.rs @@ -3,9 +3,9 @@ pub use typst_macros::{scope, ty}; use std::cmp::Ordering; use std::fmt::{self, Debug, Display, Formatter}; +use std::sync::LazyLock; use ecow::{eco_format, EcoString}; -use once_cell::sync::Lazy; use typst_utils::Static; use crate::diag::StrResult; @@ -207,8 +207,8 @@ pub struct NativeTypeData { /// A list of alternate search terms for this type. pub keywords: &'static [&'static str], /// The constructor for this type. - pub constructor: Lazy>, - pub scope: Lazy, + pub constructor: LazyLock>, + pub scope: LazyLock, } impl From<&'static NativeTypeData> for Type { diff --git a/crates/typst-library/src/model/bibliography.rs b/crates/typst-library/src/model/bibliography.rs index d11055b95..569167311 100644 --- a/crates/typst-library/src/model/bibliography.rs +++ b/crates/typst-library/src/model/bibliography.rs @@ -4,7 +4,7 @@ use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; use std::num::NonZeroUsize; use std::path::Path; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use comemo::Tracked; use ecow::{eco_format, EcoString, EcoVec}; @@ -15,7 +15,6 @@ use hayagriva::{ SpecificLocator, }; use indexmap::IndexMap; -use once_cell::sync::Lazy; use smallvec::{smallvec, SmallVec}; use typed_arena::Arena; use typst_syntax::{Span, Spanned}; @@ -633,8 +632,8 @@ impl<'a> Generator<'a> { /// Drives hayagriva's citation driver. fn drive(&mut self) -> hayagriva::Rendered { - static LOCALES: Lazy> = - Lazy::new(hayagriva::archive::locales); + static LOCALES: LazyLock> = + LazyLock::new(hayagriva::archive::locales); let database = self.bibliography.bibliography(); let bibliography_style = self.bibliography.style(StyleChain::default()); diff --git a/crates/typst-library/src/text/mod.rs b/crates/typst-library/src/text/mod.rs index acf100b57..6aeebbbaf 100644 --- a/crates/typst-library/src/text/mod.rs +++ b/crates/typst-library/src/text/mod.rs @@ -29,12 +29,12 @@ pub use self::smartquote::*; pub use self::space::*; use std::fmt::{self, Debug, Formatter}; +use std::sync::LazyLock; use ecow::{eco_format, EcoString}; use icu_properties::sets::CodePointSetData; use icu_provider::AsDeserializingBufferProvider; use icu_provider_blob::BlobDataProvider; -use once_cell::sync::Lazy; use rustybuzz::Feature; use smallvec::SmallVec; use ttf_parser::Tag; @@ -1275,7 +1275,7 @@ cast! { /// Whether a codepoint is Unicode `Default_Ignorable`. pub fn is_default_ignorable(c: char) -> bool { /// The set of Unicode default ignorables. - static DEFAULT_IGNORABLE_DATA: Lazy = Lazy::new(|| { + static DEFAULT_IGNORABLE_DATA: LazyLock = LazyLock::new(|| { icu_properties::sets::load_default_ignorable_code_point( &BlobDataProvider::try_new_from_static_blob(typst_assets::icu::ICU) .unwrap() diff --git a/crates/typst-library/src/text/raw.rs b/crates/typst-library/src/text/raw.rs index 5ce773481..8691afcb3 100644 --- a/crates/typst-library/src/text/raw.rs +++ b/crates/typst-library/src/text/raw.rs @@ -1,10 +1,9 @@ +use std::cell::LazyCell; use std::hash::Hash; use std::ops::Range; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use ecow::{eco_format, EcoString, EcoVec}; -use once_cell::sync::Lazy; -use once_cell::unsync::Lazy as UnsyncLazy; use syntect::highlighting::{self as synt, Theme}; use syntect::parsing::{SyntaxDefinition, SyntaxSet, SyntaxSetBuilder}; use typst_syntax::{split_newlines, LinkedNode, Span, Spanned}; @@ -325,7 +324,7 @@ impl Packed { .map(|s| s.to_lowercase()) .or(Some("txt".into())); - let extra_syntaxes = UnsyncLazy::new(|| { + let extra_syntaxes = LazyCell::new(|| { load_syntaxes(&elem.syntaxes(styles), &elem.syntaxes_data(styles)).unwrap() }); let non_highlighted_result = |lines: EcoVec<(EcoString, Span)>| { @@ -838,11 +837,11 @@ fn parse_theme( /// /// Syntax set is generated from the syntaxes from the `bat` project /// -pub static RAW_SYNTAXES: Lazy = - Lazy::new(two_face::syntax::extra_no_newlines); +pub static RAW_SYNTAXES: LazyLock = + LazyLock::new(two_face::syntax::extra_no_newlines); /// The default theme used for syntax highlighting. -pub static RAW_THEME: Lazy = Lazy::new(|| synt::Theme { +pub static RAW_THEME: LazyLock = LazyLock::new(|| synt::Theme { name: Some("Typst Light".into()), author: Some("The Typst Project Developers".into()), settings: synt::ThemeSettings::default(), diff --git a/crates/typst-library/src/visualize/color.rs b/crates/typst-library/src/visualize/color.rs index 1a279fbb8..8ff8dbdbc 100644 --- a/crates/typst-library/src/visualize/color.rs +++ b/crates/typst-library/src/visualize/color.rs @@ -1,9 +1,9 @@ use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; use std::str::FromStr; +use std::sync::LazyLock; use ecow::{eco_format, EcoString, EcoVec}; -use once_cell::sync::Lazy; use palette::encoding::{self, Linear}; use palette::{ Alpha, Darken, Desaturate, FromColor, Lighten, OklabHue, RgbHue, Saturate, ShiftHue, @@ -33,17 +33,18 @@ pub type Luma = palette::luma::Lumaa; /// to convert from CMYK to RGB. It is based on the CGATS TR 001-1995 /// specification. See /// . -static CMYK_TO_XYZ: Lazy> = - Lazy::new(|| Profile::new_from_slice(typst_assets::icc::CMYK_TO_XYZ, false).unwrap()); +static CMYK_TO_XYZ: LazyLock> = LazyLock::new(|| { + Profile::new_from_slice(typst_assets::icc::CMYK_TO_XYZ, false).unwrap() +}); /// The target sRGB profile. -static SRGB_PROFILE: Lazy> = Lazy::new(|| { +static SRGB_PROFILE: LazyLock> = LazyLock::new(|| { let mut out = Profile::new_sRGB(); out.precache_output_transform(); out }); -static TO_SRGB: Lazy = Lazy::new(|| { +static TO_SRGB: LazyLock = LazyLock::new(|| { qcms::Transform::new_to( &CMYK_TO_XYZ, &SRGB_PROFILE, diff --git a/crates/typst-macros/src/elem.rs b/crates/typst-macros/src/elem.rs index 3b968f762..78a3c1800 100644 --- a/crates/typst-macros/src/elem.rs +++ b/crates/typst-macros/src/elem.rs @@ -432,8 +432,8 @@ fn create_default_static(field: &Field) -> TokenStream { }; quote! { - static #const_ident: ::once_cell::sync::Lazy<#ty> = - ::once_cell::sync::Lazy::new(#init); + static #const_ident: ::std::sync::LazyLock<#ty> = + ::std::sync::LazyLock::new(#init); } } @@ -660,8 +660,8 @@ fn create_native_elem_impl(element: &Elem) -> TokenStream { field_name: |id| id.try_into().ok().map(Fields::to_str), field_from_styles: <#ident as #foundations::Fields>::field_from_styles, local_name: #local_name, - scope: #foundations::Lazy::new(|| #scope), - params: #foundations::Lazy::new(|| ::std::vec![#(#params),*]) + scope: ::std::sync::LazyLock::new(|| #scope), + params: ::std::sync::LazyLock::new(|| ::std::vec![#(#params),*]) } }; diff --git a/crates/typst-macros/src/func.rs b/crates/typst-macros/src/func.rs index d402efd99..b8ab7a364 100644 --- a/crates/typst-macros/src/func.rs +++ b/crates/typst-macros/src/func.rs @@ -321,9 +321,9 @@ fn create_func_data(func: &Func) -> TokenStream { docs: #docs, keywords: &[#(#keywords),*], contextual: #contextual, - scope: #foundations::Lazy::new(|| #scope), - params: #foundations::Lazy::new(|| ::std::vec![#(#params),*]), - returns: #foundations::Lazy::new(|| <#returns as #foundations::Reflect>::output()), + scope: ::std::sync::LazyLock::new(|| #scope), + params: ::std::sync::LazyLock::new(|| ::std::vec![#(#params),*]), + returns: ::std::sync::LazyLock::new(|| <#returns as #foundations::Reflect>::output()), } } } diff --git a/crates/typst-macros/src/ty.rs b/crates/typst-macros/src/ty.rs index 200798af0..662849044 100644 --- a/crates/typst-macros/src/ty.rs +++ b/crates/typst-macros/src/ty.rs @@ -102,8 +102,8 @@ fn create(ty: &Type, item: Option<&syn::Item>) -> TokenStream { title: #title, docs: #docs, keywords: &[#(#keywords),*], - constructor: #foundations::Lazy::new(|| #constructor), - scope: #foundations::Lazy::new(|| #scope), + constructor: ::std::sync::LazyLock::new(|| #constructor), + scope: ::std::sync::LazyLock::new(|| #scope), } }; diff --git a/crates/typst-pdf/Cargo.toml b/crates/typst-pdf/Cargo.toml index 4cef14bd9..bc0da06c3 100644 --- a/crates/typst-pdf/Cargo.toml +++ b/crates/typst-pdf/Cargo.toml @@ -27,7 +27,6 @@ ecow = { workspace = true } image = { workspace = true } indexmap = { workspace = true } miniz_oxide = { workspace = true } -once_cell = { workspace = true } pdf-writer = { workspace = true } serde = { workspace = true } subsetter = { workspace = true } diff --git a/crates/typst-pdf/src/color.rs b/crates/typst-pdf/src/color.rs index 26f2044c8..7097c14a5 100644 --- a/crates/typst-pdf/src/color.rs +++ b/crates/typst-pdf/src/color.rs @@ -1,5 +1,6 @@ +use std::sync::LazyLock; + use arrayvec::ArrayVec; -use once_cell::sync::Lazy; use pdf_writer::{writers, Chunk, Dict, Filter, Name, Ref}; use typst_library::diag::{bail, SourceResult}; use typst_library::visualize::{Color, ColorSpace, Paint}; @@ -13,10 +14,10 @@ pub const D65_GRAY: Name<'static> = Name(b"d65gray"); pub const LINEAR_SRGB: Name<'static> = Name(b"linearrgb"); // The ICC profiles. -static SRGB_ICC_DEFLATED: Lazy> = - Lazy::new(|| deflate(typst_assets::icc::S_RGB_V4)); -static GRAY_ICC_DEFLATED: Lazy> = - Lazy::new(|| deflate(typst_assets::icc::S_GREY_V4)); +static SRGB_ICC_DEFLATED: LazyLock> = + LazyLock::new(|| deflate(typst_assets::icc::S_RGB_V4)); +static GRAY_ICC_DEFLATED: LazyLock> = + LazyLock::new(|| deflate(typst_assets::icc::S_GREY_V4)); /// The color spaces present in the PDF document #[derive(Default)] diff --git a/crates/typst-realize/Cargo.toml b/crates/typst-realize/Cargo.toml index 78bda2598..cbf0cbca5 100644 --- a/crates/typst-realize/Cargo.toml +++ b/crates/typst-realize/Cargo.toml @@ -22,7 +22,6 @@ arrayvec = { workspace = true } bumpalo = { workspace = true } comemo = { workspace = true } ecow = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } [lints] diff --git a/crates/typst-realize/src/lib.rs b/crates/typst-realize/src/lib.rs index 586689625..ec7ee0ae5 100644 --- a/crates/typst-realize/src/lib.rs +++ b/crates/typst-realize/src/lib.rs @@ -5,12 +5,12 @@ //! further. use std::borrow::Cow; +use std::cell::LazyCell; use arrayvec::ArrayVec; use bumpalo::collections::{String as BumpString, Vec as BumpVec}; use comemo::Track; use ecow::EcoString; -use once_cell::unsync::Lazy; use typst_library::diag::{bail, At, SourceResult}; use typst_library::engine::Engine; use typst_library::foundations::{ @@ -420,7 +420,7 @@ fn verdict<'a>( // it to determine whether a particular show rule was already applied to the // `target` previously. For this purpose, show rules are indexed from the // top of the chain as the chain might grow to the bottom. - let depth = Lazy::new(|| styles.recipes().count()); + let depth = LazyCell::new(|| styles.recipes().count()); for (r, recipe) in styles.recipes().enumerate() { // We're not interested in recipes that don't match. @@ -1032,7 +1032,7 @@ fn find_regex_match_in_str<'a>( let mut revoked = SmallBitSet::new(); let mut leftmost: Option<(regex::Match, RecipeIndex, &Recipe)> = None; - let depth = Lazy::new(|| styles.recipes().count()); + let depth = LazyCell::new(|| styles.recipes().count()); for entry in styles.entries() { let recipe = match &**entry { diff --git a/crates/typst-syntax/Cargo.toml b/crates/typst-syntax/Cargo.toml index 3dc983ebd..263595bd4 100644 --- a/crates/typst-syntax/Cargo.toml +++ b/crates/typst-syntax/Cargo.toml @@ -16,7 +16,6 @@ readme = { workspace = true } typst-timing = { workspace = true } typst-utils = { workspace = true } ecow = { workspace = true } -once_cell = { workspace = true } serde = { workspace = true } toml = { workspace = true } unicode-ident = { workspace = true } diff --git a/crates/typst-syntax/src/file.rs b/crates/typst-syntax/src/file.rs index bc7dd3148..e24fc8fb1 100644 --- a/crates/typst-syntax/src/file.rs +++ b/crates/typst-syntax/src/file.rs @@ -2,16 +2,15 @@ use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; -use std::sync::RwLock; - -use once_cell::sync::Lazy; +use std::sync::{LazyLock, RwLock}; use crate::package::PackageSpec; use crate::VirtualPath; /// The global package-path interner. -static INTERNER: Lazy> = - Lazy::new(|| RwLock::new(Interner { to_id: HashMap::new(), from_id: Vec::new() })); +static INTERNER: LazyLock> = LazyLock::new(|| { + RwLock::new(Interner { to_id: HashMap::new(), from_id: Vec::new() }) +}); /// A package-path interner. struct Interner { diff --git a/crates/typst-utils/src/macros.rs b/crates/typst-utils/src/macros.rs index dd60a2e0f..e286d05fe 100644 --- a/crates/typst-utils/src/macros.rs +++ b/crates/typst-utils/src/macros.rs @@ -2,8 +2,7 @@ #[macro_export] macro_rules! singleton { ($ty:ty, $value:expr) => {{ - static VALUE: $crate::once_cell::sync::Lazy<$ty> = - $crate::once_cell::sync::Lazy::new(|| $value); + static VALUE: ::std::sync::LazyLock<$ty> = ::std::sync::LazyLock::new(|| $value); &*VALUE }}; } diff --git a/crates/typst-utils/src/pico.rs b/crates/typst-utils/src/pico.rs index d531f14a5..dcab39b6d 100644 --- a/crates/typst-utils/src/pico.rs +++ b/crates/typst-utils/src/pico.rs @@ -1,13 +1,12 @@ use std::cmp::Ordering; use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; -use std::sync::RwLock; - -use once_cell::sync::Lazy; +use std::sync::{LazyLock, RwLock}; /// The global string interner. -static INTERNER: Lazy> = - Lazy::new(|| RwLock::new(Interner { to_id: HashMap::new(), from_id: Vec::new() })); +static INTERNER: LazyLock> = LazyLock::new(|| { + RwLock::new(Interner { to_id: HashMap::new(), from_id: Vec::new() }) +}); /// A string interner. struct Interner { diff --git a/docs/Cargo.toml b/docs/Cargo.toml index a24ee2545..41a5645e8 100644 --- a/docs/Cargo.toml +++ b/docs/Cargo.toml @@ -22,7 +22,6 @@ typst-dev-assets = { workspace = true } clap = { workspace = true, optional = true } ecow = { workspace = true } heck = { workspace = true } -once_cell = { workspace = true } pulldown-cmark = { workspace = true } serde = { workspace = true } serde_json = { workspace = true, optional = true } diff --git a/docs/src/lib.rs b/docs/src/lib.rs index 59fc1cbff..bc9b53c9a 100644 --- a/docs/src/lib.rs +++ b/docs/src/lib.rs @@ -12,9 +12,9 @@ pub use self::model::*; use std::collections::HashSet; use ecow::{eco_format, EcoString}; -use once_cell::sync::Lazy; use serde::Deserialize; use serde_yaml as yaml; +use std::sync::LazyLock; use typst::diag::{bail, StrResult}; use typst::foundations::{ AutoValue, Bytes, CastInfo, Category, Func, Module, NoneValue, ParamInfo, Repr, @@ -37,7 +37,7 @@ macro_rules! load { }; } -static GROUPS: Lazy> = Lazy::new(|| { +static GROUPS: LazyLock> = LazyLock::new(|| { let mut groups: Vec = yaml::from_str(load!("reference/groups.yml")).unwrap(); for group in &mut groups { @@ -54,7 +54,7 @@ static GROUPS: Lazy> = Lazy::new(|| { groups }); -static LIBRARY: Lazy> = Lazy::new(|| { +static LIBRARY: LazyLock> = LazyLock::new(|| { let mut lib = Library::default(); let scope = lib.global.scope_mut(); @@ -74,7 +74,7 @@ static LIBRARY: Lazy> = Lazy::new(|| { LazyHash::new(lib) }); -static FONTS: Lazy<(LazyHash, Vec)> = Lazy::new(|| { +static FONTS: LazyLock<(LazyHash, Vec)> = LazyLock::new(|| { let fonts: Vec<_> = typst_assets::fonts() .chain(typst_dev_assets::fonts()) .flat_map(|data| Font::iter(Bytes::from_static(data))) diff --git a/tests/Cargo.toml b/tests/Cargo.toml index b2fedd75a..b1855b496 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -22,7 +22,6 @@ typst-svg = { workspace = true } clap = { workspace = true } comemo = { workspace = true } ecow = { workspace = true } -once_cell = { workspace = true } oxipng = { workspace = true } parking_lot = { workspace = true } rayon = { workspace = true } diff --git a/tests/src/collect.rs b/tests/src/collect.rs index cc3ff7360..80e5e5a8b 100644 --- a/tests/src/collect.rs +++ b/tests/src/collect.rs @@ -3,9 +3,9 @@ use std::fmt::{self, Display, Formatter}; use std::ops::Range; use std::path::{Path, PathBuf}; use std::str::FromStr; +use std::sync::LazyLock; use ecow::{eco_format, EcoString}; -use once_cell::sync::Lazy; use typst::syntax::package::PackageVersion; use typst::syntax::{is_id_continue, is_ident, is_newline, FileId, Source, VirtualPath}; use unscanny::Scanner; @@ -390,7 +390,7 @@ impl<'a> Parser<'a> { /// Whether a test is within the selected set to run. fn selected(name: &str, abs: PathBuf) -> bool { - static SKIPPED: Lazy> = Lazy::new(|| { + static SKIPPED: LazyLock> = LazyLock::new(|| { String::leak(std::fs::read_to_string(crate::SKIP_PATH).unwrap()) .lines() .map(|line| line.trim()) diff --git a/tests/src/tests.rs b/tests/src/tests.rs index 58bd7cf7e..940c9e3c4 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -8,10 +8,10 @@ mod run; mod world; use std::path::Path; +use std::sync::LazyLock; use std::time::Duration; use clap::Parser; -use once_cell::sync::Lazy; use parking_lot::Mutex; use rayon::iter::{ParallelBridge, ParallelIterator}; @@ -19,7 +19,7 @@ use crate::args::{CliArguments, Command}; use crate::logger::Logger; /// The parsed command line arguments. -static ARGS: Lazy = Lazy::new(CliArguments::parse); +static ARGS: LazyLock = LazyLock::new(CliArguments::parse); /// The directory where the test suite is located. const SUITE_PATH: &str = "tests/suite";