Skip to main content

rune/ast/
item_static.rs

1use crate::ast::prelude::*;
2
3#[test]
4#[cfg(not(miri))]
5fn ast_parse() {
6    rt::<ast::ItemStatic>("static value = #{}");
7    rt::<ast::ItemStatic>("static value");
8}
9
10/// A static declaration.
11#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
12#[rune(parse = "meta_only")]
13#[non_exhaustive]
14pub struct ItemStatic {
15    /// The *inner* attributes that are applied to the static declaration.
16    #[rune(iter, meta)]
17    pub attributes: Vec<ast::Attribute>,
18    /// The visibility of the static.
19    #[rune(option, meta)]
20    pub visibility: ast::Visibility,
21    /// The `static` keyword.
22    #[rune(meta)]
23    pub static_token: T![static],
24    /// The name of the static.
25    pub name: ast::Ident,
26    /// The initializer, if the static has one.
27    ///
28    /// A static without an initializer has to be assigned before it can be
29    /// read, either by a script or by the caller through the storage the
30    /// virtual machine has been configured with.
31    #[rune(iter)]
32    pub init: Option<ItemStaticInit>,
33    /// Opaque identifier for the static.
34    #[rune(skip)]
35    pub(crate) id: ItemId,
36}
37
38impl ItemStatic {
39    /// Get the descriptive span of this item, e.g. `static ITEM` instead of the
40    /// span for the whole expression.
41    pub(crate) fn descriptive_span(&self) -> Span {
42        self.static_token.span().join(self.name.span())
43    }
44}
45
46/// The initializer of a [`ItemStatic`].
47#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
48#[non_exhaustive]
49pub struct ItemStaticInit {
50    /// The equals token.
51    pub eq: T![=],
52    /// The expression the static is initialized with, which has to be
53    /// constant.
54    pub expr: ast::Expr,
55}
56
57impl Peek for ItemStaticInit {
58    fn peek(p: &mut Peeker<'_>) -> bool {
59        matches!(p.nth(0), K![=])
60    }
61}
62
63item_parse!(Static, ItemStatic, "static item");