Remove public Dynamic::as_str.
This commit is contained in:
parent
ca1ce6b6b8
commit
a251219730
@ -16,6 +16,7 @@ Bug fixes
|
|||||||
Breaking changes
|
Breaking changes
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
* `Dynamic::as_str` is removed because it does not properly handle shared values.
|
||||||
* Zero step in the `range` function now raises an error instead of creating an infinite stream.
|
* Zero step in the `range` function now raises an error instead of creating an infinite stream.
|
||||||
* Error variable captured by `catch` is now an _object map_ containing error fields.
|
* Error variable captured by `catch` is now an _object map_ containing error fields.
|
||||||
* `EvalAltResult::clear_position` is renamed `EvalAltResult::take_position` and returns the position taken.
|
* `EvalAltResult::clear_position` is renamed `EvalAltResult::take_position` and returns the position taken.
|
||||||
|
@ -1526,15 +1526,18 @@ impl Dynamic {
|
|||||||
_ => Err(self.type_name()),
|
_ => Err(self.type_name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Cast the [`Dynamic`] as a [`String`] and return the string slice.
|
/// Cast the [`Dynamic`] as 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.
|
||||||
///
|
///
|
||||||
/// Fails if `self` is _shared_.
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the value is shared.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn as_str(&self) -> Result<&str, &'static str> {
|
pub(crate) fn as_str(&self) -> Result<&str, &'static str> {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
Union::Str(s, _) => Ok(s),
|
Union::Str(s, _) => Ok(s),
|
||||||
Union::FnPtr(f, _) => Ok(f.fn_name()),
|
#[cfg(not(feature = "no_closure"))]
|
||||||
|
Union::Shared(_, _) => panic!("as_str() cannot be called on shared values"),
|
||||||
_ => Err(self.type_name()),
|
_ => Err(self.type_name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -353,17 +353,17 @@ impl Engine {
|
|||||||
// See if the function match print/debug (which requires special processing)
|
// See if the function match print/debug (which requires special processing)
|
||||||
return Ok(match fn_name {
|
return Ok(match fn_name {
|
||||||
KEYWORD_PRINT => {
|
KEYWORD_PRINT => {
|
||||||
let text = result.as_str().map_err(|typ| {
|
let text = result.take_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(),
|
||||||
pos,
|
pos,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
((self.print)(text).into(), false)
|
((self.print)(&text).into(), false)
|
||||||
}
|
}
|
||||||
KEYWORD_DEBUG => {
|
KEYWORD_DEBUG => {
|
||||||
let text = result.as_str().map_err(|typ| {
|
let text = result.take_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(),
|
||||||
@ -371,7 +371,7 @@ impl Engine {
|
|||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
let source = state.source.as_ref().map(|s| s.as_str());
|
let source = state.source.as_ref().map(|s| s.as_str());
|
||||||
((self.debug)(text, source, pos).into(), false)
|
((self.debug)(&text, source, pos).into(), false)
|
||||||
}
|
}
|
||||||
_ => (result, func.is_method()),
|
_ => (result, func.is_method()),
|
||||||
});
|
});
|
||||||
@ -677,7 +677,7 @@ impl Engine {
|
|||||||
crate::engine::KEYWORD_IS_DEF_FN
|
crate::engine::KEYWORD_IS_DEF_FN
|
||||||
if args.len() == 2 && args[0].is::<FnPtr>() && args[1].is::<INT>() =>
|
if args.len() == 2 && args[0].is::<FnPtr>() && args[1].is::<INT>() =>
|
||||||
{
|
{
|
||||||
let fn_name = args[0].as_str().unwrap();
|
let fn_name = mem::take(args[0]).take_immutable_string().unwrap();
|
||||||
let num_params = args[1].as_int().unwrap();
|
let num_params = args[1].as_int().unwrap();
|
||||||
|
|
||||||
return Ok((
|
return Ok((
|
||||||
@ -685,7 +685,7 @@ impl Engine {
|
|||||||
Dynamic::FALSE
|
Dynamic::FALSE
|
||||||
} else {
|
} else {
|
||||||
let hash_script =
|
let hash_script =
|
||||||
calc_script_fn_hash(empty(), fn_name, num_params as usize);
|
calc_script_fn_hash(empty(), &fn_name, num_params as usize);
|
||||||
self.has_override(Some(mods), state, lib, None, hash_script)
|
self.has_override(Some(mods), state, lib, None, hash_script)
|
||||||
.into()
|
.into()
|
||||||
},
|
},
|
||||||
@ -1131,7 +1131,7 @@ impl Engine {
|
|||||||
crate::engine::KEYWORD_IS_DEF_FN if args_expr.len() == 2 => {
|
crate::engine::KEYWORD_IS_DEF_FN if args_expr.len() == 2 => {
|
||||||
let fn_name =
|
let fn_name =
|
||||||
self.eval_expr(scope, mods, state, lib, this_ptr, &args_expr[0], level)?;
|
self.eval_expr(scope, mods, state, lib, this_ptr, &args_expr[0], level)?;
|
||||||
let fn_name = fn_name.as_str().map_err(|err| {
|
let fn_name = fn_name.take_immutable_string().map_err(|err| {
|
||||||
self.make_type_mismatch_err::<ImmutableString>(err, args_expr[0].position())
|
self.make_type_mismatch_err::<ImmutableString>(err, args_expr[0].position())
|
||||||
})?;
|
})?;
|
||||||
let num_params =
|
let num_params =
|
||||||
@ -1143,7 +1143,7 @@ impl Engine {
|
|||||||
return Ok(if num_params < 0 {
|
return Ok(if num_params < 0 {
|
||||||
Dynamic::FALSE
|
Dynamic::FALSE
|
||||||
} else {
|
} else {
|
||||||
let hash_script = calc_script_fn_hash(empty(), fn_name, num_params as usize);
|
let hash_script = calc_script_fn_hash(empty(), &fn_name, num_params as usize);
|
||||||
self.has_override(Some(mods), state, lib, None, hash_script)
|
self.has_override(Some(mods), state, lib, None, hash_script)
|
||||||
.into()
|
.into()
|
||||||
});
|
});
|
||||||
@ -1153,10 +1153,10 @@ impl Engine {
|
|||||||
KEYWORD_IS_DEF_VAR if args_expr.len() == 1 => {
|
KEYWORD_IS_DEF_VAR if args_expr.len() == 1 => {
|
||||||
let var_name =
|
let var_name =
|
||||||
self.eval_expr(scope, mods, state, lib, this_ptr, &args_expr[0], level)?;
|
self.eval_expr(scope, mods, state, lib, this_ptr, &args_expr[0], level)?;
|
||||||
let var_name = var_name.as_str().map_err(|err| {
|
let var_name = var_name.take_immutable_string().map_err(|err| {
|
||||||
self.make_type_mismatch_err::<ImmutableString>(err, args_expr[0].position())
|
self.make_type_mismatch_err::<ImmutableString>(err, args_expr[0].position())
|
||||||
})?;
|
})?;
|
||||||
return Ok(scope.contains(var_name).into());
|
return Ok(scope.contains(&var_name).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle eval()
|
// Handle eval()
|
||||||
@ -1168,7 +1168,7 @@ impl Engine {
|
|||||||
let prev_len = scope.len();
|
let prev_len = scope.len();
|
||||||
let script =
|
let script =
|
||||||
self.eval_expr(scope, mods, state, lib, this_ptr, script_expr, level)?;
|
self.eval_expr(scope, mods, state, lib, this_ptr, script_expr, level)?;
|
||||||
let script = script.as_str().map_err(|typ| {
|
let script = script.take_immutable_string().map_err(|typ| {
|
||||||
self.make_type_mismatch_err::<ImmutableString>(typ, script_pos)
|
self.make_type_mismatch_err::<ImmutableString>(typ, script_pos)
|
||||||
})?;
|
})?;
|
||||||
let result = self.eval_script_expr_in_place(
|
let result = self.eval_script_expr_in_place(
|
||||||
@ -1176,7 +1176,7 @@ impl Engine {
|
|||||||
mods,
|
mods,
|
||||||
state,
|
state,
|
||||||
lib,
|
lib,
|
||||||
script,
|
&script,
|
||||||
script_pos,
|
script_pos,
|
||||||
level + 1,
|
level + 1,
|
||||||
);
|
);
|
||||||
|
@ -190,7 +190,7 @@ impl fmt::Display for EvalAltResult {
|
|||||||
| Self::ErrorTerminated(_, _) => f.write_str(desc)?,
|
| Self::ErrorTerminated(_, _) => f.write_str(desc)?,
|
||||||
|
|
||||||
Self::ErrorRuntime(d, _) if d.is::<ImmutableString>() => {
|
Self::ErrorRuntime(d, _) if d.is::<ImmutableString>() => {
|
||||||
let s = d.as_str().unwrap();
|
let s = &*d.read_lock::<ImmutableString>().unwrap();
|
||||||
write!(f, "{}: {}", desc, if s.is_empty() { desc } else { s })?
|
write!(f, "{}: {}", desc, if s.is_empty() { desc } else { s })?
|
||||||
}
|
}
|
||||||
Self::ErrorRuntime(d, _) if d.is::<()>() => f.write_str(desc)?,
|
Self::ErrorRuntime(d, _) if d.is::<()>() => f.write_str(desc)?,
|
||||||
|
@ -300,7 +300,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_str(), Ok("hello42"));
|
assert_eq!(f(42)?.take_string(), Ok("hello42".to_string()));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user