mlua/
stdlib.rs

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