From 69b79e3a73f9a1b820cf6d5ebc9968d8b08d4e68 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Jul 2020 22:53:12 +0200 Subject: Replace ad hocery with science --- crates/vfs/src/vfs_path.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'crates/vfs/src/vfs_path.rs') diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index dc3031ada..04a42264e 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -48,6 +48,37 @@ impl VfsPath { (VfsPathRepr::VirtualPath(_), _) => false, } } + + // Don't make this `pub` + pub(crate) fn encode(&self, buf: &mut Vec) { + let tag = match &self.0 { + VfsPathRepr::PathBuf(_) => 0, + VfsPathRepr::VirtualPath(_) => 1, + }; + buf.push(tag); + match &self.0 { + VfsPathRepr::PathBuf(it) => { + let path: &std::ffi::OsStr = it.as_os_str(); + #[cfg(windows)] + { + use std::os::windows::ffi::OsStrExt; + for wchar in path.encode_wide() { + buf.extend(wchar.to_le_bytes().iter().copied()); + } + } + #[cfg(unix)] + { + use std::os::unix::ffi::OsStrExt; + buf.extend(path.as_bytes()); + } + #[cfg(not(any(windows, unix)))] + { + buf.extend(path.to_string_lossy().as_bytes()); + } + } + VfsPathRepr::VirtualPath(VirtualPath(s)) => buf.extend(s.as_bytes()), + } + } } #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] -- cgit v1.2.3