comparison rust/hg-core/src/utils.rs @ 49929:5f1cd6839c69

rust-utils: strip redundant prefix from enum This was highlighted by `clippy`, I think this makes the code cleaner.
author Rapha?l Gom?s <rgomes@octobus.net>
date Mon, 09 Jan 2023 19:14:14 +0100
parents ec399ddf6764
children e98fd81bb151
comparison
equal deleted inserted replaced
49928:ccb6cfb0f2c0 49929:5f1cd6839c69
292 let s = b"before $SOME_LONG_NAME_THAT_WE_ASSUME_IS_NOT_AN_ACTUAL_ENV_VAR after"; 292 let s = b"before $SOME_LONG_NAME_THAT_WE_ASSUME_IS_NOT_AN_ACTUAL_ENV_VAR after";
293 assert_eq!(expand_vars(s), &s[..]); 293 assert_eq!(expand_vars(s), &s[..]);
294 } 294 }
295 295
296 pub(crate) enum MergeResult<V> { 296 pub(crate) enum MergeResult<V> {
297 UseLeftValue, 297 LeftValue,
298 UseRightValue, 298 RightValue,
299 UseNewValue(V), 299 NewValue(V),
300 } 300 }
301 301
302 /// Return the union of the two given maps, 302 /// Return the union of the two given maps,
303 /// calling `merge(key, left_value, right_value)` to resolve keys that exist in 303 /// calling `merge(key, left_value, right_value)` to resolve keys that exist in
304 /// both. 304 /// both.
335 } else if left.len() < right.len() / 2 { 335 } else if left.len() < right.len() / 2 {
336 // Same as above but with `left` and `right` swapped 336 // Same as above but with `left` and `right` swapped
337 ordmap_union_with_merge_by_iter(right, left, |key, a, b| { 337 ordmap_union_with_merge_by_iter(right, left, |key, a, b| {
338 // Also swapped in `merge` arguments: 338 // Also swapped in `merge` arguments:
339 match merge(key, b, a) { 339 match merge(key, b, a) {
340 MergeResult::UseNewValue(v) => MergeResult::UseNewValue(v), 340 MergeResult::NewValue(v) => MergeResult::NewValue(v),
341 // … and swap back in `merge` result: 341 // … and swap back in `merge` result:
342 MergeResult::UseLeftValue => MergeResult::UseRightValue, 342 MergeResult::LeftValue => MergeResult::RightValue,
343 MergeResult::UseRightValue => MergeResult::UseLeftValue, 343 MergeResult::RightValue => MergeResult::LeftValue,
344 } 344 }
345 }) 345 })
346 } else { 346 } else {
347 // For maps of similar size, use the algorithm based on `OrdMap::diff` 347 // For maps of similar size, use the algorithm based on `OrdMap::diff`
348 ordmap_union_with_merge_by_diff(left, right, merge) 348 ordmap_union_with_merge_by_diff(left, right, merge)
363 match left.get(&key) { 363 match left.get(&key) {
364 None => { 364 None => {
365 left.insert(key, right_value); 365 left.insert(key, right_value);
366 } 366 }
367 Some(left_value) => match merge(&key, left_value, &right_value) { 367 Some(left_value) => match merge(&key, left_value, &right_value) {
368 MergeResult::UseLeftValue => {} 368 MergeResult::LeftValue => {}
369 MergeResult::UseRightValue => { 369 MergeResult::RightValue => {
370 left.insert(key, right_value); 370 left.insert(key, right_value);
371 } 371 }
372 MergeResult::UseNewValue(new_value) => { 372 MergeResult::UseNewValue(new_value) => {
373 left.insert(key, new_value); 373 left.insert(key, new_value);
374 } 374 }
392 // in order to turn it into the union. 392 // in order to turn it into the union.
393 // 393 //
394 // TODO: if/when https://github.com/bodil/im-rs/pull/168 is accepted, 394 // TODO: if/when https://github.com/bodil/im-rs/pull/168 is accepted,
395 // change these from `Vec<(K, V)>` to `Vec<(&K, Cow<V>)>` 395 // change these from `Vec<(K, V)>` to `Vec<(&K, Cow<V>)>`
396 // with `left_updates` only borrowing from `right` and `right_updates` from 396 // with `left_updates` only borrowing from `right` and `right_updates` from
397 // `left`, and with `Cow::Owned` used for `MergeResult::UseNewValue`. 397 // `left`, and with `Cow::Owned` used for `MergeResult::NewValue`.
398 // 398 //
399 // This would allow moving all `.clone()` calls to after we’ve decided 399 // This would allow moving all `.clone()` calls to after we’ve decided
400 // which of `right_updates` or `left_updates` to use 400 // which of `right_updates` or `left_updates` to use
401 // (value ones becoming `Cow::into_owned`), 401 // (value ones becoming `Cow::into_owned`),
402 // and avoid making clones we don’t end up using. 402 // and avoid making clones we don’t end up using.
413 } 413 }
414 DiffItem::Update { 414 DiffItem::Update {
415 old: (key, left_value), 415 old: (key, left_value),
416 new: (_, right_value), 416 new: (_, right_value),
417 } => match merge(key, left_value, right_value) { 417 } => match merge(key, left_value, right_value) {
418 MergeResult::UseLeftValue => { 418 MergeResult::LeftValue => {
419 right_updates.push((key.clone(), left_value.clone())) 419 right_updates.push((key.clone(), left_value.clone()))
420 } 420 }
421 MergeResult::UseRightValue => { 421 MergeResult::RightValue => {
422 left_updates.push((key.clone(), right_value.clone())) 422 left_updates.push((key.clone(), right_value.clone()))
423 } 423 }
424 MergeResult::UseNewValue(new_value) => { 424 MergeResult::NewValue(new_value) => {
425 left_updates.push((key.clone(), new_value.clone())); 425 left_updates.push((key.clone(), new_value.clone()));
426 right_updates.push((key.clone(), new_value)) 426 right_updates.push((key.clone(), new_value))
427 } 427 }
428 }, 428 },
429 } 429 }