Skip to content

Commit 26acebf

Browse files
committed
Make multiple arguments more convenient.
1 parent b95095c commit 26acebf

File tree

2 files changed

+122
-12
lines changed

2 files changed

+122
-12
lines changed

src/actor.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ impl Actor {
282282
lua.load(source).eval()
283283
}
284284

285-
pub fn call(&self, name: &'static str, args: MultiLuaMessage) -> Result<LuaMessage, Error> {
285+
pub fn call(
286+
&self,
287+
name: &'static str,
288+
args: impl Into<MultiLuaMessage> + Clone + Sync + Send + 'static,
289+
) -> Result<LuaMessage, Error> {
286290
match self.handler.clone() {
287291
Some(_handler) => {
288292
let lua = self.lua.clone();
@@ -293,7 +297,11 @@ impl Actor {
293297
None => Self::_call(&self.lua.clone(), name, args),
294298
}
295299
}
296-
pub fn call_nowait(&self, name: &'static str, args: MultiLuaMessage) -> Result<(), Error> {
300+
pub fn call_nowait(
301+
&self,
302+
name: &'static str,
303+
args: impl Into<MultiLuaMessage> + Clone + Sync + Send + 'static,
304+
) -> Result<(), Error> {
297305
match self.handler.clone() {
298306
Some(_handler) => {
299307
let lua = self.lua.clone();
@@ -311,14 +319,14 @@ impl Actor {
311319
fn _call(
312320
lua: &Arc<Mutex<Lua>>,
313321
name: &str,
314-
args: MultiLuaMessage,
322+
args: impl Into<MultiLuaMessage>,
315323
) -> Result<LuaMessage, Error> {
316324
let vm = lua.lock().unwrap();
317325
vm.context(|lua| {
318326
lua.globals()
319327
.get::<_, Function>(name)
320328
.unwrap()
321-
.call::<_, _>(args)
329+
.call::<_, _>(args.into())
322330
})
323331
}
324332
/*
@@ -365,7 +373,7 @@ fn test_actor_new() {
365373
)
366374
.ok()
367375
.unwrap();
368-
match act.call("testit", LuaMessage::from(1).into()) {
376+
match act.call("testit", 1) {
369377
Ok(_v) => {
370378
assert_eq!(Some(2), Option::from(_v));
371379
}
@@ -387,7 +395,8 @@ fn test_actor_new() {
387395
match act.call(
388396
"testlist",
389397
// Vec::<LuaMessage>::from_iter([3.into(), 2.into()]).into(),
390-
LuaMessage::from_iter([3.into(), 2.into()]).into(),
398+
// LuaMessage::from_iter([3.into(), 2.into()]),
399+
LuaMessage::from_slice([3, 2]),
391400
) {
392401
Ok(_v) => {
393402
assert_eq!(Some(2), Option::from(_v));
@@ -409,7 +418,9 @@ fn test_actor_new() {
409418
println!("222");
410419
match act.call(
411420
"testvargs",
412-
Variadic::<LuaMessage>::from_iter([6.into(), 7.into()]).into(),
421+
// Variadic::<LuaMessage>::from_iter([6.into(), 7.into()]),
422+
// Vec::<LuaMessage>::from([6.into(), 7.into()]),
423+
MultiLuaMessage::from_slice([6, 7]),
413424
) {
414425
Ok(_v) => {
415426
assert_eq!(Some(13), Option::from(_v));

src/message.rs

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ pub enum LuaMessage {
2121
Variadic(VariadicLuaMessage),
2222
}
2323

24+
impl LuaMessage {
25+
pub fn from_slice<I: IntoIterator<Item = impl Into<LuaMessage>>>(iter: I) -> Self {
26+
LuaMessage::from(
27+
iter.into_iter()
28+
.map(|v| v.into())
29+
.collect::<Vec<LuaMessage>>(),
30+
)
31+
}
32+
}
33+
2434
impl From<bool> for LuaMessage {
2535
fn from(s: bool) -> Self {
2636
LuaMessage::Boolean(s)
@@ -79,16 +89,87 @@ impl PartialEq<VariadicLuaMessage> for VariadicLuaMessage {
7989
}
8090
}
8191

92+
impl From<VariadicLuaMessage> for LuaMessage {
93+
fn from(s: VariadicLuaMessage) -> Self {
94+
LuaMessage::from(s.0)
95+
}
96+
}
8297
impl From<Variadic<LuaMessage>> for LuaMessage {
8398
fn from(s: Variadic<LuaMessage>) -> Self {
84-
LuaMessage::from(
85-
s.into_iter()
86-
.map(|v| LuaMessage::from(v.clone()))
87-
.collect::<Vec<LuaMessage>>(),
88-
)
99+
LuaMessage::from(s.into_iter().map(|v| v).collect::<Vec<LuaMessage>>())
89100
}
90101
}
91102

103+
macro_rules! lua_message_convert_from_collection {
104+
($x:tt, $y:ty) => {
105+
impl From<$x<$y>> for LuaMessage {
106+
fn from(s: $x<$y>) -> Self {
107+
LuaMessage::from(
108+
s.into_iter()
109+
.map(|v| LuaMessage::from(v))
110+
.collect::<Vec<LuaMessage>>(),
111+
)
112+
}
113+
}
114+
impl From<$x<$y>> for MultiLuaMessage {
115+
fn from(s: $x<$y>) -> Self {
116+
LuaMessage::from(s).into()
117+
}
118+
}
119+
};
120+
}
121+
macro_rules! lua_message_convert_from_collection_option {
122+
($x:tt, $y:ty) => {
123+
impl From<$x<Option<$y>>> for LuaMessage {
124+
fn from(s: $x<Option<$y>>) -> Self {
125+
LuaMessage::from(
126+
s.into_iter()
127+
.map(|v| match v {
128+
Some(s) => LuaMessage::from(s),
129+
None => LuaMessage::Nil,
130+
})
131+
.collect::<Vec<LuaMessage>>(),
132+
)
133+
}
134+
}
135+
impl From<$x<Option<$y>>> for MultiLuaMessage {
136+
fn from(s: $x<Option<$y>>) -> Self {
137+
LuaMessage::from(s).into()
138+
}
139+
}
140+
};
141+
}
142+
macro_rules! lua_message_convert_from_collection_and_types {
143+
($y:ty) => {
144+
lua_message_convert_from_collection!(Vec, $y);
145+
lua_message_convert_from_collection!(Variadic, $y);
146+
lua_message_convert_from_collection_option!(Vec, $y);
147+
lua_message_convert_from_collection_option!(Variadic, $y);
148+
impl From<$y> for MultiLuaMessage {
149+
fn from(s: $y) -> Self {
150+
LuaMessage::from(s).into()
151+
}
152+
}
153+
};
154+
}
155+
lua_message_convert_from_collection_and_types!(String);
156+
lua_message_convert_from_collection_and_types!(bool);
157+
lua_message_convert_from_collection_and_types!(i8);
158+
lua_message_convert_from_collection_and_types!(u8);
159+
lua_message_convert_from_collection_and_types!(i16);
160+
lua_message_convert_from_collection_and_types!(u16);
161+
lua_message_convert_from_collection_and_types!(i32);
162+
lua_message_convert_from_collection_and_types!(u32);
163+
lua_message_convert_from_collection_and_types!(i64);
164+
lua_message_convert_from_collection_and_types!(isize);
165+
lua_message_convert_from_collection_and_types!(usize);
166+
lua_message_convert_from_collection_and_types!(f32);
167+
lua_message_convert_from_collection_and_types!(f64);
168+
lua_message_convert_from_collection_and_types!(HashMap<String, LuaMessage>);
169+
// lua_message_convert_from_collection_and_types!(Vec<LuaMessage>);
170+
// lua_message_convert_from_collection_and_types!(VariadicLuaMessage);
171+
// lua_message_convert_from_collection_and_types!(Variadic<LuaMessage>);
172+
92173
impl Into<Variadic<LuaMessage>> for LuaMessage {
93174
fn into(self) -> Variadic<LuaMessage> {
94175
return Variadic::from_iter([self]);
@@ -213,6 +294,13 @@ impl FromIterator<LuaMessage> for LuaMessage {
213294
LuaMessage::Array(Vec::<LuaMessage>::from_iter(iter))
214295
}
215296
}
297+
/*
298+
impl<I: IntoIterator<Item = LuaMessage>> From<I> for LuaMessage {
299+
fn from(s: I) -> Self {
300+
LuaMessage::Array(Vec::<LuaMessage>::from_iter(s))
301+
}
302+
}
303+
// */
216304
impl From<LuaMessage> for Option<Vec<LuaMessage>> {
217305
fn from(s: LuaMessage) -> Self {
218306
match s {
@@ -293,6 +381,17 @@ impl<'lua> ToLua<'lua> for LuaMessage {
293381
#[derive(Debug, PartialEq, Clone)]
294382
pub struct MultiLuaMessage(LuaMessage);
295383

384+
impl MultiLuaMessage {
385+
pub fn from_slice<I: IntoIterator<Item = impl Into<LuaMessage>>>(iter: I) -> Self {
386+
LuaMessage::Variadic(VariadicLuaMessage(
387+
iter.into_iter()
388+
.map(|v| v.into())
389+
.collect::<Variadic<LuaMessage>>(),
390+
))
391+
.into()
392+
}
393+
}
394+
296395
impl<'lua> ToLuaMulti<'lua> for MultiLuaMessage {
297396
fn to_lua_multi(self, lua: Context<'lua>) -> LuaResult<MultiValue<'lua>> {
298397
match self.0 {

0 commit comments

Comments
 (0)