Skip to content
Myr

Limit the debug outputs from ensure to 40 bytes

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

authored by David Tolnay · committed by David Tolnay · parent 9f2e32f · Browse files · 1 file+61 added−2 removed
This commit

State

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

Diff stats

1 file+61 added−2 removed

  • other:+61 added−2 removed

1 file+61 added−2 removed

modified:src/ensure.rs+61 added-2 removed
1use crate::{anyhow, Error};2use core::fmt::Debug;2use core::fmt::{self, Debug, Display, Write};3use core::mem::MaybeUninit;4use core::ptr;5use core::slice;6use core::str;78#[doc(hidden)]9pub trait BothDebug {10 fn __dispatch_ensure(self, msg: &'static str) -> Error;
4 unmodified lines
15 A: Debug,16 B: Debug,17{18 fn __dispatch_ensure(self, msg: &'static str) -> Error {15 anyhow!("{} ({:?} vs {:?})", msg, self.0, self.1)19 render(msg, &self.0, &self.1)20 }21}2223#[doc(hidden)]
6 unmodified lines
30 Error::msg(msg)31 }32}3334struct Buf {35 bytes: [MaybeUninit<u8>; 40],36 written: usize,37}3839impl Buf {40 fn new() -> Self {41 Buf {42 bytes: [MaybeUninit::uninit(); 40],43 written: 0,44 }45 }46}4748impl Write for Buf {49 fn write_str(&mut self, s: &str) -> fmt::Result {50 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)63 }64 }65}6667impl Display for Buf {68 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {69 formatter.write_str(unsafe {70 str::from_utf8_unchecked(slice::from_raw_parts(71 self.bytes.as_ptr().cast::<u8>(),72 self.written,73 ))74 })75 }76}7778fn render(msg: &'static str, lhs: &dyn Debug, rhs: &dyn Debug) -> Error {79 let mut lhs_buf = Buf::new();80 if fmt::write(&mut lhs_buf, format_args!("{:?}", lhs)).is_ok() {81 let mut rhs_buf = Buf::new();82 if fmt::write(&mut rhs_buf, format_args!("{:?}", rhs)).is_ok() {83 return anyhow!("{} ({} vs {})", msg, lhs_buf, rhs_buf);84 }85 }86 Error::msg(msg)87}8889#[doc(hidden)]90#[macro_export]91macro_rules! __parse_ensure {92 (atom () $bail:tt $fuel:tt {($($rhs:tt)+) ($($lhs:tt)+) $op:tt} $(,)?) => {
438 unmodified lines