Skip to main content

ElementSpec

Struct ElementSpec 

Source
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: ElementName

The element’s type name, such as flm.heading. Several element values may share it.

§docs: String

A 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: AspectMap

The 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

Source

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.

Source

pub fn docs(self, docs: impl Into<String>) -> ElementSpec

Builder: sets the element’s one-line description, replacing any set before.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ElementSpec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for ElementSpec

§

impl !UnwindSafe for ElementSpec

§

impl Forget for ElementSpec

§

impl Freeze for ElementSpec
where *const ArcInner<str> is !null: Freeze, PhantomData<ArcInner<str>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze, PhantomData<AspectEntry>: Freeze, *const ArcInner<dyn Any + Send + Sync> is !null: Freeze, PhantomData<ArcInner<dyn Any + Send + Sync>>: Freeze,

§

impl Move for ElementSpec

§

impl Send for ElementSpec
where Arc<str>: Send, RawVecInner: Send, TypeId: Send, &'static str: Send, Arc<dyn Any + Send + Sync>: Send,

§

impl Sync for ElementSpec
where Arc<str>: Sync, RawVecInner: Sync, TypeId: Sync, Arc<dyn Any + Send + Sync>: Sync,

§

impl Unpin for ElementSpec
where Arc<str>: Unpin, *const u8 is !null: Unpin, *const (): Unpin, &'static str: Unpin, Arc<dyn Any + Send + Sync>: Unpin,

§

impl UnsafeUnpin for ElementSpec
where *const ArcInner<str> is !null: UnsafeUnpin, PhantomData<ArcInner<str>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, PhantomData<AspectEntry>: UnsafeUnpin, *const ArcInner<dyn Any + Send + Sync> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn Any + Send + Sync>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.