Mercurial > hg
comparison rust/hg-core/src/utils/files.rs @ 46596:d2e61f00ee9d
rust: Introduce a get_bytes_from_os_str utility function
It does the same as get_bytes_from_path but takes an `OsStr`
instead of a `Path`. The implementation is the same so using either
ends up correct but the function name suggests it’s not.
Differential Revision: https://phab.mercurial-scm.org/D10007
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Wed, 17 Feb 2021 11:21:34 +0100 |
parents | 0d734c0ae1cf |
children | 91ab5190a3de |
comparison
equal
deleted
inserted
replaced
46595:98a455a62699 | 46596:d2e61f00ee9d |
---|---|
15 replace_slice, | 15 replace_slice, |
16 }; | 16 }; |
17 use lazy_static::lazy_static; | 17 use lazy_static::lazy_static; |
18 use same_file::is_same_file; | 18 use same_file::is_same_file; |
19 use std::borrow::{Cow, ToOwned}; | 19 use std::borrow::{Cow, ToOwned}; |
20 use std::ffi::OsStr; | |
20 use std::fs::Metadata; | 21 use std::fs::Metadata; |
21 use std::iter::FusedIterator; | 22 use std::iter::FusedIterator; |
22 use std::ops::Deref; | 23 use std::ops::Deref; |
23 use std::path::{Path, PathBuf}; | 24 use std::path::{Path, PathBuf}; |
24 | 25 |
38 | 39 |
39 // TODO: need to convert from WTF8 to MBCS bytes on Windows. | 40 // TODO: need to convert from WTF8 to MBCS bytes on Windows. |
40 // that's why Vec<u8> is returned. | 41 // that's why Vec<u8> is returned. |
41 #[cfg(unix)] | 42 #[cfg(unix)] |
42 pub fn get_bytes_from_path(path: impl AsRef<Path>) -> Vec<u8> { | 43 pub fn get_bytes_from_path(path: impl AsRef<Path>) -> Vec<u8> { |
44 get_bytes_from_os_str(path.as_ref()) | |
45 } | |
46 | |
47 #[cfg(unix)] | |
48 pub fn get_bytes_from_os_str(str: impl AsRef<OsStr>) -> Vec<u8> { | |
43 use std::os::unix::ffi::OsStrExt; | 49 use std::os::unix::ffi::OsStrExt; |
44 path.as_ref().as_os_str().as_bytes().to_vec() | 50 str.as_ref().as_bytes().to_vec() |
45 } | 51 } |
46 | 52 |
47 /// An iterator over repository path yielding itself and its ancestors. | 53 /// An iterator over repository path yielding itself and its ancestors. |
48 #[derive(Copy, Clone, Debug)] | 54 #[derive(Copy, Clone, Debug)] |
49 pub struct Ancestors<'a> { | 55 pub struct Ancestors<'a> { |