Skip to content

Commit 935cf96

Browse files
committed
Remove ascii_part()
Name didn't fit in with the other methods, and can be replaced with `s.into_ascii().map_err(|err| s[..err.index()].into_ascii().unwrap() )`.
1 parent f9d8fe9 commit 935cf96

File tree

1 file changed

+4
-41
lines changed

1 file changed

+4
-41
lines changed

src/ascii_str.rs

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,13 @@ impl Error for IntoAsciiStrError {
376376
pub trait IntoAsciiStr : AsciiExt {
377377
/// Convert to `AsciiStr`, not doing any range asserts.
378378
unsafe fn into_ascii_unchecked(&self) -> &AsciiStr;
379-
/// Convert the portion from start of self that is valid ASCII to `AsciiStr`.
380-
fn ascii_part(&self) -> &AsciiStr;
381379
/// Convert to `AsciiStr`.
382380
fn into_ascii(&self) -> Result<&AsciiStr,IntoAsciiStrError>;
383381
}
384382
/// Trait for converting various slices into `AsciiStr`.
385383
pub trait IntoMutAsciiStr : AsciiExt {
386384
/// Convert to `AsciiStr`, not doing any range asserts.
387385
unsafe fn into_ascii_mut_unchecked(&mut self) -> &mut AsciiStr;
388-
/// Convert the portion from start of self that is valid ASCII to `AsciiStr`.
389-
fn ascii_part_mut(&mut self) -> &mut AsciiStr;
390386
/// Convert to `AsciiStr`.
391387
fn into_ascii_mut(&mut self) -> Result<&mut AsciiStr,IntoAsciiStrError>;
392388
}
@@ -396,9 +392,6 @@ impl IntoAsciiStr for AsciiStr {
396392
fn into_ascii(&self) -> Result<&AsciiStr,IntoAsciiStrError> {
397393
Ok(self)
398394
}
399-
fn ascii_part(&self) -> &AsciiStr {
400-
self
401-
}
402395
unsafe fn into_ascii_unchecked(&self) -> &AsciiStr {
403396
self
404397
}
@@ -408,9 +401,6 @@ impl IntoMutAsciiStr for AsciiStr {
408401
fn into_ascii_mut(&mut self) -> Result<&mut AsciiStr,IntoAsciiStrError> {
409402
Ok(self)
410403
}
411-
fn ascii_part_mut(&mut self) -> &mut AsciiStr {
412-
self
413-
}
414404
unsafe fn into_ascii_mut_unchecked(&mut self) -> &mut AsciiStr {
415405
self
416406
}
@@ -426,12 +416,6 @@ impl IntoAsciiStr for [u8] {
426416
None => unsafe{ Ok(self.into_ascii_unchecked()) },
427417
}
428418
}
429-
fn ascii_part(&self) -> &AsciiStr {
430-
let ascii = self.iter().position(|b| *b > 127 )
431-
.map(|l| &self[..l] )
432-
.unwrap_or(self);
433-
unsafe{ ascii.into_ascii_unchecked() }
434-
}
435419
unsafe fn into_ascii_unchecked(&self) -> &AsciiStr {
436420
AsciiStr::from_bytes_unchecked(self)
437421
}
@@ -446,32 +430,17 @@ impl IntoMutAsciiStr for [u8] {
446430
None => unsafe{ Ok(self.into_ascii_mut_unchecked()) },
447431
}
448432
}
449-
fn ascii_part_mut(&mut self) -> &mut AsciiStr {
450-
let ascii = match self.iter_mut().position(|b| *b > 127 ) {
451-
Some(l) => &mut self[..l],
452-
None => self,
453-
};
454-
unsafe{ ascii.into_ascii_mut_unchecked() }
455-
}
456433
unsafe fn into_ascii_mut_unchecked(&mut self) -> &mut AsciiStr {
457434
mem::transmute(self)
458435
}
459436
}
460437

461438
impl IntoAsciiStr for str {
462439
fn into_ascii(&self) -> Result<&AsciiStr,IntoAsciiStrError> {
463-
let s = self.ascii_part();
464-
if s.len() == self.len() {
465-
Ok(s)
466-
} else {
467-
Err(IntoAsciiStrError{
468-
index: s.len(),
469-
not_ascii: self[s.len()..].chars().next().unwrap(),
470-
})
471-
}
472-
}
473-
fn ascii_part(&self) -> &AsciiStr {
474-
self.as_bytes().ascii_part()
440+
self.as_bytes().into_ascii().map_err(|err| IntoAsciiStrError{
441+
not_ascii: self[err.index..].chars().next().unwrap(),
442+
index: err.index,
443+
})
475444
}
476445
unsafe fn into_ascii_unchecked(&self) -> &AsciiStr {
477446
mem::transmute(self)
@@ -487,12 +456,6 @@ impl IntoMutAsciiStr for str {
487456
None => unsafe{ Ok(self.into_ascii_mut_unchecked()) },
488457
}
489458
}
490-
fn ascii_part_mut(&mut self) -> &mut AsciiStr {
491-
unsafe{ match self.bytes().position(|b| b > 127 ) {
492-
Some(index) => self.slice_mut_unchecked(0, index).into_ascii_mut_unchecked(),
493-
None => self.into_ascii_mut_unchecked(),
494-
}}
495-
}
496459
unsafe fn into_ascii_mut_unchecked(&mut self) -> &mut AsciiStr {
497460
mem::transmute(self)
498461
}

0 commit comments

Comments
 (0)