mirror of
https://github.com/Lokathor/bytemuck.git
synced 2024-11-25 00:02:22 +00:00
guard against zero sized types with an error
We could update this to just not allocate in the future but I don't believe that the docs give us assurances in that area.
This commit is contained in:
parent
6fea4bfa58
commit
dad138dba3
@ -42,6 +42,9 @@ pub fn try_cast_box<A: Pod, B: Pod>(input: Box<A>) -> Result<Box<B>, (PodCastErr
|
||||
/// large boxes without fear of a stack overflow.
|
||||
#[inline]
|
||||
pub fn try_zeroed_box<T: Zeroable>() -> Result<Box<T>, ()> {
|
||||
if size_of::<T>() == 0 {
|
||||
return Err(());
|
||||
}
|
||||
let layout = Layout::from_size_align(size_of::<T>(), align_of::<T>()).unwrap();
|
||||
let ptr = unsafe { alloc_zeroed(layout) };
|
||||
if ptr.is_null() {
|
||||
|
Loading…
Reference in New Issue
Block a user