Skip to content

Commit ecc774f

Browse files
committed
auto merge of rust-lang#13395 : Ryman/rust/bytecontainer_impl_container, r=alexcrichton
Also some minor cleanup in Path related to this.
2 parents b7e9306 + 9b9ad9b commit ecc774f

File tree

2 files changed

+32
-46
lines changed

2 files changed

+32
-46
lines changed

Diff for: src/libstd/path/mod.rs

+28-41
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
158158
/// See individual Path impls for additional restrictions.
159159
#[inline]
160160
fn new<T: BytesContainer>(path: T) -> Self {
161-
assert!(!contains_nul(path.container_as_bytes()));
161+
assert!(!contains_nul(&path));
162162
unsafe { GenericPathUnsafe::new_unchecked(path) }
163163
}
164164

165165
/// Creates a new Path from a byte vector or string, if possible.
166166
/// The resulting Path will always be normalized.
167167
#[inline]
168168
fn new_opt<T: BytesContainer>(path: T) -> Option<Self> {
169-
if contains_nul(path.container_as_bytes()) {
169+
if contains_nul(&path) {
170170
None
171171
} else {
172172
Some(unsafe { GenericPathUnsafe::new_unchecked(path) })
@@ -274,7 +274,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
274274
/// Fails the task if the filename contains a NUL.
275275
#[inline]
276276
fn set_filename<T: BytesContainer>(&mut self, filename: T) {
277-
assert!(!contains_nul(filename.container_as_bytes()));
277+
assert!(!contains_nul(&filename));
278278
unsafe { self.set_filename_unchecked(filename) }
279279
}
280280
/// Replaces the extension with the given byte vector or string.
@@ -286,43 +286,30 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
286286
///
287287
/// Fails the task if the extension contains a NUL.
288288
fn set_extension<T: BytesContainer>(&mut self, extension: T) {
289-
assert!(!contains_nul(extension.container_as_bytes()));
290-
// borrowck causes problems here too
291-
let val = {
292-
match self.filename() {
293-
None => None,
294-
Some(name) => {
295-
let dot = '.' as u8;
296-
match name.rposition_elem(&dot) {
297-
None | Some(0) => {
298-
if extension.container_as_bytes().is_empty() {
299-
None
300-
} else {
301-
let mut v;
302-
let extension = extension.container_as_bytes();
303-
v = slice::with_capacity(name.len() + extension.len() + 1);
304-
v.push_all(name);
305-
v.push(dot);
306-
v.push_all(extension);
307-
Some(v)
308-
}
309-
}
310-
Some(idx) => {
311-
if extension.container_as_bytes().is_empty() {
312-
Some(name.slice_to(idx).to_owned())
313-
} else {
314-
let mut v;
315-
let extension = extension.container_as_bytes();
316-
v = slice::with_capacity(idx + extension.len() + 1);
317-
v.push_all(name.slice_to(idx+1));
318-
v.push_all(extension);
319-
Some(v)
320-
}
321-
}
322-
}
289+
assert!(!contains_nul(&extension));
290+
291+
let val = self.filename().and_then(|name| {
292+
let dot = '.' as u8;
293+
let extlen = extension.container_as_bytes().len();
294+
match (name.rposition_elem(&dot), extlen) {
295+
(None, 0) | (Some(0), 0) => None,
296+
(Some(idx), 0) => Some(name.slice_to(idx).to_owned()),
297+
(idx, extlen) => {
298+
let idx = match idx {
299+
None | Some(0) => name.len(),
300+
Some(val) => val
301+
};
302+
303+
let mut v;
304+
v = slice::with_capacity(idx + extlen + 1);
305+
v.push_all(name.slice_to(idx));
306+
v.push(dot);
307+
v.push_all(extension.container_as_bytes());
308+
Some(v)
323309
}
324310
}
325-
};
311+
});
312+
326313
match val {
327314
None => (),
328315
Some(v) => unsafe { self.set_filename_unchecked(v) }
@@ -376,7 +363,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
376363
/// Fails the task if the path contains a NUL.
377364
#[inline]
378365
fn push<T: BytesContainer>(&mut self, path: T) {
379-
assert!(!contains_nul(path.container_as_bytes()));
366+
assert!(!contains_nul(&path));
380367
unsafe { self.push_unchecked(path) }
381368
}
382369
/// Pushes multiple paths (as byte vectors or strings) onto `self`.
@@ -589,8 +576,8 @@ impl<'a> BytesContainer for str::MaybeOwned<'a> {
589576
}
590577

591578
#[inline(always)]
592-
fn contains_nul(v: &[u8]) -> bool {
593-
v.iter().any(|&x| x == 0)
579+
fn contains_nul<T: BytesContainer>(v: &T) -> bool {
580+
v.container_as_bytes().iter().any(|&x| x == 0)
594581
}
595582

596583
#[cfg(test)]

Diff for: src/libstd/path/windows.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,13 @@ impl GenericPathUnsafe for Path {
306306
impl GenericPath for Path {
307307
#[inline]
308308
fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
309-
let s = path.container_as_str();
310-
match s {
309+
match path.container_as_str() {
311310
None => None,
312-
Some(s) => {
313-
if contains_nul(s.as_bytes()) {
311+
Some(ref s) => {
312+
if contains_nul(s) {
314313
None
315314
} else {
316-
Some(unsafe { GenericPathUnsafe::new_unchecked(s) })
315+
Some(unsafe { GenericPathUnsafe::new_unchecked(*s) })
317316
}
318317
}
319318
}

0 commit comments

Comments
 (0)