@@ -617,6 +617,42 @@ impl<H, T> HeaderVec<H, T> {
617617 }
618618 }
619619
620+ /// Consumes a `HeaderVec`, returning references to the header and data.
621+ ///
622+ /// Note that the header type H must outlive the chosen lifetime 'a and the data type T
623+ /// must outlive the chosen lifetime 'b. When the types have only static references, or
624+ /// none at all, then these may be chosen to be 'static.
625+ ///
626+ /// This method does not reallocate or shrink the `HeaderVec`, so the leaked allocation
627+ /// may include unused capacity that is not part of the returned slice.
628+ ///
629+ /// This function is mainly useful for data that lives for the remainder of the program’s
630+ /// life. Dropping the returned references will cause a memory leak.
631+ ///
632+ /// # Example
633+ ///
634+ // This example can't be run in miri because it leaks memory.
635+ /// ```
636+ /// # #[cfg(miri)] fn main() {}
637+ /// # #[cfg(not(miri))]
638+ /// # fn main() {
639+ /// use header_vec::HeaderVec;
640+ ///
641+ /// let mut hv = HeaderVec::from_header_elements(42, [1, 2, 3]);
642+ /// let (header, data) = hv.leak();
643+ /// assert_eq!(header, &42);
644+ /// assert_eq!(data, &[1, 2, 3]);
645+ /// # }
646+ /// ```
647+ pub fn leak < ' a , ' b > ( mut self ) -> ( & ' a H , & ' b mut [ T ] ) {
648+ let len = self . len_exact ( ) ;
649+ let ptr = self . as_mut_ptr ( ) ;
650+ let header = & mut self . header_mut ( ) . head as * mut H ;
651+ let slice = unsafe { slice:: from_raw_parts_mut ( ptr, len) } ;
652+ mem:: forget ( self ) ;
653+ ( unsafe { header. as_mut ( ) . unwrap_unchecked ( ) } , slice)
654+ }
655+
620656 /// Gives the offset in units of T (as if the pointer started at an array of T) that the slice actually starts at.
621657 #[ mutants:: skip]
622658 #[ inline( always) ]
0 commit comments