]> git.lizzy.rs Git - rust.git/commitdiff
Add a comment on the usage of Layout::new::<RcBox<()>>()
authorSimon Sapin <simon.sapin@exyr.org>
Mon, 5 Aug 2019 15:50:44 +0000 (17:50 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Fri, 16 Aug 2019 15:11:18 +0000 (17:11 +0200)
src/liballoc/rc.rs
src/liballoc/sync.rs

index a9aa822173fdc7b2996d1907834f6803dd579fc9..9d608c886b88e97203bea086ba96ce0a76008797 100644 (file)
@@ -441,6 +441,8 @@ impl<T> Rc<[T]> {
     #[unstable(feature = "new_uninit", issue = "63291")]
     pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
         let data_layout = Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
+        // This relies on `value` being the last field of `RcBox` in memory,
+        // so that the layout of `RcBox<T>` is the same as that of `RcBox<()>` followed by `T`.
         let (layout, offset) = Layout::new::<RcBox<()>>().extend(data_layout).unwrap();
         unsafe {
             let allocated_ptr = Global.alloc(layout)
index 50fd98508930f3226146ea5045a15e2a6899330e..1bd177be0d5430366b68c71b0bc6fea5a39076b3 100644 (file)
@@ -425,6 +425,8 @@ impl<T> Arc<[T]> {
     #[unstable(feature = "new_uninit", issue = "63291")]
     pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
         let data_layout = Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
+        // This relies on `value` being the last field of `RcBox` in memory,
+        // so that the layout of `RcBox<T>` is the same as that of `RcBox<()>` followed by `T`.
         let (layout, offset) = Layout::new::<ArcInner<()>>().extend(data_layout).unwrap();
         unsafe {
             let allocated_ptr = Global.alloc(layout)