Skip to main content

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    #[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    /// [`jit`](http://luajit.org/ext_jit.html) library
77    #[cfg(any(feature = "luajit", doc))]
78    #[cfg_attr(docsrs, doc(cfg(feature = "luajit")))]
79    pub const JIT: StdLib = StdLib(1 << 11);
80
81    /// (**unsafe**) [`ffi`](http://luajit.org/ext_ffi.html) library
82    #[cfg(any(feature = "luajit", doc))]
83    #[cfg_attr(docsrs, doc(cfg(feature = "luajit")))]
84    pub const FFI: StdLib = StdLib(1 << 30);
85
86    /// (**unsafe**) [`debug`](https://www.lua.org/manual/5.4/manual.html#6.10) library
87    pub const DEBUG: StdLib = StdLib(1 << 31);
88
89    /// No libraries
90    pub const NONE: StdLib = StdLib(0);
91    /// (**unsafe**) All standard libraries
92    pub const ALL: StdLib = StdLib(u32::MAX);
93    /// The safe subset of the standard libraries
94    #[cfg(not(feature = "luau"))]
95    pub const ALL_SAFE: StdLib = StdLib((1 << 30) - 1);
96    #[cfg(feature = "luau")]
97    pub const ALL_SAFE: StdLib = StdLib(u32::MAX);
98
99    pub fn contains(self, lib: Self) -> bool {
100        (self & lib).0 != 0
101    }
102}
103
104impl BitAnd for StdLib {
105    type Output = Self;
106    fn bitand(self, rhs: Self) -> Self::Output {
107        StdLib(self.0 & rhs.0)
108    }
109}
110
111impl BitAndAssign for StdLib {
112    fn bitand_assign(&mut self, rhs: Self) {
113        *self = StdLib(self.0 & rhs.0)
114    }
115}
116
117impl BitOr for StdLib {
118    type Output = Self;
119    fn bitor(self, rhs: Self) -> Self::Output {
120        StdLib(self.0 | rhs.0)
121    }
122}
123
124impl BitOrAssign for StdLib {
125    fn bitor_assign(&mut self, rhs: Self) {
126        *self = StdLib(self.0 | rhs.0)
127    }
128}
129
130impl BitXor for StdLib {
131    type Output = Self;
132    fn bitxor(self, rhs: Self) -> Self::Output {
133        StdLib(self.0 ^ rhs.0)
134    }
135}
136
137impl BitXorAssign for StdLib {
138    fn bitxor_assign(&mut self, rhs: Self) {
139        *self = StdLib(self.0 ^ rhs.0)
140    }
141}