| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 |
|
| 135 | #![stable(feature = "rust1", since = "1.0.0")]
|
| 136 |
|
| 137 | use core::any::Any;
|
| 138 | use core::async_iter::AsyncIterator;
|
| 139 | use core::borrow;
|
| 140 | use core::cmp::Ordering;
|
| 141 | use core::convert::{From, TryFrom};
|
| 142 | use core::fmt;
|
| 143 | use core::future::Future;
|
| 144 | use core::hash::{Hash, Hasher};
|
| 145 | #[cfg(not(no_global_oom_handling))]
|
| 146 | use core::iter::FromIterator;
|
| 147 | use core::iter::{FusedIterator, Iterator};
|
| 148 | use core::marker::{Destruct, Unpin, Unsize};
|
| 149 | use core::mem;
|
| 150 | use core::ops::{
|
| 151 | CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver,
|
| 152 | };
|
| 153 | use core::pin::Pin;
|
| 154 | use core::ptr::{self, Unique};
|
| 155 | use core::task::{Context, Poll};
|
| 156 |
|
| 157 | #[cfg(not(no_global_oom_handling))]
|
| 158 | use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
|
| 159 | use crate::alloc::{AllocError, Allocator, Global, Layout};
|
| 160 | #[cfg(not(no_global_oom_handling))]
|
| 161 | use crate::borrow::Cow;
|
| 162 | use crate::raw_vec::RawVec;
|
| 163 | #[cfg(not(no_global_oom_handling))]
|
| 164 | use crate::str::from_boxed_utf8_unchecked;
|
| 165 | #[cfg(not(no_global_oom_handling))]
|
| 166 | use crate::vec::Vec;
|
| 167 |
|
| 168 | #[cfg(not(no_thin))]
|
| 169 | #[unstable(feature = "thin_box", issue = "92791")]
|
| 170 | pub use thin::ThinBox;
|
| 171 |
|
| 172 | #[cfg(not(no_thin))]
|
| 173 | mod thin;
|
| 174 |
|
| 175 | |
| 176 | |
| 177 | |
| 178 | #[lang = "owned_box"]
|
| 179 | #[fundamental]
|
| 180 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 181 | |
| 182 | |
| 183 | |
| 184 | pub struct Box<
|
| 185 | T: ?Sized,
|
| 186 | #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
|
| 187 | >(Unique<T>, A);
|
| 188 |
|
| 189 | impl<T> Box<T> {
|
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | #[cfg(not(no_global_oom_handling))]
|
| 200 | #[inline(always)]
|
| 201 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 202 | #[must_use]
|
| 203 | pub fn new(x: T) -> Self {
|
| 204 | box x
|
| 205 | }
|
| 206 |
|
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | #[cfg(not(no_global_oom_handling))]
|
| 226 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 227 | #[must_use]
|
| 228 | #[inline]
|
| 229 | pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
|
| 230 | Self::new_uninit_in(Global)
|
| 231 | }
|
| 232 |
|
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | #[cfg(not(no_global_oom_handling))]
|
| 252 | #[inline]
|
| 253 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 254 | #[must_use]
|
| 255 | pub fn new_zeroed() -> Box<mem::MaybeUninit<T>> {
|
| 256 | Self::new_zeroed_in(Global)
|
| 257 | }
|
| 258 |
|
| 259 | |
| 260 | |
| 261 | #[cfg(not(no_global_oom_handling))]
|
| 262 | #[stable(feature = "pin", since = "1.33.0")]
|
| 263 | #[must_use]
|
| 264 | #[inline(always)]
|
| 265 | pub fn pin(x: T) -> Pin<Box<T>> {
|
| 266 | (box x).into()
|
| 267 | }
|
| 268 |
|
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 283 | #[inline]
|
| 284 | pub fn try_new(x: T) -> Result<Self, AllocError> {
|
| 285 | Self::try_new_in(x, Global)
|
| 286 | }
|
| 287 |
|
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 309 | |
| 310 | #[inline]
|
| 311 | pub fn try_new_uninit() -> Result<Box<mem::MaybeUninit<T>>, AllocError> {
|
| 312 | Box::try_new_uninit_in(Global)
|
| 313 | }
|
| 314 |
|
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 335 | |
| 336 | #[inline]
|
| 337 | pub fn try_new_zeroed() -> Result<Box<mem::MaybeUninit<T>>, AllocError> {
|
| 338 | Box::try_new_zeroed_in(Global)
|
| 339 | }
|
| 340 | }
|
| 341 |
|
| 342 | impl<T, A: Allocator> Box<T, A> {
|
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | #[cfg(not(no_global_oom_handling))]
|
| 357 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 358 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 359 | #[must_use]
|
| 360 | #[inline]
|
| 361 | pub const fn new_in(x: T, alloc: A) -> Self
|
| 362 | where
|
| 363 | A: ~const Allocator + ~const Destruct,
|
| 364 | {
|
| 365 | let mut boxed = Self::new_uninit_in(alloc);
|
| 366 | unsafe {
|
| 367 | boxed.as_mut_ptr().write(x);
|
| 368 | boxed.assume_init()
|
| 369 | }
|
| 370 | }
|
| 371 |
|
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 388 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 389 | #[inline]
|
| 390 | pub const fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
|
| 391 | where
|
| 392 | T: ~const Destruct,
|
| 393 | A: ~const Allocator + ~const Destruct,
|
| 394 | {
|
| 395 | let mut boxed = Self::try_new_uninit_in(alloc)?;
|
| 396 | unsafe {
|
| 397 | boxed.as_mut_ptr().write(x);
|
| 398 | Ok(boxed.assume_init())
|
| 399 | }
|
| 400 | }
|
| 401 |
|
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 423 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 424 | #[cfg(not(no_global_oom_handling))]
|
| 425 | #[must_use]
|
| 426 | |
| 427 | pub const fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
|
| 428 | where
|
| 429 | A: ~const Allocator + ~const Destruct,
|
| 430 | {
|
| 431 | let layout = Layout::new::<mem::MaybeUninit<T>>();
|
| 432 | |
| 433 | |
| 434 | match Box::try_new_uninit_in(alloc) {
|
| 435 | Ok(m) => m,
|
| 436 | Err(_) => handle_alloc_error(layout),
|
| 437 | }
|
| 438 | }
|
| 439 |
|
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 463 | |
| 464 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 465 | pub const fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
|
| 466 | where
|
| 467 | A: ~const Allocator + ~const Destruct,
|
| 468 | {
|
| 469 | let layout = Layout::new::<mem::MaybeUninit<T>>();
|
| 470 | let ptr = alloc.allocate(layout)?.cast();
|
| 471 | unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
|
| 472 | }
|
| 473 |
|
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 495 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 496 | #[cfg(not(no_global_oom_handling))]
|
| 497 | |
| 498 | #[must_use]
|
| 499 | pub const fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
|
| 500 | where
|
| 501 | A: ~const Allocator + ~const Destruct,
|
| 502 | {
|
| 503 | let layout = Layout::new::<mem::MaybeUninit<T>>();
|
| 504 | |
| 505 | |
| 506 | match Box::try_new_zeroed_in(alloc) {
|
| 507 | Ok(m) => m,
|
| 508 | Err(_) => handle_alloc_error(layout),
|
| 509 | }
|
| 510 | }
|
| 511 |
|
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 535 | |
| 536 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 537 | pub const fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
|
| 538 | where
|
| 539 | A: ~const Allocator + ~const Destruct,
|
| 540 | {
|
| 541 | let layout = Layout::new::<mem::MaybeUninit<T>>();
|
| 542 | let ptr = alloc.allocate_zeroed(layout)?.cast();
|
| 543 | unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
|
| 544 | }
|
| 545 |
|
| 546 | |
| 547 | |
| 548 | #[cfg(not(no_global_oom_handling))]
|
| 549 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 550 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 551 | #[must_use]
|
| 552 | #[inline(always)]
|
| 553 | pub const fn pin_in(x: T, alloc: A) -> Pin<Self>
|
| 554 | where
|
| 555 | A: 'static + ~const Allocator + ~const Destruct,
|
| 556 | {
|
| 557 | Self::into_pin(Self::new_in(x, alloc))
|
| 558 | }
|
| 559 |
|
| 560 | |
| 561 | |
| 562 | |
| 563 | #[unstable(feature = "box_into_boxed_slice", issue = "71582")]
|
| 564 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 565 | pub const fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
|
| 566 | let (raw, alloc) = Box::into_raw_with_allocator(boxed);
|
| 567 | unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) }
|
| 568 | }
|
| 569 |
|
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | #[unstable(feature = "box_into_inner", issue = "80437")]
|
| 582 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 583 | #[inline]
|
| 584 | pub const fn into_inner(boxed: Self) -> T
|
| 585 | where
|
| 586 | Self: ~const Destruct,
|
| 587 | {
|
| 588 | *boxed
|
| 589 | }
|
| 590 | }
|
| 591 |
|
| 592 | impl<T> Box<[T]> {
|
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | #[cfg(not(no_global_oom_handling))]
|
| 614 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 615 | #[must_use]
|
| 616 | pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
|
| 617 | unsafe { RawVec::with_capacity(len).into_box(len) }
|
| 618 | }
|
| 619 |
|
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | #[cfg(not(no_global_oom_handling))]
|
| 639 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 640 | #[must_use]
|
| 641 | pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
|
| 642 | unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
|
| 643 | }
|
| 644 |
|
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 666 | #[inline]
|
| 667 | pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
|
| 668 | unsafe {
|
| 669 | let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
|
| 670 | Ok(l) => l,
|
| 671 | Err(_) => return Err(AllocError),
|
| 672 | };
|
| 673 | let ptr = Global.allocate(layout)?;
|
| 674 | Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
|
| 675 | }
|
| 676 | }
|
| 677 |
|
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 698 | #[inline]
|
| 699 | pub fn try_new_zeroed_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
|
| 700 | unsafe {
|
| 701 | let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
|
| 702 | Ok(l) => l,
|
| 703 | Err(_) => return Err(AllocError),
|
| 704 | };
|
| 705 | let ptr = Global.allocate_zeroed(layout)?;
|
| 706 | Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
|
| 707 | }
|
| 708 | }
|
| 709 | }
|
| 710 |
|
| 711 | impl<T, A: Allocator> Box<[T], A> {
|
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | #[cfg(not(no_global_oom_handling))]
|
| 735 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 736 | |
| 737 | #[must_use]
|
| 738 | pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
|
| 739 | unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
|
| 740 | }
|
| 741 |
|
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | #[cfg(not(no_global_oom_handling))]
|
| 763 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 764 | |
| 765 | #[must_use]
|
| 766 | pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
|
| 767 | unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
|
| 768 | }
|
| 769 | }
|
| 770 |
|
| 771 | impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
|
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 801 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 802 | #[inline]
|
| 803 | pub const unsafe fn assume_init(self) -> Box<T, A> {
|
| 804 | let (raw, alloc) = Box::into_raw_with_allocator(self);
|
| 805 | unsafe { Box::from_raw_in(raw as *mut T, alloc) }
|
| 806 | }
|
| 807 |
|
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 836 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 837 | #[inline]
|
| 838 | pub const fn write(mut boxed: Self, value: T) -> Box<T, A> {
|
| 839 | unsafe {
|
| 840 | (*boxed).write(value);
|
| 841 | boxed.assume_init()
|
| 842 | }
|
| 843 | }
|
| 844 | }
|
| 845 |
|
| 846 | impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> {
|
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 878 | #[inline]
|
| 879 | pub unsafe fn assume_init(self) -> Box<[T], A> {
|
| 880 | let (raw, alloc) = Box::into_raw_with_allocator(self);
|
| 881 | unsafe { Box::from_raw_in(raw as *mut [T], alloc) }
|
| 882 | }
|
| 883 | }
|
| 884 |
|
| 885 | impl<T: ?Sized> Box<T> {
|
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | #[stable(feature = "box_raw", since = "1.4.0")]
|
| 928 | #[inline]
|
| 929 | pub unsafe fn from_raw(raw: *mut T) -> Self {
|
| 930 | unsafe { Self::from_raw_in(raw, Global) }
|
| 931 | }
|
| 932 | }
|
| 933 |
|
| 934 | impl<T: ?Sized, A: Allocator> Box<T, A> {
|
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 983 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 984 | #[inline]
|
| 985 | pub const unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self {
|
| 986 | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
|
| 987 | }
|
| 988 |
|
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | #[stable(feature = "box_raw", since = "1.4.0")]
|
| 1029 | #[inline]
|
| 1030 | pub fn into_raw(b: Self) -> *mut T {
|
| 1031 | Self::into_raw_with_allocator(b).0
|
| 1032 | }
|
| 1033 |
|
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 1081 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1082 | #[inline]
|
| 1083 | pub const fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
|
| 1084 | let (leaked, alloc) = Box::into_unique(b);
|
| 1085 | (leaked.as_ptr(), alloc)
|
| 1086 | }
|
| 1087 |
|
| 1088 | #[unstable(
|
| 1089 | feature = "ptr_internals",
|
| 1090 | issue = "none",
|
| 1091 | reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
|
| 1092 | )]
|
| 1093 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1094 | #[inline]
|
| 1095 | #[doc(hidden)]
|
| 1096 | pub const fn into_unique(b: Self) -> (Unique<T>, A) {
|
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | let alloc = unsafe { ptr::read(&b.1) };
|
| 1103 | (Unique::from(Box::leak(b)), alloc)
|
| 1104 | }
|
| 1105 |
|
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 1112 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1113 | #[inline]
|
| 1114 | pub const fn allocator(b: &Self) -> &A {
|
| 1115 | &b.1
|
| 1116 | }
|
| 1117 |
|
| 1118 | |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | #[stable(feature = "box_leak", since = "1.26.0")]
|
| 1154 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1155 | #[inline]
|
| 1156 | pub const fn leak<'a>(b: Self) -> &'a mut T
|
| 1157 | where
|
| 1158 | A: 'a,
|
| 1159 | {
|
| 1160 | unsafe { &mut *mem::ManuallyDrop::new(b).0.as_ptr() }
|
| 1161 | }
|
| 1162 |
|
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | #[unstable(feature = "box_into_pin", issue = "62370")]
|
| 1169 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1170 | pub const fn into_pin(boxed: Self) -> Pin<Self>
|
| 1171 | where
|
| 1172 | A: 'static,
|
| 1173 | {
|
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | unsafe { Pin::new_unchecked(boxed) }
|
| 1178 | }
|
| 1179 | }
|
| 1180 |
|
| 1181 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1182 | unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
|
| 1183 | fn drop(&mut self) {
|
| 1184 | |
| 1185 | }
|
| 1186 | }
|
| 1187 |
|
| 1188 | #[cfg(not(no_global_oom_handling))]
|
| 1189 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1190 | impl<T: Default> Default for Box<T> {
|
| 1191 | |
| 1192 | fn default() -> Self {
|
| 1193 | box T::default()
|
| 1194 | }
|
| 1195 | }
|
| 1196 |
|
| 1197 | #[cfg(not(no_global_oom_handling))]
|
| 1198 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1199 | #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
|
| 1200 | impl<T> const Default for Box<[T]> {
|
| 1201 | fn default() -> Self {
|
| 1202 | let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling();
|
| 1203 | Box(ptr, Global)
|
| 1204 | }
|
| 1205 | }
|
| 1206 |
|
| 1207 | #[cfg(not(no_global_oom_handling))]
|
| 1208 | #[stable(feature = "default_box_extra", since = "1.17.0")]
|
| 1209 | #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
|
| 1210 | impl const Default for Box<str> {
|
| 1211 | fn default() -> Self {
|
| 1212 | |
| 1213 | let ptr: Unique<str> = unsafe {
|
| 1214 | let bytes: Unique<[u8]> = Unique::<[u8; 0]>::dangling();
|
| 1215 | Unique::new_unchecked(bytes.as_ptr() as *mut str)
|
| 1216 | };
|
| 1217 | Box(ptr, Global)
|
| 1218 | }
|
| 1219 | }
|
| 1220 |
|
| 1221 | #[cfg(not(no_global_oom_handling))]
|
| 1222 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1223 | impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {
|
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | #[inline]
|
| 1239 | fn clone(&self) -> Self {
|
| 1240 | |
| 1241 | let mut boxed = Self::new_uninit_in(self.1.clone());
|
| 1242 | unsafe {
|
| 1243 | (**self).write_clone_into_raw(boxed.as_mut_ptr());
|
| 1244 | boxed.assume_init()
|
| 1245 | }
|
| 1246 | }
|
| 1247 |
|
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | #[inline]
|
| 1266 | fn clone_from(&mut self, source: &Self) {
|
| 1267 | (**self).clone_from(&(**source));
|
| 1268 | }
|
| 1269 | }
|
| 1270 |
|
| 1271 | #[cfg(not(no_global_oom_handling))]
|
| 1272 | #[stable(feature = "box_slice_clone", since = "1.3.0")]
|
| 1273 | impl Clone for Box<str> {
|
| 1274 | fn clone(&self) -> Self {
|
| 1275 | |
| 1276 | let buf: Box<[u8]> = self.as_bytes().into();
|
| 1277 | unsafe { from_boxed_utf8_unchecked(buf) }
|
| 1278 | }
|
| 1279 | }
|
| 1280 |
|
| 1281 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1282 | impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for Box<T, A> {
|
| 1283 | #[inline]
|
| 1284 | fn eq(&self, other: &Self) -> bool {
|
| 1285 | PartialEq::eq(&**self, &**other)
|
| 1286 | }
|
| 1287 | #[inline]
|
| 1288 | fn ne(&self, other: &Self) -> bool {
|
| 1289 | PartialEq::ne(&**self, &**other)
|
| 1290 | }
|
| 1291 | }
|
| 1292 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1293 | impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for Box<T, A> {
|
| 1294 | #[inline]
|
| 1295 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
| 1296 | PartialOrd::partial_cmp(&**self, &**other)
|
| 1297 | }
|
| 1298 | #[inline]
|
| 1299 | fn lt(&self, other: &Self) -> bool {
|
| 1300 | PartialOrd::lt(&**self, &**other)
|
| 1301 | }
|
| 1302 | #[inline]
|
| 1303 | fn le(&self, other: &Self) -> bool {
|
| 1304 | PartialOrd::le(&**self, &**other)
|
| 1305 | }
|
| 1306 | #[inline]
|
| 1307 | fn ge(&self, other: &Self) -> bool {
|
| 1308 | PartialOrd::ge(&**self, &**other)
|
| 1309 | }
|
| 1310 | #[inline]
|
| 1311 | fn gt(&self, other: &Self) -> bool {
|
| 1312 | PartialOrd::gt(&**self, &**other)
|
| 1313 | }
|
| 1314 | }
|
| 1315 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1316 | impl<T: ?Sized + Ord, A: Allocator> Ord for Box<T, A> {
|
| 1317 | #[inline]
|
| 1318 | fn cmp(&self, other: &Self) -> Ordering {
|
| 1319 | Ord::cmp(&**self, &**other)
|
| 1320 | }
|
| 1321 | }
|
| 1322 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1323 | impl<T: ?Sized + Eq, A: Allocator> Eq for Box<T, A> {}
|
| 1324 |
|
| 1325 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1326 | impl<T: ?Sized + Hash, A: Allocator> Hash for Box<T, A> {
|
| 1327 | fn hash<H: Hasher>(&self, state: &mut H) {
|
| 1328 | (**self).hash(state);
|
| 1329 | }
|
| 1330 | }
|
| 1331 |
|
| 1332 | #[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
|
| 1333 | impl<T: ?Sized + Hasher, A: Allocator> Hasher for Box<T, A> {
|
| 1334 | fn finish(&self) -> u64 {
|
| 1335 | (**self).finish()
|
| 1336 | }
|
| 1337 | fn write(&mut self, bytes: &[u8]) {
|
| 1338 | (**self).write(bytes)
|
| 1339 | }
|
| 1340 | fn write_u8(&mut self, i: u8) {
|
| 1341 | (**self).write_u8(i)
|
| 1342 | }
|
| 1343 | fn write_u16(&mut self, i: u16) {
|
| 1344 | (**self).write_u16(i)
|
| 1345 | }
|
| 1346 | fn write_u32(&mut self, i: u32) {
|
| 1347 | (**self).write_u32(i)
|
| 1348 | }
|
| 1349 | fn write_u64(&mut self, i: u64) {
|
| 1350 | (**self).write_u64(i)
|
| 1351 | }
|
| 1352 | fn write_u128(&mut self, i: u128) {
|
| 1353 | (**self).write_u128(i)
|
| 1354 | }
|
| 1355 | fn write_usize(&mut self, i: usize) {
|
| 1356 | (**self).write_usize(i)
|
| 1357 | }
|
| 1358 | fn write_i8(&mut self, i: i8) {
|
| 1359 | (**self).write_i8(i)
|
| 1360 | }
|
| 1361 | fn write_i16(&mut self, i: i16) {
|
| 1362 | (**self).write_i16(i)
|
| 1363 | }
|
| 1364 | fn write_i32(&mut self, i: i32) {
|
| 1365 | (**self).write_i32(i)
|
| 1366 | }
|
| 1367 | fn write_i64(&mut self, i: i64) {
|
| 1368 | (**self).write_i64(i)
|
| 1369 | }
|
| 1370 | fn write_i128(&mut self, i: i128) {
|
| 1371 | (**self).write_i128(i)
|
| 1372 | }
|
| 1373 | fn write_isize(&mut self, i: isize) {
|
| 1374 | (**self).write_isize(i)
|
| 1375 | }
|
| 1376 | fn write_length_prefix(&mut self, len: usize) {
|
| 1377 | (**self).write_length_prefix(len)
|
| 1378 | }
|
| 1379 | fn write_str(&mut self, s: &str) {
|
| 1380 | (**self).write_str(s)
|
| 1381 | }
|
| 1382 | }
|
| 1383 |
|
| 1384 | #[cfg(not(no_global_oom_handling))]
|
| 1385 | #[stable(feature = "from_for_ptrs", since = "1.6.0")]
|
| 1386 | impl<T> From<T> for Box<T> {
|
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | |
| 1400 | fn from(t: T) -> Self {
|
| 1401 | Box::new(t)
|
| 1402 | }
|
| 1403 | }
|
| 1404 |
|
| 1405 | #[stable(feature = "pin", since = "1.33.0")]
|
| 1406 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1407 | impl<T: ?Sized, A: Allocator> const From<Box<T, A>> for Pin<Box<T, A>>
|
| 1408 | where
|
| 1409 | A: 'static,
|
| 1410 | {
|
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | fn from(boxed: Box<T, A>) -> Self {
|
| 1415 | Box::into_pin(boxed)
|
| 1416 | }
|
| 1417 | }
|
| 1418 |
|
| 1419 | #[cfg(not(no_global_oom_handling))]
|
| 1420 | #[stable(feature = "box_from_slice", since = "1.17.0")]
|
| 1421 | impl<T: Copy> From<&[T]> for Box<[T]> {
|
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | fn from(slice: &[T]) -> Box<[T]> {
|
| 1436 | let len = slice.len();
|
| 1437 | let buf = RawVec::with_capacity(len);
|
| 1438 | unsafe {
|
| 1439 | ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
|
| 1440 | buf.into_box(slice.len()).assume_init()
|
| 1441 | }
|
| 1442 | }
|
| 1443 | }
|
| 1444 |
|
| 1445 | #[cfg(not(no_global_oom_handling))]
|
| 1446 | #[stable(feature = "box_from_cow", since = "1.45.0")]
|
| 1447 | impl<T: Copy> From<Cow<'_, [T]>> for Box<[T]> {
|
| 1448 | |
| 1449 | |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | |
| 1454 | #[inline]
|
| 1455 | fn from(cow: Cow<'_, [T]>) -> Box<[T]> {
|
| 1456 | match cow {
|
| 1457 | Cow::Borrowed(slice) => Box::from(slice),
|
| 1458 | Cow::Owned(slice) => Box::from(slice),
|
| 1459 | }
|
| 1460 | }
|
| 1461 | }
|
| 1462 |
|
| 1463 | #[cfg(not(no_global_oom_handling))]
|
| 1464 | #[stable(feature = "box_from_slice", since = "1.17.0")]
|
| 1465 | impl From<&str> for Box<str> {
|
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 | #[inline]
|
| 1478 | fn from(s: &str) -> Box<str> {
|
| 1479 | unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
|
| 1480 | }
|
| 1481 | }
|
| 1482 |
|
| 1483 | #[cfg(not(no_global_oom_handling))]
|
| 1484 | #[stable(feature = "box_from_cow", since = "1.45.0")]
|
| 1485 | impl From<Cow<'_, str>> for Box<str> {
|
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | |
| 1498 | |
| 1499 | |
| 1500 | |
| 1501 | |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | #[inline]
|
| 1510 | fn from(cow: Cow<'_, str>) -> Box<str> {
|
| 1511 | match cow {
|
| 1512 | Cow::Borrowed(s) => Box::from(s),
|
| 1513 | Cow::Owned(s) => Box::from(s),
|
| 1514 | }
|
| 1515 | }
|
| 1516 | }
|
| 1517 |
|
| 1518 | #[stable(feature = "boxed_str_conv", since = "1.19.0")]
|
| 1519 | impl<A: Allocator> From<Box<str, A>> for Box<[u8], A> {
|
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | #[inline]
|
| 1537 | fn from(s: Box<str, A>) -> Self {
|
| 1538 | let (raw, alloc) = Box::into_raw_with_allocator(s);
|
| 1539 | unsafe { Box::from_raw_in(raw as *mut [u8], alloc) }
|
| 1540 | }
|
| 1541 | }
|
| 1542 |
|
| 1543 | #[cfg(not(no_global_oom_handling))]
|
| 1544 | #[stable(feature = "box_from_array", since = "1.45.0")]
|
| 1545 | impl<T, const N: usize> From<[T; N]> for Box<[T]> {
|
| 1546 | |
| 1547 | |
| 1548 | |
| 1549 | |
| 1550 | |
| 1551 | |
| 1552 | |
| 1553 | |
| 1554 | |
| 1555 | |
| 1556 | fn from(array: [T; N]) -> Box<[T]> {
|
| 1557 | box array
|
| 1558 | }
|
| 1559 | }
|
| 1560 |
|
| 1561 | #[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
|
| 1562 | impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
|
| 1563 | type Error = Box<[T]>;
|
| 1564 |
|
| 1565 | |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | |
| 1572 | |
| 1573 | |
| 1574 | fn try_from(boxed_slice: Box<[T]>) -> Result<Self, Self::Error> {
|
| 1575 | if boxed_slice.len() == N {
|
| 1576 | Ok(unsafe { Box::from_raw(Box::into_raw(boxed_slice) as *mut [T; N]) })
|
| 1577 | } else {
|
| 1578 | Err(boxed_slice)
|
| 1579 | }
|
| 1580 | }
|
| 1581 | }
|
| 1582 |
|
| 1583 | impl<A: Allocator> Box<dyn Any, A> {
|
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | |
| 1601 | #[inline]
|
| 1602 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1603 | pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
|
| 1604 | if self.is::<T>() { unsafe { Ok(self.downcast_unchecked::<T>()) } } else { Err(self) }
|
| 1605 | }
|
| 1606 |
|
| 1607 | |
| 1608 | |
| 1609 | |
| 1610 | |
| 1611 | |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | |
| 1623 | |
| 1624 | |
| 1625 | |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | |
| 1631 | #[inline]
|
| 1632 | #[unstable(feature = "downcast_unchecked", issue = "90850")]
|
| 1633 | pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
|
| 1634 | debug_assert!(self.is::<T>());
|
| 1635 | unsafe {
|
| 1636 | let (raw, alloc): (*mut dyn Any, _) = Box::into_raw_with_allocator(self);
|
| 1637 | Box::from_raw_in(raw as *mut T, alloc)
|
| 1638 | }
|
| 1639 | }
|
| 1640 | }
|
| 1641 |
|
| 1642 | impl<A: Allocator> Box<dyn Any + Send, A> {
|
| 1643 | |
| 1644 | |
| 1645 | |
| 1646 | |
| 1647 | |
| 1648 | |
| 1649 | |
| 1650 | |
| 1651 | |
| 1652 | |
| 1653 | |
| 1654 | |
| 1655 | |
| 1656 | |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | #[inline]
|
| 1661 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1662 | pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
|
| 1663 | if self.is::<T>() { unsafe { Ok(self.downcast_unchecked::<T>()) } } else { Err(self) }
|
| 1664 | }
|
| 1665 |
|
| 1666 | |
| 1667 | |
| 1668 | |
| 1669 | |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | |
| 1679 | |
| 1680 | |
| 1681 | |
| 1682 | |
| 1683 | |
| 1684 | |
| 1685 | |
| 1686 | |
| 1687 | |
| 1688 | |
| 1689 | |
| 1690 | #[inline]
|
| 1691 | #[unstable(feature = "downcast_unchecked", issue = "90850")]
|
| 1692 | pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
|
| 1693 | debug_assert!(self.is::<T>());
|
| 1694 | unsafe {
|
| 1695 | let (raw, alloc): (*mut (dyn Any + Send), _) = Box::into_raw_with_allocator(self);
|
| 1696 | Box::from_raw_in(raw as *mut T, alloc)
|
| 1697 | }
|
| 1698 | }
|
| 1699 | }
|
| 1700 |
|
| 1701 | impl<A: Allocator> Box<dyn Any + Send + Sync, A> {
|
| 1702 | |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | |
| 1711 | |
| 1712 | |
| 1713 | |
| 1714 | |
| 1715 | |
| 1716 | |
| 1717 | |
| 1718 | |
| 1719 | #[inline]
|
| 1720 | #[stable(feature = "box_send_sync_any_downcast", since = "1.51.0")]
|
| 1721 | pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
|
| 1722 | if self.is::<T>() { unsafe { Ok(self.downcast_unchecked::<T>()) } } else { Err(self) }
|
| 1723 | }
|
| 1724 |
|
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | |
| 1729 | |
| 1730 | |
| 1731 | |
| 1732 | |
| 1733 | |
| 1734 | |
| 1735 | |
| 1736 | |
| 1737 | |
| 1738 | |
| 1739 | |
| 1740 | |
| 1741 | |
| 1742 | |
| 1743 | |
| 1744 | |
| 1745 | |
| 1746 | |
| 1747 | |
| 1748 | |
| 1749 | #[inline]
|
| 1750 | #[unstable(feature = "downcast_unchecked", issue = "90850")]
|
| 1751 | pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
|
| 1752 | debug_assert!(self.is::<T>());
|
| 1753 | unsafe {
|
| 1754 | let (raw, alloc): (*mut (dyn Any + Send + Sync), _) =
|
| 1755 | Box::into_raw_with_allocator(self);
|
| 1756 | Box::from_raw_in(raw as *mut T, alloc)
|
| 1757 | }
|
| 1758 | }
|
| 1759 | }
|
| 1760 |
|
| 1761 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1762 | impl<T: fmt::Display + ?Sized, A: Allocator> fmt::Display for Box<T, A> {
|
| 1763 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 1764 | fmt::Display::fmt(&**self, f)
|
| 1765 | }
|
| 1766 | }
|
| 1767 |
|
| 1768 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1769 | impl<T: fmt::Debug + ?Sized, A: Allocator> fmt::Debug for Box<T, A> {
|
| 1770 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 1771 | fmt::Debug::fmt(&**self, f)
|
| 1772 | }
|
| 1773 | }
|
| 1774 |
|
| 1775 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1776 | impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
|
| 1777 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 1778 | |
| 1779 | |
| 1780 | let ptr: *const T = &**self;
|
| 1781 | fmt::Pointer::fmt(&ptr, f)
|
| 1782 | }
|
| 1783 | }
|
| 1784 |
|
| 1785 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1786 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1787 | impl<T: ?Sized, A: Allocator> const Deref for Box<T, A> {
|
| 1788 | type Target = T;
|
| 1789 |
|
| 1790 | fn deref(&self) -> &T {
|
| 1791 | &**self
|
| 1792 | }
|
| 1793 | }
|
| 1794 |
|
| 1795 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1796 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1797 | impl<T: ?Sized, A: Allocator> const DerefMut for Box<T, A> {
|
| 1798 | fn deref_mut(&mut self) -> &mut T {
|
| 1799 | &mut **self
|
| 1800 | }
|
| 1801 | }
|
| 1802 |
|
| 1803 | #[unstable(feature = "receiver_trait", issue = "none")]
|
| 1804 | impl<T: ?Sized, A: Allocator> Receiver for Box<T, A> {}
|
| 1805 |
|
| 1806 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1807 | impl<I: Iterator + ?Sized, A: Allocator> Iterator for Box<I, A> {
|
| 1808 | type Item = I::Item;
|
| 1809 | fn next(&mut self) -> Option<I::Item> {
|
| 1810 | (**self).next()
|
| 1811 | }
|
| 1812 | fn size_hint(&self) -> (usize, Option<usize>) {
|
| 1813 | (**self).size_hint()
|
| 1814 | }
|
| 1815 | fn nth(&mut self, n: usize) -> Option<I::Item> {
|
| 1816 | (**self).nth(n)
|
| 1817 | }
|
| 1818 | fn last(self) -> Option<I::Item> {
|
| 1819 | BoxIter::last(self)
|
| 1820 | }
|
| 1821 | }
|
| 1822 |
|
| 1823 | trait BoxIter {
|
| 1824 | type Item;
|
| 1825 | fn last(self) -> Option<Self::Item>;
|
| 1826 | }
|
| 1827 |
|
| 1828 | impl<I: Iterator + ?Sized, A: Allocator> BoxIter for Box<I, A> {
|
| 1829 | type Item = I::Item;
|
| 1830 | default fn last(self) -> Option<I::Item> {
|
| 1831 | #[inline]
|
| 1832 | fn some<T>(_: Option<T>, x: T) -> Option<T> {
|
| 1833 | Some(x)
|
| 1834 | }
|
| 1835 |
|
| 1836 | self.fold(None, some)
|
| 1837 | }
|
| 1838 | }
|
| 1839 |
|
| 1840 | |
| 1841 | |
| 1842 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1843 | impl<I: Iterator, A: Allocator> BoxIter for Box<I, A> {
|
| 1844 | fn last(self) -> Option<I::Item> {
|
| 1845 | (*self).last()
|
| 1846 | }
|
| 1847 | }
|
| 1848 |
|
| 1849 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1850 | impl<I: DoubleEndedIterator + ?Sized, A: Allocator> DoubleEndedIterator for Box<I, A> {
|
| 1851 | fn next_back(&mut self) -> Option<I::Item> {
|
| 1852 | (**self).next_back()
|
| 1853 | }
|
| 1854 | fn nth_back(&mut self, n: usize) -> Option<I::Item> {
|
| 1855 | (**self).nth_back(n)
|
| 1856 | }
|
| 1857 | }
|
| 1858 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1859 | impl<I: ExactSizeIterator + ?Sized, A: Allocator> ExactSizeIterator for Box<I, A> {
|
| 1860 | fn len(&self) -> usize {
|
| 1861 | (**self).len()
|
| 1862 | }
|
| 1863 | fn is_empty(&self) -> bool {
|
| 1864 | (**self).is_empty()
|
| 1865 | }
|
| 1866 | }
|
| 1867 |
|
| 1868 | #[stable(feature = "fused", since = "1.26.0")]
|
| 1869 | impl<I: FusedIterator + ?Sized, A: Allocator> FusedIterator for Box<I, A> {}
|
| 1870 |
|
| 1871 | #[stable(feature = "boxed_closure_impls", since = "1.35.0")]
|
| 1872 | impl<Args, F: FnOnce<Args> + ?Sized, A: Allocator> FnOnce<Args> for Box<F, A> {
|
| 1873 | type Output = <F as FnOnce<Args>>::Output;
|
| 1874 |
|
| 1875 | extern "rust-call" fn call_once(self, args: Args) -> Self::Output {
|
| 1876 | <F as FnOnce<Args>>::call_once(*self, args)
|
| 1877 | }
|
| 1878 | }
|
| 1879 |
|
| 1880 | #[stable(feature = "boxed_closure_impls", since = "1.35.0")]
|
| 1881 | impl<Args, F: FnMut<Args> + ?Sized, A: Allocator> FnMut<Args> for Box<F, A> {
|
| 1882 | extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output {
|
| 1883 | <F as FnMut<Args>>::call_mut(self, args)
|
| 1884 | }
|
| 1885 | }
|
| 1886 |
|
| 1887 | #[stable(feature = "boxed_closure_impls", since = "1.35.0")]
|
| 1888 | impl<Args, F: Fn<Args> + ?Sized, A: Allocator> Fn<Args> for Box<F, A> {
|
| 1889 | extern "rust-call" fn call(&self, args: Args) -> Self::Output {
|
| 1890 | <F as Fn<Args>>::call(self, args)
|
| 1891 | }
|
| 1892 | }
|
| 1893 |
|
| 1894 | #[unstable(feature = "coerce_unsized", issue = "27732")]
|
| 1895 | impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}
|
| 1896 |
|
| 1897 | #[unstable(feature = "dispatch_from_dyn", issue = "none")]
|
| 1898 | impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T, Global> {}
|
| 1899 |
|
| 1900 | #[cfg(not(no_global_oom_handling))]
|
| 1901 | #[stable(feature = "boxed_slice_from_iter", since = "1.32.0")]
|
| 1902 | impl<I> FromIterator<I> for Box<[I]> {
|
| 1903 | fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
|
| 1904 | iter.into_iter().collect::<Vec<_>>().into_boxed_slice()
|
| 1905 | }
|
| 1906 | }
|
| 1907 |
|
| 1908 | #[cfg(not(no_global_oom_handling))]
|
| 1909 | #[stable(feature = "box_slice_clone", since = "1.3.0")]
|
| 1910 | impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
|
| 1911 | fn clone(&self) -> Self {
|
| 1912 | let alloc = Box::allocator(self).clone();
|
| 1913 | self.to_vec_in(alloc).into_boxed_slice()
|
| 1914 | }
|
| 1915 |
|
| 1916 | fn clone_from(&mut self, other: &Self) {
|
| 1917 | if self.len() == other.len() {
|
| 1918 | self.clone_from_slice(&other);
|
| 1919 | } else {
|
| 1920 | *self = other.clone();
|
| 1921 | }
|
| 1922 | }
|
| 1923 | }
|
| 1924 |
|
| 1925 | #[stable(feature = "box_borrow", since = "1.1.0")]
|
| 1926 | impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for Box<T, A> {
|
| 1927 | fn borrow(&self) -> &T {
|
| 1928 | &**self
|
| 1929 | }
|
| 1930 | }
|
| 1931 |
|
| 1932 | #[stable(feature = "box_borrow", since = "1.1.0")]
|
| 1933 | impl<T: ?Sized, A: Allocator> borrow::BorrowMut<T> for Box<T, A> {
|
| 1934 | fn borrow_mut(&mut self) -> &mut T {
|
| 1935 | &mut **self
|
| 1936 | }
|
| 1937 | }
|
| 1938 |
|
| 1939 | #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
|
| 1940 | impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A> {
|
| 1941 | fn as_ref(&self) -> &T {
|
| 1942 | &**self
|
| 1943 | }
|
| 1944 | }
|
| 1945 |
|
| 1946 | #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
|
| 1947 | impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
|
| 1948 | fn as_mut(&mut self) -> &mut T {
|
| 1949 | &mut **self
|
| 1950 | }
|
| 1951 | }
|
| 1952 |
|
| 1953 | |
| 1954 | *
|
| 1955 | * We could have chosen not to add this impl, and instead have written a
|
| 1956 | * function of Pin<Box<T>> to Pin<T>. Such a function would not be sound,
|
| 1957 | * because Box<T> implements Unpin even when T does not, as a result of
|
| 1958 | * this impl.
|
| 1959 | *
|
| 1960 | * We chose this API instead of the alternative for a few reasons:
|
| 1961 | * - Logically, it is helpful to understand pinning in regard to the
|
| 1962 | * memory region being pointed to. For this reason none of the
|
| 1963 | * standard library pointer types support projecting through a pin
|
| 1964 | * (Box<T> is the only pointer type in std for which this would be
|
| 1965 | * safe.)
|
| 1966 | * - It is in practice very useful to have Box<T> be unconditionally
|
| 1967 | * Unpin because of trait objects, for which the structural auto
|
| 1968 | * trait functionality does not apply (e.g., Box<dyn Foo> would
|
| 1969 | * otherwise not be Unpin).
|
| 1970 | *
|
| 1971 | * Another type with the same semantics as Box but only a conditional
|
| 1972 | * implementation of `Unpin` (where `T: Unpin`) would be valid/safe, and
|
| 1973 | * could have a method to project a Pin<T> from it.
|
| 1974 |
|
| 1975 | #[stable(feature = "pin", since = "1.33.0")]
|
| 1976 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1977 | impl<T: ?Sized, A: Allocator> const Unpin for Box<T, A> where A: 'static {}
|
| 1978 |
|
| 1979 | #[unstable(feature = "generator_trait", issue = "43122")]
|
| 1980 | impl<G: ?Sized + Generator<R> + Unpin, R, A: Allocator> Generator<R> for Box<G, A>
|
| 1981 | where
|
| 1982 | A: 'static,
|
| 1983 | {
|
| 1984 | type Yield = G::Yield;
|
| 1985 | type Return = G::Return;
|
| 1986 |
|
| 1987 | fn resume(mut self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return> {
|
| 1988 | G::resume(Pin::new(&mut *self), arg)
|
| 1989 | }
|
| 1990 | }
|
| 1991 |
|
| 1992 | #[unstable(feature = "generator_trait", issue = "43122")]
|
| 1993 | impl<G: ?Sized + Generator<R>, R, A: Allocator> Generator<R> for Pin<Box<G, A>>
|
| 1994 | where
|
| 1995 | A: 'static,
|
| 1996 | {
|
| 1997 | type Yield = G::Yield;
|
| 1998 | type Return = G::Return;
|
| 1999 |
|
| 2000 | fn resume(mut self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return> {
|
| 2001 | G::resume((*self).as_mut(), arg)
|
| 2002 | }
|
| 2003 | }
|
| 2004 |
|
| 2005 | #[stable(feature = "futures_api", since = "1.36.0")]
|
| 2006 | impl<F: ?Sized + Future + Unpin, A: Allocator> Future for Box<F, A>
|
| 2007 | where
|
| 2008 | A: 'static,
|
| 2009 | {
|
| 2010 | type Output = F::Output;
|
| 2011 |
|
| 2012 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
| 2013 | F::poll(Pin::new(&mut *self), cx)
|
| 2014 | }
|
| 2015 | }
|
| 2016 |
|
| 2017 | #[unstable(feature = "async_iterator", issue = "79024")]
|
| 2018 | impl<S: ?Sized + AsyncIterator + Unpin> AsyncIterator for Box<S> {
|
| 2019 | type Item = S::Item;
|
| 2020 |
|
| 2021 | fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
| 2022 | Pin::new(&mut **self).poll_next(cx)
|
| 2023 | }
|
| 2024 |
|
| 2025 | fn size_hint(&self) -> (usize, Option<usize>) {
|
| 2026 | (**self).size_hint()
|
| 2027 | }
|
| 2028 | }
|