D6393: rust-dirstate: add "dirs" Rust implementation

Yuya Nishihara yuya at tcha.org
Sat Jun 29 23:15:36 EDT 2019


> +impl DirsMultiset {
> +    /// Initializes the multiset from a dirstate or a manifest.
> +    ///
> +    /// If `skip_state` is provided, skips dirstate entries with equal state.
> +    pub fn new(iterable: DirsIterable, skip_state: Option<i8>) -> Self {
> +        let mut multiset = DirsMultiset {
> +            inner: HashMap::new(),
> +        };
> +
> +        match iterable {
> +            DirsIterable::Dirstate(vec) => {
> +                for (ref filename, DirstateEntry { state, .. }) in vec {
> +                    // This `if` is optimized out of the loop
> +                    if let Some(skip) = skip_state {
> +                        if skip != state {
> +                            multiset.add_path(filename);
> +                        }
> +                    } else {
> +                        multiset.add_path(filename);
> +                    }
> +                }
> +            }
> +            DirsIterable::Manifest(vec) => {
> +                for ref filename in vec {
> +                    multiset.add_path(filename);
> +                }
> +            }
> +        }
> +
> +        multiset
> +    }

Could be `from_dirstate(vec, skip_state)` and `from_vec(vec)` since the
`skip_state` argument only applies to the `Dirstate` variant.

> +    /// Returns the slice up to the next directory name from right to left,
> +    /// without trailing slash
> +    fn find_dir(path: &[u8]) -> &[u8] {
> +        let mut path = path;
> +        loop {
> +            if let Some(new_pos) = path.len().checked_sub(1) {
> +                if path[new_pos] == b'/' {
> +                    break &path[..new_pos];
> +                }
> +                path = &path[..new_pos];
> +            } else {
> +                break &[];
> +            }
> +        }
> +    }


Maybe use Iterator::rposition()?

```
let p = path.iter().rposition(|&c| c == b'/').unwrap_or(0);
&path[..p]
```

Anyway, we'll probably want an iterator which yields parent directories.


More information about the Mercurial-devel mailing list