Skip to content

Commit edac915

Browse files
committed
primitive_list::as_slice(): move empty list construction out of unsafe {}
1 parent 929cd4e commit edac915

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

capnp/src/primitive_list.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
//! List of primitives.
2323
2424
use core::marker;
25-
use core::ptr::NonNull;
2625

2726
use crate::introspect;
2827
use crate::private::layout::{
@@ -125,16 +124,13 @@ impl<'a, T: PrimitiveElement> Reader<'a, T> {
125124
// This is a List(Void).
126125
self.len() as usize
127126
};
128-
Some(unsafe {
129-
core::slice::from_raw_parts(
130-
if slice_length == 0 {
131-
NonNull::dangling().as_ptr()
132-
} else {
133-
bytes.as_ptr() as *mut T
134-
},
135-
slice_length,
136-
)
137-
})
127+
if slice_length == 0 {
128+
Some(&[])
129+
} else {
130+
Some(unsafe {
131+
core::slice::from_raw_parts(bytes.as_ptr() as *const T, slice_length)
132+
})
133+
}
138134
} else {
139135
None
140136
}
@@ -193,16 +189,13 @@ where
193189
// This is a List(Void).
194190
self.len() as usize
195191
};
196-
Some(unsafe {
197-
core::slice::from_raw_parts_mut(
198-
if slice_length == 0 {
199-
NonNull::dangling().as_ptr()
200-
} else {
201-
bytes.as_mut_ptr() as *mut T
202-
},
203-
slice_length,
204-
)
205-
})
192+
if slice_length == 0 {
193+
Some(&mut [])
194+
} else {
195+
Some(unsafe {
196+
core::slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut T, slice_length)
197+
})
198+
}
206199
} else {
207200
None
208201
}

0 commit comments

Comments
 (0)