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
40/// The initializer of a [`ItemStatic`].
41#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
42#[non_exhaustive]
43pub struct ItemStaticInit {
44    /// The equals token.
45    pub eq: T![=],
46    /// The expression the static is initialized with, which has to be
47    /// constant.
48    pub expr: ast::Expr,
49}
50
51impl Peek for ItemStaticInit {
52    fn peek(p: &mut Peeker<'_>) -> bool {
53        matches!(p.nth(0), K![=])
54    }
55}
56
57item_parse!(Static, ItemStatic, "static item");