Skip to content
Myr

Merge pull request #196 from dtolnay/ensure

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Exclude messages containing whitespace

authored by David Tolnay · committed by GitHub · parents 4ff48fe, c866f2b · Browse files · 2 files+29 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−12 removed

  • tests:+13 added−0 removed
  • other:+16 added−12 removed

2 files+29 added−12 removed

modified:src/ensure.rs+16 added-12 removed
45 unmodified lines
46}4748impl Write for Buf {49 fn write_str(&mut self, s: &str) -> fmt::Result {50 if s.bytes().any(|b| b == b' ' || b == b'\n') {51 return Err(fmt::Error);52 }5354 let remaining = self.bytes.len() - self.written;51 if s.len() <= remaining {52 unsafe {53 ptr::copy_nonoverlapping(54 s.as_ptr(),55 self.bytes.as_mut_ptr().add(self.written).cast::<u8>(),56 s.len(),57 );58 }59 self.written += s.len();60 Ok(())61 } else {62 Err(fmt::Error)55 if s.len() > remaining {56 return Err(fmt::Error);57 }5859 unsafe {60 ptr::copy_nonoverlapping(61 s.as_ptr(),62 self.bytes.as_mut_ptr().add(self.written).cast::<u8>(),63 s.len(),64 );65 }66 self.written += s.len();67 Ok(())68 }69}7071impl Display for Buf {
463 unmodified lines
modified:tests/test_ensure.rs+13 added
365 unmodified lines
366 );367}368369#[test]370fn test_whitespace() {371 #[derive(Debug)]372 pub struct Point {373 pub x: i32,374 pub y: i32,375 }376377 let point = Point { x: 0, y: 0 };378 let test = || Ok(ensure!("" == format!("{:#?}", point)));379 assert_err(test, "Condition failed: `\"\" == format!(\"{:#?}\", point)`");380}381382#[test]383fn test_too_long() {384 let test = || Ok(ensure!("" == "x".repeat(10)));385 assert_err(386 test,
6 unmodified lines