From 8013f69714456043f5334670b6ecec2963309622 Mon Sep 17 00:00:00 2001 From: tingerrr Date: Thu, 4 Apr 2024 11:37:23 +0200 Subject: [PATCH] Implement `World` for common pointer types of `World` (#3838) Co-authored-by: Ilia <43654815+istudyatuni@users.noreply.github.com> --- crates/typst/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/crates/typst/src/lib.rs b/crates/typst/src/lib.rs index 3b09cb494..35915e2b1 100644 --- a/crates/typst/src/lib.rs +++ b/crates/typst/src/lib.rs @@ -57,7 +57,7 @@ pub mod visualize; pub use typst_syntax as syntax; use std::collections::HashSet; -use std::ops::Range; +use std::ops::{Deref, Range}; use comemo::{Prehashed, Track, Tracked, Validate}; use ecow::{EcoString, EcoVec}; @@ -231,6 +231,48 @@ pub trait World { } } +macro_rules! delegate_for_ptr { + ($W:ident for $ptr:ty) => { + impl<$W: World> World for $ptr { + fn library(&self) -> &Prehashed { + self.deref().library() + } + + fn book(&self) -> &Prehashed { + self.deref().book() + } + + fn main(&self) -> Source { + self.deref().main() + } + + fn source(&self, id: FileId) -> FileResult { + self.deref().source(id) + } + + fn file(&self, id: FileId) -> FileResult { + self.deref().file(id) + } + + fn font(&self, index: usize) -> Option { + self.deref().font(index) + } + + fn today(&self, offset: Option) -> Option { + self.deref().today(offset) + } + + fn packages(&self) -> &[(PackageSpec, Option)] { + self.deref().packages() + } + } + }; +} + +delegate_for_ptr!(W for std::boxed::Box); +delegate_for_ptr!(W for std::sync::Arc); +delegate_for_ptr!(W for &W); + /// Helper methods on [`World`] implementations. pub trait WorldExt { /// Get the byte range for a span.