Remove the first item from the list whose value is equal to x
(
mc: &'gc Mutation<'gc>,
receiver: Value<'gc>,
args: Vec<Value<'gc>>,
)
| 110 | |
| 111 | // Remove the first item from the list whose value is equal to x |
| 112 | fn remove<'gc>( |
| 113 | mc: &'gc Mutation<'gc>, |
| 114 | receiver: Value<'gc>, |
| 115 | args: Vec<Value<'gc>>, |
| 116 | ) -> Result<Value<'gc>, VmError> { |
| 117 | let list = receiver.as_array()?; |
| 118 | |
| 119 | if args.is_empty() { |
| 120 | return Err(VmError::RuntimeError("remove: expected 1 argument".into())); |
| 121 | } |
| 122 | |
| 123 | let value_to_remove = &args[0]; |
| 124 | |
| 125 | let mut list_mut = list.borrow_mut(mc); |
| 126 | if let Some(index) = list_mut |
| 127 | .data |
| 128 | .iter() |
| 129 | .position(|item| item.equals(value_to_remove)) |
| 130 | { |
| 131 | list_mut.data.remove(index); |
| 132 | Ok(receiver) |
| 133 | } else { |
| 134 | Err(VmError::RuntimeError(format!( |
| 135 | "remove: value {} not found in list", |
| 136 | value_to_remove |
| 137 | ))) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Remove the item at the given position and return it |
| 142 | fn pop<'gc>( |
nothing calls this directly
no test coverage detected