pass value not reference to fun

This commit is contained in:
Guy Korland 2019-08-11 18:07:15 +03:00 committed by freestrings
parent f5e46882da
commit 8b85ec9d61
2 changed files with 8 additions and 12 deletions

View File

@ -460,7 +460,7 @@ pub fn delete(value: Value, path: &str) -> Result<Value, JsonPathError> {
/// ``` /// ```
pub fn replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError> pub fn replace_with<F>(value: Value, path: &str, fun: &mut F) -> Result<Value, JsonPathError>
where where
F: FnMut(&Value) -> Value, F: FnMut(Value) -> Value,
{ {
let mut selector = SelectorMut::default(); let mut selector = SelectorMut::default();
let value = selector.str_path(path)?.value(value).replace_with(fun)?; let value = selector.str_path(path)?.value(value).replace_with(fun)?;

View File

@ -1026,7 +1026,7 @@ pub struct SelectorMut {
value: Option<Value>, value: Option<Value>,
} }
fn replace_value<F: FnMut(&Value) -> Value>(tokens: Vec<String>, value: &mut Value, fun: &mut F) { fn replace_value<F: FnMut(Value) -> Value>(tokens: Vec<String>, value: &mut Value, fun: &mut F) {
let mut target = value; let mut target = value;
for (i, token) in tokens.iter().enumerate() { for (i, token) in tokens.iter().enumerate() {
@ -1035,13 +1035,9 @@ fn replace_value<F: FnMut(&Value) -> Value>(tokens: Vec<String>, value: &mut Val
let target_opt = match *target_once { let target_opt = match *target_once {
Value::Object(ref mut map) => { Value::Object(ref mut map) => {
if is_last { if is_last {
let v = if let Some(v) = map.get(token) { if let Some(v) = map.remove(token) {
fun(v) map.insert(token.clone(), fun(v));
} else { }
return;
};
map.insert(token.clone(), v);
return; return;
} }
map.get_mut(token) map.get_mut(token)
@ -1049,8 +1045,8 @@ fn replace_value<F: FnMut(&Value) -> Value>(tokens: Vec<String>, value: &mut Val
Value::Array(ref mut vec) => { Value::Array(ref mut vec) => {
if let Ok(x) = token.parse::<usize>() { if let Ok(x) = token.parse::<usize>() {
if is_last { if is_last {
let v = { fun(&vec[x]) }; let v = std::mem::replace(&mut vec[x], Value::Null);
vec[x] = v; vec[x] = fun(v);
return; return;
} }
vec.get_mut(x) vec.get_mut(x)
@ -1174,7 +1170,7 @@ impl SelectorMut {
} }
} }
pub fn replace_with<F: FnMut(&Value) -> Value>( pub fn replace_with<F: FnMut(Value) -> Value>(
&mut self, &mut self,
fun: &mut F, fun: &mut F,
) -> Result<&mut Self, JsonPathError> { ) -> Result<&mut Self, JsonPathError> {