]> git.lizzy.rs Git - rust.git/blob - src/liballoc/boxed.rs
cdaad973a7123a1aa4830bb896397af5629d3572
[rust.git] / src / liballoc / boxed.rs
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.
4 //
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.
10
11 //! A pointer type for heap allocation.
12 //!
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.
16 //!
17 //! # Examples
18 //!
19 //! Creating a box:
20 //!
21 //! ```
22 //! let x = Box::new(5);
23 //! ```
24 //!
25 //! Creating a recursive data structure:
26 //!
27 //! ```
28 //! #[derive(Debug)]
29 //! enum List<T> {
30 //!     Cons(T, Box<List<T>>),
31 //!     Nil,
32 //! }
33 //!
34 //! fn main() {
35 //!     let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
36 //!     println!("{:?}", list);
37 //! }
38 //! ```
39 //!
40 //! This will print `Cons(1, Cons(2, Nil))`.
41 //!
42 //! Recursive structures must be boxed, because if the definition of `Cons`
43 //! looked like this:
44 //!
45 //! ```compile_fail,E0072
46 //! # enum List<T> {
47 //! Cons(T, List<T>),
48 //! # }
49 //! ```
50 //!
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.
55
56 #![stable(feature = "rust1", since = "1.0.0")]
57
58 use heap::{Heap, Layout, Alloc};
59 use raw_vec::RawVec;
60
61 use core::any::Any;
62 use core::borrow;
63 use core::cmp::Ordering;
64 use core::fmt;
65 use core::hash::{self, Hash, Hasher};
66 use core::iter::FusedIterator;
67 use core::marker::{self, Unsize};
68 use core::mem;
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;
74
75 /// A value that represents the heap. This is the default place that the `box`
76 /// keyword allocates into when no place is supplied.
77 ///
78 /// The following two examples are equivalent:
79 ///
80 /// ```
81 /// #![feature(box_heap)]
82 ///
83 /// #![feature(box_syntax, placement_in_syntax)]
84 /// use std::boxed::HEAP;
85 ///
86 /// fn main() {
87 ///     let foo: Box<i32> = in HEAP { 5 };
88 ///     let foo = box 5;
89 /// }
90 /// ```
91 #[unstable(feature = "box_heap",
92            reason = "may be renamed; uncertain about custom allocator design",
93            issue = "27779")]
94 pub const HEAP: ExchangeHeapSingleton = ExchangeHeapSingleton { _force_singleton: () };
95
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",
99            issue = "27779")]
100 #[allow(missing_debug_implementations)]
101 #[derive(Copy, Clone)]
102 pub struct ExchangeHeapSingleton {
103     _force_singleton: (),
104 }
105
106 /// A pointer type for heap allocation.
107 ///
108 /// See the [module-level documentation](../../std/boxed/index.html) for more.
109 #[lang = "owned_box"]
110 #[fundamental]
111 #[stable(feature = "rust1", since = "1.0.0")]
112 pub struct Box<T: ?Sized>(Unique<T>);
113
114 /// `IntermediateBox` represents uninitialized backing storage for `Box`.
115 ///
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.
121 ///
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
131 /// necessary).
132 #[unstable(feature = "placement_in",
133            reason = "placement box design is still being worked out.",
134            issue = "27779")]
135 #[allow(missing_debug_implementations)]
136 pub struct IntermediateBox<T: ?Sized> {
137     ptr: *mut u8,
138     layout: Layout,
139     marker: marker::PhantomData<*mut T>,
140 }
141
142 #[unstable(feature = "placement_in",
143            reason = "placement box design is still being worked out.",
144            issue = "27779")]
145 unsafe impl<T> Place<T> for IntermediateBox<T> {
146     fn pointer(&mut self) -> *mut T {
147         self.ptr as *mut T
148     }
149 }
150
151 unsafe fn finalize<T>(b: IntermediateBox<T>) -> Box<T> {
152     let p = b.ptr as *mut T;
153     mem::forget(b);
154     Box::from_raw(p)
155 }
156
157 fn make_place<T>() -> IntermediateBox<T> {
158     let layout = Layout::new::<T>();
159
160     let p = if layout.size() == 0 {
161         mem::align_of::<T>() as *mut u8
162     } else {
163         unsafe {
164             Heap.alloc(layout.clone()).unwrap_or_else(|err| {
165                 Heap.oom(err)
166             })
167         }
168     };
169
170     IntermediateBox {
171         ptr: p,
172         layout,
173         marker: marker::PhantomData,
174     }
175 }
176
177 #[unstable(feature = "placement_in",
178            reason = "placement box design is still being worked out.",
179            issue = "27779")]
180 impl<T> BoxPlace<T> for IntermediateBox<T> {
181     fn make_place() -> IntermediateBox<T> {
182         make_place()
183     }
184 }
185
186 #[unstable(feature = "placement_in",
187            reason = "placement box design is still being worked out.",
188            issue = "27779")]
189 impl<T> InPlace<T> for IntermediateBox<T> {
190     type Owner = Box<T>;
191     unsafe fn finalize(self) -> Box<T> {
192         finalize(self)
193     }
194 }
195
196 #[unstable(feature = "placement_new_protocol", issue = "27779")]
197 impl<T> Boxed for Box<T> {
198     type Data = T;
199     type Place = IntermediateBox<T>;
200     unsafe fn finalize(b: IntermediateBox<T>) -> Box<T> {
201         finalize(b)
202     }
203 }
204
205 #[unstable(feature = "placement_in",
206            reason = "placement box design is still being worked out.",
207            issue = "27779")]
208 impl<T> Placer<T> for ExchangeHeapSingleton {
209     type Place = IntermediateBox<T>;
210
211     fn make_place(self) -> IntermediateBox<T> {
212         make_place()
213     }
214 }
215
216 #[unstable(feature = "placement_in",
217            reason = "placement box design is still being worked out.",
218            issue = "27779")]
219 impl<T: ?Sized> Drop for IntermediateBox<T> {
220     fn drop(&mut self) {
221         if self.layout.size() > 0 {
222             unsafe {
223                 Heap.dealloc(self.ptr, self.layout.clone())
224             }
225         }
226     }
227 }
228
229 impl<T> Box<T> {
230     /// Allocates memory on the heap and then places `x` into it.
231     ///
232     /// This doesn't actually allocate if `T` is zero-sized.
233     ///
234     /// # Examples
235     ///
236     /// ```
237     /// let five = Box::new(5);
238     /// ```
239     #[stable(feature = "rust1", since = "1.0.0")]
240     #[inline(always)]
241     pub fn new(x: T) -> Box<T> {
242         box x
243     }
244 }
245
246 impl<T: ?Sized> Box<T> {
247     /// Constructs a box from a raw pointer.
248     ///
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.
255     ///
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.
259     ///
260     /// [`Box::into_raw`]: struct.Box.html#method.into_raw
261     ///
262     /// # Examples
263     ///
264     /// ```
265     /// let x = Box::new(5);
266     /// let ptr = Box::into_raw(x);
267     /// let x = unsafe { Box::from_raw(ptr) };
268     /// ```
269     #[stable(feature = "box_raw", since = "1.4.0")]
270     #[inline]
271     pub unsafe fn from_raw(raw: *mut T) -> Self {
272         Box(Unique::new_unchecked(raw))
273     }
274
275     /// Consumes the `Box`, returning the wrapped raw pointer.
276     ///
277     /// After calling this function, the caller is responsible for the
278     /// memory previously managed by the `Box`. In particular, the
279     /// caller should properly destroy `T` and release the memory. The
280     /// proper way to do so is to convert the raw pointer back into a
281     /// `Box` with the [`Box::from_raw`] function.
282     ///
283     /// Note: this is an associated function, which means that you have
284     /// to call it as `Box::into_raw(b)` instead of `b.into_raw()`. This
285     /// is so that there is no conflict with a method on the inner type.
286     ///
287     /// [`Box::from_raw`]: struct.Box.html#method.from_raw
288     ///
289     /// # Examples
290     ///
291     /// ```
292     /// let x = Box::new(5);
293     /// let ptr = Box::into_raw(x);
294     /// ```
295     #[stable(feature = "box_raw", since = "1.4.0")]
296     #[inline]
297     pub fn into_raw(b: Box<T>) -> *mut T {
298         Box::into_raw_non_null(b).as_ptr()
299     }
300
301     /// Consumes the `Box`, returning the wrapped pointer as `NonNull<T>`.
302     ///
303     /// After calling this function, the caller is responsible for the
304     /// memory previously managed by the `Box`. In particular, the
305     /// caller should properly destroy `T` and release the memory. The
306     /// proper way to do so is to convert the `NonNull<T>` pointer
307     /// into a raw pointer and back into a `Box` with the [`Box::from_raw`]
308     /// function.
309     ///
310     /// Note: this is an associated function, which means that you have
311     /// to call it as `Box::into_raw_non_null(b)`
312     /// instead of `b.into_raw_non_null()`. This
313     /// is so that there is no conflict with a method on the inner type.
314     ///
315     /// [`Box::from_raw`]: struct.Box.html#method.from_raw
316     ///
317     /// # Examples
318     ///
319     /// ```
320     /// #![feature(box_into_raw_non_null)]
321     ///
322     /// fn main() {
323     ///     let x = Box::new(5);
324     ///     let ptr = Box::into_raw_non_null(x);
325     /// }
326     /// ```
327     #[unstable(feature = "box_into_raw_non_null", issue = "47336")]
328     #[inline]
329     pub fn into_raw_non_null(b: Box<T>) -> NonNull<T> {
330         Box::into_unique(b).into()
331     }
332
333     #[unstable(feature = "ptr_internals", issue = "0", reason = "use into_raw_non_null instead")]
334     #[inline]
335     pub fn into_unique(b: Box<T>) -> Unique<T> {
336         let unique = b.0;
337         mem::forget(b);
338         unique
339     }
340
341     /// Consumes and leaks the `Box`, returning a mutable reference,
342     /// `&'a mut T`. Here, the lifetime `'a` may be chosen to be `'static`.
343     ///
344     /// This function is mainly useful for data that lives for the remainder of
345     /// the program's life. Dropping the returned reference will cause a memory
346     /// leak. If this is not acceptable, the reference should first be wrapped
347     /// with the [`Box::from_raw`] function producing a `Box`. This `Box` can
348     /// then be dropped which will properly destroy `T` and release the
349     /// allocated memory.
350     ///
351     /// Note: this is an associated function, which means that you have
352     /// to call it as `Box::leak(b)` instead of `b.leak()`. This
353     /// is so that there is no conflict with a method on the inner type.
354     ///
355     /// [`Box::from_raw`]: struct.Box.html#method.from_raw
356     ///
357     /// # Examples
358     ///
359     /// Simple usage:
360     ///
361     /// ```
362     /// #![feature(box_leak)]
363     ///
364     /// fn main() {
365     ///     let x = Box::new(41);
366     ///     let static_ref: &'static mut usize = Box::leak(x);
367     ///     *static_ref += 1;
368     ///     assert_eq!(*static_ref, 42);
369     /// }
370     /// ```
371     ///
372     /// Unsized data:
373     ///
374     /// ```
375     /// #![feature(box_leak)]
376     ///
377     /// fn main() {
378     ///     let x = vec![1, 2, 3].into_boxed_slice();
379     ///     let static_ref = Box::leak(x);
380     ///     static_ref[0] = 4;
381     ///     assert_eq!(*static_ref, [4, 2, 3]);
382     /// }
383     /// ```
384     #[unstable(feature = "box_leak", reason = "needs an FCP to stabilize",
385                issue = "46179")]
386     #[inline]
387     pub fn leak<'a>(b: Box<T>) -> &'a mut T
388     where
389         T: 'a // Technically not needed, but kept to be explicit.
390     {
391         unsafe { &mut *Box::into_raw(b) }
392     }
393 }
394
395 #[stable(feature = "rust1", since = "1.0.0")]
396 unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> {
397     fn drop(&mut self) {
398         // FIXME: Do nothing, drop is currently performed by compiler.
399     }
400 }
401
402 #[stable(feature = "rust1", since = "1.0.0")]
403 impl<T: Default> Default for Box<T> {
404     /// Creates a `Box<T>`, with the `Default` value for T.
405     fn default() -> Box<T> {
406         box Default::default()
407     }
408 }
409
410 #[stable(feature = "rust1", since = "1.0.0")]
411 impl<T> Default for Box<[T]> {
412     fn default() -> Box<[T]> {
413         Box::<[T; 0]>::new([])
414     }
415 }
416
417 #[stable(feature = "default_box_extra", since = "1.17.0")]
418 impl Default for Box<str> {
419     fn default() -> Box<str> {
420         unsafe { from_boxed_utf8_unchecked(Default::default()) }
421     }
422 }
423
424 #[stable(feature = "rust1", since = "1.0.0")]
425 impl<T: Clone> Clone for Box<T> {
426     /// Returns a new box with a `clone()` of this box's contents.
427     ///
428     /// # Examples
429     ///
430     /// ```
431     /// let x = Box::new(5);
432     /// let y = x.clone();
433     /// ```
434     #[rustfmt_skip]
435     #[inline]
436     fn clone(&self) -> Box<T> {
437         box { (**self).clone() }
438     }
439     /// Copies `source`'s contents into `self` without creating a new allocation.
440     ///
441     /// # Examples
442     ///
443     /// ```
444     /// let x = Box::new(5);
445     /// let mut y = Box::new(10);
446     ///
447     /// y.clone_from(&x);
448     ///
449     /// assert_eq!(*y, 5);
450     /// ```
451     #[inline]
452     fn clone_from(&mut self, source: &Box<T>) {
453         (**self).clone_from(&(**source));
454     }
455 }
456
457
458 #[stable(feature = "box_slice_clone", since = "1.3.0")]
459 impl Clone for Box<str> {
460     fn clone(&self) -> Self {
461         let len = self.len();
462         let buf = RawVec::with_capacity(len);
463         unsafe {
464             ptr::copy_nonoverlapping(self.as_ptr(), buf.ptr(), len);
465             from_boxed_utf8_unchecked(buf.into_box())
466         }
467     }
468 }
469
470 #[stable(feature = "rust1", since = "1.0.0")]
471 impl<T: ?Sized + PartialEq> PartialEq for Box<T> {
472     #[inline]
473     fn eq(&self, other: &Box<T>) -> bool {
474         PartialEq::eq(&**self, &**other)
475     }
476     #[inline]
477     fn ne(&self, other: &Box<T>) -> bool {
478         PartialEq::ne(&**self, &**other)
479     }
480 }
481 #[stable(feature = "rust1", since = "1.0.0")]
482 impl<T: ?Sized + PartialOrd> PartialOrd for Box<T> {
483     #[inline]
484     fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
485         PartialOrd::partial_cmp(&**self, &**other)
486     }
487     #[inline]
488     fn lt(&self, other: &Box<T>) -> bool {
489         PartialOrd::lt(&**self, &**other)
490     }
491     #[inline]
492     fn le(&self, other: &Box<T>) -> bool {
493         PartialOrd::le(&**self, &**other)
494     }
495     #[inline]
496     fn ge(&self, other: &Box<T>) -> bool {
497         PartialOrd::ge(&**self, &**other)
498     }
499     #[inline]
500     fn gt(&self, other: &Box<T>) -> bool {
501         PartialOrd::gt(&**self, &**other)
502     }
503 }
504 #[stable(feature = "rust1", since = "1.0.0")]
505 impl<T: ?Sized + Ord> Ord for Box<T> {
506     #[inline]
507     fn cmp(&self, other: &Box<T>) -> Ordering {
508         Ord::cmp(&**self, &**other)
509     }
510 }
511 #[stable(feature = "rust1", since = "1.0.0")]
512 impl<T: ?Sized + Eq> Eq for Box<T> {}
513
514 #[stable(feature = "rust1", since = "1.0.0")]
515 impl<T: ?Sized + Hash> Hash for Box<T> {
516     fn hash<H: hash::Hasher>(&self, state: &mut H) {
517         (**self).hash(state);
518     }
519 }
520
521 #[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
522 impl<T: ?Sized + Hasher> Hasher for Box<T> {
523     fn finish(&self) -> u64 {
524         (**self).finish()
525     }
526     fn write(&mut self, bytes: &[u8]) {
527         (**self).write(bytes)
528     }
529     fn write_u8(&mut self, i: u8) {
530         (**self).write_u8(i)
531     }
532     fn write_u16(&mut self, i: u16) {
533         (**self).write_u16(i)
534     }
535     fn write_u32(&mut self, i: u32) {
536         (**self).write_u32(i)
537     }
538     fn write_u64(&mut self, i: u64) {
539         (**self).write_u64(i)
540     }
541     fn write_u128(&mut self, i: u128) {
542         (**self).write_u128(i)
543     }
544     fn write_usize(&mut self, i: usize) {
545         (**self).write_usize(i)
546     }
547     fn write_i8(&mut self, i: i8) {
548         (**self).write_i8(i)
549     }
550     fn write_i16(&mut self, i: i16) {
551         (**self).write_i16(i)
552     }
553     fn write_i32(&mut self, i: i32) {
554         (**self).write_i32(i)
555     }
556     fn write_i64(&mut self, i: i64) {
557         (**self).write_i64(i)
558     }
559     fn write_i128(&mut self, i: i128) {
560         (**self).write_i128(i)
561     }
562     fn write_isize(&mut self, i: isize) {
563         (**self).write_isize(i)
564     }
565 }
566
567 #[stable(feature = "from_for_ptrs", since = "1.6.0")]
568 impl<T> From<T> for Box<T> {
569     fn from(t: T) -> Self {
570         Box::new(t)
571     }
572 }
573
574 #[stable(feature = "box_from_slice", since = "1.17.0")]
575 impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
576     fn from(slice: &'a [T]) -> Box<[T]> {
577         let mut boxed = unsafe { RawVec::with_capacity(slice.len()).into_box() };
578         boxed.copy_from_slice(slice);
579         boxed
580     }
581 }
582
583 #[stable(feature = "box_from_slice", since = "1.17.0")]
584 impl<'a> From<&'a str> for Box<str> {
585     fn from(s: &'a str) -> Box<str> {
586         unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
587     }
588 }
589
590 #[stable(feature = "boxed_str_conv", since = "1.19.0")]
591 impl From<Box<str>> for Box<[u8]> {
592     fn from(s: Box<str>) -> Self {
593         unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }
594     }
595 }
596
597 impl Box<Any> {
598     #[inline]
599     #[stable(feature = "rust1", since = "1.0.0")]
600     /// Attempt to downcast the box to a concrete type.
601     ///
602     /// # Examples
603     ///
604     /// ```
605     /// use std::any::Any;
606     ///
607     /// fn print_if_string(value: Box<Any>) {
608     ///     if let Ok(string) = value.downcast::<String>() {
609     ///         println!("String ({}): {}", string.len(), string);
610     ///     }
611     /// }
612     ///
613     /// fn main() {
614     ///     let my_string = "Hello World".to_string();
615     ///     print_if_string(Box::new(my_string));
616     ///     print_if_string(Box::new(0i8));
617     /// }
618     /// ```
619     pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> {
620         if self.is::<T>() {
621             unsafe {
622                 let raw: *mut Any = Box::into_raw(self);
623                 Ok(Box::from_raw(raw as *mut T))
624             }
625         } else {
626             Err(self)
627         }
628     }
629 }
630
631 impl Box<Any + Send> {
632     #[inline]
633     #[stable(feature = "rust1", since = "1.0.0")]
634     /// Attempt to downcast the box to a concrete type.
635     ///
636     /// # Examples
637     ///
638     /// ```
639     /// use std::any::Any;
640     ///
641     /// fn print_if_string(value: Box<Any + Send>) {
642     ///     if let Ok(string) = value.downcast::<String>() {
643     ///         println!("String ({}): {}", string.len(), string);
644     ///     }
645     /// }
646     ///
647     /// fn main() {
648     ///     let my_string = "Hello World".to_string();
649     ///     print_if_string(Box::new(my_string));
650     ///     print_if_string(Box::new(0i8));
651     /// }
652     /// ```
653     pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any + Send>> {
654         <Box<Any>>::downcast(self).map_err(|s| unsafe {
655             // reapply the Send marker
656             Box::from_raw(Box::into_raw(s) as *mut (Any + Send))
657         })
658     }
659 }
660
661 #[stable(feature = "rust1", since = "1.0.0")]
662 impl<T: fmt::Display + ?Sized> fmt::Display for Box<T> {
663     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
664         fmt::Display::fmt(&**self, f)
665     }
666 }
667
668 #[stable(feature = "rust1", since = "1.0.0")]
669 impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> {
670     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
671         fmt::Debug::fmt(&**self, f)
672     }
673 }
674
675 #[stable(feature = "rust1", since = "1.0.0")]
676 impl<T: ?Sized> fmt::Pointer for Box<T> {
677     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
678         // It's not possible to extract the inner Uniq directly from the Box,
679         // instead we cast it to a *const which aliases the Unique
680         let ptr: *const T = &**self;
681         fmt::Pointer::fmt(&ptr, f)
682     }
683 }
684
685 #[stable(feature = "rust1", since = "1.0.0")]
686 impl<T: ?Sized> Deref for Box<T> {
687     type Target = T;
688
689     fn deref(&self) -> &T {
690         &**self
691     }
692 }
693
694 #[stable(feature = "rust1", since = "1.0.0")]
695 impl<T: ?Sized> DerefMut for Box<T> {
696     fn deref_mut(&mut self) -> &mut T {
697         &mut **self
698     }
699 }
700
701 #[stable(feature = "rust1", since = "1.0.0")]
702 impl<I: Iterator + ?Sized> Iterator for Box<I> {
703     type Item = I::Item;
704     fn next(&mut self) -> Option<I::Item> {
705         (**self).next()
706     }
707     fn size_hint(&self) -> (usize, Option<usize>) {
708         (**self).size_hint()
709     }
710     fn nth(&mut self, n: usize) -> Option<I::Item> {
711         (**self).nth(n)
712     }
713 }
714 #[stable(feature = "rust1", since = "1.0.0")]
715 impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
716     fn next_back(&mut self) -> Option<I::Item> {
717         (**self).next_back()
718     }
719 }
720 #[stable(feature = "rust1", since = "1.0.0")]
721 impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {
722     fn len(&self) -> usize {
723         (**self).len()
724     }
725     fn is_empty(&self) -> bool {
726         (**self).is_empty()
727     }
728 }
729
730 #[unstable(feature = "fused", issue = "35602")]
731 impl<I: FusedIterator + ?Sized> FusedIterator for Box<I> {}
732
733
734 /// `FnBox` is a version of the `FnOnce` intended for use with boxed
735 /// closure objects. The idea is that where one would normally store a
736 /// `Box<FnOnce()>` in a data structure, you should use
737 /// `Box<FnBox()>`. The two traits behave essentially the same, except
738 /// that a `FnBox` closure can only be called if it is boxed. (Note
739 /// that `FnBox` may be deprecated in the future if `Box<FnOnce()>`
740 /// closures become directly usable.)
741 ///
742 /// # Examples
743 ///
744 /// Here is a snippet of code which creates a hashmap full of boxed
745 /// once closures and then removes them one by one, calling each
746 /// closure as it is removed. Note that the type of the closures
747 /// stored in the map is `Box<FnBox() -> i32>` and not `Box<FnOnce()
748 /// -> i32>`.
749 ///
750 /// ```
751 /// #![feature(fnbox)]
752 ///
753 /// use std::boxed::FnBox;
754 /// use std::collections::HashMap;
755 ///
756 /// fn make_map() -> HashMap<i32, Box<FnBox() -> i32>> {
757 ///     let mut map: HashMap<i32, Box<FnBox() -> i32>> = HashMap::new();
758 ///     map.insert(1, Box::new(|| 22));
759 ///     map.insert(2, Box::new(|| 44));
760 ///     map
761 /// }
762 ///
763 /// fn main() {
764 ///     let mut map = make_map();
765 ///     for i in &[1, 2] {
766 ///         let f = map.remove(&i).unwrap();
767 ///         assert_eq!(f(), i * 22);
768 ///     }
769 /// }
770 /// ```
771 #[rustc_paren_sugar]
772 #[unstable(feature = "fnbox",
773            reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
774 pub trait FnBox<A> {
775     type Output;
776
777     fn call_box(self: Box<Self>, args: A) -> Self::Output;
778 }
779
780 #[unstable(feature = "fnbox",
781            reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
782 impl<A, F> FnBox<A> for F
783     where F: FnOnce<A>
784 {
785     type Output = F::Output;
786
787     fn call_box(self: Box<F>, args: A) -> F::Output {
788         self.call_once(args)
789     }
790 }
791
792 #[unstable(feature = "fnbox",
793            reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
794 impl<'a, A, R> FnOnce<A> for Box<FnBox<A, Output = R> + 'a> {
795     type Output = R;
796
797     extern "rust-call" fn call_once(self, args: A) -> R {
798         self.call_box(args)
799     }
800 }
801
802 #[unstable(feature = "fnbox",
803            reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
804 impl<'a, A, R> FnOnce<A> for Box<FnBox<A, Output = R> + Send + 'a> {
805     type Output = R;
806
807     extern "rust-call" fn call_once(self, args: A) -> R {
808         self.call_box(args)
809     }
810 }
811
812 #[unstable(feature = "coerce_unsized", issue = "27732")]
813 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
814
815 #[stable(feature = "box_slice_clone", since = "1.3.0")]
816 impl<T: Clone> Clone for Box<[T]> {
817     fn clone(&self) -> Self {
818         let mut new = BoxBuilder {
819             data: RawVec::with_capacity(self.len()),
820             len: 0,
821         };
822
823         let mut target = new.data.ptr();
824
825         for item in self.iter() {
826             unsafe {
827                 ptr::write(target, item.clone());
828                 target = target.offset(1);
829             };
830
831             new.len += 1;
832         }
833
834         return unsafe { new.into_box() };
835
836         // Helper type for responding to panics correctly.
837         struct BoxBuilder<T> {
838             data: RawVec<T>,
839             len: usize,
840         }
841
842         impl<T> BoxBuilder<T> {
843             unsafe fn into_box(self) -> Box<[T]> {
844                 let raw = ptr::read(&self.data);
845                 mem::forget(self);
846                 raw.into_box()
847             }
848         }
849
850         impl<T> Drop for BoxBuilder<T> {
851             fn drop(&mut self) {
852                 let mut data = self.data.ptr();
853                 let max = unsafe { data.offset(self.len as isize) };
854
855                 while data != max {
856                     unsafe {
857                         ptr::read(data);
858                         data = data.offset(1);
859                     }
860                 }
861             }
862         }
863     }
864 }
865
866 #[stable(feature = "box_borrow", since = "1.1.0")]
867 impl<T: ?Sized> borrow::Borrow<T> for Box<T> {
868     fn borrow(&self) -> &T {
869         &**self
870     }
871 }
872
873 #[stable(feature = "box_borrow", since = "1.1.0")]
874 impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> {
875     fn borrow_mut(&mut self) -> &mut T {
876         &mut **self
877     }
878 }
879
880 #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
881 impl<T: ?Sized> AsRef<T> for Box<T> {
882     fn as_ref(&self) -> &T {
883         &**self
884     }
885 }
886
887 #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
888 impl<T: ?Sized> AsMut<T> for Box<T> {
889     fn as_mut(&mut self) -> &mut T {
890         &mut **self
891     }
892 }
893
894 #[unstable(feature = "generator_trait", issue = "43122")]
895 impl<T> Generator for Box<T>
896     where T: Generator + ?Sized
897 {
898     type Yield = T::Yield;
899     type Return = T::Return;
900     fn resume(&mut self) -> GeneratorState<Self::Yield, Self::Return> {
901         (**self).resume()
902     }
903 }