path2: Add opt variants for from_vec/from_str
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user