Add specific implementations for to_string.

This commit is contained in:
Stephen Chung 2022-02-12 16:23:36 +08:00
parent 78794d7dfc
commit 83e1e88531

View File

@ -64,22 +64,65 @@ mod print_debug_functions {
pub fn to_debug_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString { pub fn to_debug_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString {
ctx.engine().map_type_name(&format!("{:?}", item)).into() ctx.engine().map_type_name(&format!("{:?}", item)).into()
} }
/// Return the empty string. /// Return the empty string.
#[rhai_fn(name = "print", name = "debug")] #[rhai_fn(name = "print", name = "debug")]
pub fn print_empty_string(ctx: NativeCallContext) -> ImmutableString { pub fn print_empty_string(ctx: NativeCallContext) -> ImmutableString {
ctx.engine().const_empty_string() ctx.engine().const_empty_string()
} }
/// Return the `string`. /// Return the `string`.
#[rhai_fn(name = "print", name = "to_string")] #[rhai_fn(name = "print", name = "to_string")]
pub fn print_string(string: ImmutableString) -> ImmutableString { pub fn print_string(string: ImmutableString) -> ImmutableString {
string 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. /// Convert the function pointer into a string in debug format.
#[rhai_fn(name = "debug", name = "to_debug", pure)] #[rhai_fn(name = "debug", name = "to_debug", pure)]
pub fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString { pub fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
f.to_string().into() 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. /// Convert the value of `number` into a string.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
#[rhai_fn(name = "print", name = "to_string")] #[rhai_fn(name = "print", name = "to_string")]