From db41ab3cb1bfb6eb1adcafa678bb076cbf4986c3 Mon Sep 17 00:00:00 2001 From: Kogia-sima Date: Fri, 12 Jun 2020 14:54:25 +0900 Subject: [PATCH] Use `capacity == 0` instead of `ptr::is_null` This change allows more fast conversion between String --- sailfish/src/runtime/buffer.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sailfish/src/runtime/buffer.rs b/sailfish/src/runtime/buffer.rs index 9c3d69e..14fe30f 100644 --- a/sailfish/src/runtime/buffer.rs +++ b/sailfish/src/runtime/buffer.rs @@ -4,8 +4,6 @@ use std::mem::ManuallyDrop; use std::ops::{Add, AddAssign}; use std::ptr; -const MEMORY_LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(1, 1) }; - /// Buffer for rendered contents /// /// This struct is quite simular to `String`, but some methods are @@ -125,7 +123,7 @@ impl Buffer { } 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); self.data = alloc(new_layout); } else { @@ -154,9 +152,10 @@ impl fmt::Debug for Buffer { impl Drop for Buffer { fn drop(&mut self) { - if !self.data.is_null() { + if self.capacity != 0 { unsafe { - dealloc(self.data, MEMORY_LAYOUT); + let layout = Layout::from_size_align_unchecked(self.capacity, 1); + dealloc(self.data, layout); } } }