Use `capacity == 0` instead of `ptr::is_null`

This change allows more fast conversion between String
This commit is contained in:
Kogia-sima 2020-06-12 14:54:25 +09:00
parent 1fa5186e34
commit db41ab3cb1
1 changed files with 4 additions and 5 deletions

View File

@ -4,8 +4,6 @@ use std::mem::ManuallyDrop;
use std::ops::{Add, AddAssign}; use std::ops::{Add, AddAssign};
use std::ptr; use std::ptr;
const MEMORY_LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(1, 1) };
/// Buffer for rendered contents /// Buffer for rendered contents
/// ///
/// This struct is quite simular to `String`, but some methods are /// This struct is quite simular to `String`, but some methods are
@ -125,7 +123,7 @@ impl Buffer {
} }
unsafe fn realloc(&mut self, cap: usize) { unsafe fn realloc(&mut self, cap: usize) {
if self.data.is_null() { if self.capacity == 0 {
let new_layout = Layout::from_size_align_unchecked(cap, 1); let new_layout = Layout::from_size_align_unchecked(cap, 1);
self.data = alloc(new_layout); self.data = alloc(new_layout);
} else { } else {
@ -154,9 +152,10 @@ impl fmt::Debug for Buffer {
impl Drop for Buffer { impl Drop for Buffer {
fn drop(&mut self) { fn drop(&mut self) {
if !self.data.is_null() { if self.capacity != 0 {
unsafe { unsafe {
dealloc(self.data, MEMORY_LAYOUT); let layout = Layout::from_size_align_unchecked(self.capacity, 1);
dealloc(self.data, layout);
} }
} }
} }