diff --git a/src/model/array.rs b/src/model/array.rs index 06d1b5885..02607547a 100644 --- a/src/model/array.rs +++ b/src/model/array.rs @@ -93,15 +93,14 @@ impl Array { } /// Remove and return the value at the specified index. - pub fn remove(&mut self, index: i64) -> StrResult<()> { + pub fn remove(&mut self, index: i64) -> StrResult { let len = self.len(); let i = self .locate(index) .filter(|&i| i < self.0.len()) .ok_or_else(|| out_of_bounds(index, len))?; - Arc::make_mut(&mut self.0).remove(i); - Ok(()) + Ok(Arc::make_mut(&mut self.0).remove(i)) } /// Extract a contigous subregion of the array. diff --git a/src/model/dict.rs b/src/model/dict.rs index 6e014d7e9..e3c5454e6 100644 --- a/src/model/dict.rs +++ b/src/model/dict.rs @@ -79,10 +79,10 @@ impl Dict { Arc::make_mut(&mut self.0).insert(key, value); } - /// Remove a mapping by `key`. - pub fn remove(&mut self, key: &str) -> StrResult<()> { + /// Remove a mapping by `key` and return the value. + pub fn remove(&mut self, key: &str) -> StrResult { match Arc::make_mut(&mut self.0).remove(key) { - Some(_) => Ok(()), + Some(value) => Ok(value), None => Err(missing_key(key)), } } diff --git a/src/model/eval.rs b/src/model/eval.rs index 3223ef8f9..6ad27d6ec 100644 --- a/src/model/eval.rs +++ b/src/model/eval.rs @@ -816,7 +816,7 @@ impl Eval for ast::MethodCall { let result = if methods::is_mutating(&method) { let args = self.args().eval(vm)?; let value = self.target().access(vm)?; - methods::call_mut(value, &method, args, span).map(|()| Value::None) + methods::call_mut(value, &method, args, span) } else { let value = self.target().eval(vm)?; let args = self.args().eval(vm)?; diff --git a/src/model/methods.rs b/src/model/methods.rs index fc33bea97..dac36be25 100644 --- a/src/model/methods.rs +++ b/src/model/methods.rs @@ -128,9 +128,10 @@ pub fn call_mut( method: &str, mut args: Args, span: Span, -) -> SourceResult<()> { +) -> SourceResult { let name = value.type_name(); let missing = || Err(missing_method(name, method)).at(span); + let mut output = Value::None; match value { Value::Array(array) => match method { @@ -139,12 +140,14 @@ pub fn call_mut( "insert" => { array.insert(args.expect("index")?, args.expect("value")?).at(span)? } - "remove" => array.remove(args.expect("index")?).at(span)?, + "remove" => output = array.remove(args.expect("index")?).at(span)?, _ => return missing(), }, Value::Dict(dict) => match method { - "remove" => dict.remove(&args.expect::("key")?).at(span)?, + "remove" => { + output = dict.remove(&args.expect::("key")?).at(span)? + } _ => return missing(), }, @@ -152,7 +155,7 @@ pub fn call_mut( } args.finish()?; - Ok(()) + Ok(output) } /// Whether a specific method is mutating.