diff --git a/src/boxed.rs b/src/boxed.rs index 4ba9859..05b53f6 100644 --- a/src/boxed.rs +++ b/src/boxed.rs @@ -1,12 +1,15 @@ use std::{alloc, mem, ptr}; - /// A typesafe helper that stores the allocated pointer without the data initialized. pub struct BoxAllocation(*mut T); impl BoxAllocation { /// Consumes self and writes the given value into the allocation. pub fn init(mut self, value: T) -> Box { + if mem::size_of::() == 0 { + return Box::new(value); + } + let ptr = mem::replace(&mut self.0, ptr::null_mut()); unsafe { ptr::write(ptr, value); @@ -35,6 +38,10 @@ pub trait BoxHelper { impl BoxHelper for Box { fn alloc() -> BoxAllocation { + if mem::size_of::() == 0 { + return BoxAllocation(ptr::null_mut()); + } + let layout = alloc::Layout::new::(); BoxAllocation(unsafe { alloc::alloc(layout) as *mut T