boxed.rs (2612e3bbc0386368a850140a6c9b990cd496a5ec) | boxed.rs (89eed1ab1161e7d60595917e3b982e03dfcc0f8d) |
---|---|
1// SPDX-License-Identifier: Apache-2.0 OR MIT 2 3//! The `Box<T>` type for heap allocation. 4//! 5//! [`Box<T>`], casually referred to as a 'box', provides the simplest form of 6//! heap allocation in Rust. Boxes provide ownership for this allocation, and 7//! drop their contents when they go out of scope. Boxes also ensure that they 8//! never allocate more than `isize::MAX` bytes. --- 138 unchanged lines hidden (view full) --- 147//! [valid]: ptr#safety 148 149#![stable(feature = "rust1", since = "1.0.0")] 150 151use core::any::Any; 152use core::async_iter::AsyncIterator; 153use core::borrow; 154use core::cmp::Ordering; | 1// SPDX-License-Identifier: Apache-2.0 OR MIT 2 3//! The `Box<T>` type for heap allocation. 4//! 5//! [`Box<T>`], casually referred to as a 'box', provides the simplest form of 6//! heap allocation in Rust. Boxes provide ownership for this allocation, and 7//! drop their contents when they go out of scope. Boxes also ensure that they 8//! never allocate more than `isize::MAX` bytes. --- 138 unchanged lines hidden (view full) --- 147//! [valid]: ptr#safety 148 149#![stable(feature = "rust1", since = "1.0.0")] 150 151use core::any::Any; 152use core::async_iter::AsyncIterator; 153use core::borrow; 154use core::cmp::Ordering; |
155use core::convert::{From, TryFrom}; | |
156use core::error::Error; 157use core::fmt; 158use core::future::Future; 159use core::hash::{Hash, Hasher}; | 155use core::error::Error; 156use core::fmt; 157use core::future::Future; 158use core::hash::{Hash, Hasher}; |
160#[cfg(not(no_global_oom_handling))] 161use core::iter::FromIterator; 162use core::iter::{FusedIterator, Iterator}; | 159use core::iter::FusedIterator; |
163use core::marker::Tuple; | 160use core::marker::Tuple; |
164use core::marker::{Destruct, Unpin, Unsize}; | 161use core::marker::Unsize; |
165use core::mem; 166use core::ops::{ 167 CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver, 168}; 169use core::pin::Pin; 170use core::ptr::{self, Unique}; 171use core::task::{Context, Poll}; 172 --- 40 unchanged lines hidden (view full) --- 213 /// 214 /// ``` 215 /// let five = Box::new(5); 216 /// ``` 217 #[cfg(all(not(no_global_oom_handling)))] 218 #[inline(always)] 219 #[stable(feature = "rust1", since = "1.0.0")] 220 #[must_use] | 162use core::mem; 163use core::ops::{ 164 CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver, 165}; 166use core::pin::Pin; 167use core::ptr::{self, Unique}; 168use core::task::{Context, Poll}; 169 --- 40 unchanged lines hidden (view full) --- 210 /// 211 /// ``` 212 /// let five = Box::new(5); 213 /// ``` 214 #[cfg(all(not(no_global_oom_handling)))] 215 #[inline(always)] 216 #[stable(feature = "rust1", since = "1.0.0")] 217 #[must_use] |
218 #[rustc_diagnostic_item = "box_new"] |
|
221 pub fn new(x: T) -> Self { 222 #[rustc_box] 223 Box::new(x) 224 } 225 226 /// Constructs a new box with uninitialized contents. 227 /// 228 /// # Examples --- 53 unchanged lines hidden (view full) --- 282 /// does the same as <code>[Box::into_pin]\([Box::new]\(x))</code>. Consider using 283 /// [`into_pin`](Box::into_pin) if you already have a `Box<T>`, or if you want to 284 /// construct a (pinned) `Box` in a different way than with [`Box::new`]. 285 #[cfg(not(no_global_oom_handling))] 286 #[stable(feature = "pin", since = "1.33.0")] 287 #[must_use] 288 #[inline(always)] 289 pub fn pin(x: T) -> Pin<Box<T>> { | 219 pub fn new(x: T) -> Self { 220 #[rustc_box] 221 Box::new(x) 222 } 223 224 /// Constructs a new box with uninitialized contents. 225 /// 226 /// # Examples --- 53 unchanged lines hidden (view full) --- 280 /// does the same as <code>[Box::into_pin]\([Box::new]\(x))</code>. Consider using 281 /// [`into_pin`](Box::into_pin) if you already have a `Box<T>`, or if you want to 282 /// construct a (pinned) `Box` in a different way than with [`Box::new`]. 283 #[cfg(not(no_global_oom_handling))] 284 #[stable(feature = "pin", since = "1.33.0")] 285 #[must_use] 286 #[inline(always)] 287 pub fn pin(x: T) -> Pin<Box<T>> { |
290 (#[rustc_box] 291 Box::new(x)) 292 .into() | 288 Box::new(x).into() |
293 } 294 295 /// Allocates memory on the heap then places `x` into it, 296 /// returning an error if the allocation fails 297 /// 298 /// This doesn't actually allocate if `T` is zero-sized. 299 /// 300 /// # Examples --- 75 unchanged lines hidden (view full) --- 376 /// #![feature(allocator_api)] 377 /// 378 /// use std::alloc::System; 379 /// 380 /// let five = Box::new_in(5, System); 381 /// ``` 382 #[cfg(not(no_global_oom_handling))] 383 #[unstable(feature = "allocator_api", issue = "32838")] | 289 } 290 291 /// Allocates memory on the heap then places `x` into it, 292 /// returning an error if the allocation fails 293 /// 294 /// This doesn't actually allocate if `T` is zero-sized. 295 /// 296 /// # Examples --- 75 unchanged lines hidden (view full) --- 372 /// #![feature(allocator_api)] 373 /// 374 /// use std::alloc::System; 375 /// 376 /// let five = Box::new_in(5, System); 377 /// ``` 378 #[cfg(not(no_global_oom_handling))] 379 #[unstable(feature = "allocator_api", issue = "32838")] |
384 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
385 #[must_use] 386 #[inline] | 380 #[must_use] 381 #[inline] |
387 pub const fn new_in(x: T, alloc: A) -> Self | 382 pub fn new_in(x: T, alloc: A) -> Self |
388 where | 383 where |
389 A: ~const Allocator + ~const Destruct, | 384 A: Allocator, |
390 { 391 let mut boxed = Self::new_uninit_in(alloc); 392 unsafe { 393 boxed.as_mut_ptr().write(x); 394 boxed.assume_init() 395 } 396 } 397 --- 8 unchanged lines hidden (view full) --- 406 /// #![feature(allocator_api)] 407 /// 408 /// use std::alloc::System; 409 /// 410 /// let five = Box::try_new_in(5, System)?; 411 /// # Ok::<(), std::alloc::AllocError>(()) 412 /// ``` 413 #[unstable(feature = "allocator_api", issue = "32838")] | 385 { 386 let mut boxed = Self::new_uninit_in(alloc); 387 unsafe { 388 boxed.as_mut_ptr().write(x); 389 boxed.assume_init() 390 } 391 } 392 --- 8 unchanged lines hidden (view full) --- 401 /// #![feature(allocator_api)] 402 /// 403 /// use std::alloc::System; 404 /// 405 /// let five = Box::try_new_in(5, System)?; 406 /// # Ok::<(), std::alloc::AllocError>(()) 407 /// ``` 408 #[unstable(feature = "allocator_api", issue = "32838")] |
414 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
415 #[inline] | 409 #[inline] |
416 pub const fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError> | 410 pub fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError> |
417 where | 411 where |
418 T: ~const Destruct, 419 A: ~const Allocator + ~const Destruct, | 412 A: Allocator, |
420 { 421 let mut boxed = Self::try_new_uninit_in(alloc)?; 422 unsafe { 423 boxed.as_mut_ptr().write(x); 424 Ok(boxed.assume_init()) 425 } 426 } 427 --- 13 unchanged lines hidden (view full) --- 441 /// five.as_mut_ptr().write(5); 442 /// 443 /// five.assume_init() 444 /// }; 445 /// 446 /// assert_eq!(*five, 5) 447 /// ``` 448 #[unstable(feature = "allocator_api", issue = "32838")] | 413 { 414 let mut boxed = Self::try_new_uninit_in(alloc)?; 415 unsafe { 416 boxed.as_mut_ptr().write(x); 417 Ok(boxed.assume_init()) 418 } 419 } 420 --- 13 unchanged lines hidden (view full) --- 434 /// five.as_mut_ptr().write(5); 435 /// 436 /// five.assume_init() 437 /// }; 438 /// 439 /// assert_eq!(*five, 5) 440 /// ``` 441 #[unstable(feature = "allocator_api", issue = "32838")] |
449 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
450 #[cfg(not(no_global_oom_handling))] 451 #[must_use] 452 // #[unstable(feature = "new_uninit", issue = "63291")] | 442 #[cfg(not(no_global_oom_handling))] 443 #[must_use] 444 // #[unstable(feature = "new_uninit", issue = "63291")] |
453 pub const fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> | 445 pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> |
454 where | 446 where |
455 A: ~const Allocator + ~const Destruct, | 447 A: Allocator, |
456 { 457 let layout = Layout::new::<mem::MaybeUninit<T>>(); 458 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable. 459 // That would make code size bigger. 460 match Box::try_new_uninit_in(alloc) { 461 Ok(m) => m, 462 Err(_) => handle_alloc_error(layout), 463 } --- 18 unchanged lines hidden (view full) --- 482 /// five.assume_init() 483 /// }; 484 /// 485 /// assert_eq!(*five, 5); 486 /// # Ok::<(), std::alloc::AllocError>(()) 487 /// ``` 488 #[unstable(feature = "allocator_api", issue = "32838")] 489 // #[unstable(feature = "new_uninit", issue = "63291")] | 448 { 449 let layout = Layout::new::<mem::MaybeUninit<T>>(); 450 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable. 451 // That would make code size bigger. 452 match Box::try_new_uninit_in(alloc) { 453 Ok(m) => m, 454 Err(_) => handle_alloc_error(layout), 455 } --- 18 unchanged lines hidden (view full) --- 474 /// five.assume_init() 475 /// }; 476 /// 477 /// assert_eq!(*five, 5); 478 /// # Ok::<(), std::alloc::AllocError>(()) 479 /// ``` 480 #[unstable(feature = "allocator_api", issue = "32838")] 481 // #[unstable(feature = "new_uninit", issue = "63291")] |
490 #[rustc_const_unstable(feature = "const_box", issue = "92521")] 491 pub const fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError> | 482 pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError> |
492 where | 483 where |
493 A: ~const Allocator + ~const Destruct, | 484 A: Allocator, |
494 { 495 let layout = Layout::new::<mem::MaybeUninit<T>>(); 496 let ptr = alloc.allocate(layout)?.cast(); 497 unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) } 498 } 499 500 /// Constructs a new `Box` with uninitialized contents, with the memory 501 /// being filled with `0` bytes in the provided allocator. --- 11 unchanged lines hidden (view full) --- 513 /// let zero = Box::<u32, _>::new_zeroed_in(System); 514 /// let zero = unsafe { zero.assume_init() }; 515 /// 516 /// assert_eq!(*zero, 0) 517 /// ``` 518 /// 519 /// [zeroed]: mem::MaybeUninit::zeroed 520 #[unstable(feature = "allocator_api", issue = "32838")] | 485 { 486 let layout = Layout::new::<mem::MaybeUninit<T>>(); 487 let ptr = alloc.allocate(layout)?.cast(); 488 unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) } 489 } 490 491 /// Constructs a new `Box` with uninitialized contents, with the memory 492 /// being filled with `0` bytes in the provided allocator. --- 11 unchanged lines hidden (view full) --- 504 /// let zero = Box::<u32, _>::new_zeroed_in(System); 505 /// let zero = unsafe { zero.assume_init() }; 506 /// 507 /// assert_eq!(*zero, 0) 508 /// ``` 509 /// 510 /// [zeroed]: mem::MaybeUninit::zeroed 511 #[unstable(feature = "allocator_api", issue = "32838")] |
521 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
522 #[cfg(not(no_global_oom_handling))] 523 // #[unstable(feature = "new_uninit", issue = "63291")] 524 #[must_use] | 512 #[cfg(not(no_global_oom_handling))] 513 // #[unstable(feature = "new_uninit", issue = "63291")] 514 #[must_use] |
525 pub const fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> | 515 pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> |
526 where | 516 where |
527 A: ~const Allocator + ~const Destruct, | 517 A: Allocator, |
528 { 529 let layout = Layout::new::<mem::MaybeUninit<T>>(); 530 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable. 531 // That would make code size bigger. 532 match Box::try_new_zeroed_in(alloc) { 533 Ok(m) => m, 534 Err(_) => handle_alloc_error(layout), 535 } --- 18 unchanged lines hidden (view full) --- 554 /// 555 /// assert_eq!(*zero, 0); 556 /// # Ok::<(), std::alloc::AllocError>(()) 557 /// ``` 558 /// 559 /// [zeroed]: mem::MaybeUninit::zeroed 560 #[unstable(feature = "allocator_api", issue = "32838")] 561 // #[unstable(feature = "new_uninit", issue = "63291")] | 518 { 519 let layout = Layout::new::<mem::MaybeUninit<T>>(); 520 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable. 521 // That would make code size bigger. 522 match Box::try_new_zeroed_in(alloc) { 523 Ok(m) => m, 524 Err(_) => handle_alloc_error(layout), 525 } --- 18 unchanged lines hidden (view full) --- 544 /// 545 /// assert_eq!(*zero, 0); 546 /// # Ok::<(), std::alloc::AllocError>(()) 547 /// ``` 548 /// 549 /// [zeroed]: mem::MaybeUninit::zeroed 550 #[unstable(feature = "allocator_api", issue = "32838")] 551 // #[unstable(feature = "new_uninit", issue = "63291")] |
562 #[rustc_const_unstable(feature = "const_box", issue = "92521")] 563 pub const fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError> | 552 pub fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError> |
564 where | 553 where |
565 A: ~const Allocator + ~const Destruct, | 554 A: Allocator, |
566 { 567 let layout = Layout::new::<mem::MaybeUninit<T>>(); 568 let ptr = alloc.allocate_zeroed(layout)?.cast(); 569 unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) } 570 } 571 572 /// Constructs a new `Pin<Box<T, A>>`. If `T` does not implement [`Unpin`], then 573 /// `x` will be pinned in memory and unable to be moved. 574 /// 575 /// Constructing and pinning of the `Box` can also be done in two steps: `Box::pin_in(x, alloc)` 576 /// does the same as <code>[Box::into_pin]\([Box::new_in]\(x, alloc))</code>. Consider using 577 /// [`into_pin`](Box::into_pin) if you already have a `Box<T, A>`, or if you want to 578 /// construct a (pinned) `Box` in a different way than with [`Box::new_in`]. 579 #[cfg(not(no_global_oom_handling))] 580 #[unstable(feature = "allocator_api", issue = "32838")] | 555 { 556 let layout = Layout::new::<mem::MaybeUninit<T>>(); 557 let ptr = alloc.allocate_zeroed(layout)?.cast(); 558 unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) } 559 } 560 561 /// Constructs a new `Pin<Box<T, A>>`. If `T` does not implement [`Unpin`], then 562 /// `x` will be pinned in memory and unable to be moved. 563 /// 564 /// Constructing and pinning of the `Box` can also be done in two steps: `Box::pin_in(x, alloc)` 565 /// does the same as <code>[Box::into_pin]\([Box::new_in]\(x, alloc))</code>. Consider using 566 /// [`into_pin`](Box::into_pin) if you already have a `Box<T, A>`, or if you want to 567 /// construct a (pinned) `Box` in a different way than with [`Box::new_in`]. 568 #[cfg(not(no_global_oom_handling))] 569 #[unstable(feature = "allocator_api", issue = "32838")] |
581 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
582 #[must_use] 583 #[inline(always)] | 570 #[must_use] 571 #[inline(always)] |
584 pub const fn pin_in(x: T, alloc: A) -> Pin<Self> | 572 pub fn pin_in(x: T, alloc: A) -> Pin<Self> |
585 where | 573 where |
586 A: 'static + ~const Allocator + ~const Destruct, | 574 A: 'static + Allocator, |
587 { 588 Self::into_pin(Self::new_in(x, alloc)) 589 } 590 591 /// Converts a `Box<T>` into a `Box<[T]>` 592 /// 593 /// This conversion does not allocate on the heap and happens in place. 594 #[unstable(feature = "box_into_boxed_slice", issue = "71582")] | 575 { 576 Self::into_pin(Self::new_in(x, alloc)) 577 } 578 579 /// Converts a `Box<T>` into a `Box<[T]>` 580 /// 581 /// This conversion does not allocate on the heap and happens in place. 582 #[unstable(feature = "box_into_boxed_slice", issue = "71582")] |
595 #[rustc_const_unstable(feature = "const_box", issue = "92521")] 596 pub const fn into_boxed_slice(boxed: Self) -> Box<[T], A> { | 583 pub fn into_boxed_slice(boxed: Self) -> Box<[T], A> { |
597 let (raw, alloc) = Box::into_raw_with_allocator(boxed); 598 unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) } 599 } 600 601 /// Consumes the `Box`, returning the wrapped value. 602 /// 603 /// # Examples 604 /// 605 /// ``` 606 /// #![feature(box_into_inner)] 607 /// 608 /// let c = Box::new(5); 609 /// 610 /// assert_eq!(Box::into_inner(c), 5); 611 /// ``` 612 #[unstable(feature = "box_into_inner", issue = "80437")] | 584 let (raw, alloc) = Box::into_raw_with_allocator(boxed); 585 unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) } 586 } 587 588 /// Consumes the `Box`, returning the wrapped value. 589 /// 590 /// # Examples 591 /// 592 /// ``` 593 /// #![feature(box_into_inner)] 594 /// 595 /// let c = Box::new(5); 596 /// 597 /// assert_eq!(Box::into_inner(c), 5); 598 /// ``` 599 #[unstable(feature = "box_into_inner", issue = "80437")] |
613 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
614 #[inline] | 600 #[inline] |
615 pub const fn into_inner(boxed: Self) -> T 616 where 617 Self: ~const Destruct, 618 { | 601 pub fn into_inner(boxed: Self) -> T { |
619 *boxed 620 } 621} 622 623impl<T> Box<[T]> { 624 /// Constructs a new boxed slice with uninitialized contents. 625 /// 626 /// # Examples --- 197 unchanged lines hidden (view full) --- 824 /// five.as_mut_ptr().write(5); 825 /// 826 /// five.assume_init() 827 /// }; 828 /// 829 /// assert_eq!(*five, 5) 830 /// ``` 831 #[unstable(feature = "new_uninit", issue = "63291")] | 602 *boxed 603 } 604} 605 606impl<T> Box<[T]> { 607 /// Constructs a new boxed slice with uninitialized contents. 608 /// 609 /// # Examples --- 197 unchanged lines hidden (view full) --- 807 /// five.as_mut_ptr().write(5); 808 /// 809 /// five.assume_init() 810 /// }; 811 /// 812 /// assert_eq!(*five, 5) 813 /// ``` 814 #[unstable(feature = "new_uninit", issue = "63291")] |
832 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
833 #[inline] | 815 #[inline] |
834 pub const unsafe fn assume_init(self) -> Box<T, A> { | 816 pub unsafe fn assume_init(self) -> Box<T, A> { |
835 let (raw, alloc) = Box::into_raw_with_allocator(self); 836 unsafe { Box::from_raw_in(raw as *mut T, alloc) } 837 } 838 839 /// Writes the value and converts to `Box<T, A>`. 840 /// 841 /// This method converts the box similarly to [`Box::assume_init`] but 842 /// writes `value` into it before conversion thus guaranteeing safety. --- 16 unchanged lines hidden (view full) --- 859 /// // to heap directly. 860 /// let big_box = Box::write(big_box, array); 861 /// 862 /// for (i, x) in big_box.iter().enumerate() { 863 /// assert_eq!(*x, i); 864 /// } 865 /// ``` 866 #[unstable(feature = "new_uninit", issue = "63291")] | 817 let (raw, alloc) = Box::into_raw_with_allocator(self); 818 unsafe { Box::from_raw_in(raw as *mut T, alloc) } 819 } 820 821 /// Writes the value and converts to `Box<T, A>`. 822 /// 823 /// This method converts the box similarly to [`Box::assume_init`] but 824 /// writes `value` into it before conversion thus guaranteeing safety. --- 16 unchanged lines hidden (view full) --- 841 /// // to heap directly. 842 /// let big_box = Box::write(big_box, array); 843 /// 844 /// for (i, x) in big_box.iter().enumerate() { 845 /// assert_eq!(*x, i); 846 /// } 847 /// ``` 848 #[unstable(feature = "new_uninit", issue = "63291")] |
867 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
868 #[inline] | 849 #[inline] |
869 pub const fn write(mut boxed: Self, value: T) -> Box<T, A> { | 850 pub fn write(mut boxed: Self, value: T) -> Box<T, A> { |
870 unsafe { 871 (*boxed).write(value); 872 boxed.assume_init() 873 } 874 } 875} 876 877impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> { --- 227 unchanged lines hidden (view full) --- 1105 /// ptr::drop_in_place(ptr); 1106 /// let non_null = NonNull::new_unchecked(ptr); 1107 /// alloc.deallocate(non_null.cast(), Layout::new::<String>()); 1108 /// } 1109 /// ``` 1110 /// 1111 /// [memory layout]: self#memory-layout 1112 #[unstable(feature = "allocator_api", issue = "32838")] | 851 unsafe { 852 (*boxed).write(value); 853 boxed.assume_init() 854 } 855 } 856} 857 858impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> { --- 227 unchanged lines hidden (view full) --- 1086 /// ptr::drop_in_place(ptr); 1087 /// let non_null = NonNull::new_unchecked(ptr); 1088 /// alloc.deallocate(non_null.cast(), Layout::new::<String>()); 1089 /// } 1090 /// ``` 1091 /// 1092 /// [memory layout]: self#memory-layout 1093 #[unstable(feature = "allocator_api", issue = "32838")] |
1113 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
1114 #[inline] | 1094 #[inline] |
1115 pub const fn into_raw_with_allocator(b: Self) -> (*mut T, A) { | 1095 pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) { |
1116 let (leaked, alloc) = Box::into_unique(b); 1117 (leaked.as_ptr(), alloc) 1118 } 1119 1120 #[unstable( 1121 feature = "ptr_internals", 1122 issue = "none", 1123 reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead" 1124 )] | 1096 let (leaked, alloc) = Box::into_unique(b); 1097 (leaked.as_ptr(), alloc) 1098 } 1099 1100 #[unstable( 1101 feature = "ptr_internals", 1102 issue = "none", 1103 reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead" 1104 )] |
1125 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
1126 #[inline] 1127 #[doc(hidden)] | 1105 #[inline] 1106 #[doc(hidden)] |
1128 pub const fn into_unique(b: Self) -> (Unique<T>, A) { | 1107 pub fn into_unique(b: Self) -> (Unique<T>, A) { |
1129 // Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a 1130 // raw pointer for the type system. Turning it directly into a raw pointer would not be 1131 // recognized as "releasing" the unique pointer to permit aliased raw accesses, 1132 // so all raw pointer methods have to go through `Box::leak`. Turning *that* to a raw pointer 1133 // behaves correctly. 1134 let alloc = unsafe { ptr::read(&b.1) }; 1135 (Unique::from(Box::leak(b)), alloc) 1136 } --- 41 unchanged lines hidden (view full) --- 1178 /// 1179 /// ``` 1180 /// let x = vec![1, 2, 3].into_boxed_slice(); 1181 /// let static_ref = Box::leak(x); 1182 /// static_ref[0] = 4; 1183 /// assert_eq!(*static_ref, [4, 2, 3]); 1184 /// ``` 1185 #[stable(feature = "box_leak", since = "1.26.0")] | 1108 // Box is recognized as a "unique pointer" by Stacked Borrows, but internally it is a 1109 // raw pointer for the type system. Turning it directly into a raw pointer would not be 1110 // recognized as "releasing" the unique pointer to permit aliased raw accesses, 1111 // so all raw pointer methods have to go through `Box::leak`. Turning *that* to a raw pointer 1112 // behaves correctly. 1113 let alloc = unsafe { ptr::read(&b.1) }; 1114 (Unique::from(Box::leak(b)), alloc) 1115 } --- 41 unchanged lines hidden (view full) --- 1157 /// 1158 /// ``` 1159 /// let x = vec![1, 2, 3].into_boxed_slice(); 1160 /// let static_ref = Box::leak(x); 1161 /// static_ref[0] = 4; 1162 /// assert_eq!(*static_ref, [4, 2, 3]); 1163 /// ``` 1164 #[stable(feature = "box_leak", since = "1.26.0")] |
1186 #[rustc_const_unstable(feature = "const_box", issue = "92521")] | |
1187 #[inline] | 1165 #[inline] |
1188 pub const fn leak<'a>(b: Self) -> &'a mut T | 1166 pub fn leak<'a>(b: Self) -> &'a mut T |
1189 where 1190 A: 'a, 1191 { 1192 unsafe { &mut *mem::ManuallyDrop::new(b).0.as_ptr() } 1193 } 1194 1195 /// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then 1196 /// `*boxed` will be pinned in memory and unable to be moved. --- 44 unchanged lines hidden (view full) --- 1241 // FIXME: Do nothing, drop is currently performed by compiler. 1242 } 1243} 1244 1245#[cfg(not(no_global_oom_handling))] 1246#[stable(feature = "rust1", since = "1.0.0")] 1247impl<T: Default> Default for Box<T> { 1248 /// Creates a `Box<T>`, with the `Default` value for T. | 1167 where 1168 A: 'a, 1169 { 1170 unsafe { &mut *mem::ManuallyDrop::new(b).0.as_ptr() } 1171 } 1172 1173 /// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then 1174 /// `*boxed` will be pinned in memory and unable to be moved. --- 44 unchanged lines hidden (view full) --- 1219 // FIXME: Do nothing, drop is currently performed by compiler. 1220 } 1221} 1222 1223#[cfg(not(no_global_oom_handling))] 1224#[stable(feature = "rust1", since = "1.0.0")] 1225impl<T: Default> Default for Box<T> { 1226 /// Creates a `Box<T>`, with the `Default` value for T. |
1227 #[inline] |
|
1249 fn default() -> Self { | 1228 fn default() -> Self { |
1250 #[rustc_box] | |
1251 Box::new(T::default()) 1252 } 1253} 1254 1255#[cfg(not(no_global_oom_handling))] 1256#[stable(feature = "rust1", since = "1.0.0")] | 1229 Box::new(T::default()) 1230 } 1231} 1232 1233#[cfg(not(no_global_oom_handling))] 1234#[stable(feature = "rust1", since = "1.0.0")] |
1257#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")] 1258impl<T> const Default for Box<[T]> { | 1235impl<T> Default for Box<[T]> { 1236 #[inline] |
1259 fn default() -> Self { 1260 let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling(); 1261 Box(ptr, Global) 1262 } 1263} 1264 1265#[cfg(not(no_global_oom_handling))] 1266#[stable(feature = "default_box_extra", since = "1.17.0")] | 1237 fn default() -> Self { 1238 let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling(); 1239 Box(ptr, Global) 1240 } 1241} 1242 1243#[cfg(not(no_global_oom_handling))] 1244#[stable(feature = "default_box_extra", since = "1.17.0")] |
1267#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")] 1268impl const Default for Box<str> { | 1245impl Default for Box<str> { 1246 #[inline] |
1269 fn default() -> Self { 1270 // SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`. 1271 let ptr: Unique<str> = unsafe { 1272 let bytes: Unique<[u8]> = Unique::<[u8; 0]>::dangling(); 1273 Unique::new_unchecked(bytes.as_ptr() as *mut str) 1274 }; 1275 Box(ptr, Global) 1276 } --- 179 unchanged lines hidden (view full) --- 1456 /// assert_eq!(Box::from(x), boxed); 1457 /// ``` 1458 fn from(t: T) -> Self { 1459 Box::new(t) 1460 } 1461} 1462 1463#[stable(feature = "pin", since = "1.33.0")] | 1247 fn default() -> Self { 1248 // SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`. 1249 let ptr: Unique<str> = unsafe { 1250 let bytes: Unique<[u8]> = Unique::<[u8; 0]>::dangling(); 1251 Unique::new_unchecked(bytes.as_ptr() as *mut str) 1252 }; 1253 Box(ptr, Global) 1254 } --- 179 unchanged lines hidden (view full) --- 1434 /// assert_eq!(Box::from(x), boxed); 1435 /// ``` 1436 fn from(t: T) -> Self { 1437 Box::new(t) 1438 } 1439} 1440 1441#[stable(feature = "pin", since = "1.33.0")] |
1464#[rustc_const_unstable(feature = "const_box", issue = "92521")] 1465impl<T: ?Sized, A: Allocator> const From<Box<T, A>> for Pin<Box<T, A>> | 1442impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Pin<Box<T, A>> |
1466where 1467 A: 'static, 1468{ 1469 /// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then 1470 /// `*boxed` will be pinned in memory and unable to be moved. 1471 /// 1472 /// This conversion does not allocate on the heap and happens in place. 1473 /// 1474 /// This is also available via [`Box::into_pin`]. 1475 /// 1476 /// Constructing and pinning a `Box` with <code><Pin<Box\<T>>>::from([Box::new]\(x))</code> 1477 /// can also be written more concisely using <code>[Box::pin]\(x)</code>. 1478 /// This `From` implementation is useful if you already have a `Box<T>`, or you are 1479 /// constructing a (pinned) `Box` in a different way than with [`Box::new`]. 1480 fn from(boxed: Box<T, A>) -> Self { 1481 Box::into_pin(boxed) 1482 } 1483} 1484 | 1443where 1444 A: 'static, 1445{ 1446 /// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then 1447 /// `*boxed` will be pinned in memory and unable to be moved. 1448 /// 1449 /// This conversion does not allocate on the heap and happens in place. 1450 /// 1451 /// This is also available via [`Box::into_pin`]. 1452 /// 1453 /// Constructing and pinning a `Box` with <code><Pin<Box\<T>>>::from([Box::new]\(x))</code> 1454 /// can also be written more concisely using <code>[Box::pin]\(x)</code>. 1455 /// This `From` implementation is useful if you already have a `Box<T>`, or you are 1456 /// constructing a (pinned) `Box` in a different way than with [`Box::new`]. 1457 fn from(boxed: Box<T, A>) -> Self { 1458 Box::into_pin(boxed) 1459 } 1460} 1461 |
1462/// Specialization trait used for `From<&[T]>`. |
|
1485#[cfg(not(no_global_oom_handling))] | 1463#[cfg(not(no_global_oom_handling))] |
1464trait BoxFromSlice<T> { 1465 fn from_slice(slice: &[T]) -> Self; 1466} 1467 1468#[cfg(not(no_global_oom_handling))] 1469impl<T: Clone> BoxFromSlice<T> for Box<[T]> { 1470 #[inline] 1471 default fn from_slice(slice: &[T]) -> Self { 1472 slice.to_vec().into_boxed_slice() 1473 } 1474} 1475 1476#[cfg(not(no_global_oom_handling))] 1477impl<T: Copy> BoxFromSlice<T> for Box<[T]> { 1478 #[inline] 1479 fn from_slice(slice: &[T]) -> Self { 1480 let len = slice.len(); 1481 let buf = RawVec::with_capacity(len); 1482 unsafe { 1483 ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len); 1484 buf.into_box(slice.len()).assume_init() 1485 } 1486 } 1487} 1488 1489#[cfg(not(no_global_oom_handling))] |
|
1486#[stable(feature = "box_from_slice", since = "1.17.0")] | 1490#[stable(feature = "box_from_slice", since = "1.17.0")] |
1487impl<T: Copy> From<&[T]> for Box<[T]> { | 1491impl<T: Clone> From<&[T]> for Box<[T]> { |
1488 /// Converts a `&[T]` into a `Box<[T]>` 1489 /// 1490 /// This conversion allocates on the heap 1491 /// and performs a copy of `slice` and its contents. 1492 /// 1493 /// # Examples 1494 /// ```rust 1495 /// // create a &[u8] which will be used to create a Box<[u8]> 1496 /// let slice: &[u8] = &[104, 101, 108, 108, 111]; 1497 /// let boxed_slice: Box<[u8]> = Box::from(slice); 1498 /// 1499 /// println!("{boxed_slice:?}"); 1500 /// ``` | 1492 /// Converts a `&[T]` into a `Box<[T]>` 1493 /// 1494 /// This conversion allocates on the heap 1495 /// and performs a copy of `slice` and its contents. 1496 /// 1497 /// # Examples 1498 /// ```rust 1499 /// // create a &[u8] which will be used to create a Box<[u8]> 1500 /// let slice: &[u8] = &[104, 101, 108, 108, 111]; 1501 /// let boxed_slice: Box<[u8]> = Box::from(slice); 1502 /// 1503 /// println!("{boxed_slice:?}"); 1504 /// ``` |
1505 #[inline] |
|
1501 fn from(slice: &[T]) -> Box<[T]> { | 1506 fn from(slice: &[T]) -> Box<[T]> { |
1502 let len = slice.len(); 1503 let buf = RawVec::with_capacity(len); 1504 unsafe { 1505 ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len); 1506 buf.into_box(slice.len()).assume_init() 1507 } | 1507 <Self as BoxFromSlice<T>>::from_slice(slice) |
1508 } 1509} 1510 1511#[cfg(not(no_global_oom_handling))] 1512#[stable(feature = "box_from_cow", since = "1.45.0")] | 1508 } 1509} 1510 1511#[cfg(not(no_global_oom_handling))] 1512#[stable(feature = "box_from_cow", since = "1.45.0")] |
1513impl<T: Copy> From<Cow<'_, [T]>> for Box<[T]> { | 1513impl<T: Clone> From<Cow<'_, [T]>> for Box<[T]> { |
1514 /// Converts a `Cow<'_, [T]>` into a `Box<[T]>` 1515 /// 1516 /// When `cow` is the `Cow::Borrowed` variant, this 1517 /// conversion allocates on the heap and copies the 1518 /// underlying slice. Otherwise, it will try to reuse the owned 1519 /// `Vec`'s allocation. 1520 #[inline] 1521 fn from(cow: Cow<'_, [T]>) -> Box<[T]> { --- 93 unchanged lines hidden (view full) --- 1615 /// 1616 /// # Examples 1617 /// 1618 /// ```rust 1619 /// let boxed: Box<[u8]> = Box::from([4, 2]); 1620 /// println!("{boxed:?}"); 1621 /// ``` 1622 fn from(array: [T; N]) -> Box<[T]> { | 1514 /// Converts a `Cow<'_, [T]>` into a `Box<[T]>` 1515 /// 1516 /// When `cow` is the `Cow::Borrowed` variant, this 1517 /// conversion allocates on the heap and copies the 1518 /// underlying slice. Otherwise, it will try to reuse the owned 1519 /// `Vec`'s allocation. 1520 #[inline] 1521 fn from(cow: Cow<'_, [T]>) -> Box<[T]> { --- 93 unchanged lines hidden (view full) --- 1615 /// 1616 /// # Examples 1617 /// 1618 /// ```rust 1619 /// let boxed: Box<[u8]> = Box::from([4, 2]); 1620 /// println!("{boxed:?}"); 1621 /// ``` 1622 fn from(array: [T; N]) -> Box<[T]> { |
1623 #[rustc_box] | |
1624 Box::new(array) 1625 } 1626} 1627 1628/// Casts a boxed slice to a boxed array. 1629/// 1630/// # Safety 1631/// --- 262 unchanged lines hidden (view full) --- 1894 // It's not possible to extract the inner Uniq directly from the Box, 1895 // instead we cast it to a *const which aliases the Unique 1896 let ptr: *const T = &**self; 1897 fmt::Pointer::fmt(&ptr, f) 1898 } 1899} 1900 1901#[stable(feature = "rust1", since = "1.0.0")] | 1623 Box::new(array) 1624 } 1625} 1626 1627/// Casts a boxed slice to a boxed array. 1628/// 1629/// # Safety 1630/// --- 262 unchanged lines hidden (view full) --- 1893 // It's not possible to extract the inner Uniq directly from the Box, 1894 // instead we cast it to a *const which aliases the Unique 1895 let ptr: *const T = &**self; 1896 fmt::Pointer::fmt(&ptr, f) 1897 } 1898} 1899 1900#[stable(feature = "rust1", since = "1.0.0")] |
1902#[rustc_const_unstable(feature = "const_box", issue = "92521")] 1903impl<T: ?Sized, A: Allocator> const Deref for Box<T, A> { | 1901impl<T: ?Sized, A: Allocator> Deref for Box<T, A> { |
1904 type Target = T; 1905 1906 fn deref(&self) -> &T { 1907 &**self 1908 } 1909} 1910 1911#[stable(feature = "rust1", since = "1.0.0")] | 1902 type Target = T; 1903 1904 fn deref(&self) -> &T { 1905 &**self 1906 } 1907} 1908 1909#[stable(feature = "rust1", since = "1.0.0")] |
1912#[rustc_const_unstable(feature = "const_box", issue = "92521")] 1913impl<T: ?Sized, A: Allocator> const DerefMut for Box<T, A> { | 1910impl<T: ?Sized, A: Allocator> DerefMut for Box<T, A> { |
1914 fn deref_mut(&mut self) -> &mut T { 1915 &mut **self 1916 } 1917} 1918 1919#[unstable(feature = "receiver_trait", issue = "none")] 1920impl<T: ?Sized, A: Allocator> Receiver for Box<T, A> {} 1921 --- 511 unchanged lines hidden --- | 1911 fn deref_mut(&mut self) -> &mut T { 1912 &mut **self 1913 } 1914} 1915 1916#[unstable(feature = "receiver_trait", issue = "none")] 1917impl<T: ?Sized, A: Allocator> Receiver for Box<T, A> {} 1918 --- 511 unchanged lines hidden --- |