diff --git a/RELEASES.md b/RELEASES.md index 95ee14e7..ba52e9cc 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -19,6 +19,7 @@ New features * `OptimizationLevel::Simple` now eagerly evaluates built-in binary operators of primary types (if not overloaded). * Added `is_def_var()` to detect if variable is defined, and `is_def_fn()` to detect if script function is defined. +* `Dynamic::from(&str)` now constructs a `Dynamic` with a copy of the string as value. Version 0.19.0 diff --git a/src/any.rs b/src/any.rs index 10133b43..2ef41da6 100644 --- a/src/any.rs +++ b/src/any.rs @@ -572,6 +572,12 @@ impl Dynamic { .clone() .into(); } + if TypeId::of::() == TypeId::of::<&str>() { + return ::downcast_ref::<&str>(&value) + .unwrap() + .to_string() + .into(); + } if TypeId::of::() == TypeId::of::<()>() { return ().into(); } diff --git a/tests/string.rs b/tests/string.rs index 13f78640..cd4aa6cb 100644 --- a/tests/string.rs +++ b/tests/string.rs @@ -1,4 +1,4 @@ -use rhai::{Engine, EvalAltResult, ImmutableString, RegisterFn, INT}; +use rhai::{Dynamic, Engine, EvalAltResult, ImmutableString, RegisterFn, Scope, INT}; #[test] fn test_string() -> Result<(), Box> { @@ -49,6 +49,21 @@ fn test_string() -> Result<(), Box> { Ok(()) } +#[test] +fn test_string_dynamic() -> Result<(), Box> { + let engine = Engine::new(); + let mut scope = Scope::new(); + scope.push("x", Dynamic::from("foo")); + scope.push("y", String::from("foo")); + scope.push("z", "foo"); + + assert!(engine.eval_with_scope::(&mut scope, r#"x == "foo""#)?); + assert!(engine.eval_with_scope::(&mut scope, r#"y == "foo""#)?); + assert!(engine.eval_with_scope::(&mut scope, r#"z == "foo""#)?); + + Ok(()) +} + #[cfg(not(feature = "no_object"))] #[test] fn test_string_substring() -> Result<(), Box> {