Lines Matching defs:Box

2597     let mut m = LinkedList::<Box<_>>::new();
3401 let iter = (0..SHARED_ITER_MAX).filter(|x| x % 2 == 0).map(Box::new);
3422 let iter = (0..SHARED_ITER_MAX).map(Box::new);
3429 assert_eq!(mem::size_of::<Box<u16>>() * SHARED_ITER_MAX as usize, mem::size_of_val(&*rc));
3461 _ => Box::new(val),
3473 struct Iter(std::vec::IntoIter<Option<Box<u8>>>);
3482 type Item = Box<u8>;
3489 let vec = vec![Some(Box::new(42)), Some(Box::new(24)), None, Some(Box::new(12))];
3492 assert_eq!(&[Box::new(42), Box::new(24)], &*iter.collect::<Rc<[_]>>());
5189 // The only way to acquire a Box<str> in the first place is through a String, so just
5190 // test that we can round-trip between Box<str> and String.
6484 test_from_cow!($value => Box<$ty>, Rc<$ty>, Arc<$ty>);
6794 let three: Vec<Box<_>> = vec![box 1, box 2, box 3];
6795 let two: Vec<Box<_>> = vec![box 4, box 5];
6935 let mut v0: Vec<Box<_>> = vec![box 1, box 1, box 2, box 3];
6937 let mut v1: Vec<Box<_>> = vec![box 1, box 2, box 2, box 3];
6939 let mut v2: Vec<Box<_>> = vec![box 1, box 2, box 3, box 3];
7588 DroppedTwice(Box<i32>),
7614 let mut v = vec![Droppable::DroppedTwice(Box::new(123)), Droppable::PanicOnDrop];
8592 let el = Box::new(1);
8594 assert_eq!(vec![el; n], vec![Box::new(1), Box::new(1), Box::new(1)]);
8902 let mut hasher_1 = Box::new(DefaultHasher::new());
8906 let mut hasher_2 = Box::new(DefaultHasher::new()) as Box<dyn Hasher>;
9154 let iter = (0..SHARED_ITER_MAX).filter(|x| x % 2 == 0).map(Box::new);
9175 let iter = (0..SHARED_ITER_MAX).map(Box::new);
9182 assert_eq!(mem::size_of::<Box<u16>>() * SHARED_ITER_MAX as usize, mem::size_of_val(&*rc));
9214 _ => Box::new(val),
9226 struct Iter(std::vec::IntoIter<Option<Box<u8>>>);
9235 type Item = Box<u8>;
9242 let vec = vec![Some(Box::new(42)), Some(Box::new(24)), None, Some(Box::new(12))];
9245 assert_eq!(&[Box::new(42), Box::new(24)], &*iter.collect::<Rc<[_]>>());
9449 // Test `Box<[T]>`
9515 let mut v: Vec<Box<_>> = Vec::new();
9544 let mut v: Vec<Box<_>> = vec![box 6, box 5, box 4];
9554 let mut v: Vec<Box<_>> = vec![box 6, box 5, box 4];
10704 let xs: Box<_> = box [1, 2, 3];
10711 let src: Box<[usize]> = box [1, 2, 3];
10713 let sink: Box<_> = src.into_vec().into_iter().map(std::convert::identity).collect();
10972 panic::set_hook(Box::new(move |info| {
13177 let a = Box::new(3);
13468 let mut heap = BinaryHeap::<Box<_>>::from(vec![box 2, box 4, box 9]);
13779 &*Box::<()>::new_uninit() as *const _,
13783 Box::<[()]>::new_uninit_slice(4).as_ptr(),
13787 Box::<[String]>::new_uninit_slice(0).as_ptr(),
13827 let x = Box::new(Cell::new(5));
14324 // This signature has to be the same as `Box`, otherwise an ICE will happen.
14325 // When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
14327 // For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
14402 /// Used by `Box::clone` and `Rc`/`Arc::make_mut`.
14435 use crate::boxed::Box;
14469 /// `Box<[T]>`, since `capacity()` won't yield the length.
14568 /// Converts a `Box<[T]>` into a `RawVec<T>`.
14569 pub fn from_box(slice: Box<[T], A>) -> Self {
14571 let (slice, alloc) = Box::into_raw_with_allocator(slice);
14576 /// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
14588 pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<T>], A> {
14598 Box::from_raw_in(slice, ptr::read(&me.alloc))
15007 use crate::boxed::Box;
15196 /// Converts a `Box<str>` into a `Box<[u8]>` without copying or allocating.
15210 pub fn into_boxed_bytes(self: Box<str>) -> Box<[u8]> {
15429 /// Converts a [`Box<str>`] into a [`String`] without copying or allocating.
15443 pub fn into_string(self: Box<str>) -> String {
15444 let slice = Box::<[u8]>::from(self);
15542 /// let smile_utf8 = Box::new([226, 152, 186]);
15549 pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
15550 unsafe { Box::from_raw(Box::into_raw(v) as *mut str) }
15554 use std::boxed::Box;
16021 let b: Box<u32> = box 123;
16050 let b: Box<dyn Display> = box 123;
16060 let b: Box<dyn Debug> = box ();
16227 use crate::boxed::Box;
17813 /// Converts this `String` into a [`Box`]`<`[`str`]`>`.
17830 pub fn into_boxed_str(self) -> Box<str> {
17979 impl FromIterator<Box<str>> for String {
17980 fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> String {
18056 impl Extend<Box<str>> for String {
18057 fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) {
18540 impl From<Box<str>> for String {
18550 /// let s2: Box<str> = s1.into_boxed_str();
18555 fn from(s: Box<str>) -> String {
18561 impl From<String> for Box<str> {
18570 /// let s2: Box<str> = Box::from(s1);
18575 fn from(s: String) -> Box<str> {
19312 //! The [`Box`] type is a smart pointer type. There can only be one owner of a
19313 //! [`Box`], and the owner can decide to mutate the contents, which live on the
19316 //! This type can be sent among threads efficiently as the size of a `Box` value
19326 //! This type is useful when inherited mutability (such as using [`Box`]) is too
19353 //! [`Box`]: boxed
19467 // to allow code to have `use boxed::Box;` declarations.
19472 pub use std::boxed::Box;
19741 use crate::boxed::Box;
19743 use std::boxed::Box;
19843 Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
19875 let uninit_ptr: NonNull<_> = Box::leak(box RcBox {
19992 Box::leak(Box::try_new(RcBox { strong: Cell::new(1), weak: Cell::new(1), value })?)
20754 fn from_box(v: Box<T>) -> Rc<T> {
20756 let (box_unique, alloc) = Box::into_unique(v);
21270 impl<T: ?Sized> From<Box<T>> for Rc<T> {
21277 /// let original: Box<i32> = Box::new(1);
21282 fn from(v: Box<T>) -> Rc<T> {
21295 /// let original: Box<Vec<i32>> = Box::new(vec![1, 2, 3]);
22063 use crate::boxed::Box;
22118 use crate::boxed::Box;
22124 pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A> {
22127 let (b, alloc) = Box::into_raw_with_allocator(b);
22458 /// let s: Box<[i32]> = Box::new([10, 40, 30]);
22466 pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
23172 /// let mut fut = Box::pin(fut);
23299 use crate::boxed::Box;
23602 let x: Box<_> = box ArcInner {
23607 Self::from_inner(Box::leak(x).into())
23635 let uninit_ptr: NonNull<_> = Box::leak(box ArcInner {
23769 let x: Box<_> = Box::try_new(ArcInner {
23774 Ok(Self::from_inner(Box::leak(x).into()))
24404 fn from_box(v: Box<T>) -> Arc<T> {
24406 let (box_unique, alloc) = Box::into_unique(v);
25611 impl<T: ?Sized> From<Box<T>> for Arc<T> {
25618 /// let unique: Box<str> = Box::from("eggplant");
25623 fn from(v: Box<T>) -> Arc<T> {
26386 use std::boxed::Box;
26418 let x: Rc<Box<_>> = Rc::new(box 5);
26795 let b: Box<u32> = box 123;
26824 let b: Box<dyn Display> = box 123;
26834 let b: Box<dyn Debug> = box ();
26968 use crate::boxed::Box;
26987 marker: PhantomData<Box<Node<T>>>,
27069 fn into_element(self: Box<Self>) -> T {
27078 fn push_front_node(&mut self, mut node: Box<Node<T>>) {
27084 let node = Some(Box::leak(node).into());
27099 fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
27103 let node = Box::from_raw(node.as_ptr());
27119 fn push_back_node(&mut self, mut node: Box<Node<T>>) {
27125 let node = Some(Box::leak(node).into());
27140 fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
27144 let node = Box::from_raw(node.as_ptr());
28317 let spliced_node = Box::leak(Box::new(Node::new(item))).into();
28337 let spliced_node = Box::leak(Box::new(Node::new(item))).into();
28360 let unlinked_node = Box::from_raw(unlinked_node.as_ptr());
28492 return Some(Box::from_raw(node.as_ptr()).element);
35563 use crate::boxed::Box;
36781 map.insert(Box::new(0), 1);
36787 map.insert(Box::new([0, 1]) as Box<[i32]>, 1);
36798 fn get<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
36803 fn get_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
36808 fn get_key_value<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
36813 fn contains_key<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
36818 fn range<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: T) {
36823 fn range_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: T) {
36828 fn remove<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
36833 fn remove_entry<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
36838 fn split_off<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
37927 // root: Option<Box<Node<K, V, height>>>
37933 // edges: [if height > 0 { Box<Node<K, V, height - 1>> } else { () }; 2 * B],
37961 use crate::boxed::Box;
38002 fn new() -> Box<Self> {
38004 let mut leaf = Box::new_uninit();
38034 unsafe fn new() -> Box<Self> {
38036 let mut node = Box::<Self>::new_uninit();
38068 /// - When this is `Owned`, the `NodeRef` acts roughly like `Box<Node>`,
38070 /// - When this is `Dying`, the `NodeRef` still acts roughly like `Box<Node>`,
38141 fn from_new_leaf(leaf: Box<LeafNode<K, V>>) -> Self {
38142 NodeRef { height: 0, node: NonNull::from(Box::leak(leaf)), _marker: PhantomData }
38155 unsafe fn from_new_internal(internal: Box<InternalNode<K, V>>, height: usize) -> Self {
38157 let node = NonNull::from(Box::leak(internal)).cast();
44064 use crate::boxed::Box;
44067 dst.push_front(Box::new(1));
44068 dst.push_front(Box::new(2));
44072 src.push_front(Box::new(2));
46496 use crate::boxed::Box;
46547 // `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
46561 unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
46878 use crate::boxed::Box;
47150 /// and from a [`Box<[T]>`][owned slice] without reallocating or moving the elements.
47179 /// [owned slice]: Box
47749 /// Converts the vector into [`Box<[T]>`][owned slice].
47753 /// [owned slice]: Box
47774 pub fn into_boxed_slice(mut self) -> Box<[T], A> {
48720 /// This function is similar to the [`leak`][Box::leak] function on [`Box`]
48743 Box::leak(self.into_boxed_slice())
49608 impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> {
49615 /// let b: Box<[i32]> = vec![1, 2, 3].into_boxed_slice();
49618 fn from(s: Box<[T], A>) -> Self {
49626 impl<T, A: Allocator> From<Vec<T, A>> for Box<[T], A> {
49635 /// assert_eq!(Box::from(vec![1, 2, 3]), vec![1, 2, 3].into_boxed_slice());
50136 pub use crate::boxed::Box;
50144 use crate::boxed::Box;
50168 let _: Box<_> = box 10;
50173 //! [`Box<T>`], casually referred to as a 'box', provides the simplest form of
50180 //! Move a value from the stack to the heap by creating a [`Box`]:
50184 //! let boxed: Box<u8> = Box::new(val);
50187 //! Move a value from a [`Box`] back to the stack by [dereferencing]:
50190 //! let boxed: Box<u8> = Box::new(5);
50199 //! Cons(T, Box<List<T>>),
50203 //! let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))));
50220 //! for a `Cons`. By introducing a [`Box<T>`], which has a defined size, we know how
50225 //! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for
50226 //! its allocation. It is valid to convert both ways between a [`Box`] and a
50231 //! [`Box::<T>::from_raw(value)`]. Conversely, the memory backing a `value: *mut
50232 //! T` obtained from [`Box::<T>::into_raw`] may be deallocated using the
50235 //! For zero-sized values, the `Box` pointer still has to be [valid] for reads
50239 //! not valid. The recommended way to build a Box to a ZST if `Box::new` cannot
50242 //! So long as `T: Sized`, a `Box<T>` is guaranteed to be represented
50246 //! Rust functions using `Box<T>` types, and use `T*` as corresponding
50262 //! `struct Foo*` type from C is translated to `Box<Foo>`, which captures
50264 //! `foo_delete` is represented in Rust as `Option<Box<Foo>>`, since `Box<Foo>`
50272 //! pub extern "C" fn foo_new() -> Box<Foo> {
50273 //! Box::new(Foo)
50277 //! pub extern "C" fn foo_delete(_: Option<Box<Foo>>) {}
50280 //! Even though `Box<T>` has the same representation and C ABI as a C pointer,
50281 //! this does not mean that you can convert an arbitrary `T*` into a `Box<T>`
50282 //! and expect things to work. `Box<T>` values will always be fully aligned,
50283 //! non-null pointers. Moreover, the destructor for `Box<T>` will attempt to
50285 //! is to only use `Box<T>` for pointers that originated from the global
50289 //! `Box<T>` types for functions that are defined in C but invoked
50291 //! as closely as possible. Using types like `Box<T>` where the C
50297 //! [`Box::<T>::from_raw(value)`]: Box::from_raw
50335 pub struct Box<
50340 impl<T> Box<T> {
50348 /// let five = Box::new(5);
50365 /// let mut five = Box::<u32>::new_uninit();
50378 pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
50382 /// Constructs a new `Box` with uninitialized contents, with the memory
50393 /// let zero = Box::<u32>::new_zeroed();
50403 pub fn new_zeroed() -> Box<mem::MaybeUninit<T>> {
50407 /// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
50411 pub fn pin(x: T) -> Pin<Box<T>> {
50425 /// let five = Box::try_new(5)?;
50442 /// let mut five = Box::<u32>::try_new_uninit()?;
50457 pub fn try_new_uninit() -> Result<Box<mem::MaybeUninit<T>>, AllocError> {
50458 Box::try_new_uninit_in(Global)
50461 /// Constructs a new `Box` with uninitialized contents, with the memory
50472 /// let zero = Box::<u32>::try_new_zeroed()?;
50483 pub fn try_new_zeroed() -> Result<Box<mem::MaybeUninit<T>>, AllocError> {
50484 Box::try_new_zeroed_in(Global)
50488 impl<T, A: Allocator> Box<T, A> {
50500 /// let five = Box::new_in(5, System);
50524 /// let five = Box::try_new_in(5, System)?;
50546 /// let mut five = Box::<u32, _>::new_uninit_in(System);
50559 pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
50563 match Box::try_new_uninit_in(alloc) {
50579 /// let mut five = Box::<u32, _>::try_new_uninit_in(System)?;
50593 pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError> {
50596 unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
50599 /// Constructs a new `Box` with uninitialized contents, with the memory
50612 /// let zero = Box::<u32, _>::new_zeroed_in(System);
50621 pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
50625 match Box::try_new_zeroed_in(alloc) {
50631 /// Constructs a new `Box` with uninitialized contents, with the memory
50645 /// let zero = Box::<u32, _>::try_new_zeroed_in(System)?;
50655 pub fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError> {
50658 unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
50661 /// Constructs a new `Pin<Box<T, A>>`. If `T` does not implement `Unpin`, then
50672 /// Converts a `Box<T>` into a `Box<[T]>`
50676 pub fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
50677 let (raw, alloc) = Box::into_raw_with_allocator(boxed);
50678 unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) }
50681 /// Consumes the `Box`, returning the wrapped value.
50688 /// let c = Box::new(5);
50690 /// assert_eq!(Box::into_inner(c), 5);
50699 impl<T> Box<[T]> {
50707 /// let mut values = Box::<[u32]>::new_uninit_slice(3);
50721 pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
50736 /// let values = Box::<[u32]>::new_zeroed_slice(3);
50744 pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
50749 impl<T, A: Allocator> Box<[T], A> {
50759 /// let mut values = Box::<[u32], _>::new_uninit_slice_in(3, System);
50774 pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
50791 /// let values = Box::<[u32], _>::new_zeroed_slice_in(3, System);
50800 pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
50805 impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
50806 /// Converts to `Box<T, A>`.
50823 /// let mut five = Box::<u32>::new_uninit();
50825 /// let five: Box<u32> = unsafe {
50836 pub unsafe fn assume_init(self) -> Box<T, A> {
50837 let (raw, alloc) = Box::into_raw_with_allocator(self);
50838 unsafe { Box::from_raw_in(raw as *mut T, alloc) }
50842 impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> {
50843 /// Converts to `Box<[T], A>`.
50860 /// let mut values = Box::<[u32]>::new_uninit_slice(3);
50875 pub unsafe fn assume_init(self) -> Box<[T], A> {
50876 let (raw, alloc) = Box::into_raw_with_allocator(self);
50877 unsafe { Box::from_raw_in(raw as *mut [T], alloc) }
50881 impl<T: ?Sized> Box<T> {
50885 /// resulting `Box`. Specifically, the `Box` destructor will call
50888 /// with the [memory layout] used by `Box` .
50900 /// Recreate a `Box` which was previously converted to a raw pointer
50901 /// using [`Box::into_raw`]:
50903 /// let x = Box::new(5);
50904 /// let ptr = Box::into_raw(x);
50905 /// let x = unsafe { Box::from_raw(ptr) };
50907 /// Manually create a `Box` from scratch by using the global allocator:
50917 /// let x = Box::from_raw(ptr);
50930 impl<T: ?Sized, A: Allocator> Box<T, A> {
50934 /// resulting `Box`. Specifically, the `Box` destructor will call
50937 /// with the [memory layout] used by `Box` .
50948 /// Recreate a `Box` which was previously converted to a raw pointer
50949 /// using [`Box::into_raw_with_allocator`]:
50955 /// let x = Box::new_in(5, System);
50956 /// let (ptr, alloc) = Box::into_raw_with_allocator(x);
50957 /// let x = unsafe { Box::from_raw_in(ptr, alloc) };
50959 /// Manually create a `Box` from scratch by using the system allocator:
50971 /// let x = Box::from_raw_in(ptr, System);
50981 Box(unsafe { Unique::new_unchecked(raw) }, alloc)
50984 /// Consumes the `Box`, returning a wrapped raw pointer.
50989 /// memory previously managed by the `Box`. In particular, the
50991 /// into account the [memory layout] used by `Box`. The easiest way to
50992 /// do this is to convert the raw pointer back into a `Box` with the
50993 /// [`Box::from_raw`] function, allowing the `Box` destructor to perform
50997 /// to call it as `Box::into_raw(b)` instead of `b.into_raw()`. This
51001 /// Converting the raw pointer back into a `Box` with [`Box::from_raw`]
51004 /// let x = Box::new(String::from("Hello"));
51005 /// let ptr = Box::into_raw(x);
51006 /// let x = unsafe { Box::from_raw(ptr) };
51014 /// let x = Box::new(String::from("Hello"));
51015 /// let p = Box::into_raw(x);
51029 /// Consumes the `Box`, returning a wrapped raw pointer and the allocator.
51034 /// memory previously managed by the `Box`. In particular, the
51036 /// into account the [memory layout] used by `Box`. The easiest way to
51037 /// do this is to convert the raw pointer back into a `Box` with the
51038 /// [`Box::from_raw_in`] function, allowing the `Box` destructor to perform
51042 /// to call it as `Box::into_raw_with_allocator(b)` instead of `b.into_raw_with_allocator()`. This
51046 /// Converting the raw pointer back into a `Box` with [`Box::from_raw_in`]
51053 /// let x = Box::new_in(String::from("Hello"), System);
51054 /// let (ptr, alloc) = Box::into_raw_with_allocator(x);
51055 /// let x = unsafe { Box::from_raw_in(ptr, alloc) };
51065 /// let x = Box::new_in(String::from("Hello"), System);
51066 /// let (ptr, alloc) = Box::into_raw_with_allocator(x);
51078 let (leaked, alloc) = Box::into_unique(b);
51085 reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
51090 // Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a
51093 // so all raw pointer methods have to go through `Box::leak`. Turning *that* to a raw pointer
51096 (Unique::from(Box::leak(b)), alloc)
51102 /// to call it as `Box::allocator(&b)` instead of `b.allocator()`. This
51110 /// Consumes and leaks the `Box`, returning a mutable reference,
51118 /// with the [`Box::from_raw`] function producing a `Box`. This `Box` can
51123 /// to call it as `Box::leak(b)` instead of `b.leak()`. This
51131 /// let x = Box::new(41);
51132 /// let static_ref: &'static mut usize = Box::leak(x);
51141 /// let static_ref = Box::leak(x);
51154 /// Converts a `Box<T>` into a `Pin<Box<T>>`
51164 // It's not possible to move or replace the insides of a `Pin<Box<T>>`
51172 unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
51179 impl<T: Default> Default for Box<T> {
51180 /// Creates a `Box<T>`, with the `Default` value for T.
51187 impl<T> Default for Box<[T]> {
51189 Box::<[T; 0]>::new([])
51194 impl Default for Box<str> {
51201 impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {
51207 /// let x = Box::new(5);
51231 /// let x = Box::new(5);
51232 /// let mut y = Box::new(10);
51250 impl Clone for Box<str> {
51253 let buf: Box<[u8]> = self.as_bytes().into();
51259 impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for Box<T, A> {
51270 impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for Box<T, A> {
51293 impl<T: ?Sized + Ord, A: Allocator> Ord for Box<T, A> {
51300 impl<T: ?Sized + Eq, A: Allocator> Eq for Box<T, A> {}
51303 impl<T: ?Sized + Hash, A: Allocator> Hash for Box<T, A> {
51310 impl<T: ?Sized + Hasher, A: Allocator> Hasher for Box<T, A> {
51356 impl<T> From<T> for Box<T> {
51357 /// Converts a generic type `T` into a `Box<T>`
51365 /// let boxed = Box::new(5);
51367 /// assert_eq!(Box::from(x), boxed);
51370 Box::new(t)
51375 impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Pin<Box<T, A>>
51379 /// Converts a `Box<T>` into a `Pin<Box<T>>`
51382 fn from(boxed: Box<T, A>) -> Self {
51383 Box::into_pin(boxed)
51388 impl<T: Copy> From<&[T]> for Box<[T]> {
51389 /// Converts a `&[T]` into a `Box<[T]>`
51396 /// // create a &[u8] which will be used to create a Box<[u8]>
51398 /// let boxed_slice: Box<[u8]> = Box::from(slice);
51402 fn from(slice: &[T]) -> Box<[T]> {
51413 impl<T: Copy> From<Cow<'_, [T]>> for Box<[T]> {
51415 fn from(cow: Cow<'_, [T]>) -> Box<[T]> {
51417 Cow::Borrowed(slice) => Box::from(slice),
51418 Cow::Owned(slice) => Box::from(slice),
51424 impl From<&str> for Box<str> {
51425 /// Converts a `&str` into a `Box<str>`
51432 /// let boxed: Box<str> = Box::from("hello");
51436 fn from(s: &str) -> Box<str> {
51437 unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
51442 impl From<Cow<'_, str>> for Box<str> {
51444 fn from(cow: Cow<'_, str>) -> Box<str> {
51446 Cow::Borrowed(s) => Box::from(s),
51447 Cow::Owned(s) => Box::from(s),
51453 impl<A: Allocator> From<Box<str, A>> for Box<[u8], A> {
51454 /// Converts a `Box<str>` into a `Box<[u8]>`
51460 /// // create a Box<str> which will be used to create a Box<[u8]>
51461 /// let boxed: Box<str> = Box::from("hello");
51462 /// let boxed_str: Box<[u8]> = Box::from(boxed);
51464 /// // create a &[u8] which will be used to create a Box<[u8]>
51466 /// let boxed_slice = Box::from(slice);
51471 fn from(s: Box<str, A>) -> Self {
51472 let (raw, alloc) = Box::into_raw_with_allocator(s);
51473 unsafe { Box::from_raw_in(raw as *mut [u8], alloc) }
51478 impl<T, const N: usize> From<[T; N]> for Box<[T]> {
51479 /// Converts a `[T; N]` into a `Box<[T]>`
51485 /// let boxed: Box<[u8]> = Box::from([4, 2]);
51488 fn from(array: [T; N]) -> Box<[T]> {
51494 impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
51495 type Error = Box<[T]>;
51497 fn try_from(boxed_slice: Box<[T]>) -> Result<Self, Self::Error> {
51499 Ok(unsafe { Box::from_raw(Box::into_raw(boxed_slice) as *mut [T; N]) })
51506 impl<A: Allocator> Box<dyn Any, A> {
51516 /// fn print_if_string(value: Box<dyn Any>) {
51523 /// print_if_string(Box::new(my_string));
51524 /// print_if_string(Box::new(0i8));
51526 pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
51529 let (raw, alloc): (*mut dyn Any, _) = Box::into_raw_with_allocator(self);
51530 Ok(Box::from_raw_in(raw as *mut T, alloc))
51538 impl<A: Allocator> Box<dyn Any + Send, A> {
51548 /// fn print_if_string(value: Box<dyn Any + Send>) {
51555 /// print_if_string(Box::new(my_string));
51556 /// print_if_string(Box::new(0i8));
51558 pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
51561 let (raw, alloc): (*mut (dyn Any + Send), _) = Box::into_raw_with_allocator(self);
51562 Ok(Box::from_raw_in(raw as *mut T, alloc))
51570 impl<A: Allocator> Box<dyn Any + Send + Sync, A> {
51580 /// fn print_if_string(value: Box<dyn Any + Send + Sync>) {
51587 /// print_if_string(Box::new(my_string));
51588 /// print_if_string(Box::new(0i8));
51590 pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
51594 Box::into_raw_with_allocator(self);
51595 Ok(Box::from_raw_in(raw as *mut T, alloc))
51604 impl<T: fmt::Display + ?Sized, A: Allocator> fmt::Display for Box<T, A> {
51611 impl<T: fmt::Debug + ?Sized, A: Allocator> fmt::Debug for Box<T, A> {
51618 impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
51620 // It's not possible to extract the inner Uniq directly from the Box,
51628 impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
51637 impl<T: ?Sized, A: Allocator> DerefMut for Box<T, A> {
51644 impl<T: ?Sized, A: Allocator> Receiver for Box<T, A> {}
51647 impl<I: Iterator + ?Sized, A: Allocator> Iterator for Box<I, A> {
51668 impl<I: Iterator + ?Sized, A: Allocator> BoxIter for Box<I, A> {
51683 impl<I: Iterator, A: Allocator> BoxIter for Box<I, A> {
51690 impl<I: DoubleEndedIterator + ?Sized, A: Allocator> DoubleEndedIterator for Box<I, A> {
51699 impl<I: ExactSizeIterator + ?Sized, A: Allocator> ExactSizeIterator for Box<I, A> {
51709 impl<I: FusedIterator + ?Sized, A: Allocator> FusedIterator for Box<I, A> {}
51712 impl<Args, F: FnOnce<Args> + ?Sized, A: Allocator> FnOnce<Args> for Box<F, A> {
51721 impl<Args, F: FnMut<Args> + ?Sized, A: Allocator> FnMut<Args> for Box<F, A> {
51728 impl<Args, F: Fn<Args> + ?Sized, A: Allocator> Fn<Args> for Box<F, A> {
51735 impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}
51738 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T, Global> {}
51741 impl<I> FromIterator<I> for Box<[I]> {
51748 impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
51750 let alloc = Box::allocator(self).clone();
51764 impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for Box<T, A> {
51771 impl<T: ?Sized, A: Allocator> borrow::BorrowMut<T> for Box<T, A> {
51778 impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A> {
51785 impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
51794 * function of Pin<Box<T>> to Pin<T>. Such a function would not be sound,
51795 * because Box<T> implements Unpin even when T does not, as a result of
51802 * (Box<T> is the only pointer type in std for which this would be
51804 * - It is in practice very useful to have Box<T> be unconditionally
51806 * trait functionality does not apply (e.g., Box<dyn Foo> would
51809 * Another type with the same semantics as Box but only a conditional
51814 impl<T: ?Sized, A: Allocator> Unpin for Box<T, A> where A: 'static {}
51817 impl<G: ?Sized + Generator<R> + Unpin, R, A: Allocator> Generator<R> for Box<G, A>
51830 impl<G: ?Sized + Generator<R>, R, A: Allocator> Generator<R> for Pin<Box<G, A>>
51843 impl<F: ?Sized + Future + Unpin, A: Allocator> Future for Box<F, A>
51855 impl<S: ?Sized + Stream + Unpin> Stream for Box<S> {
51953 use std::boxed::Box;
51957 let a = Box::new(5);
51958 let b: Box<i32> = a.clone();
51967 let a = Box::new(8) as Box<dyn Any>;
51968 let b = Box::new(Test) as Box<dyn Any>;
51972 assert!(a == Box::new(8));
51978 assert!(a == Box::new(Test));
51983 let a = Box::new(8) as Box<dyn Any>;
51984 let b = Box::new(Test) as Box<dyn Any>;
51986 assert!(a.downcast::<Box<Test>>().is_err());
51987 assert!(b.downcast::<Box<i32>>().is_err());
51992 let a = Box::new(8) as Box<dyn Any>;
51993 let b = Box::new(Test) as Box<dyn Any>;
52012 homura(Box::new(765));
52017 let x = Box::new(17);
52018 let p = Box::into_raw(x);
52022 let y = Box::from_raw(p);
52046 let x: Box<dyn Foo> = Box::new(Bar(17));
52047 let p = Box::into_raw(x);
52051 let y: Box<dyn Foo> = Box::from_raw(p);
52059 let boxed: Box<[f64]> = Box::from(slice);
52066 let boxed: Box<[i64]> = Box::from(slice);
52073 let boxed: Box<str> = Box::from(s);
52080 let boxed: Box<[u32]> = iter.collect();
52088 let r: Box<[u32]> = v.into_boxed_slice();
52090 let a: Result<Box<[u32; 3]>, _> = r.clone().try_into();
52093 let a: Result<Box<[u32; 2]>, _> = r.clone().try_into();