path2: Add opt variants for from_vec/from_str

This commit is contained in:
Kevin Ballard
2013-09-26 00:54:30 -07:00
parent c16d7a4394
commit 1dfe5088d8
3 changed files with 73 additions and 0 deletions

View File

@@ -287,6 +287,12 @@ impl Path {
GenericPath::from_vec(v)
}
/// Returns a new Path from a byte vector, if possible
#[inline]
pub fn from_vec_opt(v: &[u8]) -> Option<Path> {
GenericPath::from_vec_opt(v)
}
/// Returns a new Path from a string
///
/// # Failure
@@ -297,6 +303,12 @@ impl Path {
GenericPath::from_str(s)
}
/// Returns a new Path from a string, if possible
#[inline]
pub fn from_str_opt(s: &str) -> Option<Path> {
GenericPath::from_str_opt(s)
}
/// Converts the Path into an owned byte vector
pub fn into_vec(self) -> ~[u8] {
self.repr
@@ -475,6 +487,14 @@ mod tests {
assert_eq!(Path::from_vec(b!("foo", 0xff, "/bar")).into_str(), None);
}
#[test]
fn test_opt_paths() {
assert_eq!(Path::from_vec_opt(b!("foo/bar", 0)), None);
t!(v: Path::from_vec_opt(b!("foo/bar")).unwrap(), b!("foo/bar"));
assert_eq!(Path::from_str_opt("foo/bar\0"), None);
t!(s: Path::from_str_opt("foo/bar").unwrap(), "foo/bar");
}
#[test]
fn test_null_byte() {
use path2::null_byte::cond;