1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A pointer type for heap allocation.
13 //! `Box<T>`, casually referred to as a 'box', provides the simplest form of
14 //! heap allocation in Rust. Boxes provide ownership for this allocation, and
15 //! drop their contents when they go out of scope.
22 //! let x = Box::new(5);
25 //! Creating a recursive data structure:
30 //! Cons(T, Box<List<T>>),
35 //! let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
36 //! println!("{:?}", list);
40 //! This will print `Cons(1, Cons(2, Nil))`.
42 //! Recursive structures must be boxed, because if the definition of `Cons`
45 //! ```compile_fail,E0072
51 //! It wouldn't work. This is because the size of a `List` depends on how many
52 //! elements are in the list, and so we don't know how much memory to allocate
53 //! for a `Cons`. By introducing a `Box`, which has a defined size, we know how
54 //! big `Cons` needs to be.
56 #![stable(feature = "rust1", since = "1.0.0")]
58 use heap::{Heap, Layout, Alloc};
63 use core::cmp::Ordering;
65 use core::hash::{self, Hash, Hasher};
66 use core::iter::FusedIterator;
67 use core::marker::{self, Unsize};
69 use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
70 use core::ops::{BoxPlace, Boxed, InPlace, Place, Placer};
71 use core::ptr::{self, NonNull, Unique};
72 use core::convert::From;
73 use str::from_boxed_utf8_unchecked;
75 /// A value that represents the heap. This is the default place that the `box`
76 /// keyword allocates into when no place is supplied.
78 /// The following two examples are equivalent:
81 /// #![feature(box_heap)]
83 /// #![feature(box_syntax, placement_in_syntax)]
84 /// use std::boxed::HEAP;
87 /// let foo: Box<i32> = in HEAP { 5 };
91 #[unstable(feature = "box_heap",
92 reason = "may be renamed; uncertain about custom allocator design",
94 pub const HEAP: ExchangeHeapSingleton = ExchangeHeapSingleton { _force_singleton: () };
96 /// This the singleton type used solely for `boxed::HEAP`.
97 #[unstable(feature = "box_heap",
98 reason = "may be renamed; uncertain about custom allocator design",
100 #[allow(missing_debug_implementations)]
101 #[derive(Copy, Clone)]
102 pub struct ExchangeHeapSingleton {
103 _force_singleton: (),
106 /// A pointer type for heap allocation.
108 /// See the [module-level documentation](../../std/boxed/index.html) for more.
109 #[lang = "owned_box"]
111 #[stable(feature = "rust1", since = "1.0.0")]
112 pub struct Box<T: ?Sized>(Unique<T>);
114 /// `IntermediateBox` represents uninitialized backing storage for `Box`.
116 /// FIXME (pnkfelix): Ideally we would just reuse `Box<T>` instead of
117 /// introducing a separate `IntermediateBox<T>`; but then you hit
118 /// issues when you e.g. attempt to destructure an instance of `Box`,
119 /// since it is a lang item and so it gets special handling by the
120 /// compiler. Easier just to make this parallel type for now.
122 /// FIXME (pnkfelix): Currently the `box` protocol only supports
123 /// creating instances of sized types. This IntermediateBox is
124 /// designed to be forward-compatible with a future protocol that
125 /// supports creating instances of unsized types; that is why the type
126 /// parameter has the `?Sized` generalization marker, and is also why
127 /// this carries an explicit size. However, it probably does not need
128 /// to carry the explicit alignment; that is just a work-around for
129 /// the fact that the `align_of` intrinsic currently requires the
130 /// input type to be Sized (which I do not think is strictly
132 #[unstable(feature = "placement_in",
133 reason = "placement box design is still being worked out.",
135 #[allow(missing_debug_implementations)]
136 pub struct IntermediateBox<T: ?Sized> {
139 marker: marker::PhantomData<*mut T>,
142 #[unstable(feature = "placement_in",
143 reason = "placement box design is still being worked out.",
145 impl<T> Place<T> for IntermediateBox<T> {
146 fn pointer(&mut self) -> *mut T {
151 unsafe fn finalize<T>(b: IntermediateBox<T>) -> Box<T> {
152 let p = b.ptr as *mut T;
157 fn make_place<T>() -> IntermediateBox<T> {
158 let layout = Layout::new::<T>();
160 let p = if layout.size() == 0 {
161 mem::align_of::<T>() as *mut u8
164 Heap.alloc(layout.clone()).unwrap_or_else(|err| {
173 marker: marker::PhantomData,
177 #[unstable(feature = "placement_in",
178 reason = "placement box design is still being worked out.",
180 impl<T> BoxPlace<T> for IntermediateBox<T> {
181 fn make_place() -> IntermediateBox<T> {
186 #[unstable(feature = "placement_in",
187 reason = "placement box design is still being worked out.",
189 impl<T> InPlace<T> for IntermediateBox<T> {
191 unsafe fn finalize(self) -> Box<T> {
196 #[unstable(feature = "placement_new_protocol", issue = "27779")]
197 impl<T> Boxed for Box<T> {
199 type Place = IntermediateBox<T>;
200 unsafe fn finalize(b: IntermediateBox<T>) -> Box<T> {
205 #[unstable(feature = "placement_in",
206 reason = "placement box design is still being worked out.",
208 impl<T> Placer<T> for ExchangeHeapSingleton {
209 type Place = IntermediateBox<T>;
211 fn make_place(self) -> IntermediateBox<T> {
216 #[unstable(feature = "placement_in",
217 reason = "placement box design is still being worked out.",
219 impl<T: ?Sized> Drop for IntermediateBox<T> {
221 if self.layout.size() > 0 {
223 Heap.dealloc(self.ptr, self.layout.clone())
230 /// Allocates memory on the heap and then places `x` into it.
232 /// This doesn't actually allocate if `T` is zero-sized.
237 /// let five = Box::new(5);
239 #[stable(feature = "rust1", since = "1.0.0")]
241 pub fn new(x: T) -> Box<T> {
246 impl<T: ?Sized> Box<T> {
247 /// Constructs a box from a raw pointer.
249 /// After calling this function, the raw pointer is owned by the
250 /// resulting `Box`. Specifically, the `Box` destructor will call
251 /// the destructor of `T` and free the allocated memory. Since the
252 /// way `Box` allocates and releases memory is unspecified, the
253 /// only valid pointer to pass to this function is the one taken
254 /// from another `Box` via the [`Box::into_raw`] function.
256 /// This function is unsafe because improper use may lead to
257 /// memory problems. For example, a double-free may occur if the
258 /// function is called twice on the same raw pointer.
260 /// [`Box::into_raw`]: struct.Box.html#method.into_raw
265 /// let x = Box::new(5);
266 /// let ptr = Box::into_raw(x);
267 /// let x = unsafe { Box::from_raw(ptr) };
269 #[stable(feature = "box_raw", since = "1.4.0")]
271 pub unsafe fn from_raw(raw: *mut T) -> Self {
272 Box(Unique::new_unchecked(raw))
275 /// Constructs a `Box` from a `NonNull<T>` pointer.
277 /// After calling this function, the memory is owned by a `Box` and `T` can
278 /// then be destroyed and released upon drop.
282 /// A `NonNull<T>` can be safely created via [`NonNull::new`] and thus doesn't
283 /// necessarily own the data pointed to nor is the data guaranteed to live
284 /// as long as the pointer.
286 /// [`NonNull::new`]: ../../core/ptr/struct.NonNull.html#method.new
291 /// #![feature(nonnull)]
294 /// let x = Box::new(5);
295 /// let ptr = Box::into_nonnull_raw(x);
296 /// let x = unsafe { Box::from_nonnull_raw(ptr) };
299 #[unstable(feature = "nonnull", reason = "needs an RFC to flesh out design",
302 pub unsafe fn from_nonnull_raw(u: NonNull<T>) -> Self {
306 /// Consumes the `Box`, returning the wrapped raw pointer.
308 /// After calling this function, the caller is responsible for the
309 /// memory previously managed by the `Box`. In particular, the
310 /// caller should properly destroy `T` and release the memory. The
311 /// proper way to do so is to convert the raw pointer back into a
312 /// `Box` with the [`Box::from_raw`] function.
314 /// Note: this is an associated function, which means that you have
315 /// to call it as `Box::into_raw(b)` instead of `b.into_raw()`. This
316 /// is so that there is no conflict with a method on the inner type.
318 /// [`Box::from_raw`]: struct.Box.html#method.from_raw
323 /// let x = Box::new(5);
324 /// let ptr = Box::into_raw(x);
326 #[stable(feature = "box_raw", since = "1.4.0")]
328 pub fn into_raw(b: Box<T>) -> *mut T {
329 Box::into_nonnull_raw(b).as_ptr()
332 /// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
334 /// After calling this function, the caller is responsible for the
335 /// memory previously managed by the `Box`. In particular, the
336 /// caller should properly destroy `T` and release the memory. The
337 /// proper way to do so is to either convert the `NonNull<T>` pointer:
339 /// - Into a `Box` with the [`Box::from_nonnull_raw`] function.
341 /// - Into a raw pointer and back into a `Box` with the [`Box::from_raw`]
344 /// Note: this is an associated function, which means that you have
345 /// to call it as `Box::into_nonnull_raw(b)`
346 /// instead of `b.into_nonnull_raw()`. This
347 /// is so that there is no conflict with a method on the inner type.
349 /// [`Box::from_nonnull_raw`]: struct.Box.html#method.from_nonnull_raw
350 /// [`Box::from_raw`]: struct.Box.html#method.from_raw
355 /// #![feature(nonnull)]
358 /// let x = Box::new(5);
359 /// let ptr = Box::into_nonnull_raw(x);
362 #[unstable(feature = "nonnull", reason = "needs an RFC to flesh out design",
365 pub fn into_nonnull_raw(b: Box<T>) -> NonNull<T> {
366 Box::into_unique(b).into()
369 #[unstable(feature = "ptr_internals", issue = "0", reason = "use into_nonnull_raw instead")]
371 pub fn into_unique(b: Box<T>) -> Unique<T> {
377 /// Consumes and leaks the `Box`, returning a mutable reference,
378 /// `&'a mut T`. Here, the lifetime `'a` may be chosen to be `'static`.
380 /// This function is mainly useful for data that lives for the remainder of
381 /// the program's life. Dropping the returned reference will cause a memory
382 /// leak. If this is not acceptable, the reference should first be wrapped
383 /// with the [`Box::from_raw`] function producing a `Box`. This `Box` can
384 /// then be dropped which will properly destroy `T` and release the
385 /// allocated memory.
387 /// Note: this is an associated function, which means that you have
388 /// to call it as `Box::leak(b)` instead of `b.leak()`. This
389 /// is so that there is no conflict with a method on the inner type.
391 /// [`Box::from_raw`]: struct.Box.html#method.from_raw
398 /// #![feature(box_leak)]
401 /// let x = Box::new(41);
402 /// let static_ref: &'static mut usize = Box::leak(x);
403 /// *static_ref += 1;
404 /// assert_eq!(*static_ref, 42);
411 /// #![feature(box_leak)]
414 /// let x = vec![1, 2, 3].into_boxed_slice();
415 /// let static_ref = Box::leak(x);
416 /// static_ref[0] = 4;
417 /// assert_eq!(*static_ref, [4, 2, 3]);
420 #[unstable(feature = "box_leak", reason = "needs an FCP to stabilize",
423 pub fn leak<'a>(b: Box<T>) -> &'a mut T
425 T: 'a // Technically not needed, but kept to be explicit.
427 unsafe { &mut *Box::into_raw(b) }
431 #[stable(feature = "rust1", since = "1.0.0")]
432 unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> {
434 // FIXME: Do nothing, drop is currently performed by compiler.
438 #[stable(feature = "rust1", since = "1.0.0")]
439 impl<T: Default> Default for Box<T> {
440 /// Creates a `Box<T>`, with the `Default` value for T.
441 fn default() -> Box<T> {
442 box Default::default()
446 #[stable(feature = "rust1", since = "1.0.0")]
447 impl<T> Default for Box<[T]> {
448 fn default() -> Box<[T]> {
449 Box::<[T; 0]>::new([])
453 #[stable(feature = "default_box_extra", since = "1.17.0")]
454 impl Default for Box<str> {
455 fn default() -> Box<str> {
456 unsafe { from_boxed_utf8_unchecked(Default::default()) }
460 #[stable(feature = "rust1", since = "1.0.0")]
461 impl<T: Clone> Clone for Box<T> {
462 /// Returns a new box with a `clone()` of this box's contents.
467 /// let x = Box::new(5);
468 /// let y = x.clone();
472 fn clone(&self) -> Box<T> {
473 box { (**self).clone() }
475 /// Copies `source`'s contents into `self` without creating a new allocation.
480 /// let x = Box::new(5);
481 /// let mut y = Box::new(10);
483 /// y.clone_from(&x);
485 /// assert_eq!(*y, 5);
488 fn clone_from(&mut self, source: &Box<T>) {
489 (**self).clone_from(&(**source));
494 #[stable(feature = "box_slice_clone", since = "1.3.0")]
495 impl Clone for Box<str> {
496 fn clone(&self) -> Self {
497 let len = self.len();
498 let buf = RawVec::with_capacity(len);
500 ptr::copy_nonoverlapping(self.as_ptr(), buf.ptr(), len);
501 from_boxed_utf8_unchecked(buf.into_box())
506 #[stable(feature = "rust1", since = "1.0.0")]
507 impl<T: ?Sized + PartialEq> PartialEq for Box<T> {
509 fn eq(&self, other: &Box<T>) -> bool {
510 PartialEq::eq(&**self, &**other)
513 fn ne(&self, other: &Box<T>) -> bool {
514 PartialEq::ne(&**self, &**other)
517 #[stable(feature = "rust1", since = "1.0.0")]
518 impl<T: ?Sized + PartialOrd> PartialOrd for Box<T> {
520 fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
521 PartialOrd::partial_cmp(&**self, &**other)
524 fn lt(&self, other: &Box<T>) -> bool {
525 PartialOrd::lt(&**self, &**other)
528 fn le(&self, other: &Box<T>) -> bool {
529 PartialOrd::le(&**self, &**other)
532 fn ge(&self, other: &Box<T>) -> bool {
533 PartialOrd::ge(&**self, &**other)
536 fn gt(&self, other: &Box<T>) -> bool {
537 PartialOrd::gt(&**self, &**other)
540 #[stable(feature = "rust1", since = "1.0.0")]
541 impl<T: ?Sized + Ord> Ord for Box<T> {
543 fn cmp(&self, other: &Box<T>) -> Ordering {
544 Ord::cmp(&**self, &**other)
547 #[stable(feature = "rust1", since = "1.0.0")]
548 impl<T: ?Sized + Eq> Eq for Box<T> {}
550 #[stable(feature = "rust1", since = "1.0.0")]
551 impl<T: ?Sized + Hash> Hash for Box<T> {
552 fn hash<H: hash::Hasher>(&self, state: &mut H) {
553 (**self).hash(state);
557 #[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
558 impl<T: ?Sized + Hasher> Hasher for Box<T> {
559 fn finish(&self) -> u64 {
562 fn write(&mut self, bytes: &[u8]) {
563 (**self).write(bytes)
565 fn write_u8(&mut self, i: u8) {
568 fn write_u16(&mut self, i: u16) {
569 (**self).write_u16(i)
571 fn write_u32(&mut self, i: u32) {
572 (**self).write_u32(i)
574 fn write_u64(&mut self, i: u64) {
575 (**self).write_u64(i)
577 fn write_u128(&mut self, i: u128) {
578 (**self).write_u128(i)
580 fn write_usize(&mut self, i: usize) {
581 (**self).write_usize(i)
583 fn write_i8(&mut self, i: i8) {
586 fn write_i16(&mut self, i: i16) {
587 (**self).write_i16(i)
589 fn write_i32(&mut self, i: i32) {
590 (**self).write_i32(i)
592 fn write_i64(&mut self, i: i64) {
593 (**self).write_i64(i)
595 fn write_i128(&mut self, i: i128) {
596 (**self).write_i128(i)
598 fn write_isize(&mut self, i: isize) {
599 (**self).write_isize(i)
603 #[stable(feature = "from_for_ptrs", since = "1.6.0")]
604 impl<T> From<T> for Box<T> {
605 fn from(t: T) -> Self {
610 #[stable(feature = "box_from_slice", since = "1.17.0")]
611 impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
612 fn from(slice: &'a [T]) -> Box<[T]> {
613 let mut boxed = unsafe { RawVec::with_capacity(slice.len()).into_box() };
614 boxed.copy_from_slice(slice);
619 #[stable(feature = "box_from_slice", since = "1.17.0")]
620 impl<'a> From<&'a str> for Box<str> {
621 fn from(s: &'a str) -> Box<str> {
622 unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
626 #[stable(feature = "boxed_str_conv", since = "1.19.0")]
627 impl From<Box<str>> for Box<[u8]> {
628 fn from(s: Box<str>) -> Self {
629 unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }
635 #[stable(feature = "rust1", since = "1.0.0")]
636 /// Attempt to downcast the box to a concrete type.
641 /// use std::any::Any;
643 /// fn print_if_string(value: Box<Any>) {
644 /// if let Ok(string) = value.downcast::<String>() {
645 /// println!("String ({}): {}", string.len(), string);
650 /// let my_string = "Hello World".to_string();
651 /// print_if_string(Box::new(my_string));
652 /// print_if_string(Box::new(0i8));
655 pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> {
658 let raw: *mut Any = Box::into_raw(self);
659 Ok(Box::from_raw(raw as *mut T))
667 impl Box<Any + Send> {
669 #[stable(feature = "rust1", since = "1.0.0")]
670 /// Attempt to downcast the box to a concrete type.
675 /// use std::any::Any;
677 /// fn print_if_string(value: Box<Any + Send>) {
678 /// if let Ok(string) = value.downcast::<String>() {
679 /// println!("String ({}): {}", string.len(), string);
684 /// let my_string = "Hello World".to_string();
685 /// print_if_string(Box::new(my_string));
686 /// print_if_string(Box::new(0i8));
689 pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any + Send>> {
690 <Box<Any>>::downcast(self).map_err(|s| unsafe {
691 // reapply the Send marker
692 Box::from_raw(Box::into_raw(s) as *mut (Any + Send))
697 #[stable(feature = "rust1", since = "1.0.0")]
698 impl<T: fmt::Display + ?Sized> fmt::Display for Box<T> {
699 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
700 fmt::Display::fmt(&**self, f)
704 #[stable(feature = "rust1", since = "1.0.0")]
705 impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> {
706 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
707 fmt::Debug::fmt(&**self, f)
711 #[stable(feature = "rust1", since = "1.0.0")]
712 impl<T: ?Sized> fmt::Pointer for Box<T> {
713 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
714 // It's not possible to extract the inner Uniq directly from the Box,
715 // instead we cast it to a *const which aliases the Unique
716 let ptr: *const T = &**self;
717 fmt::Pointer::fmt(&ptr, f)
721 #[stable(feature = "rust1", since = "1.0.0")]
722 impl<T: ?Sized> Deref for Box<T> {
725 fn deref(&self) -> &T {
730 #[stable(feature = "rust1", since = "1.0.0")]
731 impl<T: ?Sized> DerefMut for Box<T> {
732 fn deref_mut(&mut self) -> &mut T {
737 #[stable(feature = "rust1", since = "1.0.0")]
738 impl<I: Iterator + ?Sized> Iterator for Box<I> {
740 fn next(&mut self) -> Option<I::Item> {
743 fn size_hint(&self) -> (usize, Option<usize>) {
746 fn nth(&mut self, n: usize) -> Option<I::Item> {
750 #[stable(feature = "rust1", since = "1.0.0")]
751 impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
752 fn next_back(&mut self) -> Option<I::Item> {
756 #[stable(feature = "rust1", since = "1.0.0")]
757 impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {
758 fn len(&self) -> usize {
761 fn is_empty(&self) -> bool {
766 #[unstable(feature = "fused", issue = "35602")]
767 impl<I: FusedIterator + ?Sized> FusedIterator for Box<I> {}
770 /// `FnBox` is a version of the `FnOnce` intended for use with boxed
771 /// closure objects. The idea is that where one would normally store a
772 /// `Box<FnOnce()>` in a data structure, you should use
773 /// `Box<FnBox()>`. The two traits behave essentially the same, except
774 /// that a `FnBox` closure can only be called if it is boxed. (Note
775 /// that `FnBox` may be deprecated in the future if `Box<FnOnce()>`
776 /// closures become directly usable.)
780 /// Here is a snippet of code which creates a hashmap full of boxed
781 /// once closures and then removes them one by one, calling each
782 /// closure as it is removed. Note that the type of the closures
783 /// stored in the map is `Box<FnBox() -> i32>` and not `Box<FnOnce()
787 /// #![feature(fnbox)]
789 /// use std::boxed::FnBox;
790 /// use std::collections::HashMap;
792 /// fn make_map() -> HashMap<i32, Box<FnBox() -> i32>> {
793 /// let mut map: HashMap<i32, Box<FnBox() -> i32>> = HashMap::new();
794 /// map.insert(1, Box::new(|| 22));
795 /// map.insert(2, Box::new(|| 44));
800 /// let mut map = make_map();
801 /// for i in &[1, 2] {
802 /// let f = map.remove(&i).unwrap();
803 /// assert_eq!(f(), i * 22);
808 #[unstable(feature = "fnbox",
809 reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
813 fn call_box(self: Box<Self>, args: A) -> Self::Output;
816 #[unstable(feature = "fnbox",
817 reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
818 impl<A, F> FnBox<A> for F
821 type Output = F::Output;
823 fn call_box(self: Box<F>, args: A) -> F::Output {
828 #[unstable(feature = "fnbox",
829 reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
830 impl<'a, A, R> FnOnce<A> for Box<FnBox<A, Output = R> + 'a> {
833 extern "rust-call" fn call_once(self, args: A) -> R {
838 #[unstable(feature = "fnbox",
839 reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
840 impl<'a, A, R> FnOnce<A> for Box<FnBox<A, Output = R> + Send + 'a> {
843 extern "rust-call" fn call_once(self, args: A) -> R {
848 #[unstable(feature = "coerce_unsized", issue = "27732")]
849 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
851 #[stable(feature = "box_slice_clone", since = "1.3.0")]
852 impl<T: Clone> Clone for Box<[T]> {
853 fn clone(&self) -> Self {
854 let mut new = BoxBuilder {
855 data: RawVec::with_capacity(self.len()),
859 let mut target = new.data.ptr();
861 for item in self.iter() {
863 ptr::write(target, item.clone());
864 target = target.offset(1);
870 return unsafe { new.into_box() };
872 // Helper type for responding to panics correctly.
873 struct BoxBuilder<T> {
878 impl<T> BoxBuilder<T> {
879 unsafe fn into_box(self) -> Box<[T]> {
880 let raw = ptr::read(&self.data);
886 impl<T> Drop for BoxBuilder<T> {
888 let mut data = self.data.ptr();
889 let max = unsafe { data.offset(self.len as isize) };
894 data = data.offset(1);
902 #[stable(feature = "box_borrow", since = "1.1.0")]
903 impl<T: ?Sized> borrow::Borrow<T> for Box<T> {
904 fn borrow(&self) -> &T {
909 #[stable(feature = "box_borrow", since = "1.1.0")]
910 impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> {
911 fn borrow_mut(&mut self) -> &mut T {
916 #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
917 impl<T: ?Sized> AsRef<T> for Box<T> {
918 fn as_ref(&self) -> &T {
923 #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
924 impl<T: ?Sized> AsMut<T> for Box<T> {
925 fn as_mut(&mut self) -> &mut T {
930 #[unstable(feature = "generator_trait", issue = "43122")]
931 impl<T> Generator for Box<T>
932 where T: Generator + ?Sized
934 type Yield = T::Yield;
935 type Return = T::Return;
936 fn resume(&mut self) -> GeneratorState<Self::Yield, Self::Return> {