factorio_mlua/
stdlib.rs

1use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign};
2use std::u32;
3
4/// Flags describing the set of lua standard libraries to load.
5#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
6pub struct StdLib(u32);
7
8impl StdLib {
9    /// [`coroutine`](https://www.lua.org/manual/5.4/manual.html#6.2) library
10    ///
11    /// Requires `feature = "lua54/lua53/lua52/luau"`
12    #[cfg(any(
13        feature = "lua54",
14        feature = "lua53",
15        feature = "lua52",
16        feature = "luau"
17    ))]
18    pub const COROUTINE: StdLib = StdLib(1);
19    /// [`table`](https://www.lua.org/manual/5.4/manual.html#6.6) library
20    pub const TABLE: StdLib = StdLib(1 << 1);
21    /// [`io`](https://www.lua.org/manual/5.4/manual.html#6.8) library
22    #[cfg(not(any(feature = "luau", feature = "lua-factorio")))]
23    #[cfg_attr(docsrs, doc(cfg(not(any(feature = "luau", feature = "lua-factorio")))))]
24    pub const IO: StdLib = StdLib(1 << 2);
25    /// [`os`](https://www.lua.org/manual/5.4/manual.html#6.9) library
26    ///
27    /// Not compatible with `feature = "lua-factorio"`
28    #[cfg(not(feature = "lua-factorio"))]
29    pub const OS: StdLib = StdLib(1 << 3);
30    /// [`string`](https://www.lua.org/manual/5.4/manual.html#6.4) library
31    pub const STRING: StdLib = StdLib(1 << 4);
32    /// [`utf8`](https://www.lua.org/manual/5.4/manual.html#6.5) library
33    ///
34    /// Requires `feature = "lua54/lua53/luau"`
35    #[cfg(any(feature = "lua54", feature = "lua53", feature = "luau"))]
36    pub const UTF8: StdLib = StdLib(1 << 5);
37    /// [`bit`](https://www.lua.org/manual/5.2/manual.html#6.7) library
38    ///
39    /// Requires `feature = "lua52/luajit/luau/lua-factorio"`
40    #[cfg(any(feature = "lua52", feature = "luajit", feature = "luau", feature = "lua-factorio", doc))]
41    pub const BIT: StdLib = StdLib(1 << 6);
42    /// [`math`](https://www.lua.org/manual/5.4/manual.html#6.7) library
43    pub const MATH: StdLib = StdLib(1 << 7);
44    /// [`package`](https://www.lua.org/manual/5.4/manual.html#6.3) library
45    #[cfg(not(feature = "luau"))]
46    #[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
47    pub const PACKAGE: StdLib = StdLib(1 << 8);
48    /// [`jit`](http://luajit.org/ext_jit.html) library
49    ///
50    /// Requires `feature = "luajit"`
51    #[cfg(any(feature = "luajit", doc))]
52    #[cfg_attr(docsrs, doc(cfg(feature = "luajit")))]
53    pub const JIT: StdLib = StdLib(1 << 9);
54
55    /// (**unsafe**) [`ffi`](http://luajit.org/ext_ffi.html) library
56    ///
57    /// Requires `feature = "luajit"`
58    #[cfg(any(feature = "luajit", doc))]
59    #[cfg_attr(docsrs, doc(cfg(feature = "luajit")))]
60    pub const FFI: StdLib = StdLib(1 << 30);
61    /// (**unsafe**) [`debug`](https://www.lua.org/manual/5.4/manual.html#6.10) library
62    pub const DEBUG: StdLib = StdLib(1 << 31);
63
64    /// No libraries
65    pub const NONE: StdLib = StdLib(0);
66    /// (**unsafe**) All standard libraries
67    pub const ALL: StdLib = StdLib(u32::MAX);
68    /// The safe subset of the standard libraries
69    #[cfg(not(feature = "luau"))]
70    pub const ALL_SAFE: StdLib = StdLib((1 << 30) - 1);
71    #[cfg(feature = "luau")]
72    pub const ALL_SAFE: StdLib = StdLib(u32::MAX);
73
74    pub fn contains(self, lib: Self) -> bool {
75        (self & lib).0 != 0
76    }
77}
78
79impl BitAnd for StdLib {
80    type Output = Self;
81    fn bitand(self, rhs: Self) -> Self::Output {
82        StdLib(self.0 & rhs.0)
83    }
84}
85
86impl BitAndAssign for StdLib {
87    fn bitand_assign(&mut self, rhs: Self) {
88        *self = StdLib(self.0 & rhs.0)
89    }
90}
91
92impl BitOr for StdLib {
93    type Output = Self;
94    fn bitor(self, rhs: Self) -> Self::Output {
95        StdLib(self.0 | rhs.0)
96    }
97}
98
99impl BitOrAssign for StdLib {
100    fn bitor_assign(&mut self, rhs: Self) {
101        *self = StdLib(self.0 | rhs.0)
102    }
103}
104
105impl BitXor for StdLib {
106    type Output = Self;
107    fn bitxor(self, rhs: Self) -> Self::Output {
108        StdLib(self.0 ^ rhs.0)
109    }
110}
111
112impl BitXorAssign for StdLib {
113    fn bitxor_assign(&mut self, rhs: Self) {
114        *self = StdLib(self.0 ^ rhs.0)
115    }
116}