use raw_vec::RawVec;
use core::any::Any;
+use core::borrow;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{self, Hash};
use core::ops::{CoerceUnsized, Deref, DerefMut};
use core::ops::{Placer, Boxed, Place, InPlace, BoxPlace};
use core::ptr::{self, Unique};
-use core::raw::{TraitObject};
+use core::raw::TraitObject;
/// A value that represents the heap. This is the default place that the `box`
/// keyword allocates into when no place is supplied.
/// use std::boxed::HEAP;
///
/// fn main() {
-/// let foo = box(HEAP) 5;
+/// let foo: Box<i32> = in HEAP { 5 };
/// let foo = box 5;
/// }
/// ```
-#[lang = "exchange_heap"]
#[unstable(feature = "box_heap",
- reason = "may be renamed; uncertain about custom allocator design")]
-#[allow(deprecated)]
+ reason = "may be renamed; uncertain about custom allocator design",
+ issue = "27779")]
pub const HEAP: ExchangeHeapSingleton =
ExchangeHeapSingleton { _force_singleton: () };
/// This the singleton type used solely for `boxed::HEAP`.
#[unstable(feature = "box_heap",
- reason = "may be renamed; uncertain about custom allocator design")]
+ reason = "may be renamed; uncertain about custom allocator design",
+ issue = "27779")]
#[derive(Copy, Clone)]
-pub struct ExchangeHeapSingleton { _force_singleton: () }
+pub struct ExchangeHeapSingleton {
+ _force_singleton: (),
+}
/// A pointer type for heap allocation.
///
/// the fact that the `align_of` intrinsic currently requires the
/// input type to be Sized (which I do not think is strictly
/// necessary).
-#[unstable(feature = "placement_in", reason = "placement box design is still being worked out.")]
-pub struct IntermediateBox<T: ?Sized>{
+#[unstable(feature = "placement_in",
+ reason = "placement box design is still being worked out.",
+ issue = "27779")]
+pub struct IntermediateBox<T: ?Sized> {
ptr: *mut u8,
size: usize,
align: usize,
let p = if size == 0 {
heap::EMPTY as *mut u8
} else {
- let p = unsafe {
- heap::allocate(size, align)
- };
+ let p = unsafe { heap::allocate(size, align) };
if p.is_null() {
panic!("Box make_place allocation failure.");
}
}
impl<T> BoxPlace<T> for IntermediateBox<T> {
- fn make_place() -> IntermediateBox<T> { make_place() }
+ fn make_place() -> IntermediateBox<T> {
+ make_place()
+ }
}
impl<T> InPlace<T> for IntermediateBox<T> {
type Owner = Box<T>;
- unsafe fn finalize(self) -> Box<T> { finalize(self) }
+ unsafe fn finalize(self) -> Box<T> {
+ finalize(self)
+ }
}
impl<T> Boxed for Box<T> {
type Data = T;
type Place = IntermediateBox<T>;
- unsafe fn finalize(b: IntermediateBox<T>) -> Box<T> { finalize(b) }
+ unsafe fn finalize(b: IntermediateBox<T>) -> Box<T> {
+ finalize(b)
+ }
}
impl<T> Placer<T> for ExchangeHeapSingleton {
impl<T: ?Sized> Drop for IntermediateBox<T> {
fn drop(&mut self) {
if self.size > 0 {
- unsafe {
- heap::deallocate(self.ptr, self.size, self.align)
- }
+ unsafe { heap::deallocate(self.ptr, self.size, self.align) }
}
}
}
/// Function is unsafe, because improper use of this function may
/// lead to memory problems like double-free, for example if the
/// function is called twice on the same raw pointer.
- #[unstable(feature = "box_raw",
- reason = "may be renamed or moved out of Box scope")]
+ #[stable(feature = "box_raw", since = "1.4.0")]
#[inline]
- // NB: may want to be called from_ptr, see comments on CStr::from_ptr
pub unsafe fn from_raw(raw: *mut T) -> Self {
mem::transmute(raw)
}
/// `Box` does not specify, how memory is allocated.
///
/// # Examples
- /// ```
- /// #![feature(box_raw)]
///
+ /// ```
/// let seventeen = Box::new(17u32);
/// let raw = Box::into_raw(seventeen);
/// let boxed_again = unsafe { Box::from_raw(raw) };
/// ```
- #[unstable(feature = "box_raw", reason = "may be renamed")]
+ #[stable(feature = "box_raw", since = "1.4.0")]
#[inline]
- // NB: may want to be called into_ptr, see comments on CStr::from_ptr
pub fn into_raw(b: Box<T>) -> *mut T {
unsafe { mem::transmute(b) }
}
}
-/// Consumes the `Box`, returning the wrapped raw pointer.
-///
-/// After call to this function, caller is responsible for the memory
-/// previously managed by `Box`, in particular caller should properly
-/// destroy `T` and release memory. The proper way to do it is to
-/// convert pointer back to `Box` with `Box::from_raw` function, because
-/// `Box` does not specify, how memory is allocated.
-///
-/// # Examples
-/// ```
-/// #![feature(box_raw)]
-///
-/// use std::boxed;
-///
-/// let seventeen = Box::new(17u32);
-/// let raw = boxed::into_raw(seventeen);
-/// let boxed_again = unsafe { Box::from_raw(raw) };
-/// ```
-#[unstable(feature = "box_raw", reason = "may be renamed")]
-#[deprecated(since = "1.2.0", reason = "renamed to Box::into_raw")]
-#[inline]
-pub fn into_raw<T : ?Sized>(b: Box<T>) -> *mut T {
- Box::into_raw(b)
-}
-
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Default> Default for Box<T> {
#[stable(feature = "rust1", since = "1.0.0")]
- fn default() -> Box<T> { box Default::default() }
+ fn default() -> Box<T> {
+ box Default::default()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Default for Box<[T]> {
#[stable(feature = "rust1", since = "1.0.0")]
- fn default() -> Box<[T]> { Box::<[T; 0]>::new([]) }
+ fn default() -> Box<[T]> {
+ Box::<[T; 0]>::new([])
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
/// let x = Box::new(5);
/// let y = x.clone();
/// ```
+ #[rustfmt_skip]
#[inline]
- fn clone(&self) -> Box<T> { box {(**self).clone()} }
+ fn clone(&self) -> Box<T> {
+ box { (**self).clone() }
+ }
/// Copies `source`'s contents into `self` without creating a new allocation.
///
/// # Examples
///
/// ```
- /// #![feature(box_raw)]
- ///
/// let x = Box::new(5);
/// let mut y = Box::new(10);
///
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialEq> PartialEq for Box<T> {
#[inline]
- fn eq(&self, other: &Box<T>) -> bool { PartialEq::eq(&**self, &**other) }
+ fn eq(&self, other: &Box<T>) -> bool {
+ PartialEq::eq(&**self, &**other)
+ }
#[inline]
- fn ne(&self, other: &Box<T>) -> bool { PartialEq::ne(&**self, &**other) }
+ fn ne(&self, other: &Box<T>) -> bool {
+ PartialEq::ne(&**self, &**other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialOrd> PartialOrd for Box<T> {
PartialOrd::partial_cmp(&**self, &**other)
}
#[inline]
- fn lt(&self, other: &Box<T>) -> bool { PartialOrd::lt(&**self, &**other) }
+ fn lt(&self, other: &Box<T>) -> bool {
+ PartialOrd::lt(&**self, &**other)
+ }
#[inline]
- fn le(&self, other: &Box<T>) -> bool { PartialOrd::le(&**self, &**other) }
+ fn le(&self, other: &Box<T>) -> bool {
+ PartialOrd::le(&**self, &**other)
+ }
#[inline]
- fn ge(&self, other: &Box<T>) -> bool { PartialOrd::ge(&**self, &**other) }
+ fn ge(&self, other: &Box<T>) -> bool {
+ PartialOrd::ge(&**self, &**other)
+ }
#[inline]
- fn gt(&self, other: &Box<T>) -> bool { PartialOrd::gt(&**self, &**other) }
+ fn gt(&self, other: &Box<T>) -> bool {
+ PartialOrd::gt(&**self, &**other)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Ord> Ord for Box<T> {
unsafe {
// Get the raw representation of the trait object
let raw = Box::into_raw(self);
- let to: TraitObject =
- mem::transmute::<*mut Any, TraitObject>(raw);
+ let to: TraitObject = mem::transmute::<*mut Any, TraitObject>(raw);
// Extract the data pointer
Ok(Box::from_raw(to.data as *mut T))
impl<T: ?Sized> Deref for Box<T> {
type Target = T;
- fn deref(&self) -> &T { &**self }
+ fn deref(&self) -> &T {
+ &**self
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> DerefMut for Box<T> {
- fn deref_mut(&mut self) -> &mut T { &mut **self }
+ fn deref_mut(&mut self) -> &mut T {
+ &mut **self
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I: Iterator + ?Sized> Iterator for Box<I> {
type Item = I::Item;
- fn next(&mut self) -> Option<I::Item> { (**self).next() }
- fn size_hint(&self) -> (usize, Option<usize>) { (**self).size_hint() }
+ fn next(&mut self) -> Option<I::Item> {
+ (**self).next()
+ }
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ (**self).size_hint()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
- fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
+ fn next_back(&mut self) -> Option<I::Item> {
+ (**self).next_back()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}
/// }
/// ```
#[rustc_paren_sugar]
-#[unstable(feature = "fnbox", reason = "Newly introduced")]
+#[unstable(feature = "fnbox", reason = "Newly introduced", issue = "0")]
pub trait FnBox<A> {
type Output;
#[stable(feature = "box_slice_clone", since = "1.3.0")]
impl<T: Clone> Clone for Box<[T]> {
fn clone(&self) -> Self {
- let mut new = BoxBuilder {
- data: RawVec::with_capacity(self.len()),
- len: 0
- };
+ let mut new = BoxBuilder { data: RawVec::with_capacity(self.len()), len: 0 };
let mut target = new.data.ptr();
}
}
+impl<T: ?Sized> borrow::Borrow<T> for Box<T> {
+ fn borrow(&self) -> &T {
+ &**self
+ }
+}
+
+impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> {
+ fn borrow_mut(&mut self) -> &mut T {
+ &mut **self
+ }
+}