提交 e47fec56 编写于 作者: C Christopher Durham 提交者: CAD97

Make Layout::new const

上级 c404ce68
......@@ -17,7 +17,7 @@
#[derive(Debug)]
pub struct Excess(pub NonNull<u8>, pub usize);
fn size_align<T>() -> (usize, usize) {
const fn size_align<T>() -> (usize, usize) {
(mem::size_of::<T>(), mem::align_of::<T>())
}
......@@ -121,13 +121,12 @@ pub fn align(&self) -> usize {
/// Constructs a `Layout` suitable for holding a value of type `T`.
#[stable(feature = "alloc_layout", since = "1.28.0")]
#[inline]
pub fn new<T>() -> Self {
pub const fn new<T>() -> Self {
let (size, align) = size_align::<T>();
// Note that the align is guaranteed by rustc to be a power of two and
// the size+align combo is guaranteed to fit in our address space. As a
// result use the unchecked constructor here to avoid inserting code
// that panics if it isn't optimized well enough.
debug_assert!(Layout::from_size_align(size, align).is_ok());
unsafe { Layout::from_size_align_unchecked(size, align) }
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册