From e77dc4cc3423e8b5233858ffa8040766b2d6d32d Mon Sep 17 00:00:00 2001 From: J Henry Waugh Date: Thu, 13 Aug 2020 22:00:01 -0500 Subject: [PATCH 1/2] Fix missing import in test_functions --- codegen/tests/test_functions.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/codegen/tests/test_functions.rs b/codegen/tests/test_functions.rs index b61ae340..cfc73275 100644 --- a/codegen/tests/test_functions.rs +++ b/codegen/tests/test_functions.rs @@ -1,4 +1,5 @@ use rhai::module_resolvers::*; +use rhai::plugin::*; use rhai::{Array, Engine, EvalAltResult, Module, RegisterFn, FLOAT}; pub mod raw_fn { From b27ebb318d43fe4559ce22d4a8ed95d32a079e3f Mon Sep 17 00:00:00 2001 From: J Henry Waugh Date: Thu, 13 Aug 2020 23:04:24 -0500 Subject: [PATCH 2/2] Rearrange imports to support no_std --- codegen/src/function.rs | 69 +++++++++++++++++++++-------------------- codegen/src/module.rs | 52 ++++++++++++++++++------------- src/plugin.rs | 7 +++-- 3 files changed, 71 insertions(+), 57 deletions(-) diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 0c8391bb..f3bfd344 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -1,5 +1,8 @@ #![allow(unused)] +#[cfg(no_std)] +use core::mem; + use std::collections::HashMap; use quote::{quote, quote_spanned}; @@ -329,7 +332,7 @@ impl ExportedFn { self.name().span(), ); quote! { - pub fn #input_types_fn_name() -> Box<[std::any::TypeId]> { + pub fn #input_types_fn_name() -> Box<[TypeId]> { #token_name().input_types() } } @@ -375,7 +378,7 @@ impl ExportedFn { ); input_type_exprs.push( syn::parse2::(quote_spanned!( - arg_type.span()=> std::any::TypeId::of::<#arg_type>() + arg_type.span()=> TypeId::of::<#arg_type>() )) .unwrap(), ); @@ -407,7 +410,7 @@ impl ExportedFn { &syn::Type::Path(ref p) if p.path == str_type_path => { is_str_ref = true; quote_spanned!(arg_type.span()=> - std::mem::take(args[#i]) + mem::take(args[#i]) .clone().cast::()) } _ => panic!("internal error: why wasn't this found earlier!?"), @@ -415,7 +418,7 @@ impl ExportedFn { _ => { is_str_ref = false; quote_spanned!(arg_type.span()=> - std::mem::take(args[#i]).clone().cast::<#arg_type>()) + mem::take(args[#i]).clone().cast::<#arg_type>()) } }; @@ -428,14 +431,14 @@ impl ExportedFn { if !is_str_ref { input_type_exprs.push( syn::parse2::(quote_spanned!( - arg_type.span()=> std::any::TypeId::of::<#arg_type>() + arg_type.span()=> TypeId::of::<#arg_type>() )) .unwrap(), ); } else { input_type_exprs.push( syn::parse2::(quote_spanned!( - arg_type.span()=> std::any::TypeId::of::() + arg_type.span()=> TypeId::of::() )) .unwrap(), ); @@ -489,8 +492,8 @@ impl ExportedFn { fn is_method_call(&self) -> bool { #is_method_call } fn is_varadic(&self) -> bool { false } fn clone_boxed(&self) -> Box { Box::new(#type_name()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![#(#input_type_exprs),*].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![#(#input_type_exprs),*].into_boxed_slice() } } } @@ -781,14 +784,14 @@ mod generate_tests { fn is_method_call(&self) -> bool { false } fn is_varadic(&self) -> bool { false } fn clone_boxed(&self) -> Box { Box::new(Token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![].into_boxed_slice() } } pub fn token_callable() -> CallableFunction { CallableFunction::from_plugin(Token()) } - pub fn token_input_types() -> Box<[std::any::TypeId]> { + pub fn token_input_types() -> Box<[TypeId]> { Token().input_types() } type EvalBox = Box; @@ -822,21 +825,21 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = std::mem::take(args[0usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(do_something(arg0))) } fn is_method_call(&self) -> bool { false } fn is_varadic(&self) -> bool { false } fn clone_boxed(&self) -> Box { Box::new(Token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::()].into_boxed_slice() } } pub fn token_callable() -> CallableFunction { CallableFunction::from_plugin(Token()) } - pub fn token_input_types() -> Box<[std::any::TypeId]> { + pub fn token_input_types() -> Box<[TypeId]> { Token().input_types() } type EvalBox = Box; @@ -866,15 +869,15 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = std::mem::take(args[0usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(do_something(arg0))) } fn is_method_call(&self) -> bool { false } fn is_varadic(&self) -> bool { false } fn clone_boxed(&self) -> Box { Box::new(MyType()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::()].into_boxed_slice() } } }; @@ -903,23 +906,23 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 2usize), Position::none()))); } - let arg0 = std::mem::take(args[0usize]).clone().cast::(); - let arg1 = std::mem::take(args[1usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).clone().cast::(); + let arg1 = mem::take(args[1usize]).clone().cast::(); Ok(Dynamic::from(add_together(arg0, arg1))) } fn is_method_call(&self) -> bool { false } fn is_varadic(&self) -> bool { false } fn clone_boxed(&self) -> Box { Box::new(Token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::(), - std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::(), + TypeId::of::()].into_boxed_slice() } } pub fn token_callable() -> CallableFunction { CallableFunction::from_plugin(Token()) } - pub fn token_input_types() -> Box<[std::any::TypeId]> { + pub fn token_input_types() -> Box<[TypeId]> { Token().input_types() } type EvalBox = Box; @@ -953,7 +956,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 2usize), Position::none()))); } - let arg1 = std::mem::take(args[1usize]).clone().cast::(); + let arg1 = mem::take(args[1usize]).clone().cast::(); let arg0: &mut _ = &mut args[0usize].write_lock::().unwrap(); Ok(Dynamic::from(increment(arg0, arg1))) } @@ -961,15 +964,15 @@ mod generate_tests { fn is_method_call(&self) -> bool { true } fn is_varadic(&self) -> bool { false } fn clone_boxed(&self) -> Box { Box::new(Token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::(), - std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::(), + TypeId::of::()].into_boxed_slice() } } pub fn token_callable() -> CallableFunction { CallableFunction::from_plugin(Token()) } - pub fn token_input_types() -> Box<[std::any::TypeId]> { + pub fn token_input_types() -> Box<[TypeId]> { Token().input_types() } type EvalBox = Box; @@ -1004,21 +1007,21 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = std::mem::take(args[0usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(special_print(&arg0))) } fn is_method_call(&self) -> bool { false } fn is_varadic(&self) -> bool { false } fn clone_boxed(&self) -> Box { Box::new(Token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::()].into_boxed_slice() } } pub fn token_callable() -> CallableFunction { CallableFunction::from_plugin(Token()) } - pub fn token_input_types() -> Box<[std::any::TypeId]> { + pub fn token_input_types() -> Box<[TypeId]> { Token().input_types() } type EvalBox = Box; diff --git a/codegen/src/module.rs b/codegen/src/module.rs index 256ca062..9e042bb9 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -4,6 +4,14 @@ use syn::{parse::Parse, parse::ParseStream}; use crate::function::ExportedFn; use crate::rhai_module::ExportedConst; +#[cfg(no_std)] +use alloc::vec as new_vec; +#[cfg(not(no_std))] +use std::vec as new_vec; + +#[cfg(no_std)] +use core::mem; + #[derive(Debug)] pub(crate) struct Module { mod_all: Option, @@ -53,8 +61,8 @@ impl Parse for Module { }) .collect(); } else { - consts = vec![]; - fns = vec![]; + consts = new_vec![]; + fns = new_vec![]; } Ok(Module { mod_all: Some(mod_all), @@ -319,14 +327,14 @@ mod generate_tests { fn clone_boxed(&self) -> Box { Box::new(get_mystic_number_token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![].into_boxed_slice() } } pub fn get_mystic_number_token_callable() -> CallableFunction { CallableFunction::from_plugin(get_mystic_number_token()) } - pub fn get_mystic_number_token_input_types() -> Box<[std::any::TypeId]> { + pub fn get_mystic_number_token_input_types() -> Box<[TypeId]> { get_mystic_number_token().input_types() } } @@ -371,7 +379,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = std::mem::take(args[0usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(add_one_to(arg0))) } @@ -380,14 +388,14 @@ mod generate_tests { fn clone_boxed(&self) -> Box { Box::new(add_one_to_token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::()].into_boxed_slice() } } pub fn add_one_to_token_callable() -> CallableFunction { CallableFunction::from_plugin(add_one_to_token()) } - pub fn add_one_to_token_input_types() -> Box<[std::any::TypeId]> { + pub fn add_one_to_token_input_types() -> Box<[TypeId]> { add_one_to_token().input_types() } } @@ -433,8 +441,8 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 2usize), Position::none()))); } - let arg0 = std::mem::take(args[0usize]).clone().cast::(); - let arg1 = std::mem::take(args[1usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).clone().cast::(); + let arg1 = mem::take(args[1usize]).clone().cast::(); Ok(Dynamic::from(add_together(arg0, arg1))) } @@ -443,15 +451,15 @@ mod generate_tests { fn clone_boxed(&self) -> Box { Box::new(add_together_token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::(), - std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::(), + TypeId::of::()].into_boxed_slice() } } pub fn add_together_token_callable() -> CallableFunction { CallableFunction::from_plugin(add_together_token()) } - pub fn add_together_token_input_types() -> Box<[std::any::TypeId]> { + pub fn add_together_token_input_types() -> Box<[TypeId]> { add_together_token().input_types() } } @@ -605,7 +613,7 @@ mod generate_tests { format!("wrong arg count: {} != {}", args.len(), 1usize), Position::none()))); } - let arg0 = std::mem::take(args[0usize]).clone().cast::(); + let arg0 = mem::take(args[0usize]).clone().cast::(); Ok(Dynamic::from(print_out_to(&arg0))) } @@ -614,14 +622,14 @@ mod generate_tests { fn clone_boxed(&self) -> Box { Box::new(print_out_to_token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::()].into_boxed_slice() } } pub fn print_out_to_token_callable() -> CallableFunction { CallableFunction::from_plugin(print_out_to_token()) } - pub fn print_out_to_token_input_types() -> Box<[std::any::TypeId]> { + pub fn print_out_to_token_input_types() -> Box<[TypeId]> { print_out_to_token().input_types() } } @@ -676,14 +684,14 @@ mod generate_tests { fn clone_boxed(&self) -> Box { Box::new(increment_token()) } - fn input_types(&self) -> Box<[std::any::TypeId]> { - vec![std::any::TypeId::of::()].into_boxed_slice() + fn input_types(&self) -> Box<[TypeId]> { + new_vec![TypeId::of::()].into_boxed_slice() } } pub fn increment_token_callable() -> CallableFunction { CallableFunction::from_plugin(increment_token()) } - pub fn increment_token_input_types() -> Box<[std::any::TypeId]> { + pub fn increment_token_input_types() -> Box<[TypeId]> { increment_token().input_types() } } diff --git a/src/plugin.rs b/src/plugin.rs index 23e7f03e..19e9939a 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,8 +1,11 @@ //! Module defining plugins in Rhai. Is exported for use by plugin authors. -use crate::stdlib::{any::TypeId, boxed::Box}; - pub use crate::{ + stdlib::any::TypeId, + stdlib::boxed::Box, + stdlib::vec::Vec, + stdlib::vec as new_vec, + stdlib::mem, fn_native::CallableFunction, Dynamic, Engine,