naga_rust_back/
config.rs

1use alloc::borrow::Cow;
2use alloc::string::String;
3
4/// Configuration/builder for options for Rust code generation.
5///
6/// This configuration allows you to control syntactic characteristics of the output,
7/// and also Rust features that have no equivalent in shader languages.
8#[derive(Debug)]
9pub struct Config {
10    pub(crate) flags: WriterFlags,
11    pub(crate) runtime_path: Cow<'static, str>,
12    pub(crate) global_struct: Option<String>,
13    #[allow(dead_code, reason = "reminding ourselves of the future")]
14    pub(crate) edition: Edition,
15}
16
17impl Default for Config {
18    fn default() -> Self {
19        Self::new()
20    }
21}
22
23impl Config {
24    /// Creates a [`Config`] with default options.
25    #[must_use]
26    pub const fn new() -> Self {
27        Self {
28            flags: WriterFlags::empty(),
29            runtime_path: Cow::Borrowed("::naga_rust_rt"),
30            global_struct: None,
31            edition: Edition::Rust2024,
32        }
33    }
34
35    /// Sets whether the generated code contains explicit types when they could be omitted.
36    ///
37    /// The default is `false`.
38    #[must_use]
39    pub fn explicit_types(mut self, value: bool) -> Self {
40        self.flags.set(WriterFlags::EXPLICIT_TYPES, value);
41        self
42    }
43
44    /// Sets whether the generated code uses raw pointers instead of references.
45    ///
46    /// The resulting code is `unsafe` and may be unsound if the input module
47    /// uses pointers incorrectly.
48    ///
49    /// The default is `false`.
50    ///
51    /// TODO: This should be configurable on a per-function basis.
52    #[must_use]
53    pub fn raw_pointers(mut self, value: bool) -> Self {
54        self.flags.set(WriterFlags::RAW_POINTERS, value);
55        self
56    }
57
58    /// Sets whether generated items have `pub` visibility instead of private.
59    ///
60    /// The default is `false`.
61    #[must_use]
62    pub fn public_items(mut self, value: bool) -> Self {
63        self.flags.set(WriterFlags::PUBLIC, value);
64        self
65    }
66
67    /// Sets the Rust module path to the runtime support library.
68    ///
69    /// The default is `"::naga_rust_rt"`.
70    ///
71    /// # Panics
72    ///
73    /// May panic if the path is not syntactically valid or not an absolute path.
74    #[must_use]
75    pub fn runtime_path(mut self, value: impl Into<Cow<'static, str>>) -> Self {
76        let value = value.into();
77        assert!(
78            value.starts_with("::") || value.starts_with("crate::"),
79            "path should be an absolute path"
80        );
81        self.runtime_path = value;
82        self
83    }
84
85    /// Allow declarations of global variables, generate a struct with the given `name` to hold
86    /// them, and make all functions methods of that struct.
87    #[must_use]
88    pub fn global_struct(mut self, name: impl Into<String>) -> Self {
89        self.global_struct = Some(name.into());
90        self
91    }
92
93    pub(crate) fn use_global_struct(&self) -> bool {
94        self.global_struct.is_some()
95    }
96}
97
98bitflags::bitflags! {
99    /// Options for what Rust code is generated.
100    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
101    pub(crate) struct WriterFlags: u32 {
102        /// Always annotate the type information instead of inferring.
103        const EXPLICIT_TYPES = 0x1;
104
105        /// Generate code using raw pointers instead of references.
106        /// The resulting code is `unsafe` and may be unsound if the input module
107        /// uses pointers incorrectly.
108        const RAW_POINTERS = 0x2;
109
110        /// Generate items with `pub` visibility instead of private.
111        const PUBLIC = 0x3;
112    }
113}
114
115/// Edition of Rust code to generate.
116///
117/// We currently only support one edition, but this exists anyway to prepare to document
118/// any edition dependencies in the code generator.
119#[derive(Clone, Copy, Debug)]
120pub(crate) enum Edition {
121    Rust2024,
122}