comparison rust/hg-core/src/utils.rs @ 46653:a069639783a0

rhg: Check .hg/requires for absence of required features Some old repository layouts are not supported. Differential Revision: https://phab.mercurial-scm.org/D10076
author Simon Sapin <simon.sapin@octobus.net>
date Thu, 25 Feb 2021 21:25:04 +0100
parents 755c31a1caf9
children e8cd519a0a34
comparison
equal deleted inserted replaced
46652:f64b6953db70 46653:a069639783a0
9 9
10 use crate::errors::{HgError, IoErrorContext}; 10 use crate::errors::{HgError, IoErrorContext};
11 use crate::utils::hg_path::HgPath; 11 use crate::utils::hg_path::HgPath;
12 use im_rc::ordmap::DiffItem; 12 use im_rc::ordmap::DiffItem;
13 use im_rc::ordmap::OrdMap; 13 use im_rc::ordmap::OrdMap;
14 use std::cell::Cell;
15 use std::fmt;
14 use std::{io::Write, ops::Deref}; 16 use std::{io::Write, ops::Deref};
15 17
16 pub mod files; 18 pub mod files;
17 pub mod hg_path; 19 pub mod hg_path;
18 pub mod path_auditor; 20 pub mod path_auditor;
376 right.insert(key, value); 378 right.insert(key, value);
377 } 379 }
378 right 380 right
379 } 381 }
380 } 382 }
383
384 /// Join items of the iterable with the given separator, similar to Python’s
385 /// `separator.join(iter)`.
386 ///
387 /// Formatting the return value consumes the iterator.
388 /// Formatting it again will produce an empty string.
389 pub fn join_display(
390 iter: impl IntoIterator<Item = impl fmt::Display>,
391 separator: impl fmt::Display,
392 ) -> impl fmt::Display {
393 JoinDisplay {
394 iter: Cell::new(Some(iter.into_iter())),
395 separator,
396 }
397 }
398
399 struct JoinDisplay<I, S> {
400 iter: Cell<Option<I>>,
401 separator: S,
402 }
403
404 impl<I, T, S> fmt::Display for JoinDisplay<I, S>
405 where
406 I: Iterator<Item = T>,
407 T: fmt::Display,
408 S: fmt::Display,
409 {
410 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
411 if let Some(mut iter) = self.iter.take() {
412 if let Some(first) = iter.next() {
413 first.fmt(f)?;
414 }
415 for value in iter {
416 self.separator.fmt(f)?;
417 value.fmt(f)?;
418 }
419 }
420 Ok(())
421 }
422 }