Rename as_string and as_immutable_string.

This commit is contained in:
Stephen Chung
2021-07-24 16:24:59 +08:00
parent b21deaf052
commit 94674679d9
12 changed files with 67 additions and 32 deletions

View File

@@ -1816,14 +1816,48 @@ 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.
#[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> {
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.
/// Returns the name of the actual type if the cast fails.
#[inline]
pub fn as_immutable_string(self) -> Result<ImmutableString, &'static str> {
pub fn into_immutable_string(self) -> Result<ImmutableString, &'static str> {
match self.0 {
Union::Str(s, _, _) => Ok(s),
#[cfg(not(feature = "no_closure"))]

View File

@@ -321,7 +321,7 @@ impl Engine {
return Ok(match name {
KEYWORD_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(
self.map_type_name(type_name::<ImmutableString>()).into(),
typ.into(),
@@ -335,7 +335,7 @@ impl Engine {
}
KEYWORD_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(
self.map_type_name(type_name::<ImmutableString>()).into(),
typ.into(),
@@ -1100,7 +1100,7 @@ impl Engine {
// Fn - only in function call style
return arg
.as_immutable_string()
.into_immutable_string()
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))
.and_then(FnPtr::try_from)
.map(Into::<Dynamic>::into)
@@ -1150,7 +1150,7 @@ impl Engine {
)?;
let fn_name = arg
.as_immutable_string()
.into_immutable_string()
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))?;
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,
)?;
let var_name = arg
.as_immutable_string()
.into_immutable_string()
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, arg_pos))?;
return Ok(scope.contains(&var_name).into());
}
@@ -1189,7 +1189,7 @@ impl Engine {
scope, mods, state, lib, this_ptr, level, args_expr, constants, 0,
)?;
let script = &value
.as_immutable_string()
.into_immutable_string()
.map_err(|typ| self.make_type_mismatch_err::<ImmutableString>(typ, pos))?;
let result =
self.eval_script_expr_in_place(scope, mods, state, lib, script, pos, level + 1);

View File

@@ -53,7 +53,7 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
} else if TypeId::of::<T>() == TypeId::of::<String>() {
// If T is `String`, data must be `ImmutableString`, so map directly to it
let value = mem::take(data)
.as_string()
.into_string()
.expect("never fails because the type was checked");
unsafe_try_cast(value).expect("never fails because the type was checked")
} else {

View File

@@ -30,7 +30,7 @@ pub fn print_with_func(
) -> crate::ImmutableString {
match ctx.call_fn_dynamic_raw(fn_name, true, &mut [value]) {
Ok(result) if result.is::<crate::ImmutableString>() => result
.as_immutable_string()
.into_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(),

View File

@@ -547,7 +547,7 @@ impl SerializeMap for DynamicSerializer {
#[cfg(not(feature = "no_object"))]
{
let key = std::mem::take(&mut self._key)
.as_immutable_string()
.into_immutable_string()
.map_err(|typ| {
EvalAltResult::ErrorMismatchDataType(
"string".into(),
@@ -577,7 +577,7 @@ impl SerializeMap for DynamicSerializer {
#[cfg(not(feature = "no_object"))]
{
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)
})?;
let _value = _value.serialize(&mut *self)?;