cpython-ext: add more utilities for PyPath

Summary:
Make the type easier to use. Namely, the treestate bindings want PyPath <->
bytes since treestate internally uses bytes.

Reviewed By: xavierd

Differential Revision: D19635357

fbshipit-source-id: 37d1889b5da1d7f3869bb7820de0219b87b71a8b
This commit is contained in:
Jun Wu 2020-01-30 08:25:19 -08:00 committed by Facebook Github Bot
parent cd04a56178
commit c5dd6829c7

View File

@ -43,6 +43,18 @@ impl PyPath {
pub fn to_repo_path<'a>(&'a self) -> Result<&'a RepoPath> {
Ok(RepoPath::from_str(&self.0)?)
}
pub fn into_utf8_bytes(self) -> Vec<u8> {
self.0.into()
}
pub fn as_utf8_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
pub fn from_utf8_bytes(utf8_bytes: Vec<u8>) -> Result<Self> {
Ok(Self(String::from_utf8(utf8_bytes)?))
}
}
impl ToPyObject for PyPath {
@ -138,3 +150,9 @@ impl fmt::Display for PyPath {
fmt::Display::fmt(&*self.0, formatter)
}
}
impl From<PyPath> for String {
fn from(path: PyPath) -> String {
path.0
}
}