How to cache a vector's capacity?

Yeah, I forgot that there is actually type elision as well (in addition to lifetime elision). So the macro approach is totally flawed. It felt scary anyway :wink:

I don't understand what that means? If I pass some value to forget, isn't this the same as { let x = ManuallyDrop::new(…) }? Can you explain where is the difference?

Are you referring to Unique<T>?


Does this work?

 impl<T> VecExt<T> for Vec<T> {
     fn clear_transmute<U>(mut self) -> Vec<U> {
         assert_eq!(Layout::new::<T>(), Layout::new::<U>());
         self.clear();
         let ptr = self.as_mut_ptr().cast();
         let capacity = self.capacity();
-        forget(self);
-        unsafe {
-            Vec::from_raw_parts(ptr, 0, capacity)
-        }
+        let vec = unsafe {
+            Vec::from_raw_parts(ptr, 0, capacity)
+        };
+        forget(self);
+        vec
     }
 }

(Playground)

(I feel like this is actually worse.)