1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! This module defines [`LangOwned`], the cell in which an element stores a
//! value that was built against the language the element belongs to.
//!
//! # The reference cycle that the cell prevents
//!
//! A [`BuiltLang`] owns its techy packages, and each package owns the elements
//! declared in it, with their data and their aspects. A tree parsed under the
//! language refers back to the language: each node refers to its parsing
//! state, the parsing state refers to the packages, and a callable node refers
//! to its element.
//!
//! A feature often builds such a tree for one of its elements, once the
//! language is built. For instance, the template of a [user
//! definition](crate::lang::rewrites::userdefs::UserDefinition) is compiled
//! into a tree by a [`LateInit`](crate::core::registry::LateInit) build hook,
//! and the element of the definition reads the tree at each use site. If the
//! element owned the tree (through an `Arc`), the element and the tree would
//! refer to each other, and neither would ever be freed: the memory of the
//! whole language would stay allocated after the registry is dropped.
//!
//! # The ownership rule
//!
//! A value that refers to a built language (a parsed tree, a parsing state, a
//! compiled template) is never owned by an element of that language, directly
//! or through other values. The language owns the value, and the element
//! stores a [`LangOwned`], which refers to the value without owning it.
//!
//! The rule concerns only what an element's data and aspects own. A lowering
//! rule, a stage, a rewrite or a service is owned by the registry and not by
//! the language, so it may own such a value directly.
use Any;
use fmt;
use ;
use BuiltLang;
/// A `LangOwned<T>` is a write-once cell for a value of type `T` that is
/// owned by a [`BuiltLang`] and read by an element of that language.
///
/// Use it for every value that an element's data or aspect needs and that
/// refers to the language the element belongs to: a compiled template, a
/// parsed tree, a parsing state. The [module documentation](self) explains
/// why the element cannot own such a value.
///
/// The cell is created empty ([`LangOwned::new`]), usually when the feature
/// creates the element's data. [`set`](LangOwned::set) gives the value to the
/// language, usually in the feature's
/// [`LateInit`](crate::core::registry::LateInit) build hook.
/// [`get`](LangOwned::get) returns the value for as long as the language
/// exists. [`LangOwned::with_value`] creates a cell that already contains its
/// value.
///
/// # Examples
///
/// ```
/// use flm_core::lang::{build_language, LangInputs, LangOwned};
///
/// let cell: LangOwned<String> = LangOwned::new();
/// assert!(cell.get().is_none());
///
/// let lang = build_language(LangInputs::new());
/// assert!(cell.set(&lang, String::from("compiled against `lang`")));
/// assert_eq!(cell.get().as_deref().map(String::as_str), Some("compiled against `lang`"));
///
/// // The language owns the value: the cell is empty once the language is dropped.
/// drop(lang);
/// assert!(cell.get().is_none());
/// ```