implement Box::take · rust-lang/rust@8dfea22 (original) (raw)
`@@ -619,6 +619,37 @@ impl<T, A: Allocator> Box<T, A> {
`
619
619
`pub fn into_inner(boxed: Self) -> T {
`
620
620
`*boxed
`
621
621
`}
`
``
622
+
``
623
`` +
/// Consumes the Box without consuming its allocation, returning the wrapped value and a Box
``
``
624
`+
/// to the uninitialized memory where the wrapped value used to live.
`
``
625
`+
///
`
``
626
`` +
/// This can be used together with write to reuse the allocation for multiple
``
``
627
`+
/// boxed values.
`
``
628
`+
///
`
``
629
`+
/// # Examples
`
``
630
`+
///
`
``
631
/// ```
``
632
`+
/// #![feature(box_take)]
`
``
633
`+
///
`
``
634
`+
/// let c = Box::new(5);
`
``
635
`+
///
`
``
636
`+
/// // take the value out of the box
`
``
637
`+
/// let (value, uninit) = Box::take(c);
`
``
638
`+
/// assert_eq!(value, 5);
`
``
639
`+
///
`
``
640
`+
/// // reuse the box for a second value
`
``
641
`+
/// let c = Box::write(uninit, 6);
`
``
642
`+
/// assert_eq!(*c, 6);
`
``
643
/// ```
``
644
`+
#[unstable(feature = "box_take", issue = "147212")]
`
``
645
`+
pub fn take(boxed: Self) -> (T, Box<mem::MaybeUninit, A>) {
`
``
646
`+
unsafe {
`
``
647
`+
let (raw, alloc) = Box::into_raw_with_allocator(boxed);
`
``
648
`+
let value = raw.read();
`
``
649
`+
let uninit = Box::from_raw_in(raw.cast::<mem::MaybeUninit>(), alloc);
`
``
650
`+
(value, uninit)
`
``
651
`+
}
`
``
652
`+
}
`
622
653
`}
`
623
654
``
624
655
`impl Box<[T]> {
`