Skip to content

Commit

Permalink
primitive_list::as_slice(): move empty list construction out of unsaf…
Browse files Browse the repository at this point in the history
…e {}
  • Loading branch information
dwrensha committed May 12, 2024
1 parent 929cd4e commit edac915
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions capnp/src/primitive_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
//! List of primitives.
use core::marker;
use core::ptr::NonNull;

use crate::introspect;
use crate::private::layout::{
Expand Down Expand Up @@ -125,16 +124,13 @@ impl<'a, T: PrimitiveElement> Reader<'a, T> {
// This is a List(Void).
self.len() as usize
};
Some(unsafe {
core::slice::from_raw_parts(
if slice_length == 0 {
NonNull::dangling().as_ptr()
} else {
bytes.as_ptr() as *mut T
},
slice_length,
)
})
if slice_length == 0 {
Some(&[])
} else {
Some(unsafe {
core::slice::from_raw_parts(bytes.as_ptr() as *const T, slice_length)
})
}
} else {
None
}
Expand Down Expand Up @@ -193,16 +189,13 @@ where
// This is a List(Void).
self.len() as usize
};
Some(unsafe {
core::slice::from_raw_parts_mut(
if slice_length == 0 {
NonNull::dangling().as_ptr()
} else {
bytes.as_mut_ptr() as *mut T
},
slice_length,
)
})
if slice_length == 0 {
Some(&mut [])
} else {
Some(unsafe {
core::slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut T, slice_length)
})
}
} else {
None
}
Expand Down

0 comments on commit edac915

Please sign in to comment.