Skip to main content

luaur_rt/
multi.rs

1//! [`MultiValue`] and [`Variadic`]. Mirrors `mlua::MultiValue` / `mlua::Variadic`.
2
3use std::collections::VecDeque;
4use std::ops::{Deref, DerefMut};
5
6use crate::value::Value;
7
8/// An ordered, growable sequence of Lua values, used to represent the multiple
9/// arguments / multiple return values that Lua functions can take and produce.
10///
11/// Mirrors `mlua::MultiValue`. Backed by a `VecDeque<Value>` so values can be
12/// efficiently popped from the front (argument order) and pushed at the back.
13#[derive(Default, Debug, Clone)]
14pub struct MultiValue(VecDeque<Value>);
15
16impl MultiValue {
17    /// An empty [`MultiValue`].
18    pub const fn new() -> MultiValue {
19        MultiValue(VecDeque::new())
20    }
21
22    /// An empty [`MultiValue`] with capacity preallocated.
23    pub fn with_capacity(capacity: usize) -> MultiValue {
24        MultiValue(VecDeque::with_capacity(capacity))
25    }
26
27    /// Build from a `Vec<Value>` (front = first value).
28    pub fn from_vec(vec: Vec<Value>) -> MultiValue {
29        MultiValue(vec.into())
30    }
31
32    /// Consume into a `Vec<Value>`.
33    pub fn into_vec(self) -> Vec<Value> {
34        self.0.into()
35    }
36}
37
38impl Deref for MultiValue {
39    type Target = VecDeque<Value>;
40    fn deref(&self) -> &Self::Target {
41        &self.0
42    }
43}
44
45impl DerefMut for MultiValue {
46    fn deref_mut(&mut self) -> &mut Self::Target {
47        &mut self.0
48    }
49}
50
51impl From<Vec<Value>> for MultiValue {
52    fn from(vec: Vec<Value>) -> Self {
53        MultiValue(vec.into())
54    }
55}
56
57impl From<MultiValue> for Vec<Value> {
58    fn from(m: MultiValue) -> Self {
59        m.0.into()
60    }
61}
62
63impl FromIterator<Value> for MultiValue {
64    fn from_iter<I: IntoIterator<Item = Value>>(iter: I) -> Self {
65        MultiValue(VecDeque::from_iter(iter))
66    }
67}
68
69impl IntoIterator for MultiValue {
70    type Item = Value;
71    type IntoIter = std::collections::vec_deque::IntoIter<Value>;
72    fn into_iter(self) -> Self::IntoIter {
73        self.0.into_iter()
74    }
75}
76
77impl<'a> IntoIterator for &'a MultiValue {
78    type Item = &'a Value;
79    type IntoIter = std::collections::vec_deque::Iter<'a, Value>;
80    fn into_iter(self) -> Self::IntoIter {
81        self.0.iter()
82    }
83}
84
85/// A wrapper collecting "the rest of" a Lua argument list (or producing a
86/// variable number of return values).
87///
88/// Mirrors `mlua::Variadic`. As an argument type it consumes every remaining
89/// value; as a return type it spreads its elements onto the stack.
90#[derive(Default, Debug, Clone, PartialEq)]
91pub struct Variadic<T>(Vec<T>);
92
93impl<T> Variadic<T> {
94    /// An empty [`Variadic`].
95    pub const fn new() -> Variadic<T> {
96        Variadic(Vec::new())
97    }
98
99    /// An empty [`Variadic`] with capacity preallocated.
100    pub fn with_capacity(capacity: usize) -> Variadic<T> {
101        Variadic(Vec::with_capacity(capacity))
102    }
103}
104
105impl<T> Deref for Variadic<T> {
106    type Target = Vec<T>;
107    fn deref(&self) -> &Self::Target {
108        &self.0
109    }
110}
111
112impl<T> DerefMut for Variadic<T> {
113    fn deref_mut(&mut self) -> &mut Self::Target {
114        &mut self.0
115    }
116}
117
118impl<T> From<Vec<T>> for Variadic<T> {
119    fn from(vec: Vec<T>) -> Self {
120        Variadic(vec)
121    }
122}
123
124impl<T> From<Variadic<T>> for Vec<T> {
125    fn from(v: Variadic<T>) -> Self {
126        v.0
127    }
128}
129
130impl<T> FromIterator<T> for Variadic<T> {
131    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
132        Variadic(Vec::from_iter(iter))
133    }
134}
135
136impl<T> IntoIterator for Variadic<T> {
137    type Item = T;
138    type IntoIter = std::vec::IntoIter<T>;
139    fn into_iter(self) -> Self::IntoIter {
140        self.0.into_iter()
141    }
142}