Skip to content

Commit

Permalink
dev(compiler): add appropriate Debug and Copy impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Nov 11, 2023
1 parent be3546e commit a0dc8b5
Show file tree
Hide file tree
Showing 28 changed files with 78 additions and 12 deletions.
1 change: 1 addition & 0 deletions compiler/src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{package::browser::ProxyRegistry, vfs::browser::ProxyAccessModel};
/// It is under development.
pub type TypstBrowserWorld = crate::world::CompilerWorld<BrowserCompilerFeat>;

#[derive(Debug, Clone, Copy)]
pub struct BrowserCompilerFeat;

impl crate::world::CompilerFeat for BrowserCompilerFeat {
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/font/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use typst_ts_core::{

use crate::vfs::system::LazyFile;

#[derive(Default)]
#[derive(Debug, Default)]
struct FontProfileRebuilder {
path_items: HashMap<PathBuf, FontProfileItem>,
pub profile: FontProfile,
Expand Down Expand Up @@ -77,6 +77,7 @@ impl FontProfileRebuilder {
}

/// Searches for fonts.
#[derive(Debug)]
pub struct SystemFontSearcher {
db: Database,

Expand Down
1 change: 1 addition & 0 deletions compiler/src/font/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl WebFont {

unsafe impl Send for WebFont {}

#[derive(Debug)]
pub struct WebFontLoader {
font: WebFont,
index: u32,
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
//! - [`service::CompileDriver`]: A driver for the compiler. Examples:
//! - Single thread (Sync): <https://github.com/Myriad-Dreamin/typst.ts/blob/main/cli/src/main.rs>
//! - Multiple thread (Async): <https://github.com/Enter-tainer/typst-preview-vscode/blob/main/src/main.rs>
// #![warn(missing_docs)]
// #![warn(missing_debug_implementations)]
// #![warn(missing_copy_implementations)]

pub(crate) mod macros;

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/package/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use wasm_bindgen::{prelude::*, JsValue};
use super::{PackageError, PackageSpec, Registry};

#[wasm_bindgen]
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct ProxyContext {
context: JsValue,
}
Expand Down Expand Up @@ -70,6 +70,7 @@ impl ProxyContext {
}
}

#[derive(Debug)]
pub struct ProxyRegistry {
pub context: ProxyContext,
pub real_resolve_fn: js_sys::Function,
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/parser/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use typst::syntax::{ast, LinkedNode, Source, SyntaxKind};
use super::modifier_set::ModifierSet;
use super::typst_tokens::{Modifier, TokenType};

#[derive(serde::Deserialize, serde::Serialize)]
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
pub struct SemanticTokensLegend {
#[serde(rename = "tokenTypes")]
pub token_types: Vec<String>,
Expand All @@ -33,6 +33,7 @@ pub fn get_semantic_tokens_legend() -> SemanticTokensLegend {
}
}

#[derive(Debug, Clone, Copy)]
pub enum OffsetEncoding {
Utf8,
Utf16,
Expand Down Expand Up @@ -83,6 +84,7 @@ fn tokenize_tree(root: &LinkedNode<'_>, parent_modifiers: ModifierSet) -> Vec<Se
token.chain(children).collect()
}

#[derive(Debug, Clone, Copy)]
pub struct SemanticToken {
pub delta_line: u32,
pub delta_start_character: u32,
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/parser/typst_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use strum::EnumIter;

/// Very similar to [`typst_ide::Tag`], but with convenience traits, and
/// extensible because we want to further customize highlighting
#[derive(Clone, Copy, EnumIter)]
#[derive(Debug, Clone, Copy, EnumIter)]
#[repr(u32)]
pub enum TokenType {
// Standard LSP types
Expand Down Expand Up @@ -68,7 +68,7 @@ impl From<TokenType> for &'static str {
}
}

#[derive(Clone, Copy, EnumIter)]
#[derive(Debug, Clone, Copy, EnumIter)]
#[repr(u8)]
pub enum Modifier {
Strong,
Expand Down
1 change: 1 addition & 0 deletions compiler/src/service/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ impl<C: Compiler> CompileActor<C> {
self.latest_doc.clone()
}
}
#[derive(Debug, Clone)]
pub struct CompileClient<Ctx> {
steal_send: mpsc::UnboundedSender<BorrowTask<Ctx>>,
memory_send: mpsc::UnboundedSender<MemoryEvent>,
Expand Down
1 change: 1 addition & 0 deletions compiler/src/service/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub fn status(entry_file: TypstFileId, status: DiagStatus) -> io::Result<()> {
Ok(())
}

#[derive(Debug, Clone, Copy)]
pub struct ConsoleDiagReporter<W>(PhantomParamData<W>);

impl<W> Default for ConsoleDiagReporter<W>
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/service/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,11 @@ impl CompileReport {
pub type ReportExporter = DynExporter<CompileReport>;
pub type FeaturedReportExporter = DynExporter<(Arc<FeatureSet>, CompileReport)>;

#[allow(dead_code)]
pub struct CompileReporter<C: Compiler> {
pub compiler: C,
pub reporter: DynGenericExporter<C::World, (Arc<FeatureSet>, CompileReport)>,
}

#[allow(dead_code)]
impl<C: Compiler> CompileReporter<C>
where
C::World: 'static,
Expand Down
7 changes: 4 additions & 3 deletions compiler/src/service/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct FeatureSlot(u16);
static ALLOCATOR: Lazy<std::sync::atomic::AtomicU16> =
Lazy::new(|| std::sync::atomic::AtomicU16::new(1));

#[derive(Default)]
#[derive(Debug, Default)]
pub struct LazyFeatureSlot(once_cell::sync::OnceCell<FeatureSlot>);

impl From<&LazyFeatureSlot> for FeatureSlot {
Expand All @@ -22,7 +22,7 @@ impl From<&LazyFeatureSlot> for FeatureSlot {
}
}

#[derive(Default, Clone)]
#[derive(Debug, Default, Clone)]
pub struct FeatureSet {
features: Vec<EcoString>,
}
Expand Down Expand Up @@ -54,6 +54,7 @@ pub trait CompileFeature<T> {
fn retrieve(&self, features: &FeatureSet) -> T;
}

#[derive(Debug, Clone, Copy)]
pub struct DiagFmtFeature;
const DIAG_FEATURE: FeatureSlot = FeatureSlot(0);
pub static DIAG_FMT_FEATURE: DiagFmtFeature = DiagFmtFeature;
Expand All @@ -78,7 +79,7 @@ impl CompileFeature<DiagnosticFormat> for DiagFmtFeature {
}
}

#[derive(Default)]
#[derive(Debug, Default)]
pub struct BuiltinFeature<T>(LazyFeatureSlot, std::marker::PhantomData<T>);

impl<T> BuiltinFeature<T> {
Expand Down
1 change: 1 addition & 0 deletions compiler/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ where
}

/// The status in which the watcher can be.
#[derive(Debug, Clone, Copy)]
pub enum DiagStatus {
Stage(&'static str),
Success(std::time::Duration),
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/service/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Default for WatchState {
}

/// The data entry of a watched file.
#[derive(Debug)]
struct WatchedEntry {
/// The lifetime of the entry.
///
Expand Down Expand Up @@ -85,6 +86,7 @@ struct UndeterminedNotifyEvent {
// Drop order is significant.
/// The actor that watches files.
/// It is used to watch files and send events to the consumers
#[derive(Debug)]
pub struct NotifyActor {
/// The access model of the actor.
/// We concrete the access model to `SystemAccessModel` for now.
Expand Down
1 change: 1 addition & 0 deletions compiler/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
};

/// type trait of [`TypstSystemWorld`].
#[derive(Debug, Clone, Copy)]
pub struct SystemCompilerFeat;

impl crate::world::CompilerFeat for SystemCompilerFeat {
Expand Down
1 change: 1 addition & 0 deletions compiler/src/vfs/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::time::SystemTime;

use super::AccessModel;

#[derive(Debug)]
pub struct ProxyAccessModel {
pub context: JsValue,
pub mtime_fn: js_sys::Function,
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/vfs/cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use super::AccessModel;
/// incrementally query a value from a self holding state
type IncrQueryRef<S, E> = QueryRef<S, E, Option<S>>;

#[derive(Debug)]
pub struct FileCache<S> {
lifetime_cnt: usize,
mtime: SystemTime,
Expand All @@ -20,6 +21,7 @@ pub struct FileCache<S> {
source_state: IncrQueryRef<S, FileError>,
}

#[derive(Debug)]
pub struct CachedAccessModel<Inner: AccessModel, C> {
inner: Inner,
lifetime_cnt: usize,
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/vfs/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::time::SystemTime;

use super::AccessModel;

#[derive(Default, Debug)]
#[derive(Default, Debug, Clone, Copy)]
pub struct DummyAccessModel;

impl AccessModel for DummyAccessModel {
Expand Down
14 changes: 14 additions & 0 deletions compiler/src/vfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod path_interner;

pub(crate) use path_interner::PathInterner;

use core::fmt;
use std::{collections::HashMap, ffi::OsStr, hash::Hash, path::Path, sync::Arc};

use append_only_vec::AppendOnlyVec;
Expand Down Expand Up @@ -65,6 +66,7 @@ pub trait AccessModel {
type FileQuery<T> = QueryRef<T, FileError>;

/// Holds canonical data for all paths pointing to the same entity.
#[derive(Debug)]
pub struct PathSlot {
idx: FileId,
sampled_path: once_cell::sync::OnceCell<ImmutPath>,
Expand Down Expand Up @@ -100,6 +102,18 @@ pub struct Vfs<M: AccessModel + Sized> {
pub do_reparse: bool,
}

impl<M: AccessModel + Sized> fmt::Debug for Vfs<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Vfs")
.field("lifetime_cnt", &self.lifetime_cnt)
.field("path2slot", &self.path2slot)
.field("src2file_id", &self.src2file_id)
.field("slots", &self.slots)
.field("do_reparse", &self.do_reparse)
.finish()
}
}

impl<M: AccessModel + Sized> Vfs<M> {
pub fn new(access_model: M) -> Self {
let access_model = NotifyAccessModel::new(access_model);
Expand Down
1 change: 1 addition & 0 deletions compiler/src/vfs/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ pub enum NotifyMessage {

/// Notify shadowing access model, which the typical underlying access model is
/// [`crate::vfs::system::SystemAccessModel`]
#[derive(Debug)]
pub struct NotifyAccessModel<M: AccessModel> {
files: HashMap<ImmutPath, FileSnapshot>,
pub inner: M,
Expand Down
1 change: 1 addition & 0 deletions compiler/src/vfs/path_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_hash::FxHasher;
use super::FileId;

/// Structure to map between [`VfsPath`] and [`FileId`].
#[derive(Debug)]
pub(crate) struct PathInterner<P, Ext = ()> {
map: IndexMap<P, Ext, BuildHasherDefault<FxHasher>>,
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/vfs/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use typst_ts_core::{Bytes, ReadAllOnce};

use super::AccessModel;

#[derive(Debug)]
pub struct LazyFile {
path: std::path::PathBuf,
file: Option<std::io::Result<File>>,
Expand All @@ -34,11 +35,13 @@ impl ReadAllOnce for LazyFile {
}
}

#[derive(Debug, Clone, Copy)]
pub struct SystemFileMeta {
mt: std::time::SystemTime,
is_file: bool,
}

#[derive(Debug, Clone, Copy)]
pub struct SystemAccessModel;

impl SystemAccessModel {
Expand Down
1 change: 1 addition & 0 deletions compiler/src/vfs/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::time::SystemTime;

use super::{cached::CachedAccessModel, AccessModel};

#[derive(Debug)]
pub struct TraceAccessModel<M: AccessModel + Sized> {
inner: M,
trace: [AtomicU64; 6],
Expand Down
1 change: 1 addition & 0 deletions compiler/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub trait CompilerFeat {
}

/// A world that provides access to the operating system.
#[derive(Debug)]
pub struct CompilerWorld<F: CompilerFeat> {
/// Path to the root directory of compilation.
/// The world forbids direct access to files outside this directory.
Expand Down
1 change: 1 addition & 0 deletions core/src/concepts/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde_with::{DeserializeAs, SerializeAs};
/// Generic marker for type that only occurs in parameter position.
///
/// Safety: The signature is type checked by the compiler.
#[derive(Debug, Clone, Copy)]
pub struct PhantomParamData<X>(std::marker::PhantomData<X>);
unsafe impl<X> Send for PhantomParamData<X> {}

Expand Down
16 changes: 16 additions & 0 deletions core/src/concepts/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,19 @@ impl<T, E> Default for QueryRef<T, E> {
}
}
}

impl<T, E, QC> fmt::Debug for QueryRef<T, E, QC>
where
T: fmt::Debug,
E: fmt::Debug,
QC: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let borrowed = self.cell.borrow();
let (ref ctx, ref res) = *borrowed;
f.debug_struct("QueryRef")
.field("context", &ctx)
.field("result", &res)
.finish()
}
}
2 changes: 1 addition & 1 deletion core/src/font/partial_book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl From<&FontInfo> for FontInfoKey {
}
}

#[derive(Default)]
#[derive(Debug, Default)]
pub struct PartialFontBook {
pub revision: usize,
pub partial_hit: bool,
Expand Down
1 change: 1 addition & 0 deletions core/src/font/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub trait FontResolver {
}
}

#[derive(Debug)]
/// The default FontResolver implementation.
pub struct FontResolverImpl {
book: Prehashed<FontBook>,
Expand Down
10 changes: 10 additions & 0 deletions core/src/font/slot.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use typst::font::Font;

use crate::{FontLoader, QueryRef};
Expand Down Expand Up @@ -35,3 +37,11 @@ impl FontSlot {
{ unsafe { res.unwrap_unchecked() } }.clone()
}
}

impl fmt::Debug for FontSlot {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("FontSlot")
.field(&self.get_uninitialized())
.finish()
}
}

0 comments on commit a0dc8b5

Please sign in to comment.