diff options
Diffstat (limited to 'src/eval/module.rs')
| -rw-r--r-- | src/eval/module.rs | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/src/eval/module.rs b/src/eval/module.rs index fbfdd4e6..0bc6bf38 100644 --- a/src/eval/module.rs +++ b/src/eval/module.rs @@ -7,15 +7,20 @@ use super::{Content, Scope, Value}; use crate::diag::StrResult; /// An evaluated module, ready for importing or typesetting. +/// +/// Values of this type are cheap to clone and hash. #[derive(Clone, Hash)] #[allow(clippy::derived_hash_with_manual_eq)] -pub struct Module(Arc<Repr>); +pub struct Module { + /// The module's name. + name: EcoString, + /// The reference-counted inner fields. + inner: Arc<Repr>, +} /// The internal representation. #[derive(Clone, Hash)] struct Repr { - /// The module's name. - name: EcoString, /// The top-level definitions that were bound in this module. scope: Scope, /// The module's layoutable contents. @@ -25,38 +30,43 @@ struct Repr { impl Module { /// Create a new module. pub fn new(name: impl Into<EcoString>) -> Self { - Self(Arc::new(Repr { + Self { name: name.into(), - scope: Scope::new(), - content: Content::empty(), - })) + inner: Arc::new(Repr { scope: Scope::new(), content: Content::empty() }), + } + } + + /// Update the module's name. + pub fn with_name(mut self, name: impl Into<EcoString>) -> Self { + self.name = name.into(); + self } /// Update the module's scope. pub fn with_scope(mut self, scope: Scope) -> Self { - Arc::make_mut(&mut self.0).scope = scope; + Arc::make_mut(&mut self.inner).scope = scope; self } /// Update the module's content. pub fn with_content(mut self, content: Content) -> Self { - Arc::make_mut(&mut self.0).content = content; + Arc::make_mut(&mut self.inner).content = content; self } /// Get the module's name. pub fn name(&self) -> &EcoString { - &self.0.name + &self.name } /// Access the module's scope. pub fn scope(&self) -> &Scope { - &self.0.scope + &self.inner.scope } /// Access the module's scope, mutably. pub fn scope_mut(&mut self) -> &mut Scope { - &mut Arc::make_mut(&mut self.0).scope + &mut Arc::make_mut(&mut self.inner).scope } /// Try to access a definition in the module. @@ -68,7 +78,7 @@ impl Module { /// Extract the module's content. pub fn content(self) -> Content { - match Arc::try_unwrap(self.0) { + match Arc::try_unwrap(self.inner) { Ok(repr) => repr.content, Err(arc) => arc.content.clone(), } @@ -83,6 +93,6 @@ impl Debug for Module { impl PartialEq for Module { fn eq(&self, other: &Self) -> bool { - Arc::ptr_eq(&self.0, &other.0) + self.name == other.name && Arc::ptr_eq(&self.inner, &other.inner) } } |
