From 83e1e8853125ec1df32f81f537d684901efff5bd Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 12 Feb 2022 16:23:36 +0800 Subject: [PATCH] Add specific implementations for to_string. --- src/packages/string_basic.rs | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 7be2809a..ae3342ea 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -64,22 +64,65 @@ mod print_debug_functions { pub fn to_debug_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString { ctx.engine().map_type_name(&format!("{:?}", item)).into() } + /// Return the empty string. #[rhai_fn(name = "print", name = "debug")] pub fn print_empty_string(ctx: NativeCallContext) -> ImmutableString { ctx.engine().const_empty_string() } + /// Return the `string`. #[rhai_fn(name = "print", name = "to_string")] pub fn print_string(string: ImmutableString) -> ImmutableString { string } + /// Convert the string into debug format. + #[rhai_fn(name = "debug", name = "to_debug", pure)] + pub fn debug_string(string: &mut ImmutableString) -> ImmutableString { + format!("{:?}", string).into() + } + + /// Return the character into a string. + #[rhai_fn(name = "print", name = "to_string")] + pub fn print_char(character: char) -> ImmutableString { + character.to_string().into() + } + /// Convert the string into debug format. + #[rhai_fn(name = "debug", name = "to_debug")] + pub fn debug_char(character: char) -> ImmutableString { + format!("{:?}", character).into() + } + /// Convert the function pointer into a string in debug format. #[rhai_fn(name = "debug", name = "to_debug", pure)] pub fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString { f.to_string().into() } + /// Return the boolean value into a string. + #[rhai_fn(name = "print", name = "to_string")] + pub fn print_bool(value: bool) -> ImmutableString { + format!("{}", value).into() + } + /// Convert the boolean value into a string in debug format. + #[rhai_fn(name = "debug", name = "to_debug")] + pub fn debug_bool(value: bool) -> ImmutableString { + format!("{:?}", value).into() + } + + /// Return the empty string. + #[rhai_fn(name = "print", name = "to_string")] + pub fn print_unit(ctx: NativeCallContext, unit: ()) -> ImmutableString { + let _ = unit; + ctx.engine().const_empty_string() + } + /// Convert the unit into a string in debug format. + #[rhai_fn(name = "debug", name = "to_debug")] + pub fn debug_unit(unit: ()) -> ImmutableString { + let _ = unit; + "()".into() + } + /// Convert the value of `number` into a string. #[cfg(not(feature = "no_float"))] #[rhai_fn(name = "print", name = "to_string")]