Rename as_string and as_immutable_string.
This commit is contained in:
parent
b21deaf052
commit
94674679d9
@ -8,6 +8,7 @@ Enhancements
|
|||||||
------------
|
------------
|
||||||
|
|
||||||
* `$symbol$` is supported in custom syntax to match any symbol.
|
* `$symbol$` is supported in custom syntax to match any symbol.
|
||||||
|
* `Dynamic::as_string` and `Dynamic::as_immutable_string` are deprecated and replaced by `into_string` and `into_immutable_string` respectively.
|
||||||
* `parse_float()`, `PI()` and `E()` now defer to `Decimal` under `no_float` if `decimal` is turned on.
|
* `parse_float()`, `PI()` and `E()` now defer to `Decimal` under `no_float` if `decimal` is turned on.
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rhai_codegen"
|
name = "rhai_codegen"
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
authors = ["jhwgh1968", "Stephen Chung"]
|
authors = ["jhwgh1968", "Stephen Chung"]
|
||||||
description = "Procedural macros support package for Rhai, a scripting language and engine for Rust"
|
description = "Procedural macros support package for Rhai, a scripting language and engine for Rust"
|
||||||
|
@ -743,7 +743,7 @@ impl ExportedFn {
|
|||||||
is_string = true;
|
is_string = true;
|
||||||
is_ref = true;
|
is_ref = true;
|
||||||
quote_spanned!(arg_type.span() =>
|
quote_spanned!(arg_type.span() =>
|
||||||
mem::take(args[#i]).as_immutable_string().unwrap()
|
mem::take(args[#i]).into_immutable_string().unwrap()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
_ => panic!("internal error: why wasn't this found earlier!?"),
|
_ => panic!("internal error: why wasn't this found earlier!?"),
|
||||||
@ -752,7 +752,7 @@ impl ExportedFn {
|
|||||||
is_string = true;
|
is_string = true;
|
||||||
is_ref = false;
|
is_ref = false;
|
||||||
quote_spanned!(arg_type.span() =>
|
quote_spanned!(arg_type.span() =>
|
||||||
mem::take(args[#i]).as_string().unwrap()
|
mem::take(args[#i]).into_string().unwrap()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -525,7 +525,7 @@ mod generate_tests {
|
|||||||
impl PluginFunction for Token {
|
impl PluginFunction for Token {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
|
||||||
let arg0 = mem::take(args[0usize]).as_immutable_string().unwrap();
|
let arg0 = mem::take(args[0usize]).into_immutable_string().unwrap();
|
||||||
Ok(Dynamic::from(special_print(&arg0)))
|
Ok(Dynamic::from(special_print(&arg0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -934,7 +934,7 @@ mod generate_tests {
|
|||||||
impl PluginFunction for print_out_to_token {
|
impl PluginFunction for print_out_to_token {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
|
||||||
let arg0 = mem::take(args[0usize]).as_immutable_string().unwrap();
|
let arg0 = mem::take(args[0usize]).into_immutable_string().unwrap();
|
||||||
Ok(Dynamic::from(print_out_to(&arg0)))
|
Ok(Dynamic::from(print_out_to(&arg0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -987,7 +987,7 @@ mod generate_tests {
|
|||||||
impl PluginFunction for print_out_to_token {
|
impl PluginFunction for print_out_to_token {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
|
fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult {
|
||||||
let arg0 = mem::take(args[0usize]).as_string().unwrap();
|
let arg0 = mem::take(args[0usize]).into_string().unwrap();
|
||||||
Ok(Dynamic::from(print_out_to(arg0)))
|
Ok(Dynamic::from(print_out_to(arg0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1816,14 +1816,48 @@ impl Dynamic {
|
|||||||
/// Convert the [`Dynamic`] into a [`String`] and return it.
|
/// Convert the [`Dynamic`] into a [`String`] and return it.
|
||||||
/// If there are other references to the same string, a cloned copy is returned.
|
/// If there are other references to the same string, a cloned copy is returned.
|
||||||
/// Returns the name of the actual type if the cast fails.
|
/// Returns the name of the actual type if the cast fails.
|
||||||
#[inline]
|
///
|
||||||
|
/// # Deprecated
|
||||||
|
///
|
||||||
|
/// This method is deprecated. Use [`into_string`][Dynamic::into_string] instead.
|
||||||
|
///
|
||||||
|
/// This method will be removed in the next major version.
|
||||||
|
#[deprecated(
|
||||||
|
since = "1.0.1",
|
||||||
|
note = "this method is deprecated; use `into_string` instead"
|
||||||
|
)]
|
||||||
|
#[inline(always)]
|
||||||
pub fn as_string(self) -> Result<String, &'static str> {
|
pub fn as_string(self) -> Result<String, &'static str> {
|
||||||
self.as_immutable_string().map(ImmutableString::into_owned)
|
self.into_string()
|
||||||
|
}
|
||||||
|
/// Convert the [`Dynamic`] into a [`String`] and return it.
|
||||||
|
/// If there are other references to the same string, a cloned copy is returned.
|
||||||
|
/// Returns the name of the actual type if the cast fails.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn into_string(self) -> Result<String, &'static str> {
|
||||||
|
self.into_immutable_string()
|
||||||
|
.map(ImmutableString::into_owned)
|
||||||
|
}
|
||||||
|
/// Convert the [`Dynamic`] into an [`ImmutableString`] and return it.
|
||||||
|
/// Returns the name of the actual type if the cast fails.
|
||||||
|
///
|
||||||
|
/// # Deprecated
|
||||||
|
///
|
||||||
|
/// This method is deprecated. Use [`into_immutable_string`][Dynamic::into_immutable_string] instead.
|
||||||
|
///
|
||||||
|
/// This method will be removed in the next major version.
|
||||||
|
#[deprecated(
|
||||||
|
since = "1.0.1",
|
||||||
|
note = "this method is deprecated; use `into_string` instead"
|
||||||
|
)]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn as_immutable_string(self) -> Result<ImmutableString, &'static str> {
|
||||||
|
self.into_immutable_string()
|
||||||
}
|
}
|
||||||
/// Convert the [`Dynamic`] into an [`ImmutableString`] and return it.
|
/// Convert the [`Dynamic`] into an [`ImmutableString`] and return it.
|
||||||
/// Returns the name of the actual type if the cast fails.
|
/// Returns the name of the actual type if the cast fails.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_immutable_string(self) -> Result<ImmutableString, &'static str> {
|
pub fn into_immutable_string(self) -> Result<ImmutableString, &'static str> {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
Union::Str(s, _, _) => Ok(s),
|
Union::Str(s, _, _) => Ok(s),
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
|
@ -321,7 +321,7 @@ impl Engine {
|
|||||||
return Ok(match name {
|
return Ok(match name {
|
||||||
KEYWORD_PRINT => {
|
KEYWORD_PRINT => {
|
||||||
if let Some(ref print) = self.print {
|
if let Some(ref print) = self.print {
|
||||||
let text = result.as_immutable_string().map_err(|typ| {
|
let text = result.into_immutable_string().map_err(|typ| {
|
||||||
EvalAltResult::ErrorMismatchOutputType(
|
EvalAltResult::ErrorMismatchOutputType(
|
||||||
self.map_type_name(type_name::<ImmutableString>()).into(),
|
self.map_type_name(type_name::<ImmutableString>()).into(),
|
||||||
typ.into(),
|
typ.into(),
|
||||||
@ -335,7 +335,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
KEYWORD_DEBUG => {
|
KEYWORD_DEBUG => {
|
||||||
if let Some(ref debug) = self.debug {
|
if let Some(ref debug) = self.debug {
|
||||||
let text = result.as_immutable_string().map_err(|typ| {
|
let text = result.into_immutable_string().map_err(|typ| {
|
||||||
EvalAltResult::ErrorMismatchOutputType(
|
EvalAltResult::ErrorMismatchOutputType(
|
||||||
self.map_type_name(type_name::<ImmutableString>()).into(),
|
self.map_type_name(type_name::<ImmutableString>()).into(),
|
||||||
typ.into(),
|
typ.into(),
|
||||||
@ -1100,7 +1100,7 @@ impl Engine {
|
|||||||
|
|
||||||
// Fn - only in function call style
|
// Fn - only in function call style
|
||||||
return arg
|
return arg
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))
|
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))
|
||||||
.and_then(FnPtr::try_from)
|
.and_then(FnPtr::try_from)
|
||||||
.map(Into::<Dynamic>::into)
|
.map(Into::<Dynamic>::into)
|
||||||
@ -1150,7 +1150,7 @@ impl Engine {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
let fn_name = arg
|
let fn_name = arg
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))?;
|
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))?;
|
||||||
|
|
||||||
let (arg, arg_pos) = self.get_arg_value(
|
let (arg, arg_pos) = self.get_arg_value(
|
||||||
@ -1176,7 +1176,7 @@ impl Engine {
|
|||||||
scope, mods, state, lib, this_ptr, level, args_expr, constants, 0,
|
scope, mods, state, lib, this_ptr, level, args_expr, constants, 0,
|
||||||
)?;
|
)?;
|
||||||
let var_name = arg
|
let var_name = arg
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))?;
|
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))?;
|
||||||
return Ok(scope.contains(&var_name).into());
|
return Ok(scope.contains(&var_name).into());
|
||||||
}
|
}
|
||||||
@ -1189,7 +1189,7 @@ impl Engine {
|
|||||||
scope, mods, state, lib, this_ptr, level, args_expr, constants, 0,
|
scope, mods, state, lib, this_ptr, level, args_expr, constants, 0,
|
||||||
)?;
|
)?;
|
||||||
let script = &value
|
let script = &value
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, pos))?;
|
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, pos))?;
|
||||||
let result =
|
let result =
|
||||||
self.eval_script_expr_in_place(scope, mods, state, lib, script, pos, level + 1);
|
self.eval_script_expr_in_place(scope, mods, state, lib, script, pos, level + 1);
|
||||||
|
@ -53,7 +53,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
|
|||||||
} else if TypeId::of::<T>() == TypeId::of::<String>() {
|
} else if TypeId::of::<T>() == TypeId::of::<String>() {
|
||||||
// If T is `String`, data must be `ImmutableString`, so map directly to it
|
// If T is `String`, data must be `ImmutableString`, so map directly to it
|
||||||
let value = mem::take(data)
|
let value = mem::take(data)
|
||||||
.as_string()
|
.into_string()
|
||||||
.expect("never fails because the type was checked");
|
.expect("never fails because the type was checked");
|
||||||
unsafe_try_cast(value).expect("never fails because the type was checked")
|
unsafe_try_cast(value).expect("never fails because the type was checked")
|
||||||
} else {
|
} else {
|
||||||
|
@ -30,7 +30,7 @@ pub fn print_with_func(
|
|||||||
) -> crate::ImmutableString {
|
) -> crate::ImmutableString {
|
||||||
match ctx.call_fn_dynamic_raw(fn_name, true, &mut [value]) {
|
match ctx.call_fn_dynamic_raw(fn_name, true, &mut [value]) {
|
||||||
Ok(result) if result.is::<crate::ImmutableString>() => result
|
Ok(result) if result.is::<crate::ImmutableString>() => result
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.expect("never fails as the result is `ImmutableString`"),
|
.expect("never fails as the result is `ImmutableString`"),
|
||||||
Ok(result) => ctx.engine().map_type_name(result.type_name()).into(),
|
Ok(result) => ctx.engine().map_type_name(result.type_name()).into(),
|
||||||
Err(_) => ctx.engine().map_type_name(value.type_name()).into(),
|
Err(_) => ctx.engine().map_type_name(value.type_name()).into(),
|
||||||
|
@ -547,7 +547,7 @@ impl SerializeMap for DynamicSerializer {
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
{
|
{
|
||||||
let key = std::mem::take(&mut self._key)
|
let key = std::mem::take(&mut self._key)
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.map_err(|typ| {
|
.map_err(|typ| {
|
||||||
EvalAltResult::ErrorMismatchDataType(
|
EvalAltResult::ErrorMismatchDataType(
|
||||||
"string".into(),
|
"string".into(),
|
||||||
@ -577,7 +577,7 @@ impl SerializeMap for DynamicSerializer {
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
{
|
{
|
||||||
let _key: Dynamic = _key.serialize(&mut *self)?;
|
let _key: Dynamic = _key.serialize(&mut *self)?;
|
||||||
let _key = _key.as_immutable_string().map_err(|typ| {
|
let _key = _key.into_immutable_string().map_err(|typ| {
|
||||||
EvalAltResult::ErrorMismatchDataType("string".into(), typ.into(), Position::NONE)
|
EvalAltResult::ErrorMismatchDataType("string".into(), typ.into(), Position::NONE)
|
||||||
})?;
|
})?;
|
||||||
let _value = _value.serialize(&mut *self)?;
|
let _value = _value.serialize(&mut *self)?;
|
||||||
|
@ -343,7 +343,7 @@ fn test_closures_external() -> Result<(), Box<EvalAltResult>> {
|
|||||||
// Closure 'f' captures: the engine, the AST, and the curried function pointer
|
// Closure 'f' captures: the engine, the AST, and the curried function pointer
|
||||||
let f = move |x: INT| fn_ptr.call_dynamic(&context, None, [x.into()]);
|
let f = move |x: INT| fn_ptr.call_dynamic(&context, None, [x.into()]);
|
||||||
|
|
||||||
assert_eq!(f(42)?.as_string(), Ok("hello42".to_string()));
|
assert_eq!(f(42)?.into_string(), Ok("hello42".to_string()));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ fn test_serde_ser_struct() -> Result<(), Box<EvalAltResult>> {
|
|||||||
assert!(obj["b"].as_bool().unwrap());
|
assert!(obj["b"].as_bool().unwrap());
|
||||||
assert_eq!(Ok(42), map["int"].as_int());
|
assert_eq!(Ok(42), map["int"].as_int());
|
||||||
assert_eq!(seq.len(), 3);
|
assert_eq!(seq.len(), 3);
|
||||||
assert_eq!("kitty", seq.remove(1).as_string().unwrap());
|
assert_eq!("kitty", seq.remove(1).into_string().unwrap());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -116,10 +116,10 @@ fn test_serde_ser_unit_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let d = to_dynamic(MyEnum::VariantFoo)?;
|
let d = to_dynamic(MyEnum::VariantFoo)?;
|
||||||
assert_eq!("VariantFoo", d.as_string().unwrap());
|
assert_eq!("VariantFoo", d.into_string().unwrap());
|
||||||
|
|
||||||
let d = to_dynamic(MyEnum::VariantBar)?;
|
let d = to_dynamic(MyEnum::VariantBar)?;
|
||||||
assert_eq!("VariantBar", d.as_string().unwrap());
|
assert_eq!("VariantBar", d.into_string().unwrap());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ fn test_serde_ser_externally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
"VariantUnit",
|
"VariantUnit",
|
||||||
to_dynamic(MyEnum::VariantUnit)?
|
to_dynamic(MyEnum::VariantUnit)?
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str()
|
.as_str()
|
||||||
);
|
);
|
||||||
@ -203,7 +203,7 @@ fn test_serde_ser_internally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
"VariantEmptyStruct",
|
"VariantEmptyStruct",
|
||||||
map.remove("tag")
|
map.remove("tag")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str()
|
.as_str()
|
||||||
);
|
);
|
||||||
@ -214,7 +214,7 @@ fn test_serde_ser_internally_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
"VariantStruct",
|
"VariantStruct",
|
||||||
map.remove("tag")
|
map.remove("tag")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str()
|
.as_str()
|
||||||
);
|
);
|
||||||
@ -247,7 +247,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
"VariantUnit",
|
"VariantUnit",
|
||||||
map.remove("tag")
|
map.remove("tag")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str()
|
.as_str()
|
||||||
);
|
);
|
||||||
@ -260,7 +260,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
"VariantUnitTuple",
|
"VariantUnitTuple",
|
||||||
map.remove("tag")
|
map.remove("tag")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str()
|
.as_str()
|
||||||
);
|
);
|
||||||
@ -274,7 +274,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
"VariantNewtype",
|
"VariantNewtype",
|
||||||
map.remove("tag")
|
map.remove("tag")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str()
|
.as_str()
|
||||||
);
|
);
|
||||||
@ -289,7 +289,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
"VariantTuple",
|
"VariantTuple",
|
||||||
map.remove("tag")
|
map.remove("tag")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str()
|
.as_str()
|
||||||
);
|
);
|
||||||
@ -305,7 +305,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
"VariantEmptyStruct",
|
"VariantEmptyStruct",
|
||||||
map.remove("tag")
|
map.remove("tag")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_immutable_string()
|
.into_immutable_string()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_str()
|
.as_str()
|
||||||
);
|
);
|
||||||
@ -316,7 +316,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box<EvalAltResult>> {
|
|||||||
let mut map = to_dynamic(MyEnum::VariantStruct { a: 123 })?.cast::<Map>();
|
let mut map = to_dynamic(MyEnum::VariantStruct { a: 123 })?.cast::<Map>();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"VariantStruct",
|
"VariantStruct",
|
||||||
map.remove("tag").unwrap().as_string().unwrap()
|
map.remove("tag").unwrap().into_string().unwrap()
|
||||||
);
|
);
|
||||||
let mut map_inner = map.remove("content").unwrap().cast::<Map>();
|
let mut map_inner = map.remove("content").unwrap().cast::<Map>();
|
||||||
assert!(map.is_empty());
|
assert!(map.is_empty());
|
||||||
|
Loading…
Reference in New Issue
Block a user