Skip to content

Commit a44d392

Browse files
committed
Test IntoAsciiStr and fix bug
1 parent c44042a commit a44d392

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/ascii_str.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl<'a> AsciiCast<'a> for str {
359359

360360

361361
/// Error returned by IntoAsciiStr
362-
#[derive(PartialEq)]// allows assert_eq!
362+
#[derive(Clone,Copy)]
363363
pub struct IntoAsciiStrError {
364364
index: usize,
365365
/// If less than 128, it was a byte >= 128
@@ -498,11 +498,11 @@ impl IntoAsciiStr for str {
498498
}
499499
impl IntoMutAsciiStr for str {
500500
fn into_ascii_mut(&mut self) -> Result<&mut AsciiStr,IntoAsciiStrError> {
501-
match self.bytes().enumerate().find(|&(_,b)| b > 127 ) {
502-
Some((index, byte)) => Err(IntoAsciiStrError{
503-
index: index,
504-
not_ascii: (byte - 128) as char,
505-
}),
501+
match self.bytes().position(|b| b > 127 ) {
502+
Some(index) => Err(IntoAsciiStrError{
503+
index: index,
504+
not_ascii: self[index..].chars().next().unwrap(),
505+
}),
506506
None => unsafe{ Ok(self.into_ascii_mut_unchecked()) },
507507
}
508508
}
@@ -516,15 +516,38 @@ impl IntoMutAsciiStr for str {
516516
#[cfg(test)]
517517
mod tests {
518518
use {AsciiCast,Ascii};
519-
use super::{AsciiStr,IntoAsciiStr,IntoAsciiStrError};
519+
use super::{AsciiStr,IntoAsciiStr,IntoMutAsciiStr,IntoAsciiStrError};
520+
521+
pub fn tuplify<T>(r: Result<T,IntoAsciiStrError>) -> Result<T,(usize,char)> {
522+
r.map_err(|e| (e.index, e.not_ascii) )
523+
}
520524

521525
#[test]
522-
fn into_ascii() {
526+
fn generic_into_ascii() {
523527
fn generic<C:IntoAsciiStr+?Sized>(c: &C) -> Result<&AsciiStr,IntoAsciiStrError> {
524528
c.into_ascii()
525529
}
526-
assert_eq!(generic("A"), Ok([Ascii::A].as_ref().into()));
527-
assert_eq!(generic(&b"A"[..]), Ok([Ascii::A].as_ref().into()));
530+
let arr = [Ascii::A];
531+
let ascii_str = arr.as_ref().into();
532+
assert_eq!(tuplify(generic("A")), Ok(ascii_str));
533+
assert_eq!(tuplify(generic(&b"A"[..])), Ok(ascii_str));
534+
//assert_eq!(generic(ascii_str), Ok(ascii_str));
535+
}
536+
#[test]
537+
fn into_ascii() {
538+
let mut s: String = "abčd".to_string();
539+
let mut b: Vec<u8> = s.clone().into();
540+
assert_eq!(tuplify(s.as_str().into_ascii()), Err((2,'č')));
541+
assert_eq!(tuplify(s.as_mut_str().into_ascii_mut()), Err((2,'č')));
542+
let c = (b[2]-128) as char;
543+
assert_eq!(tuplify(b.as_slice().into_ascii()), Err((2,c)));
544+
assert_eq!(tuplify(b.as_mut_slice().into_ascii_mut()), Err((2,c)));
545+
let mut a = [Ascii::a, Ascii::b];
546+
assert_eq!(tuplify((&s[..2]).into_ascii()), Ok((&a[..]).into()));
547+
assert_eq!(tuplify((&b[..2]).into_ascii()), Ok((&a[..]).into()));
548+
let a = Ok((&mut a[..]).into());
549+
assert_eq!(tuplify((&mut s[..2]).into_ascii_mut()), a);
550+
assert_eq!(tuplify((&mut b[..2]).into_ascii_mut()), a);
528551
}
529552

530553
#[test]

0 commit comments

Comments
 (0)