D7864: rust-utils: add Rust implementation of Python's "os.path.splitdrive"

kevincox (Kevin Cox) phabricator at mercurial-scm.org
Wed Jan 15 09:58:06 EST 2020


This revision now requires changes to proceed.
kevincox added inline comments.
kevincox requested changes to this revision.

INLINE COMMENTS

> files.rs:109
> +/// Paths cannot contain both a drive letter and a UNC path.
> +pub fn split_drive(path: impl AsRef<HgPath>) -> (HgPathBuf, HgPathBuf) {
> +    let path = path.as_ref();

I think this can be simplified. See https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=78136ead96596afe6305e7542a881ca4. I had to use &[u8] to avoid having to copy all of the HgPath stuff into the playground, but the code should be easy to integrate.

Notable changes:

- Avoid creating `norm_bytes` vector (and allocation) by creating an is_sep function.
  - Note, we can probably use std::path::is_separator <https://doc.rust-lang.org/std/path/fn.is_separator.html>
- Return references to the input since we guarantee that the output will be parts of the input. The caller can always clone.
- Use slice::split_at to make it obvious that we are returning the entire path just split.
- Use pattern matching rather than unwrapping.
- Use fall-through returns to make it obvious we handle every path.

> files.rs:230
> +        );
> +    }
> +

We should add an example of a UNC and Drive path here to ensure that they don't get split.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7864/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7864

To: Alphare, #hg-reviewers, kevincox
Cc: durin42, kevincox, mercurial-devel


More information about the Mercurial-devel mailing list