flm_core/model/element.rs
1//! Elements, which say what the nodes of a document mean, and the aspect map
2//! through which the rest of the library reads an element's behavior.
3//!
4//! An element is the value that says what a node means: a declaration with a
5//! stable dotted type name ([`ElementName`], such as `flm.heading` or
6//! `thm.exercise`), identified by the element value itself (its allocation)
7//! and not by its name ([`ElementSpec`]). An element carries **aspects**:
8//! pieces of behavior attached to the element when a feature registers it,
9//! held in its [`AspectMap`] and read back through trait objects. A stage asks
10//! an element what it does through an aspect ("is this a block?", "which
11//! reference points does this node carry?") instead of matching its name. See
12//! [elements and aspects](crate::guide::concepts_overview#elements-and-aspects);
13//! the aspects that the standard features use are defined in
14//! [`crate::model::aspects`].
15//!
16//! A feature declares a construct once, and that one declaration produces both
17//! the specification the parser uses and the element
18//! ([`ElementDef`](crate::lang::mint::ElementDef)). Every node of every parsed
19//! tree carries its element in its node extension
20//! ([`FlmNodeExt`](crate::lang::FlmNodeExt)), so [`element_of`] reads a node's
21//! element without any lookup.
22//!
23//! # Identity of an element
24//!
25//! An element is identified by its element value, not by its name and not by
26//! a Rust type. `\section` and `\subsection` are two element values of the
27//! single type `flm.heading`, which differ in their data
28//! ([`ElementSpec::value`]). An element value is shared as `Arc<ElementSpec>`,
29//! and two handles denote the same element when they point at the same
30//! allocation ([`ElementSpec::same_value`]). A registry gives each registered
31//! element value a number of its own ([`ElemIdx`]) to index its lookup
32//! structures. Rust's `TypeId` appears in this module only as the key of the
33//! aspect map.
34//!
35//! # Items
36//!
37//! - [`ElementSpec`] — one element value: its type name, a description, its
38//! aspects and its data.
39//! - [`AspectMap`] — the aspects attached to one element, and
40//! [`AspectCollision`], the error of attaching one aspect twice.
41//! - [`element_of`] and [`element_value`] — a node's element, and that
42//! element's data.
43//! - [`ElemIdx`] — a registry's number for an element value.
44//! - [`AmbiguousElement`] — the error of looking up an element by a type name
45//! that several element values share.
46
47use std::any::{Any, TypeId};
48use std::fmt;
49use std::sync::Arc;
50
51use techy::core::node::NodeRef;
52
53use crate::model::names::ElementName;
54use crate::lang::Flm;
55
56/// The number a registry gives one element value, counted from zero.
57///
58/// A registry numbers every registered `Arc<ElementSpec>`, the structural
59/// elements included, in a deterministic order, and uses the numbers to index
60/// its lookup structures, such as its table of elements and its sets of
61/// elements per aspect. The registry holds the map from an element value to its
62/// number itself ([`FlmRegistry::elem_idx`]); nothing is written on the shared
63/// element value, so an element value used by several registries never carries
64/// a stale number. An `ElemIdx` therefore has a meaning only together with the
65/// registry that assigned it.
66///
67/// This module defines the type; the [registry](crate::core::registry) assigns
68/// the numbers.
69///
70/// [`FlmRegistry::elem_idx`]: crate::core::registry::FlmRegistry::elem_idx
71#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
72pub struct ElemIdx(pub u32);
73
74/// The error of attaching an aspect to an element that already has an
75/// implementation of that aspect.
76///
77/// An element carries at most one implementation of each aspect, so
78/// [`AspectMap::insert`] refuses a second one instead of replacing the first.
79/// [`ElementSpec::with`] panics on this error, since it is a mistake in the
80/// feature that builds the element. The registry build, which attaches the
81/// aspects that features declare for other features' elements
82/// ([`FeatureBuilder::attach`](crate::core::registry::FeatureBuilder::attach)),
83/// reports it as a build error.
84#[derive(Debug, Clone, PartialEq, Eq)]
85pub struct AspectCollision {
86 /// The Rust type name of the aspect trait, for messages only: the map
87 /// identifies an aspect by its `TypeId`.
88 pub aspect: &'static str,
89}
90
91impl fmt::Display for AspectCollision {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 write!(
94 f,
95 "an element can carry one implementation of each aspect, but aspect `{}` \
96 is already attached to this element",
97 self.aspect
98 )
99 }
100}
101
102impl std::error::Error for AspectCollision {}
103
104/// One entry of an [`AspectMap`]: the aspect trait's `TypeId`, its type name,
105/// and the implementation.
106///
107/// An `Arc<dyn A>` is itself a sized value, so it can be put behind one more
108/// `Arc` and erased to `Arc<dyn Any>`; `get` downcasts that back to
109/// `Arc<dyn A>`. This keeps the map within safe, stable Rust.
110#[derive(Clone)]
111struct AspectEntry {
112 id: TypeId,
113 name: &'static str,
114 imp: Arc<dyn Any + Send + Sync>,
115}
116
117/// The aspect implementations attached to one element, at most one per
118/// aspect, each stored under the aspect's trait.
119///
120/// An aspect is any object-safe trait, and nothing about an aspect is
121/// registered anywhere: a feature, or a single test, can define an aspect of
122/// its own and attach it to its elements, and code that does not use that
123/// trait is unaffected. An implementation is stored under the `TypeId` of the
124/// trait object type, such as `TypeId::of::<dyn BlockRoleAspect>()`.
125///
126/// [`ElementSpec::with`] is the usual way to attach an aspect;
127/// [`AspectMap::insert`] is the fallible form. [`AspectMap::get`],
128/// [`AspectMap::get_arc`] and [`AspectMap::has`] read the map, and
129/// [`ElementSpec::aspect`] reads it from the element.
130#[derive(Clone, Default)]
131pub struct AspectMap {
132 // A short vector scanned linearly, not a hash map: an element carries a
133 // single-digit number of aspects, where a scan beats hashing. Each entry
134 // also carries the aspect's type name, which is only ever used in messages.
135 entries: Vec<AspectEntry>,
136}
137
138impl AspectMap {
139 /// A map with no aspect attached.
140 pub fn new() -> AspectMap {
141 AspectMap::default()
142 }
143
144 /// Attaches `imp` as the implementation of the aspect `A`.
145 ///
146 /// `A` **must be written as the trait object type**:
147 /// `map.insert::<dyn BlockRoleAspect>(imp)`. A concrete `Arc<HeadingElement>`
148 /// can still be passed as `imp`, because Rust converts it to
149 /// `Arc<dyn BlockRoleAspect>` at the call.
150 ///
151 /// The trait object type matters because the map stores the implementation
152 /// under `A`: stored under a concrete implementation type, it would be
153 /// under a key that no reader looks up, and nothing would find it. A check
154 /// at compile time therefore rejects a call whose `A` is a sized type, such
155 /// as the implementing struct (a reference to a trait object is twice the
156 /// width of a plain pointer, a reference to a sized type is not), so a
157 /// forgotten `dyn` is a build error rather than an aspect missing at run
158 /// time.
159 ///
160 /// # Errors
161 ///
162 /// Returns an [`AspectCollision`] when an implementation of `A` is already
163 /// attached, and leaves the map unchanged.
164 pub fn insert<A: ?Sized + Any + Send + Sync>(
165 &mut self,
166 imp: Arc<A>,
167 ) -> Result<(), AspectCollision> {
168 const {
169 assert!(
170 size_of::<&A>() == 2 * size_of::<*const ()>(),
171 "the aspect must be given as its trait-object type, as in \
172 .with::<dyn MyAspect>(…) or .insert::<dyn MyAspect>(…)"
173 );
174 }
175 self.insert_erased(TypeId::of::<A>(), std::any::type_name::<A>(), Arc::new(imp))
176 }
177
178 /// Attaches an implementation whose type has already been erased.
179 ///
180 /// The registry build uses this to attach the aspects that features
181 /// declare for other features' elements. An entry read with
182 /// [`AspectMap::entries`] can be passed to it unchanged. `id` is the `TypeId` of the
183 /// aspect's trait object type `dyn A`; `imp` must hold the `Arc<dyn A>`
184 /// value itself, boxed in one more `Arc`, exactly as [`AspectMap::insert`]
185 /// stores it, or [`AspectMap::get`] will not find it; `name` is the
186 /// aspect's type name, used only in messages.
187 ///
188 /// # Errors
189 ///
190 /// Returns an [`AspectCollision`] when an implementation under `id` is
191 /// already attached, and leaves the map unchanged.
192 pub fn insert_erased(
193 &mut self,
194 id: TypeId,
195 name: &'static str,
196 imp: Arc<dyn Any + Send + Sync>,
197 ) -> Result<(), AspectCollision> {
198 if self.entries.iter().any(|entry| entry.id == id) {
199 return Err(AspectCollision { aspect: name });
200 }
201 self.entries.push(AspectEntry { id, name, imp });
202 Ok(())
203 }
204
205 /// The implementation of the aspect `A`, or `None` when none is attached.
206 ///
207 /// Written `map.get::<dyn BlockRoleAspect>()`: as for
208 /// [`AspectMap::insert`], `A` is the trait object type. A concrete type
209 /// compiles here but finds nothing.
210 pub fn get<A: ?Sized + Any>(&self) -> Option<&A> {
211 let id = TypeId::of::<A>();
212 self.entries
213 .iter()
214 .find(|entry| entry.id == id)
215 .and_then(|entry| entry.imp.downcast_ref::<Arc<A>>())
216 .map(|arc| &**arc)
217 }
218
219 /// A shared handle to the implementation of the aspect `A`, or `None` when
220 /// none is attached.
221 ///
222 /// The same lookup as [`AspectMap::get`], returning an owned `Arc` for a
223 /// caller that keeps the implementation after the map is no longer
224 /// borrowed.
225 pub fn get_arc<A: ?Sized + Any>(&self) -> Option<Arc<A>> {
226 let id = TypeId::of::<A>();
227 self.entries
228 .iter()
229 .find(|entry| entry.id == id)
230 .and_then(|entry| entry.imp.downcast_ref::<Arc<A>>())
231 .map(Arc::clone)
232 }
233
234 /// Returns `true` when an implementation of the aspect `A` is attached.
235 pub fn has<A: ?Sized + Any>(&self) -> bool {
236 let id = TypeId::of::<A>();
237 self.entries.iter().any(|entry| entry.id == id)
238 }
239
240 /// The attached implementations, in the order they were attached, each as
241 /// the aspect's `TypeId`, the aspect's type name and the implementation
242 /// with its type erased.
243 ///
244 /// The registry reads these to build its tables per aspect and its digest,
245 /// and each entry can be passed back to [`AspectMap::insert_erased`]
246 /// unchanged.
247 pub fn entries(
248 &self,
249 ) -> impl Iterator<Item = (TypeId, &'static str, Arc<dyn Any + Send + Sync>)> + '_ {
250 self.entries.iter().map(|entry| (entry.id, entry.name, Arc::clone(&entry.imp)))
251 }
252
253 /// The `TypeId`s of the attached aspects, in the order they were attached.
254 /// Each is the `TypeId` of an aspect's trait object type.
255 pub fn aspect_ids(&self) -> impl Iterator<Item = TypeId> + '_ {
256 self.entries.iter().map(|entry| entry.id)
257 }
258
259 /// The number of attached aspects.
260 pub fn len(&self) -> usize {
261 self.entries.len()
262 }
263
264 /// Returns `true` when no aspect is attached.
265 pub fn is_empty(&self) -> bool {
266 self.entries.is_empty()
267 }
268}
269
270impl fmt::Debug for AspectMap {
271 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
272 f.debug_list().entries(self.entries.iter().map(|entry| entry.name)).finish()
273 }
274}
275
276/// One element: the value that says what a node means, with a stable type
277/// name, a one-line description, the aspects attached to it and its own data.
278///
279/// An element is the counterpart of the parser's specification of a command (a
280/// techy callable specification; techy is the parsing library FLM is built
281/// on): the specification says how a construct is *parsed*, the element says what it
282/// *means*. An element holds its type name, description, aspects and data,
283/// and nothing else; what a backend does with an element it cannot render is
284/// decided by the plan request, not by the element.
285///
286/// One `Arc<ElementSpec>` is one **element value**, and an element is
287/// identified by its element value (see [`ElementSpec::same_value`]).
288/// Several command names may share one element value (a feature may declare
289/// two commands with the same meaning under one element value), and several element values may share one type name (`\section` and
290/// `\subsection` are two element values of type `flm.heading`). An element
291/// value does not change once it is shared: [`ElementSpec::new`] and the
292/// builder methods [`docs`](ElementSpec::docs),
293/// [`with_value`](ElementSpec::with_value) and [`with`](ElementSpec::with) fill
294/// it in before the `Arc` is made. When another feature attaches an aspect to
295/// an element type
296/// ([`FeatureBuilder::attach`](crate::core::registry::FeatureBuilder::attach)),
297/// the registry build replaces each element value of that type by a copy that
298/// carries the added aspect.
299///
300/// A feature normally builds its elements while it registers its declarations
301/// ([`ElementDef`](crate::lang::mint::ElementDef)); a node's element is read
302/// with [`element_of`].
303///
304/// # Examples
305///
306/// ```
307/// use std::sync::Arc;
308/// use flm_core::model::element::ElementSpec;
309///
310/// // An aspect is any object-safe trait; nothing registers it anywhere.
311/// trait Glossary: Send + Sync {
312/// fn term(&self) -> &str;
313/// }
314/// struct Term(&'static str);
315/// impl Glossary for Term {
316/// fn term(&self) -> &str { self.0 }
317/// }
318///
319/// let element = ElementSpec::new("thm.term")
320/// .docs("a glossary term")
321/// .with::<dyn Glossary>(Arc::new(Term("stabilizer")));
322///
323/// assert_eq!(element.name.as_str(), "thm.term");
324/// assert!(element.has_aspect::<dyn Glossary>());
325/// assert_eq!(element.aspect::<dyn Glossary>().map(|g| g.term()), Some("stabilizer"));
326/// ```
327#[derive(Clone)]
328pub struct ElementSpec {
329 /// The element's type name, such as `flm.heading`. Several element values
330 /// may share it.
331 pub name: ElementName,
332 /// A one-line description of the element, for a person who reads the
333 /// registry's declarations. The library stores it with the element and does
334 /// not display it.
335 ///
336 /// It is an owned `String` rather than a `&'static str` because a family
337 /// of elements may be built from a configuration while the registry is
338 /// built, for example one element per theorem kind, each with a
339 /// description computed then.
340 pub docs: String,
341 /// The aspects attached to this element.
342 pub aspects: AspectMap,
343 /// The element's own data, or `None` for an element without data: usually
344 /// one struct type per element type, whose values tell the element values
345 /// of that type apart (a heading element holds its level, for instance).
346 /// The aspect implementations that need the data hold it as well.
347 ///
348 /// Read it with [`ElementSpec::value_as`] given the element, or with
349 /// [`element_value`] given a node.
350 pub value: Option<Arc<dyn Any + Send + Sync>>,
351}
352
353impl ElementSpec {
354 /// A new element of the type name `name`, with an empty description, no
355 /// aspects and no data.
356 ///
357 /// The builder methods [`ElementSpec::docs`], [`ElementSpec::with_value`]
358 /// and [`ElementSpec::with`] fill it in; once it is shared as an `Arc`, it
359 /// is one element value.
360 pub fn new(name: impl Into<ElementName>) -> ElementSpec {
361 ElementSpec {
362 name: name.into(),
363 docs: String::new(),
364 aspects: AspectMap::new(),
365 value: None,
366 }
367 }
368
369 /// Builder: sets the element's one-line description, replacing any set
370 /// before.
371 pub fn docs(mut self, docs: impl Into<String>) -> ElementSpec {
372 self.docs = crate::kept_docs(docs);
373 self
374 }
375
376 /// Builder: sets the element's own data, replacing any set before. Read it
377 /// back with [`ElementSpec::value_as`] or [`element_value`].
378 pub fn with_value(mut self, value: Arc<dyn Any + Send + Sync>) -> ElementSpec {
379 self.value = Some(value);
380 self
381 }
382
383 /// Builder: attaches `imp` as the implementation of the aspect `A`.
384 ///
385 /// `A` is always written as the aspect's **trait object type**, as in
386 /// `.with::<dyn BlockRoleAspect>(Arc::clone(&value))`, never as the
387 /// implementing struct. A call whose `A` is inferred as the concrete type
388 /// does not compile (see [`AspectMap::insert`]), so a forgotten `dyn` is
389 /// caught where it is written.
390 ///
391 /// # Panics
392 ///
393 /// Panics when an implementation of `A` is already attached. Attaching one
394 /// aspect twice while building an element is a mistake in the feature that
395 /// builds it, which no document can cause. Code that attaches aspects from
396 /// several sources, such as the registry, uses the fallible
397 /// [`AspectMap::insert`] instead.
398 pub fn with<A: ?Sized + Any + Send + Sync>(mut self, imp: Arc<A>) -> ElementSpec {
399 if let Err(collision) = self.aspects.insert::<A>(imp) {
400 panic!(
401 "element `{}`: {} (code that may attach an aspect twice uses the fallible \
402 `AspectMap::insert` rather than `ElementSpec::with`)",
403 self.name, collision
404 );
405 }
406 self
407 }
408
409 /// The implementation of the aspect `A` attached to this element, or
410 /// `None` when none is attached.
411 ///
412 /// Written `element.aspect::<dyn Referenceable>()`. Shorthand for
413 /// [`AspectMap::get`] on [`ElementSpec::aspects`]. For the standard aspects
414 /// that have a default, read the result through the function of
415 /// [`crate::model::aspects`] that supplies it (such as
416 /// [`block_role_of`](crate::model::aspects::block_role_of)).
417 pub fn aspect<A: ?Sized + Any>(&self) -> Option<&A> {
418 self.aspects.get::<A>()
419 }
420
421 /// Returns `true` when an implementation of the aspect `A` is attached to
422 /// this element.
423 ///
424 /// Shorthand for [`AspectMap::has`] on [`ElementSpec::aspects`].
425 pub fn has_aspect<A: ?Sized + Any>(&self) -> bool {
426 self.aspects.has::<A>()
427 }
428
429 /// The element's own data as a `V`.
430 ///
431 /// Returns `None` when the element carries no data, or when its data is not
432 /// of type `V`. See [`element_value`] for the same read starting from a
433 /// node.
434 pub fn value_as<V: Any + Send + Sync>(&self) -> Option<&V> {
435 self.value.as_deref().and_then(|value| value.downcast_ref::<V>())
436 }
437
438 /// Returns `true` when the two handles point at the same allocation, that
439 /// is, denote the same element value.
440 ///
441 /// An element is identified by its allocation, never by its name and never
442 /// by a Rust type: two elements built separately with the same name are
443 /// different element values. The tree check of the block-structure
444 /// rewrites ([`crate::lang::rewrites::blocks::check`]) compares elements
445 /// this way, and the registry numbers element values ([`ElemIdx`]) by
446 /// their allocation.
447 pub fn same_value(this: &Arc<ElementSpec>, other: &Arc<ElementSpec>) -> bool {
448 Arc::ptr_eq(this, other)
449 }
450}
451
452impl fmt::Debug for ElementSpec {
453 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
454 f.debug_struct("ElementSpec")
455 .field("name", &self.name)
456 .field("aspects", &self.aspects)
457 .field("value", &self.value.as_ref().map(|_| "…"))
458 .finish()
459 }
460}
461
462/// The element of the node `node`.
463///
464/// Every node of every FLM tree carries its element in its node extension, so
465/// this reads a field: it cannot fail, needs no lookup, and works on parsed
466/// trees and on trees produced by a rewrite alike.
467pub fn element_of<'t>(node: &NodeRef<'t, Flm>) -> &'t Arc<ElementSpec> {
468 &node.ext().element
469}
470
471/// The data of the element of the node `node`, as a `V`.
472///
473/// Returns `None` when the element carries no data, or when its data is not
474/// of type `V`. Written `element_value::<HeadingElement>(node)`; the same read
475/// as [`ElementSpec::value_as`] on [`element_of`]`(node)`.
476pub fn element_value<'t, V: Any + Send + Sync>(node: &NodeRef<'t, Flm>) -> Option<&'t V> {
477 node.ext().element.value.as_deref().and_then(|value| value.downcast_ref::<V>())
478}
479
480/// The error of looking up an element value by a type name that several
481/// registered element values share.
482///
483/// A type name identifies an element *type*, and one type may have several
484/// element values (every heading level is an element value of type
485/// `flm.heading`), so a type name alone does not always say which element
486/// value is meant. The type names that must be looked up this way, those of
487/// synthesized elements and of presentation elements, must each belong to
488/// exactly one element value; this error reports a type name that does not,
489/// instead of taking the first match.
490///
491/// Two lookups return it: the registry's [lookup by type name][by-name], and
492/// the element resolver that reading a stored tree back uses to find a node's
493/// element from its type name ([`crate::dump`]). It is defined in this module,
494/// below both, so that they share one type.
495///
496/// [by-name]: crate::core::registry::FlmRegistry::element_value
497#[derive(Debug, Clone, PartialEq, Eq)]
498pub struct AmbiguousElement {
499 /// The type name that several element values share.
500 pub name: ElementName,
501 /// The feature that registered each element value of that type name, in
502 /// the order the lookup holds them: one entry per element value, so a
503 /// feature that registered two of them appears twice. The element resolver
504 /// of [`crate::dump`], which has no record of which feature registered each
505 /// element value, gives
506 /// [`HOST_FEATURE`](crate::core::registry::HOST_FEATURE) for each one
507 /// instead.
508 pub owners: Vec<crate::model::names::FeatureName>,
509}
510
511impl fmt::Display for AmbiguousElement {
512 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
513 write!(
514 f,
515 "the element type name `{}` must belong to exactly one registered element, \
516 but {} elements are registered under it (by {})",
517 self.name,
518 self.owners.len(),
519 self.owners.iter().map(|o| format!("`{o}`")).collect::<Vec<_>>().join(", ")
520 )
521 }
522}
523
524impl std::error::Error for AmbiguousElement {}
525
526#[cfg(test)]
527mod tests {
528 use super::*;
529
530 trait Glossary: Send + Sync {
531 fn term(&self) -> &str;
532 }
533
534 struct Term(&'static str);
535
536 impl Glossary for Term {
537 fn term(&self) -> &str {
538 self.0
539 }
540 }
541
542 #[test]
543 fn aspect_map_finds_an_attached_aspect_by_trait_object_type() {
544 let mut map = AspectMap::new();
545 map.insert::<dyn Glossary>(Arc::new(Term("stabilizer"))).unwrap();
546 assert_eq!(map.get::<dyn Glossary>().map(|g| g.term()), Some("stabilizer"));
547 assert!(map.has::<dyn Glossary>());
548 assert_eq!(map.len(), 1);
549 }
550
551 #[test]
552 fn aspect_map_reports_a_second_attachment_of_one_aspect() {
553 let mut map = AspectMap::new();
554 map.insert::<dyn Glossary>(Arc::new(Term("a"))).unwrap();
555 let collision = map.insert::<dyn Glossary>(Arc::new(Term("b"))).unwrap_err();
556 assert_eq!(collision.aspect, std::any::type_name::<dyn Glossary>());
557 assert_eq!(map.get::<dyn Glossary>().map(|g| g.term()), Some("a"));
558 }
559
560 #[test]
561 fn erased_entries_round_trip_through_insert_erased() {
562 let mut map = AspectMap::new();
563 map.insert::<dyn Glossary>(Arc::new(Term("t"))).unwrap();
564 let mut copy = AspectMap::new();
565 for (id, name, imp) in map.entries() {
566 copy.insert_erased(id, name, imp).unwrap();
567 }
568 assert_eq!(copy.get::<dyn Glossary>().map(|g| g.term()), Some("t"));
569 }
570
571 #[test]
572 fn element_value_downcasts_to_the_declared_type() {
573 struct Heading {
574 level: u8,
575 }
576 let value = Arc::new(Heading { level: 2 });
577 let element = ElementSpec::new("flm.heading").docs("a heading").with_value(value);
578 assert_eq!(element.value_as::<Heading>().map(|h| h.level), Some(2));
579 assert!(element.value_as::<Term>().is_none());
580 }
581
582 #[test]
583 #[should_panic]
584 fn attaching_one_aspect_twice_on_the_builder_panics() {
585 let _ = ElementSpec::new("flm.term")
586 .with::<dyn Glossary>(Arc::new(Term("a")))
587 .with::<dyn Glossary>(Arc::new(Term("b")));
588 }
589
590 #[test]
591 fn identity_is_the_shared_value_not_the_name() {
592 let a = Arc::new(ElementSpec::new("flm.heading"));
593 let b = Arc::new(ElementSpec::new("flm.heading"));
594 assert!(ElementSpec::same_value(&a, &Arc::clone(&a)));
595 assert!(!ElementSpec::same_value(&a, &b));
596 }
597}