diff --git a/CHANGELOG.md b/CHANGELOG.md index 844eefc8..d9442f60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Breaking changes ---------------- * To keep the API consistent, strings are no longer iterable by default. Use the `chars` method to iterator the characters in a string. +* `Dynamic::take_string` and `Dynamic::take_immutable_string` are renamed to `Dynamic::as_string` and `Dynamic::as_immutable_string` respectively. New features ------------ diff --git a/codegen/src/function.rs b/codegen/src/function.rs index 178f3645..8b4c106a 100644 --- a/codegen/src/function.rs +++ b/codegen/src/function.rs @@ -744,7 +744,7 @@ impl ExportedFn { is_string = true; is_ref = true; quote_spanned!(arg_type.span() => - mem::take(args[#i]).take_immutable_string().unwrap() + mem::take(args[#i]).as_immutable_string().unwrap() ) } _ => panic!("internal error: why wasn't this found earlier!?"), @@ -753,7 +753,7 @@ impl ExportedFn { is_string = true; is_ref = false; quote_spanned!(arg_type.span() => - mem::take(args[#i]).take_string().unwrap() + mem::take(args[#i]).as_string().unwrap() ) } _ => { diff --git a/codegen/src/test/function.rs b/codegen/src/test/function.rs index 7fb6dc3a..6d9eaa33 100644 --- a/codegen/src/test/function.rs +++ b/codegen/src/test/function.rs @@ -525,7 +525,7 @@ mod generate_tests { impl PluginFunction for Token { #[inline(always)] fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult { - let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap(); + let arg0 = mem::take(args[0usize]).as_immutable_string().unwrap(); Ok(Dynamic::from(special_print(&arg0))) } diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 6e62c4a6..620b3a1c 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -934,7 +934,7 @@ mod generate_tests { impl PluginFunction for print_out_to_token { #[inline(always)] fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult { - let arg0 = mem::take(args[0usize]).take_immutable_string().unwrap(); + let arg0 = mem::take(args[0usize]).as_immutable_string().unwrap(); Ok(Dynamic::from(print_out_to(&arg0))) } @@ -987,7 +987,7 @@ mod generate_tests { impl PluginFunction for print_out_to_token { #[inline(always)] fn call(&self, context: NativeCallContext, args: &mut [&mut Dynamic]) -> RhaiResult { - let arg0 = mem::take(args[0usize]).take_string().unwrap(); + let arg0 = mem::take(args[0usize]).as_string().unwrap(); Ok(Dynamic::from(print_out_to(arg0))) } diff --git a/src/dynamic.rs b/src/dynamic.rs index 66c01668..a95cab1f 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -1796,15 +1796,45 @@ impl Dynamic { /// 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. + /// + /// # Deprecated + /// + /// This method is deprecated and will be removed in the future. + /// Use [`as_string`][Dynamic::as_string] instead. #[inline(always)] + #[deprecated( + since = "0.20.3", + note = "this method is deprecated and will be removed in the future" + )] pub fn take_string(self) -> Result { - self.take_immutable_string() - .map(ImmutableString::into_owned) + self.as_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 as_string(self) -> Result { + self.as_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 and will be removed in the future. + /// Use [`as_immutable_string`][Dynamic::as_immutable_string] instead. + #[inline(always)] + #[deprecated( + since = "0.20.3", + note = "this method is deprecated and will be removed in the future" + )] + pub fn take_immutable_string(self) -> Result { + self.as_immutable_string() } /// Convert the [`Dynamic`] into an [`ImmutableString`] and return it. /// Returns the name of the actual type if the cast fails. #[inline(always)] - pub fn take_immutable_string(self) -> Result { + pub fn as_immutable_string(self) -> Result { match self.0 { Union::Str(s, _, _) => Ok(s), #[cfg(not(feature = "no_closure"))] diff --git a/src/fn_call.rs b/src/fn_call.rs index 0fad230e..2536c8fd 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -346,7 +346,7 @@ impl Engine { // See if the function match print/debug (which requires special processing) return Ok(match fn_name { KEYWORD_PRINT => { - let text = result.take_immutable_string().map_err(|typ| { + let text = result.as_immutable_string().map_err(|typ| { EvalAltResult::ErrorMismatchOutputType( self.map_type_name(type_name::()).into(), typ.into(), @@ -356,7 +356,7 @@ impl Engine { ((self.print)(&text).into(), false) } KEYWORD_DEBUG => { - let text = result.take_immutable_string().map_err(|typ| { + let text = result.as_immutable_string().map_err(|typ| { EvalAltResult::ErrorMismatchOutputType( self.map_type_name(type_name::()).into(), typ.into(), @@ -1103,7 +1103,7 @@ impl Engine { // Fn - only in function call style return arg - .take_immutable_string() + .as_immutable_string() .map_err(|typ| self.make_type_mismatch_err::(typ, arg_pos)) .and_then(|s| FnPtr::try_from(s)) .map(Into::::into) @@ -1153,7 +1153,7 @@ impl Engine { )?; let fn_name = arg - .take_immutable_string() + .as_immutable_string() .map_err(|err| self.make_type_mismatch_err::(err, arg_pos))?; let (arg, arg_pos) = self.get_arg_value( @@ -1179,7 +1179,7 @@ impl Engine { scope, mods, state, lib, this_ptr, level, args_expr, constants, 0, )?; let var_name = arg - .take_immutable_string() + .as_immutable_string() .map_err(|err| self.make_type_mismatch_err::(err, arg_pos))?; return Ok(scope.contains(&var_name).into()); } @@ -1192,7 +1192,7 @@ impl Engine { scope, mods, state, lib, this_ptr, level, args_expr, constants, 0, )?; let script = &value - .take_immutable_string() + .as_immutable_string() .map_err(|typ| self.make_type_mismatch_err::(typ, pos))?; let result = self.eval_script_expr_in_place(scope, mods, state, lib, script, pos, level + 1); diff --git a/src/fn_register.rs b/src/fn_register.rs index 80cfb2cf..28737808 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -50,7 +50,7 @@ pub fn by_value(data: &mut Dynamic) -> T { } else if TypeId::of::() == TypeId::of::() { // If T is `String`, data must be `ImmutableString`, so map directly to it let value = mem::take(data) - .take_string() + .as_string() .expect("never fails because the type was checked"); unsafe_try_cast(value).expect("never fails because the type was checked") } else { diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index be07f1e0..d577fe2e 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -28,7 +28,7 @@ pub fn print_with_func( ) -> crate::ImmutableString { match ctx.call_fn_dynamic_raw(fn_name, true, &mut [value]) { Ok(result) if result.is::() => result - .take_immutable_string() + .as_immutable_string() .expect("never fails as the result is `ImmutableString`"), Ok(result) => ctx.engine().map_type_name(result.type_name()).into(), Err(_) => ctx.engine().map_type_name(value.type_name()).into(), diff --git a/src/serde/ser.rs b/src/serde/ser.rs index 147f290f..779a5487 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -542,7 +542,7 @@ impl SerializeMap for DynamicSerializer { #[cfg(not(feature = "no_object"))] { let key = std::mem::take(&mut self._key) - .take_immutable_string() + .as_immutable_string() .map_err(|typ| { EvalAltResult::ErrorMismatchDataType( "string".into(), @@ -572,7 +572,7 @@ impl SerializeMap for DynamicSerializer { #[cfg(not(feature = "no_object"))] { let _key: Dynamic = _key.serialize(&mut *self)?; - let _key = _key.take_immutable_string().map_err(|typ| { + let _key = _key.as_immutable_string().map_err(|typ| { EvalAltResult::ErrorMismatchDataType("string".into(), typ.into(), Position::NONE) })?; let _value = _value.serialize(&mut *self)?; diff --git a/tests/closures.rs b/tests/closures.rs index 51a728e9..922a3cd3 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -343,7 +343,7 @@ fn test_closures_external() -> Result<(), Box> { // 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()]); - assert_eq!(f(42)?.take_string(), Ok("hello42".to_string())); + assert_eq!(f(42)?.as_string(), Ok("hello42".to_string())); Ok(()) } diff --git a/tests/serde.rs b/tests/serde.rs index f8557062..a02645f3 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -102,7 +102,7 @@ fn test_serde_ser_struct() -> Result<(), Box> { assert!(obj["b"].as_bool().unwrap()); assert_eq!(Ok(42), map["int"].as_int()); assert_eq!(seq.len(), 3); - assert_eq!("kitty", seq.remove(1).take_string().unwrap()); + assert_eq!("kitty", seq.remove(1).as_string().unwrap()); Ok(()) } @@ -116,10 +116,10 @@ fn test_serde_ser_unit_enum() -> Result<(), Box> { } let d = to_dynamic(MyEnum::VariantFoo)?; - assert_eq!("VariantFoo", d.take_string().unwrap()); + assert_eq!("VariantFoo", d.as_string().unwrap()); let d = to_dynamic(MyEnum::VariantBar)?; - assert_eq!("VariantBar", d.take_string().unwrap()); + assert_eq!("VariantBar", d.as_string().unwrap()); Ok(()) } @@ -145,7 +145,7 @@ fn test_serde_ser_externally_tagged_enum() -> Result<(), Box> { assert_eq!( "VariantUnit", to_dynamic(MyEnum::VariantUnit)? - .take_immutable_string() + .as_immutable_string() .unwrap() .as_str() ); @@ -203,7 +203,7 @@ fn test_serde_ser_internally_tagged_enum() -> Result<(), Box> { "VariantEmptyStruct", map.remove("tag") .unwrap() - .take_immutable_string() + .as_immutable_string() .unwrap() .as_str() ); @@ -214,7 +214,7 @@ fn test_serde_ser_internally_tagged_enum() -> Result<(), Box> { "VariantStruct", map.remove("tag") .unwrap() - .take_immutable_string() + .as_immutable_string() .unwrap() .as_str() ); @@ -247,7 +247,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { "VariantUnit", map.remove("tag") .unwrap() - .take_immutable_string() + .as_immutable_string() .unwrap() .as_str() ); @@ -260,7 +260,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { "VariantUnitTuple", map.remove("tag") .unwrap() - .take_immutable_string() + .as_immutable_string() .unwrap() .as_str() ); @@ -274,7 +274,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { "VariantNewtype", map.remove("tag") .unwrap() - .take_immutable_string() + .as_immutable_string() .unwrap() .as_str() ); @@ -289,7 +289,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { "VariantTuple", map.remove("tag") .unwrap() - .take_immutable_string() + .as_immutable_string() .unwrap() .as_str() ); @@ -305,7 +305,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { "VariantEmptyStruct", map.remove("tag") .unwrap() - .take_immutable_string() + .as_immutable_string() .unwrap() .as_str() ); @@ -316,7 +316,7 @@ fn test_serde_ser_adjacently_tagged_enum() -> Result<(), Box> { let mut map = to_dynamic(MyEnum::VariantStruct { a: 123 })?.cast::(); assert_eq!( "VariantStruct", - map.remove("tag").unwrap().take_string().unwrap() + map.remove("tag").unwrap().as_string().unwrap() ); let mut map_inner = map.remove("content").unwrap().cast::(); assert!(map.is_empty());