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