Use slicing instead of future slice_last

This commit is contained in:
Florian Gilcher 2015-11-04 11:07:08 +01:00
parent e8ea96ee86
commit 7f53da73b7

View File

@ -174,16 +174,15 @@ impl<'dir> File<'dir> {
let components: Vec<Component> = self.path.components().collect(); let components: Vec<Component> = self.path.components().collect();
let mut path_prefix = String::new(); let mut path_prefix = String::new();
if let Some((_, components_init)) = components.split_last() { // This slicing is safe as components always has the RootComponent
for component in components_init.iter() { // as the first element.
for component in components[..(components.len() - 1)].iter() {
path_prefix.push_str(&*component.as_os_str().to_string_lossy()); path_prefix.push_str(&*component.as_os_str().to_string_lossy());
if component != &Component::RootDir { if component != &Component::RootDir {
path_prefix.push_str("/"); path_prefix.push_str("/");
} }
} }
}
path_prefix path_prefix
} }
@ -516,6 +515,8 @@ pub mod fields {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::ext; use super::ext;
use super::File;
use std::path::Path;
#[test] #[test]
fn extension() { fn extension() {
@ -531,4 +532,16 @@ mod test {
fn no_extension() { fn no_extension() {
assert_eq!(None, ext("jarlsberg")) assert_eq!(None, ext("jarlsberg"))
} }
#[test]
fn test_no_prefix() {
let f = File::from_path(Path::new("Cargo.toml"), None).unwrap();
assert_eq!("", f.path_prefix());
let f2 = File::from_path(Path::new("src/main.rs"), None).unwrap();
assert_eq!("src/", f2.path_prefix());
let f3 = File::from_path(Path::new("src"), None).unwrap();
assert_eq!("", f3.path_prefix());
let f4 = File::from_path(Path::new("/"), None).unwrap();
assert_eq!("", f4.path_prefix());
}
} }