feat(tvix/castore/path): more conversions

Change-Id: I3ee510b444848316df520dc8ca445d0f3c7d607f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11567
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
edef 2024-05-01 11:49:51 +00:00
parent 1bb023df91
commit 687291cebc

View file

@ -46,6 +46,11 @@ impl Path {
Some(unsafe { Path::from_bytes_unchecked(bytes) })
}
pub fn into_boxed_bytes(self: Box<Path>) -> Box<[u8]> {
// SAFETY: Box<Path> and Box<[u8]> have the same representation.
unsafe { mem::transmute(self) }
}
/// Returns the path without its final component, if there is one.
///
/// Note that the parent of a bare file name is [Path::ROOT].
@ -100,7 +105,7 @@ impl Path {
self.components().last()
}
pub fn as_slice(&self) -> &[u8] {
pub fn as_bytes(&self) -> &[u8] {
&self.inner
}
}
@ -156,6 +161,19 @@ impl Borrow<Path> for PathBuf {
}
}
impl From<Box<Path>> for PathBuf {
fn from(value: Box<Path>) -> Self {
// SAFETY: Box<Path> is always a valid path.
unsafe { PathBuf::from_bytes_unchecked(value.into_boxed_bytes().into_vec()) }
}
}
impl From<&Path> for PathBuf {
fn from(value: &Path) -> Self {
value.to_owned()
}
}
impl FromStr for PathBuf {
type Err = std::io::Error;
@ -178,6 +196,23 @@ impl Display for PathBuf {
}
}
impl PathBuf {
/// Convert a byte vector to a PathBuf, without checking validity.
unsafe fn from_bytes_unchecked(bytes: Vec<u8>) -> PathBuf {
PathBuf { inner: bytes }
}
pub fn into_boxed_path(self) -> Box<Path> {
// SAFETY: Box<[u8]> and Box<Path> have the same representation,
// and PathBuf always contains a valid Path.
unsafe { mem::transmute(self.inner.into_boxed_slice()) }
}
pub fn into_bytes(self) -> Vec<u8> {
self.inner
}
}
#[cfg(test)]
mod test {
use super::{Path, PathBuf};
@ -201,7 +236,7 @@ mod test {
pub fn from_str(#[case] s: &str, #[case] num_components: usize) {
let p: PathBuf = s.parse().expect("must parse");
assert_eq!(s.as_bytes(), p.as_slice(), "inner bytes mismatch");
assert_eq!(s.as_bytes(), p.as_bytes(), "inner bytes mismatch");
assert_eq!(
num_components,
p.components().count(),