Skip to main content

mlua/
stdlib.rs

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