]> git.lizzy.rs Git - rust.git/commitdiff
Parameterize contains_nul for BytesContainer.
authorKevin Butler <haqkrs@gmail.com>
Tue, 8 Apr 2014 01:30:08 +0000 (02:30 +0100)
committerKevin Butler <haqkrs@gmail.com>
Fri, 11 Apr 2014 19:27:01 +0000 (20:27 +0100)
src/libstd/path/mod.rs
src/libstd/path/windows.rs

index a0097469e56a23c7257f5da23126b285c8c8df3d..54a8d8131375c1b6bc98f0b6e0f6c8659aac642d 100644 (file)
@@ -158,7 +158,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
     /// See individual Path impls for additional restrictions.
     #[inline]
     fn new<T: BytesContainer>(path: T) -> Self {
-        assert!(!contains_nul(path.container_as_bytes()));
+        assert!(!contains_nul(&path));
         unsafe { GenericPathUnsafe::new_unchecked(path) }
     }
 
@@ -166,7 +166,7 @@ fn new<T: BytesContainer>(path: T) -> Self {
     /// The resulting Path will always be normalized.
     #[inline]
     fn new_opt<T: BytesContainer>(path: T) -> Option<Self> {
-        if contains_nul(path.container_as_bytes()) {
+        if contains_nul(&path) {
             None
         } else {
             Some(unsafe { GenericPathUnsafe::new_unchecked(path) })
@@ -274,7 +274,7 @@ fn extension_str<'a>(&'a self) -> Option<&'a str> {
     /// Fails the task if the filename contains a NUL.
     #[inline]
     fn set_filename<T: BytesContainer>(&mut self, filename: T) {
-        assert!(!contains_nul(filename.container_as_bytes()));
+        assert!(!contains_nul(&filename));
         unsafe { self.set_filename_unchecked(filename) }
     }
     /// Replaces the extension with the given byte vector or string.
@@ -286,7 +286,7 @@ fn set_filename<T: BytesContainer>(&mut self, filename: T) {
     ///
     /// Fails the task if the extension contains a NUL.
     fn set_extension<T: BytesContainer>(&mut self, extension: T) {
-        assert!(!contains_nul(extension.container_as_bytes()));
+        assert!(!contains_nul(&extension));
         // borrowck causes problems here too
         let val = {
             match self.filename() {
@@ -376,7 +376,7 @@ fn dir_path(&self) -> Self {
     /// Fails the task if the path contains a NUL.
     #[inline]
     fn push<T: BytesContainer>(&mut self, path: T) {
-        assert!(!contains_nul(path.container_as_bytes()));
+        assert!(!contains_nul(&path));
         unsafe { self.push_unchecked(path) }
     }
     /// Pushes multiple paths (as byte vectors or strings) onto `self`.
@@ -589,8 +589,8 @@ fn is_str(_: Option<str::MaybeOwned>) -> bool { true }
 }
 
 #[inline(always)]
-fn contains_nul(v: &[u8]) -> bool {
-    v.iter().any(|&x| x == 0)
+fn contains_nul<T: BytesContainer>(v: &T) -> bool {
+    v.container_as_bytes().iter().any(|&x| x == 0)
 }
 
 #[cfg(test)]
index 57dae68b8427b15865f9311a3f9caea47421f68e..93d8d9e3eb4168d1c0a35e05cc805b4830e04374 100644 (file)
@@ -306,14 +306,13 @@ fn append_path(me: &mut Path, path: &str) {
 impl GenericPath for Path {
     #[inline]
     fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
-        let s = path.container_as_str();
-        match s {
+        match path.container_as_str() {
             None => None,
-            Some(s) => {
-                if contains_nul(s.as_bytes()) {
+            Some(ref s) => {
+                if contains_nul(s) {
                     None
                 } else {
-                    Some(unsafe { GenericPathUnsafe::new_unchecked(s) })
+                    Some(unsafe { GenericPathUnsafe::new_unchecked(*s) })
                 }
             }
         }