diff --git a/src/dynamic.rs b/src/dynamic.rs index 887e73d1..93d2c845 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -1489,15 +1489,6 @@ impl Dynamic { _ => 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 + 'a, &'static str> { - self.read_lock::() - .ok_or_else(|| self.type_name()) - } /// 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. diff --git a/src/engine_api.rs b/src/engine_api.rs index 69a2ff0e..6d12353f 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -33,7 +33,7 @@ impl Engine { /// 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. /// - /// To access a primary argument value (i.e. cloning is cheap), use: `args[n].clone().cast::()` + /// 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::()`. /// Notice that this will _consume_ the argument, replacing it with `()`. diff --git a/src/fn_builtin.rs b/src/fn_builtin.rs index b81cd500..622c6a36 100644 --- a/src/fn_builtin.rs +++ b/src/fn_builtin.rs @@ -328,7 +328,7 @@ pub fn get_builtin_binary_op_fn( "+" => { return Some(|_, args| { let x = args[0].as_char().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok(format!("{}{}", x, y).into()) }) } @@ -336,7 +336,7 @@ pub fn get_builtin_binary_op_fn( #[inline(always)] fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) { let x = args[0].as_char().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let y = &*args[1].read_lock::().unwrap(); let s1 = [x, '\0']; let mut y = y.chars(); 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 { "+" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); let y = args[1].as_char().unwrap(); Ok((x + y).into()) }) } "-" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); let y = args[1].as_char().unwrap(); Ok((x - y).into()) }) @@ -406,7 +406,7 @@ pub fn get_builtin_binary_op_fn( "==" | "!=" | ">" | ">=" | "<" | "<=" => { #[inline(always)] fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) { - let x = &*args[0].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); let y = args[1].as_char().unwrap(); let mut x = x.chars(); 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 { "+" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok((x + y).into()) }) } "-" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok((x - y).into()) }) } "==" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok((x == y).into()) }) } "!=" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok((x != y).into()) }) } ">" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok((x > y).into()) }) } ">=" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok((x >= y).into()) }) } "<" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok((x < y).into()) }) } "<=" => { return Some(|_, args| { - let x = &*args[0].as_locked_immutable_string().unwrap(); - let y = &*args[1].as_locked_immutable_string().unwrap(); + let x = &*args[0].read_lock::().unwrap(); + let y = &*args[1].read_lock::().unwrap(); Ok((x <= y).into()) }) } @@ -1003,7 +1003,7 @@ pub fn get_builtin_op_assignment_fn( "+=" => { return Some(|_, args| { 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::().unwrap().as_str()); let mut x = args[0].write_lock::().unwrap(); Ok((*x = ch.into()).into()) @@ -1199,7 +1199,7 @@ pub fn get_builtin_op_assignment_fn( return Some(|_, args| { let (first, second) = args.split_first_mut().unwrap(); let mut x = first.write_lock::().unwrap(); - let y = &*second[0].as_locked_immutable_string().unwrap(); + let y = &*second[0].read_lock::().unwrap(); Ok((*x += y).into()) }) } @@ -1207,7 +1207,7 @@ pub fn get_builtin_op_assignment_fn( return Some(|_, args| { let (first, second) = args.split_first_mut().unwrap(); let mut x = first.write_lock::().unwrap(); - let y = &*second[0].as_locked_immutable_string().unwrap(); + let y = &*second[0].read_lock::().unwrap(); Ok((*x -= y).into()) }) } diff --git a/src/module/mod.rs b/src/module/mod.rs index 4d066db5..695f8565 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -732,7 +732,7 @@ impl Module { /// The function is assumed to be a _method_, meaning that the first argument should not be consumed. /// All other arguments can be consumed. /// - /// To access a primary argument value (i.e. cloning is cheap), use: `args[n].clone().cast::()` + /// 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::()`. /// Notice that this will _consume_ the argument, replacing it with `()`.