Add more debug assertions

This commit is contained in:
Kogia-sima 2020-07-11 02:39:04 +09:00
parent 277a71cd0f
commit 5cef362dbd
2 changed files with 9 additions and 0 deletions

View File

@ -103,6 +103,7 @@ impl Buffer {
if unlikely!(self.len + size > self.capacity) {
self.reserve_internal(size);
}
debug_assert!(self.len + size <= self.capacity);
}
#[inline]
@ -115,6 +116,7 @@ impl Buffer {
/// This consumes the `Buffer`, so we do not need to copy its contents.
#[inline]
pub fn into_string(self) -> String {
debug_assert!(self.len <= self.capacity);
let buf = ManuallyDrop::new(self);
unsafe { String::from_raw_parts(buf.data, buf.len, buf.capacity) }
}
@ -128,6 +130,7 @@ impl Buffer {
std::ptr::copy_nonoverlapping(data.as_ptr(), p, size);
self.len += size;
}
debug_assert!(self.len <= self.capacity);
}
#[inline]
@ -141,9 +144,12 @@ impl Buffer {
fn reserve_internal(&mut self, size: usize) {
unsafe {
let new_capacity = std::cmp::max(self.capacity * 2, self.len + size);
debug_assert!(new_capacity > self.capacity);
self.data = safe_realloc(self.data, self.capacity, new_capacity);
self.capacity = new_capacity;
}
debug_assert!(!self.data.is_null());
debug_assert!(self.len <= self.capacity);
}
}

View File

@ -160,6 +160,7 @@ macro_rules! render_int {
let l = itoap::write_to_ptr(ptr, *self);
b.set_len(b.len() + l);
}
debug_assert!(b.len() <= b.capacity());
Ok(())
}
@ -184,6 +185,7 @@ impl Render for f32 {
let ptr = b.as_mut_ptr().add(b.len());
let l = ryu::raw::format32(*self, ptr);
b.set_len(b.len() + l);
debug_assert!(b.len() <= b.capacity());
}
} else if self.is_nan() {
b.push_str("NaN");
@ -212,6 +214,7 @@ impl Render for f64 {
let ptr = b.as_mut_ptr().add(b.len());
let l = ryu::raw::format64(*self, ptr);
b.set_len(b.len() + l);
debug_assert!(b.len() <= b.capacity());
}
} else if self.is_nan() {
b.push_str("NaN");