Remove Dynamic::as_locked_immutable_string.

This commit is contained in:
Stephen Chung 2021-03-03 18:13:41 +08:00
parent 58df3ca141
commit 41c39a2060
4 changed files with 26 additions and 35 deletions

View File

@ -1489,15 +1489,6 @@ impl Dynamic {
_ => Err(self.type_name()), _ => Err(self.type_name()),
} }
} }
/// Cast the [`Dynamic`] as an [`ImmutableString`] and return it.
/// Returns the name of the actual type if the cast fails.
#[inline]
pub(crate) fn as_locked_immutable_string<'a>(
&'a self,
) -> Result<impl Deref<Target = ImmutableString> + 'a, &'static str> {
self.read_lock::<ImmutableString>()
.ok_or_else(|| self.type_name())
}
/// 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.

View File

@ -33,7 +33,7 @@ impl Engine {
/// Arguments are simply passed in as a mutable array of [`&mut Dynamic`][Dynamic], /// Arguments are simply passed in as a mutable array of [`&mut Dynamic`][Dynamic],
/// The arguments are guaranteed to be of the correct types matching the [`TypeId`][std::any::TypeId]'s. /// The arguments are guaranteed to be of the correct types matching the [`TypeId`][std::any::TypeId]'s.
/// ///
/// To access a primary argument value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()` /// To access a primary argument value (i.e. cloning is cheap), use: `args[n].as_xxx().unwrap()`
/// ///
/// To access an argument value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`. /// To access an argument value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// Notice that this will _consume_ the argument, replacing it with `()`. /// Notice that this will _consume_ the argument, replacing it with `()`.

View File

@ -328,7 +328,7 @@ pub fn get_builtin_binary_op_fn(
"+" => { "+" => {
return Some(|_, args| { return Some(|_, args| {
let x = args[0].as_char().unwrap(); let x = args[0].as_char().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok(format!("{}{}", x, y).into()) Ok(format!("{}{}", x, y).into())
}) })
} }
@ -336,7 +336,7 @@ pub fn get_builtin_binary_op_fn(
#[inline(always)] #[inline(always)]
fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) { fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) {
let x = args[0].as_char().unwrap(); let x = args[0].as_char().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
let s1 = [x, '\0']; let s1 = [x, '\0'];
let mut y = y.chars(); let mut y = y.chars();
let s2 = [y.next().unwrap_or('\0'), y.next().unwrap_or('\0')]; let s2 = [y.next().unwrap_or('\0'), y.next().unwrap_or('\0')];
@ -391,14 +391,14 @@ pub fn get_builtin_binary_op_fn(
match op { match op {
"+" => { "+" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = args[1].as_char().unwrap(); let y = args[1].as_char().unwrap();
Ok((x + y).into()) Ok((x + y).into())
}) })
} }
"-" => { "-" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = args[1].as_char().unwrap(); let y = args[1].as_char().unwrap();
Ok((x - y).into()) Ok((x - y).into())
}) })
@ -406,7 +406,7 @@ pub fn get_builtin_binary_op_fn(
"==" | "!=" | ">" | ">=" | "<" | "<=" => { "==" | "!=" | ">" | ">=" | "<" | "<=" => {
#[inline(always)] #[inline(always)]
fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) { fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = args[1].as_char().unwrap(); let y = args[1].as_char().unwrap();
let mut x = x.chars(); let mut x = x.chars();
let s1 = [x.next().unwrap_or('\0'), x.next().unwrap_or('\0')]; let s1 = [x.next().unwrap_or('\0'), x.next().unwrap_or('\0')];
@ -691,57 +691,57 @@ pub fn get_builtin_binary_op_fn(
match op { match op {
"+" => { "+" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok((x + y).into()) Ok((x + y).into())
}) })
} }
"-" => { "-" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok((x - y).into()) Ok((x - y).into())
}) })
} }
"==" => { "==" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok((x == y).into()) Ok((x == y).into())
}) })
} }
"!=" => { "!=" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok((x != y).into()) Ok((x != y).into())
}) })
} }
">" => { ">" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok((x > y).into()) Ok((x > y).into())
}) })
} }
">=" => { ">=" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok((x >= y).into()) Ok((x >= y).into())
}) })
} }
"<" => { "<" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok((x < y).into()) Ok((x < y).into())
}) })
} }
"<=" => { "<=" => {
return Some(|_, args| { return Some(|_, args| {
let x = &*args[0].as_locked_immutable_string().unwrap(); let x = &*args[0].read_lock::<ImmutableString>().unwrap();
let y = &*args[1].as_locked_immutable_string().unwrap(); let y = &*args[1].read_lock::<ImmutableString>().unwrap();
Ok((x <= y).into()) Ok((x <= y).into())
}) })
} }
@ -1003,7 +1003,7 @@ pub fn get_builtin_op_assignment_fn(
"+=" => { "+=" => {
return Some(|_, args| { return Some(|_, args| {
let mut ch = args[0].as_char().unwrap().to_string(); let mut ch = args[0].as_char().unwrap().to_string();
ch.push_str(args[1].as_locked_immutable_string().unwrap().as_str()); ch.push_str(args[1].read_lock::<ImmutableString>().unwrap().as_str());
let mut x = args[0].write_lock::<Dynamic>().unwrap(); let mut x = args[0].write_lock::<Dynamic>().unwrap();
Ok((*x = ch.into()).into()) Ok((*x = ch.into()).into())
@ -1199,7 +1199,7 @@ pub fn get_builtin_op_assignment_fn(
return Some(|_, args| { return Some(|_, args| {
let (first, second) = args.split_first_mut().unwrap(); let (first, second) = args.split_first_mut().unwrap();
let mut x = first.write_lock::<ImmutableString>().unwrap(); let mut x = first.write_lock::<ImmutableString>().unwrap();
let y = &*second[0].as_locked_immutable_string().unwrap(); let y = &*second[0].read_lock::<ImmutableString>().unwrap();
Ok((*x += y).into()) Ok((*x += y).into())
}) })
} }
@ -1207,7 +1207,7 @@ pub fn get_builtin_op_assignment_fn(
return Some(|_, args| { return Some(|_, args| {
let (first, second) = args.split_first_mut().unwrap(); let (first, second) = args.split_first_mut().unwrap();
let mut x = first.write_lock::<ImmutableString>().unwrap(); let mut x = first.write_lock::<ImmutableString>().unwrap();
let y = &*second[0].as_locked_immutable_string().unwrap(); let y = &*second[0].read_lock::<ImmutableString>().unwrap();
Ok((*x -= y).into()) Ok((*x -= y).into())
}) })
} }

View File

@ -732,7 +732,7 @@ impl Module {
/// The function is assumed to be a _method_, meaning that the first argument should not be consumed. /// The function is assumed to be a _method_, meaning that the first argument should not be consumed.
/// All other arguments can be consumed. /// All other arguments can be consumed.
/// ///
/// To access a primary argument value (i.e. cloning is cheap), use: `args[n].clone().cast::<T>()` /// To access a primary argument value (i.e. cloning is cheap), use: `args[n].as_xxx().unwrap()`
/// ///
/// To access an argument value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`. /// To access an argument value and avoid cloning, use `std::mem::take(args[n]).cast::<T>()`.
/// Notice that this will _consume_ the argument, replacing it with `()`. /// Notice that this will _consume_ the argument, replacing it with `()`.