pub struct ElementSpec {
pub name: ElementName,
pub docs: String,
pub aspects: AspectMap,
pub value: Option<Arc<dyn Any + Send + Sync>>,
}Expand description
One element: the value that says what a node means, with a stable type name, a one-line description, the aspects attached to it and its own data.
An element is the counterpart of the parser’s specification of a command (a techy callable specification; techy is the parsing library FLM is built on): the specification says how a construct is parsed, the element says what it means. An element holds its type name, description, aspects and data, and nothing else; what a backend does with an element it cannot render is decided by the plan request, not by the element.
One Arc<ElementSpec> is one element value, and an element is
identified by its element value (see ElementSpec::same_value).
Several command names may share one element value (a feature may declare
two commands with the same meaning under one element value), and several element values may share one type name (\section and
\subsection are two element values of type flm.heading). An element
value does not change once it is shared: ElementSpec::new and the
builder methods docs,
with_value and with fill
it in before the Arc is made. When another feature attaches an aspect to
an element type
(FeatureBuilder::attach),
the registry build replaces each element value of that type by a copy that
carries the added aspect.
A feature normally builds its elements while it registers its declarations
(ElementDef); a node’s element is read
with element_of.
§Examples
use std::sync::Arc;
use flm_core::model::element::ElementSpec;
// An aspect is any object-safe trait; nothing registers it anywhere.
trait Glossary: Send + Sync {
fn term(&self) -> &str;
}
struct Term(&'static str);
impl Glossary for Term {
fn term(&self) -> &str { self.0 }
}
let element = ElementSpec::new("thm.term")
.docs("a glossary term")
.with::<dyn Glossary>(Arc::new(Term("stabilizer")));
assert_eq!(element.name.as_str(), "thm.term");
assert!(element.has_aspect::<dyn Glossary>());
assert_eq!(element.aspect::<dyn Glossary>().map(|g| g.term()), Some("stabilizer"));Fields§
§name: ElementNameThe element’s type name, such as flm.heading. Several element values
may share it.
docs: StringA one-line description of the element, for a person who reads the registry’s declarations. The library stores it with the element and does not display it.
It is an owned String rather than a &'static str because a family
of elements may be built from a configuration while the registry is
built, for example one element per theorem kind, each with a
description computed then.
aspects: AspectMapThe aspects attached to this element.
value: Option<Arc<dyn Any + Send + Sync>>The element’s own data, or None for an element without data: usually
one struct type per element type, whose values tell the element values
of that type apart (a heading element holds its level, for instance).
The aspect implementations that need the data hold it as well.
Read it with ElementSpec::value_as given the element, or with
element_value given a node.
Implementations§
Source§impl ElementSpec
impl ElementSpec
Sourcepub fn new(name: impl Into<ElementName>) -> ElementSpec
pub fn new(name: impl Into<ElementName>) -> ElementSpec
A new element of the type name name, with an empty description, no
aspects and no data.
The builder methods ElementSpec::docs, ElementSpec::with_value
and ElementSpec::with fill it in; once it is shared as an Arc, it
is one element value.
Sourcepub fn docs(self, docs: impl Into<String>) -> ElementSpec
pub fn docs(self, docs: impl Into<String>) -> ElementSpec
Builder: sets the element’s one-line description, replacing any set before.
Sourcepub fn with_value(self, value: Arc<dyn Any + Send + Sync>) -> ElementSpec
pub fn with_value(self, value: Arc<dyn Any + Send + Sync>) -> ElementSpec
Builder: sets the element’s own data, replacing any set before. Read it
back with ElementSpec::value_as or element_value.
Sourcepub fn with<A: ?Sized + Any + Send + Sync>(self, imp: Arc<A>) -> ElementSpec
pub fn with<A: ?Sized + Any + Send + Sync>(self, imp: Arc<A>) -> ElementSpec
Builder: attaches imp as the implementation of the aspect A.
A is always written as the aspect’s trait object type, as in
.with::<dyn BlockRoleAspect>(Arc::clone(&value)), never as the
implementing struct. A call whose A is inferred as the concrete type
does not compile (see AspectMap::insert), so a forgotten dyn is
caught where it is written.
§Panics
Panics when an implementation of A is already attached. Attaching one
aspect twice while building an element is a mistake in the feature that
builds it, which no document can cause. Code that attaches aspects from
several sources, such as the registry, uses the fallible
AspectMap::insert instead.
Sourcepub fn aspect<A: ?Sized + Any>(&self) -> Option<&A>
pub fn aspect<A: ?Sized + Any>(&self) -> Option<&A>
The implementation of the aspect A attached to this element, or
None when none is attached.
Written element.aspect::<dyn Referenceable>(). Shorthand for
AspectMap::get on ElementSpec::aspects. For the standard aspects
that have a default, read the result through the function of
crate::model::aspects that supplies it (such as
block_role_of).
Sourcepub fn has_aspect<A: ?Sized + Any>(&self) -> bool
pub fn has_aspect<A: ?Sized + Any>(&self) -> bool
Returns true when an implementation of the aspect A is attached to
this element.
Shorthand for AspectMap::has on ElementSpec::aspects.
Sourcepub fn value_as<V: Any + Send + Sync>(&self) -> Option<&V>
pub fn value_as<V: Any + Send + Sync>(&self) -> Option<&V>
The element’s own data as a V.
Returns None when the element carries no data, or when its data is not
of type V. See element_value for the same read starting from a
node.
Sourcepub fn same_value(this: &Arc<ElementSpec>, other: &Arc<ElementSpec>) -> bool
pub fn same_value(this: &Arc<ElementSpec>, other: &Arc<ElementSpec>) -> bool
Returns true when the two handles point at the same allocation, that
is, denote the same element value.
An element is identified by its allocation, never by its name and never
by a Rust type: two elements built separately with the same name are
different element values. The tree check of the block-structure
rewrites (crate::lang::rewrites::blocks::check) compares elements
this way, and the registry numbers element values (ElemIdx) by
their allocation.
Trait Implementations§
Source§impl Clone for ElementSpec
impl Clone for ElementSpec
Auto Trait Implementations§
impl !RefUnwindSafe for ElementSpec
impl !UnwindSafe for ElementSpec
impl Forget for ElementSpec
impl Freeze for ElementSpec
impl Move for ElementSpec
impl Send for ElementSpec
impl Sync for ElementSpec
impl Unpin for ElementSpec
impl UnsafeUnpin for ElementSpec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more