| 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 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 |
|
| 149 | #![stable(feature = "rust1", since = "1.0.0")]
|
| 150 |
|
| 151 | use core::any::Any;
|
| 152 | use core::async_iter::AsyncIterator;
|
| 153 | use core::borrow;
|
| 154 | use core::cmp::Ordering;
|
| 155 | use core::error::Error;
|
| 156 | use core::fmt;
|
| 157 | use core::future::Future;
|
| 158 | use core::hash::{Hash, Hasher};
|
| 159 | use core::iter::FusedIterator;
|
| 160 | use core::marker::Tuple;
|
| 161 | use core::marker::Unsize;
|
| 162 | use core::mem::{self, SizedTypeProperties};
|
| 163 | use core::ops::{
|
| 164 | CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver,
|
| 165 | };
|
| 166 | use core::pin::Pin;
|
| 167 | use core::ptr::{self, NonNull, Unique};
|
| 168 | use core::task::{Context, Poll};
|
| 169 |
|
| 170 | #[cfg(not(no_global_oom_handling))]
|
| 171 | use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
|
| 172 | use crate::alloc::{AllocError, Allocator, Global, Layout};
|
| 173 | #[cfg(not(no_global_oom_handling))]
|
| 174 | use crate::borrow::Cow;
|
| 175 | use crate::raw_vec::RawVec;
|
| 176 | #[cfg(not(no_global_oom_handling))]
|
| 177 | use crate::str::from_boxed_utf8_unchecked;
|
| 178 | #[cfg(not(no_global_oom_handling))]
|
| 179 | use crate::string::String;
|
| 180 | #[cfg(not(no_global_oom_handling))]
|
| 181 | use crate::vec::Vec;
|
| 182 |
|
| 183 | #[cfg(not(no_thin))]
|
| 184 | #[unstable(feature = "thin_box", issue = "92791")]
|
| 185 | pub use thin::ThinBox;
|
| 186 |
|
| 187 | #[cfg(not(no_thin))]
|
| 188 | mod thin;
|
| 189 |
|
| 190 | |
| 191 | |
| 192 | |
| 193 | #[lang = "owned_box"]
|
| 194 | #[fundamental]
|
| 195 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 196 | |
| 197 | |
| 198 | |
| 199 | pub struct Box<
|
| 200 | T: ?Sized,
|
| 201 | #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
|
| 202 | >(Unique<T>, A);
|
| 203 |
|
| 204 | impl<T> Box<T> {
|
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 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"]
|
| 219 | pub fn new(x: T) -> Self {
|
| 220 | #[rustc_box]
|
| 221 | Box::new(x)
|
| 222 | }
|
| 223 |
|
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | #[cfg(not(no_global_oom_handling))]
|
| 243 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 244 | #[must_use]
|
| 245 | #[inline]
|
| 246 | pub fn new_uninit() -> Box<mem::MaybeUninit<T>> {
|
| 247 | Self::new_uninit_in(Global)
|
| 248 | }
|
| 249 |
|
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | #[cfg(not(no_global_oom_handling))]
|
| 269 | #[inline]
|
| 270 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 271 | #[must_use]
|
| 272 | pub fn new_zeroed() -> Box<mem::MaybeUninit<T>> {
|
| 273 | Self::new_zeroed_in(Global)
|
| 274 | }
|
| 275 |
|
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 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>> {
|
| 288 | Box::new(x).into()
|
| 289 | }
|
| 290 |
|
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 305 | #[inline]
|
| 306 | pub fn try_new(x: T) -> Result<Self, AllocError> {
|
| 307 | Self::try_new_in(x, Global)
|
| 308 | }
|
| 309 |
|
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 331 | |
| 332 | #[inline]
|
| 333 | pub fn try_new_uninit() -> Result<Box<mem::MaybeUninit<T>>, AllocError> {
|
| 334 | Box::try_new_uninit_in(Global)
|
| 335 | }
|
| 336 |
|
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 357 | |
| 358 | #[inline]
|
| 359 | pub fn try_new_zeroed() -> Result<Box<mem::MaybeUninit<T>>, AllocError> {
|
| 360 | Box::try_new_zeroed_in(Global)
|
| 361 | }
|
| 362 | }
|
| 363 |
|
| 364 | impl<T, A: Allocator> Box<T, A> {
|
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | #[cfg(not(no_global_oom_handling))]
|
| 379 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 380 | #[must_use]
|
| 381 | #[inline]
|
| 382 | pub fn new_in(x: T, alloc: A) -> Self
|
| 383 | where
|
| 384 | A: Allocator,
|
| 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 |
|
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 409 | #[inline]
|
| 410 | pub fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
|
| 411 | where
|
| 412 | A: Allocator,
|
| 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 |
|
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 442 | #[cfg(not(no_global_oom_handling))]
|
| 443 | #[must_use]
|
| 444 | |
| 445 | pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
|
| 446 | where
|
| 447 | A: Allocator,
|
| 448 | {
|
| 449 | let layout = Layout::new::<mem::MaybeUninit<T>>();
|
| 450 | |
| 451 | |
| 452 | match Box::try_new_uninit_in(alloc) {
|
| 453 | Ok(m) => m,
|
| 454 | Err(_) => handle_alloc_error(layout),
|
| 455 | }
|
| 456 | }
|
| 457 |
|
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 481 | |
| 482 | pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
|
| 483 | where
|
| 484 | A: Allocator,
|
| 485 | {
|
| 486 | let ptr = if T::IS_ZST {
|
| 487 | NonNull::dangling()
|
| 488 | } else {
|
| 489 | let layout = Layout::new::<mem::MaybeUninit<T>>();
|
| 490 | alloc.allocate(layout)?.cast()
|
| 491 | };
|
| 492 | unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
|
| 493 | }
|
| 494 |
|
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 516 | #[cfg(not(no_global_oom_handling))]
|
| 517 | |
| 518 | #[must_use]
|
| 519 | pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
|
| 520 | where
|
| 521 | A: Allocator,
|
| 522 | {
|
| 523 | let layout = Layout::new::<mem::MaybeUninit<T>>();
|
| 524 | |
| 525 | |
| 526 | match Box::try_new_zeroed_in(alloc) {
|
| 527 | Ok(m) => m,
|
| 528 | Err(_) => handle_alloc_error(layout),
|
| 529 | }
|
| 530 | }
|
| 531 |
|
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 555 | |
| 556 | pub fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
|
| 557 | where
|
| 558 | A: Allocator,
|
| 559 | {
|
| 560 | let ptr = if T::IS_ZST {
|
| 561 | NonNull::dangling()
|
| 562 | } else {
|
| 563 | let layout = Layout::new::<mem::MaybeUninit<T>>();
|
| 564 | alloc.allocate_zeroed(layout)?.cast()
|
| 565 | };
|
| 566 | unsafe { Ok(Box::from_raw_in(ptr.as_ptr(), alloc)) }
|
| 567 | }
|
| 568 |
|
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | #[cfg(not(no_global_oom_handling))]
|
| 577 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 578 | #[must_use]
|
| 579 | #[inline(always)]
|
| 580 | pub fn pin_in(x: T, alloc: A) -> Pin<Self>
|
| 581 | where
|
| 582 | A: 'static + Allocator,
|
| 583 | {
|
| 584 | Self::into_pin(Self::new_in(x, alloc))
|
| 585 | }
|
| 586 |
|
| 587 | |
| 588 | |
| 589 | |
| 590 | #[unstable(feature = "box_into_boxed_slice", issue = "71582")]
|
| 591 | pub fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
|
| 592 | let (raw, alloc) = Box::into_raw_with_allocator(boxed);
|
| 593 | unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) }
|
| 594 | }
|
| 595 |
|
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | #[unstable(feature = "box_into_inner", issue = "80437")]
|
| 608 | #[inline]
|
| 609 | pub fn into_inner(boxed: Self) -> T {
|
| 610 | *boxed
|
| 611 | }
|
| 612 | }
|
| 613 |
|
| 614 | impl<T> Box<[T]> {
|
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | #[cfg(not(no_global_oom_handling))]
|
| 636 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 637 | #[must_use]
|
| 638 | pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
|
| 639 | unsafe { RawVec::with_capacity(len).into_box(len) }
|
| 640 | }
|
| 641 |
|
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | #[cfg(not(no_global_oom_handling))]
|
| 661 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 662 | #[must_use]
|
| 663 | pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
|
| 664 | unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
|
| 665 | }
|
| 666 |
|
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 688 | #[inline]
|
| 689 | pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
|
| 690 | let ptr = if T::IS_ZST || len == 0 {
|
| 691 | NonNull::dangling()
|
| 692 | } else {
|
| 693 | let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
|
| 694 | Ok(l) => l,
|
| 695 | Err(_) => return Err(AllocError),
|
| 696 | };
|
| 697 | Global.allocate(layout)?.cast()
|
| 698 | };
|
| 699 | unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
|
| 700 | }
|
| 701 |
|
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 722 | #[inline]
|
| 723 | pub fn try_new_zeroed_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
|
| 724 | let ptr = if T::IS_ZST || len == 0 {
|
| 725 | NonNull::dangling()
|
| 726 | } else {
|
| 727 | let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
|
| 728 | Ok(l) => l,
|
| 729 | Err(_) => return Err(AllocError),
|
| 730 | };
|
| 731 | Global.allocate_zeroed(layout)?.cast()
|
| 732 | };
|
| 733 | unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
|
| 734 | }
|
| 735 | }
|
| 736 |
|
| 737 | impl<T, A: Allocator> Box<[T], A> {
|
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | #[cfg(not(no_global_oom_handling))]
|
| 761 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 762 | |
| 763 | #[must_use]
|
| 764 | pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
|
| 765 | unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
|
| 766 | }
|
| 767 |
|
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | #[cfg(not(no_global_oom_handling))]
|
| 789 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 790 | |
| 791 | #[must_use]
|
| 792 | pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
|
| 793 | unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
|
| 794 | }
|
| 795 | }
|
| 796 |
|
| 797 | impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
|
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 827 | #[inline]
|
| 828 | pub unsafe fn assume_init(self) -> Box<T, A> {
|
| 829 | let (raw, alloc) = Box::into_raw_with_allocator(self);
|
| 830 | unsafe { Box::from_raw_in(raw as *mut T, alloc) }
|
| 831 | }
|
| 832 |
|
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 861 | #[inline]
|
| 862 | pub fn write(mut boxed: Self, value: T) -> Box<T, A> {
|
| 863 | unsafe {
|
| 864 | (*boxed).write(value);
|
| 865 | boxed.assume_init()
|
| 866 | }
|
| 867 | }
|
| 868 | }
|
| 869 |
|
| 870 | impl<T, A: Allocator> Box<[mem::MaybeUninit<T>], A> {
|
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | #[unstable(feature = "new_uninit", issue = "63291")]
|
| 902 | #[inline]
|
| 903 | pub unsafe fn assume_init(self) -> Box<[T], A> {
|
| 904 | let (raw, alloc) = Box::into_raw_with_allocator(self);
|
| 905 | unsafe { Box::from_raw_in(raw as *mut [T], alloc) }
|
| 906 | }
|
| 907 | }
|
| 908 |
|
| 909 | impl<T: ?Sized> Box<T> {
|
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | #[stable(feature = "box_raw", since = "1.4.0")]
|
| 952 | #[inline]
|
| 953 | #[must_use = "call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`"]
|
| 954 | pub unsafe fn from_raw(raw: *mut T) -> Self {
|
| 955 | unsafe { Self::from_raw_in(raw, Global) }
|
| 956 | }
|
| 957 | }
|
| 958 |
|
| 959 | impl<T: ?Sized, A: Allocator> Box<T, A> {
|
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 1008 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1009 | #[inline]
|
| 1010 | pub const unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self {
|
| 1011 | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
|
| 1012 | }
|
| 1013 |
|
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | #[stable(feature = "box_raw", since = "1.4.0")]
|
| 1054 | #[inline]
|
| 1055 | pub fn into_raw(b: Self) -> *mut T {
|
| 1056 | Self::into_raw_with_allocator(b).0
|
| 1057 | }
|
| 1058 |
|
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 1106 | #[inline]
|
| 1107 | pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
|
| 1108 | let (leaked, alloc) = Box::into_unique(b);
|
| 1109 | (leaked.as_ptr(), alloc)
|
| 1110 | }
|
| 1111 |
|
| 1112 | #[unstable(
|
| 1113 | feature = "ptr_internals",
|
| 1114 | issue = "none",
|
| 1115 | reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
|
| 1116 | )]
|
| 1117 | #[inline]
|
| 1118 | #[doc(hidden)]
|
| 1119 | pub fn into_unique(b: Self) -> (Unique<T>, A) {
|
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | let alloc = unsafe { ptr::read(&b.1) };
|
| 1126 | (Unique::from(Box::leak(b)), alloc)
|
| 1127 | }
|
| 1128 |
|
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 1135 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1136 | #[inline]
|
| 1137 | pub const fn allocator(b: &Self) -> &A {
|
| 1138 | &b.1
|
| 1139 | }
|
| 1140 |
|
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | #[stable(feature = "box_leak", since = "1.26.0")]
|
| 1177 | #[inline]
|
| 1178 | pub fn leak<'a>(b: Self) -> &'a mut T
|
| 1179 | where
|
| 1180 | A: 'a,
|
| 1181 | {
|
| 1182 | unsafe { &mut *mem::ManuallyDrop::new(b).0.as_ptr() }
|
| 1183 | }
|
| 1184 |
|
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | #[stable(feature = "box_into_pin", since = "1.63.0")]
|
| 1216 | #[rustc_const_unstable(feature = "const_box", issue = "92521")]
|
| 1217 | pub const fn into_pin(boxed: Self) -> Pin<Self>
|
| 1218 | where
|
| 1219 | A: 'static,
|
| 1220 | {
|
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | unsafe { Pin::new_unchecked(boxed) }
|
| 1225 | }
|
| 1226 | }
|
| 1227 |
|
| 1228 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1229 | unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
|
| 1230 | #[inline]
|
| 1231 | fn drop(&mut self) {
|
| 1232 | |
| 1233 |
|
| 1234 | let ptr = self.0;
|
| 1235 |
|
| 1236 | unsafe {
|
| 1237 | let layout = Layout::for_value_raw(ptr.as_ptr());
|
| 1238 | if layout.size() != 0 {
|
| 1239 | self.1.deallocate(From::from(ptr.cast()), layout);
|
| 1240 | }
|
| 1241 | }
|
| 1242 | }
|
| 1243 | }
|
| 1244 |
|
| 1245 | #[cfg(not(no_global_oom_handling))]
|
| 1246 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1247 | impl<T: Default> Default for Box<T> {
|
| 1248 | |
| 1249 | #[inline]
|
| 1250 | fn default() -> Self {
|
| 1251 | Box::new(T::default())
|
| 1252 | }
|
| 1253 | }
|
| 1254 |
|
| 1255 | #[cfg(not(no_global_oom_handling))]
|
| 1256 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1257 | impl<T> Default for Box<[T]> {
|
| 1258 | #[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")]
|
| 1267 | impl Default for Box<str> {
|
| 1268 | #[inline]
|
| 1269 | fn default() -> Self {
|
| 1270 | |
| 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 | }
|
| 1277 | }
|
| 1278 |
|
| 1279 | #[cfg(not(no_global_oom_handling))]
|
| 1280 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1281 | impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {
|
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | #[inline]
|
| 1297 | fn clone(&self) -> Self {
|
| 1298 | |
| 1299 | let mut boxed = Self::new_uninit_in(self.1.clone());
|
| 1300 | unsafe {
|
| 1301 | (**self).write_clone_into_raw(boxed.as_mut_ptr());
|
| 1302 | boxed.assume_init()
|
| 1303 | }
|
| 1304 | }
|
| 1305 |
|
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | #[inline]
|
| 1324 | fn clone_from(&mut self, source: &Self) {
|
| 1325 | (**self).clone_from(&(**source));
|
| 1326 | }
|
| 1327 | }
|
| 1328 |
|
| 1329 | #[cfg(not(no_global_oom_handling))]
|
| 1330 | #[stable(feature = "box_slice_clone", since = "1.3.0")]
|
| 1331 | impl Clone for Box<str> {
|
| 1332 | fn clone(&self) -> Self {
|
| 1333 | |
| 1334 | let buf: Box<[u8]> = self.as_bytes().into();
|
| 1335 | unsafe { from_boxed_utf8_unchecked(buf) }
|
| 1336 | }
|
| 1337 | }
|
| 1338 |
|
| 1339 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1340 | impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for Box<T, A> {
|
| 1341 | #[inline]
|
| 1342 | fn eq(&self, other: &Self) -> bool {
|
| 1343 | PartialEq::eq(&**self, &**other)
|
| 1344 | }
|
| 1345 | #[inline]
|
| 1346 | fn ne(&self, other: &Self) -> bool {
|
| 1347 | PartialEq::ne(&**self, &**other)
|
| 1348 | }
|
| 1349 | }
|
| 1350 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1351 | impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for Box<T, A> {
|
| 1352 | #[inline]
|
| 1353 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
| 1354 | PartialOrd::partial_cmp(&**self, &**other)
|
| 1355 | }
|
| 1356 | #[inline]
|
| 1357 | fn lt(&self, other: &Self) -> bool {
|
| 1358 | PartialOrd::lt(&**self, &**other)
|
| 1359 | }
|
| 1360 | #[inline]
|
| 1361 | fn le(&self, other: &Self) -> bool {
|
| 1362 | PartialOrd::le(&**self, &**other)
|
| 1363 | }
|
| 1364 | #[inline]
|
| 1365 | fn ge(&self, other: &Self) -> bool {
|
| 1366 | PartialOrd::ge(&**self, &**other)
|
| 1367 | }
|
| 1368 | #[inline]
|
| 1369 | fn gt(&self, other: &Self) -> bool {
|
| 1370 | PartialOrd::gt(&**self, &**other)
|
| 1371 | }
|
| 1372 | }
|
| 1373 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1374 | impl<T: ?Sized + Ord, A: Allocator> Ord for Box<T, A> {
|
| 1375 | #[inline]
|
| 1376 | fn cmp(&self, other: &Self) -> Ordering {
|
| 1377 | Ord::cmp(&**self, &**other)
|
| 1378 | }
|
| 1379 | }
|
| 1380 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1381 | impl<T: ?Sized + Eq, A: Allocator> Eq for Box<T, A> {}
|
| 1382 |
|
| 1383 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1384 | impl<T: ?Sized + Hash, A: Allocator> Hash for Box<T, A> {
|
| 1385 | fn hash<H: Hasher>(&self, state: &mut H) {
|
| 1386 | (**self).hash(state);
|
| 1387 | }
|
| 1388 | }
|
| 1389 |
|
| 1390 | #[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
|
| 1391 | impl<T: ?Sized + Hasher, A: Allocator> Hasher for Box<T, A> {
|
| 1392 | fn finish(&self) -> u64 {
|
| 1393 | (**self).finish()
|
| 1394 | }
|
| 1395 | fn write(&mut self, bytes: &[u8]) {
|
| 1396 | (**self).write(bytes)
|
| 1397 | }
|
| 1398 | fn write_u8(&mut self, i: u8) {
|
| 1399 | (**self).write_u8(i)
|
| 1400 | }
|
| 1401 | fn write_u16(&mut self, i: u16) {
|
| 1402 | (**self).write_u16(i)
|
| 1403 | }
|
| 1404 | fn write_u32(&mut self, i: u32) {
|
| 1405 | (**self).write_u32(i)
|
| 1406 | }
|
| 1407 | fn write_u64(&mut self, i: u64) {
|
| 1408 | (**self).write_u64(i)
|
| 1409 | }
|
| 1410 | fn write_u128(&mut self, i: u128) {
|
| 1411 | (**self).write_u128(i)
|
| 1412 | }
|
| 1413 | fn write_usize(&mut self, i: usize) {
|
| 1414 | (**self).write_usize(i)
|
| 1415 | }
|
| 1416 | fn write_i8(&mut self, i: i8) {
|
| 1417 | (**self).write_i8(i)
|
| 1418 | }
|
| 1419 | fn write_i16(&mut self, i: i16) {
|
| 1420 | (**self).write_i16(i)
|
| 1421 | }
|
| 1422 | fn write_i32(&mut self, i: i32) {
|
| 1423 | (**self).write_i32(i)
|
| 1424 | }
|
| 1425 | fn write_i64(&mut self, i: i64) {
|
| 1426 | (**self).write_i64(i)
|
| 1427 | }
|
| 1428 | fn write_i128(&mut self, i: i128) {
|
| 1429 | (**self).write_i128(i)
|
| 1430 | }
|
| 1431 | fn write_isize(&mut self, i: isize) {
|
| 1432 | (**self).write_isize(i)
|
| 1433 | }
|
| 1434 | fn write_length_prefix(&mut self, len: usize) {
|
| 1435 | (**self).write_length_prefix(len)
|
| 1436 | }
|
| 1437 | fn write_str(&mut self, s: &str) {
|
| 1438 | (**self).write_str(s)
|
| 1439 | }
|
| 1440 | }
|
| 1441 |
|
| 1442 | #[cfg(not(no_global_oom_handling))]
|
| 1443 | #[stable(feature = "from_for_ptrs", since = "1.6.0")]
|
| 1444 | impl<T> From<T> for Box<T> {
|
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | |
| 1454 | |
| 1455 | |
| 1456 | |
| 1457 | |
| 1458 | fn from(t: T) -> Self {
|
| 1459 | Box::new(t)
|
| 1460 | }
|
| 1461 | }
|
| 1462 |
|
| 1463 | #[stable(feature = "pin", since = "1.33.0")]
|
| 1464 | impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Pin<Box<T, A>>
|
| 1465 | where
|
| 1466 | A: 'static,
|
| 1467 | {
|
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | fn from(boxed: Box<T, A>) -> Self {
|
| 1480 | Box::into_pin(boxed)
|
| 1481 | }
|
| 1482 | }
|
| 1483 |
|
| 1484 | |
| 1485 | #[cfg(not(no_global_oom_handling))]
|
| 1486 | trait BoxFromSlice<T> {
|
| 1487 | fn from_slice(slice: &[T]) -> Self;
|
| 1488 | }
|
| 1489 |
|
| 1490 | #[cfg(not(no_global_oom_handling))]
|
| 1491 | impl<T: Clone> BoxFromSlice<T> for Box<[T]> {
|
| 1492 | #[inline]
|
| 1493 | default fn from_slice(slice: &[T]) -> Self {
|
| 1494 | slice.to_vec().into_boxed_slice()
|
| 1495 | }
|
| 1496 | }
|
| 1497 |
|
| 1498 | #[cfg(not(no_global_oom_handling))]
|
| 1499 | impl<T: Copy> BoxFromSlice<T> for Box<[T]> {
|
| 1500 | #[inline]
|
| 1501 | fn from_slice(slice: &[T]) -> Self {
|
| 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 | }
|
| 1508 | }
|
| 1509 | }
|
| 1510 |
|
| 1511 | #[cfg(not(no_global_oom_handling))]
|
| 1512 | #[stable(feature = "box_from_slice", since = "1.17.0")]
|
| 1513 | impl<T: Clone> From<&[T]> for Box<[T]> {
|
| 1514 | |
| 1515 | |
| 1516 | |
| 1517 | |
| 1518 | |
| 1519 | |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | #[inline]
|
| 1528 | fn from(slice: &[T]) -> Box<[T]> {
|
| 1529 | <Self as BoxFromSlice<T>>::from_slice(slice)
|
| 1530 | }
|
| 1531 | }
|
| 1532 |
|
| 1533 | #[cfg(not(no_global_oom_handling))]
|
| 1534 | #[stable(feature = "box_from_cow", since = "1.45.0")]
|
| 1535 | impl<T: Clone> From<Cow<'_, [T]>> for Box<[T]> {
|
| 1536 | |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | #[inline]
|
| 1543 | fn from(cow: Cow<'_, [T]>) -> Box<[T]> {
|
| 1544 | match cow {
|
| 1545 | Cow::Borrowed(slice) => Box::from(slice),
|
| 1546 | Cow::Owned(slice) => Box::from(slice),
|
| 1547 | }
|
| 1548 | }
|
| 1549 | }
|
| 1550 |
|
| 1551 | #[cfg(not(no_global_oom_handling))]
|
| 1552 | #[stable(feature = "box_from_slice", since = "1.17.0")]
|
| 1553 | impl From<&str> for Box<str> {
|
| 1554 | |
| 1555 | |
| 1556 | |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | #[inline]
|
| 1566 | fn from(s: &str) -> Box<str> {
|
| 1567 | unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
|
| 1568 | }
|
| 1569 | }
|
| 1570 |
|
| 1571 | #[cfg(not(no_global_oom_handling))]
|
| 1572 | #[stable(feature = "box_from_cow", since = "1.45.0")]
|
| 1573 | impl From<Cow<'_, str>> for Box<str> {
|
| 1574 | |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | |
| 1579 | |
| 1580 | |
| 1581 | |
| 1582 | |
| 1583 | |
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 | #[inline]
|
| 1598 | fn from(cow: Cow<'_, str>) -> Box<str> {
|
| 1599 | match cow {
|
| 1600 | Cow::Borrowed(s) => Box::from(s),
|
| 1601 | Cow::Owned(s) => Box::from(s),
|
| 1602 | }
|
| 1603 | }
|
| 1604 | }
|
| 1605 |
|
| 1606 | #[stable(feature = "boxed_str_conv", since = "1.19.0")]
|
| 1607 | impl<A: Allocator> From<Box<str, A>> for Box<[u8], A> {
|
| 1608 | |
| 1609 | |
| 1610 | |
| 1611 | |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | |
| 1623 | |
| 1624 | #[inline]
|
| 1625 | fn from(s: Box<str, A>) -> Self {
|
| 1626 | let (raw, alloc) = Box::into_raw_with_allocator(s);
|
| 1627 | unsafe { Box::from_raw_in(raw as *mut [u8], alloc) }
|
| 1628 | }
|
| 1629 | }
|
| 1630 |
|
| 1631 | #[cfg(not(no_global_oom_handling))]
|
| 1632 | #[stable(feature = "box_from_array", since = "1.45.0")]
|
| 1633 | impl<T, const N: usize> From<[T; N]> for Box<[T]> {
|
| 1634 | |
| 1635 | |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | |
| 1644 | fn from(array: [T; N]) -> Box<[T]> {
|
| 1645 | Box::new(array)
|
| 1646 | }
|
| 1647 | }
|
| 1648 |
|
| 1649 | |
| 1650 | |
| 1651 | |
| 1652 | |
| 1653 | |
| 1654 | unsafe fn boxed_slice_as_array_unchecked<T, A: Allocator, const N: usize>(
|
| 1655 | boxed_slice: Box<[T], A>,
|
| 1656 | ) -> Box<[T; N], A> {
|
| 1657 | debug_assert_eq!(boxed_slice.len(), N);
|
| 1658 |
|
| 1659 | let (ptr, alloc) = Box::into_raw_with_allocator(boxed_slice);
|
| 1660 | |
| 1661 | |
| 1662 | unsafe { Box::from_raw_in(ptr as *mut [T; N], alloc) }
|
| 1663 | }
|
| 1664 |
|
| 1665 | #[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
|
| 1666 | impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
|
| 1667 | type Error = Box<[T]>;
|
| 1668 |
|
| 1669 | |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | fn try_from(boxed_slice: Box<[T]>) -> Result<Self, Self::Error> {
|
| 1679 | if boxed_slice.len() == N {
|
| 1680 | Ok(unsafe { boxed_slice_as_array_unchecked(boxed_slice) })
|
| 1681 | } else {
|
| 1682 | Err(boxed_slice)
|
| 1683 | }
|
| 1684 | }
|
| 1685 | }
|
| 1686 |
|
| 1687 | #[cfg(not(no_global_oom_handling))]
|
| 1688 | #[stable(feature = "boxed_array_try_from_vec", since = "1.66.0")]
|
| 1689 | impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
|
| 1690 | type Error = Vec<T>;
|
| 1691 |
|
| 1692 | |
| 1693 | |
| 1694 | |
| 1695 | |
| 1696 | |
| 1697 | |
| 1698 | |
| 1699 | |
| 1700 | |
| 1701 | |
| 1702 | |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | fn try_from(vec: Vec<T>) -> Result<Self, Self::Error> {
|
| 1711 | if vec.len() == N {
|
| 1712 | let boxed_slice = vec.into_boxed_slice();
|
| 1713 | Ok(unsafe { boxed_slice_as_array_unchecked(boxed_slice) })
|
| 1714 | } else {
|
| 1715 | Err(vec)
|
| 1716 | }
|
| 1717 | }
|
| 1718 | }
|
| 1719 |
|
| 1720 | impl<A: Allocator> Box<dyn Any, A> {
|
| 1721 | |
| 1722 | |
| 1723 | |
| 1724 | |
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | |
| 1729 | |
| 1730 | |
| 1731 | |
| 1732 | |
| 1733 | |
| 1734 | |
| 1735 | |
| 1736 | |
| 1737 | |
| 1738 | #[inline]
|
| 1739 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1740 | pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
|
| 1741 | if self.is::<T>() { unsafe { Ok(self.downcast_unchecked::<T>()) } } else { Err(self) }
|
| 1742 | }
|
| 1743 |
|
| 1744 | |
| 1745 | |
| 1746 | |
| 1747 | |
| 1748 | |
| 1749 | |
| 1750 | |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | |
| 1755 | |
| 1756 | |
| 1757 | |
| 1758 | |
| 1759 | |
| 1760 | |
| 1761 | |
| 1762 | |
| 1763 | |
| 1764 | |
| 1765 | |
| 1766 | |
| 1767 | |
| 1768 | #[inline]
|
| 1769 | #[unstable(feature = "downcast_unchecked", issue = "90850")]
|
| 1770 | pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
|
| 1771 | debug_assert!(self.is::<T>());
|
| 1772 | unsafe {
|
| 1773 | let (raw, alloc): (*mut dyn Any, _) = Box::into_raw_with_allocator(self);
|
| 1774 | Box::from_raw_in(raw as *mut T, alloc)
|
| 1775 | }
|
| 1776 | }
|
| 1777 | }
|
| 1778 |
|
| 1779 | impl<A: Allocator> Box<dyn Any + Send, A> {
|
| 1780 | |
| 1781 | |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | |
| 1786 | |
| 1787 | |
| 1788 | |
| 1789 | |
| 1790 | |
| 1791 | |
| 1792 | |
| 1793 | |
| 1794 | |
| 1795 | |
| 1796 | |
| 1797 | #[inline]
|
| 1798 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1799 | pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
|
| 1800 | if self.is::<T>() { unsafe { Ok(self.downcast_unchecked::<T>()) } } else { Err(self) }
|
| 1801 | }
|
| 1802 |
|
| 1803 | |
| 1804 | |
| 1805 | |
| 1806 | |
| 1807 | |
| 1808 | |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | |
| 1813 | |
| 1814 | |
| 1815 | |
| 1816 | |
| 1817 | |
| 1818 | |
| 1819 | |
| 1820 | |
| 1821 | |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | |
| 1826 | |
| 1827 | #[inline]
|
| 1828 | #[unstable(feature = "downcast_unchecked", issue = "90850")]
|
| 1829 | pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
|
| 1830 | debug_assert!(self.is::<T>());
|
| 1831 | unsafe {
|
| 1832 | let (raw, alloc): (*mut (dyn Any + Send), _) = Box::into_raw_with_allocator(self);
|
| 1833 | Box::from_raw_in(raw as *mut T, alloc)
|
| 1834 | }
|
| 1835 | }
|
| 1836 | }
|
| 1837 |
|
| 1838 | impl<A: Allocator> Box<dyn Any + Send + Sync, A> {
|
| 1839 | |
| 1840 | |
| 1841 | |
| 1842 | |
| 1843 | |
| 1844 | |
| 1845 | |
| 1846 | |
| 1847 | |
| 1848 | |
| 1849 | |
| 1850 | |
| 1851 | |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | |
| 1856 | #[inline]
|
| 1857 | #[stable(feature = "box_send_sync_any_downcast", since = "1.51.0")]
|
| 1858 | pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
|
| 1859 | if self.is::<T>() { unsafe { Ok(self.downcast_unchecked::<T>()) } } else { Err(self) }
|
| 1860 | }
|
| 1861 |
|
| 1862 | |
| 1863 | |
| 1864 | |
| 1865 | |
| 1866 | |
| 1867 | |
| 1868 | |
| 1869 | |
| 1870 | |
| 1871 | |
| 1872 | |
| 1873 | |
| 1874 | |
| 1875 | |
| 1876 | |
| 1877 | |
| 1878 | |
| 1879 | |
| 1880 | |
| 1881 | |
| 1882 | |
| 1883 | |
| 1884 | |
| 1885 | |
| 1886 | #[inline]
|
| 1887 | #[unstable(feature = "downcast_unchecked", issue = "90850")]
|
| 1888 | pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
|
| 1889 | debug_assert!(self.is::<T>());
|
| 1890 | unsafe {
|
| 1891 | let (raw, alloc): (*mut (dyn Any + Send + Sync), _) =
|
| 1892 | Box::into_raw_with_allocator(self);
|
| 1893 | Box::from_raw_in(raw as *mut T, alloc)
|
| 1894 | }
|
| 1895 | }
|
| 1896 | }
|
| 1897 |
|
| 1898 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1899 | impl<T: fmt::Display + ?Sized, A: Allocator> fmt::Display for Box<T, A> {
|
| 1900 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 1901 | fmt::Display::fmt(&**self, f)
|
| 1902 | }
|
| 1903 | }
|
| 1904 |
|
| 1905 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1906 | impl<T: fmt::Debug + ?Sized, A: Allocator> fmt::Debug for Box<T, A> {
|
| 1907 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 1908 | fmt::Debug::fmt(&**self, f)
|
| 1909 | }
|
| 1910 | }
|
| 1911 |
|
| 1912 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1913 | impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
|
| 1914 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 1915 | |
| 1916 | |
| 1917 | let ptr: *const T = &**self;
|
| 1918 | fmt::Pointer::fmt(&ptr, f)
|
| 1919 | }
|
| 1920 | }
|
| 1921 |
|
| 1922 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1923 | impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
|
| 1924 | type Target = T;
|
| 1925 |
|
| 1926 | fn deref(&self) -> &T {
|
| 1927 | &**self
|
| 1928 | }
|
| 1929 | }
|
| 1930 |
|
| 1931 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1932 | impl<T: ?Sized, A: Allocator> DerefMut for Box<T, A> {
|
| 1933 | fn deref_mut(&mut self) -> &mut T {
|
| 1934 | &mut **self
|
| 1935 | }
|
| 1936 | }
|
| 1937 |
|
| 1938 | #[unstable(feature = "receiver_trait", issue = "none")]
|
| 1939 | impl<T: ?Sized, A: Allocator> Receiver for Box<T, A> {}
|
| 1940 |
|
| 1941 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1942 | impl<I: Iterator + ?Sized, A: Allocator> Iterator for Box<I, A> {
|
| 1943 | type Item = I::Item;
|
| 1944 | fn next(&mut self) -> Option<I::Item> {
|
| 1945 | (**self).next()
|
| 1946 | }
|
| 1947 | fn size_hint(&self) -> (usize, Option<usize>) {
|
| 1948 | (**self).size_hint()
|
| 1949 | }
|
| 1950 | fn nth(&mut self, n: usize) -> Option<I::Item> {
|
| 1951 | (**self).nth(n)
|
| 1952 | }
|
| 1953 | fn last(self) -> Option<I::Item> {
|
| 1954 | BoxIter::last(self)
|
| 1955 | }
|
| 1956 | }
|
| 1957 |
|
| 1958 | trait BoxIter {
|
| 1959 | type Item;
|
| 1960 | fn last(self) -> Option<Self::Item>;
|
| 1961 | }
|
| 1962 |
|
| 1963 | impl<I: Iterator + ?Sized, A: Allocator> BoxIter for Box<I, A> {
|
| 1964 | type Item = I::Item;
|
| 1965 | default fn last(self) -> Option<I::Item> {
|
| 1966 | #[inline]
|
| 1967 | fn some<T>(_: Option<T>, x: T) -> Option<T> {
|
| 1968 | Some(x)
|
| 1969 | }
|
| 1970 |
|
| 1971 | self.fold(None, some)
|
| 1972 | }
|
| 1973 | }
|
| 1974 |
|
| 1975 | |
| 1976 | |
| 1977 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1978 | impl<I: Iterator, A: Allocator> BoxIter for Box<I, A> {
|
| 1979 | fn last(self) -> Option<I::Item> {
|
| 1980 | (*self).last()
|
| 1981 | }
|
| 1982 | }
|
| 1983 |
|
| 1984 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1985 | impl<I: DoubleEndedIterator + ?Sized, A: Allocator> DoubleEndedIterator for Box<I, A> {
|
| 1986 | fn next_back(&mut self) -> Option<I::Item> {
|
| 1987 | (**self).next_back()
|
| 1988 | }
|
| 1989 | fn nth_back(&mut self, n: usize) -> Option<I::Item> {
|
| 1990 | (**self).nth_back(n)
|
| 1991 | }
|
| 1992 | }
|
| 1993 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1994 | impl<I: ExactSizeIterator + ?Sized, A: Allocator> ExactSizeIterator for Box<I, A> {
|
| 1995 | fn len(&self) -> usize {
|
| 1996 | (**self).len()
|
| 1997 | }
|
| 1998 | fn is_empty(&self) -> bool {
|
| 1999 | (**self).is_empty()
|
| 2000 | }
|
| 2001 | }
|
| 2002 |
|
| 2003 | #[stable(feature = "fused", since = "1.26.0")]
|
| 2004 | impl<I: FusedIterator + ?Sized, A: Allocator> FusedIterator for Box<I, A> {}
|
| 2005 |
|
| 2006 | #[stable(feature = "boxed_closure_impls", since = "1.35.0")]
|
| 2007 | impl<Args: Tuple, F: FnOnce<Args> + ?Sized, A: Allocator> FnOnce<Args> for Box<F, A> {
|
| 2008 | type Output = <F as FnOnce<Args>>::Output;
|
| 2009 |
|
| 2010 | extern "rust-call" fn call_once(self, args: Args) -> Self::Output {
|
| 2011 | <F as FnOnce<Args>>::call_once(*self, args)
|
| 2012 | }
|
| 2013 | }
|
| 2014 |
|
| 2015 | #[stable(feature = "boxed_closure_impls", since = "1.35.0")]
|
| 2016 | impl<Args: Tuple, F: FnMut<Args> + ?Sized, A: Allocator> FnMut<Args> for Box<F, A> {
|
| 2017 | extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output {
|
| 2018 | <F as FnMut<Args>>::call_mut(self, args)
|
| 2019 | }
|
| 2020 | }
|
| 2021 |
|
| 2022 | #[stable(feature = "boxed_closure_impls", since = "1.35.0")]
|
| 2023 | impl<Args: Tuple, F: Fn<Args> + ?Sized, A: Allocator> Fn<Args> for Box<F, A> {
|
| 2024 | extern "rust-call" fn call(&self, args: Args) -> Self::Output {
|
| 2025 | <F as Fn<Args>>::call(self, args)
|
| 2026 | }
|
| 2027 | }
|
| 2028 |
|
| 2029 | #[unstable(feature = "coerce_unsized", issue = "18598")]
|
| 2030 | impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}
|
| 2031 |
|
| 2032 | #[unstable(feature = "dispatch_from_dyn", issue = "none")]
|
| 2033 | impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T, Global> {}
|
| 2034 |
|
| 2035 | #[cfg(not(no_global_oom_handling))]
|
| 2036 | #[stable(feature = "boxed_slice_from_iter", since = "1.32.0")]
|
| 2037 | impl<I> FromIterator<I> for Box<[I]> {
|
| 2038 | fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
|
| 2039 | iter.into_iter().collect::<Vec<_>>().into_boxed_slice()
|
| 2040 | }
|
| 2041 | }
|
| 2042 |
|
| 2043 | #[cfg(not(no_global_oom_handling))]
|
| 2044 | #[stable(feature = "box_slice_clone", since = "1.3.0")]
|
| 2045 | impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
|
| 2046 | fn clone(&self) -> Self {
|
| 2047 | let alloc = Box::allocator(self).clone();
|
| 2048 | self.to_vec_in(alloc).into_boxed_slice()
|
| 2049 | }
|
| 2050 |
|
| 2051 | fn clone_from(&mut self, other: &Self) {
|
| 2052 | if self.len() == other.len() {
|
| 2053 | self.clone_from_slice(&other);
|
| 2054 | } else {
|
| 2055 | *self = other.clone();
|
| 2056 | }
|
| 2057 | }
|
| 2058 | }
|
| 2059 |
|
| 2060 | #[stable(feature = "box_borrow", since = "1.1.0")]
|
| 2061 | impl<T: ?Sized, A: Allocator> borrow::Borrow<T> for Box<T, A> {
|
| 2062 | fn borrow(&self) -> &T {
|
| 2063 | &**self
|
| 2064 | }
|
| 2065 | }
|
| 2066 |
|
| 2067 | #[stable(feature = "box_borrow", since = "1.1.0")]
|
| 2068 | impl<T: ?Sized, A: Allocator> borrow::BorrowMut<T> for Box<T, A> {
|
| 2069 | fn borrow_mut(&mut self) -> &mut T {
|
| 2070 | &mut **self
|
| 2071 | }
|
| 2072 | }
|
| 2073 |
|
| 2074 | #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
|
| 2075 | impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A> {
|
| 2076 | fn as_ref(&self) -> &T {
|
| 2077 | &**self
|
| 2078 | }
|
| 2079 | }
|
| 2080 |
|
| 2081 | #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
|
| 2082 | impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
|
| 2083 | fn as_mut(&mut self) -> &mut T {
|
| 2084 | &mut **self
|
| 2085 | }
|
| 2086 | }
|
| 2087 |
|
| 2088 | |
| 2089 | *
|
| 2090 | * We could have chosen not to add this impl, and instead have written a
|
| 2091 | * function of Pin<Box<T>> to Pin<T>. Such a function would not be sound,
|
| 2092 | * because Box<T> implements Unpin even when T does not, as a result of
|
| 2093 | * this impl.
|
| 2094 | *
|
| 2095 | * We chose this API instead of the alternative for a few reasons:
|
| 2096 | * - Logically, it is helpful to understand pinning in regard to the
|
| 2097 | * memory region being pointed to. For this reason none of the
|
| 2098 | * standard library pointer types support projecting through a pin
|
| 2099 | * (Box<T> is the only pointer type in std for which this would be
|
| 2100 | * safe.)
|
| 2101 | * - It is in practice very useful to have Box<T> be unconditionally
|
| 2102 | * Unpin because of trait objects, for which the structural auto
|
| 2103 | * trait functionality does not apply (e.g., Box<dyn Foo> would
|
| 2104 | * otherwise not be Unpin).
|
| 2105 | *
|
| 2106 | * Another type with the same semantics as Box but only a conditional
|
| 2107 | * implementation of `Unpin` (where `T: Unpin`) would be valid/safe, and
|
| 2108 | * could have a method to project a Pin<T> from it.
|
| 2109 |
|
| 2110 | #[stable(feature = "pin", since = "1.33.0")]
|
| 2111 | impl<T: ?Sized, A: Allocator> Unpin for Box<T, A> where A: 'static {}
|
| 2112 |
|
| 2113 | #[unstable(feature = "generator_trait", issue = "43122")]
|
| 2114 | impl<G: ?Sized + Generator<R> + Unpin, R, A: Allocator> Generator<R> for Box<G, A>
|
| 2115 | where
|
| 2116 | A: 'static,
|
| 2117 | {
|
| 2118 | type Yield = G::Yield;
|
| 2119 | type Return = G::Return;
|
| 2120 |
|
| 2121 | fn resume(mut self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return> {
|
| 2122 | G::resume(Pin::new(&mut *self), arg)
|
| 2123 | }
|
| 2124 | }
|
| 2125 |
|
| 2126 | #[unstable(feature = "generator_trait", issue = "43122")]
|
| 2127 | impl<G: ?Sized + Generator<R>, R, A: Allocator> Generator<R> for Pin<Box<G, A>>
|
| 2128 | where
|
| 2129 | A: 'static,
|
| 2130 | {
|
| 2131 | type Yield = G::Yield;
|
| 2132 | type Return = G::Return;
|
| 2133 |
|
| 2134 | fn resume(mut self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return> {
|
| 2135 | G::resume((*self).as_mut(), arg)
|
| 2136 | }
|
| 2137 | }
|
| 2138 |
|
| 2139 | #[stable(feature = "futures_api", since = "1.36.0")]
|
| 2140 | impl<F: ?Sized + Future + Unpin, A: Allocator> Future for Box<F, A>
|
| 2141 | where
|
| 2142 | A: 'static,
|
| 2143 | {
|
| 2144 | type Output = F::Output;
|
| 2145 |
|
| 2146 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
| 2147 | F::poll(Pin::new(&mut *self), cx)
|
| 2148 | }
|
| 2149 | }
|
| 2150 |
|
| 2151 | #[unstable(feature = "async_iterator", issue = "79024")]
|
| 2152 | impl<S: ?Sized + AsyncIterator + Unpin> AsyncIterator for Box<S> {
|
| 2153 | type Item = S::Item;
|
| 2154 |
|
| 2155 | fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
| 2156 | Pin::new(&mut **self).poll_next(cx)
|
| 2157 | }
|
| 2158 |
|
| 2159 | fn size_hint(&self) -> (usize, Option<usize>) {
|
| 2160 | (**self).size_hint()
|
| 2161 | }
|
| 2162 | }
|
| 2163 |
|
| 2164 | impl dyn Error {
|
| 2165 | #[inline]
|
| 2166 | #[stable(feature = "error_downcast", since = "1.3.0")]
|
| 2167 | #[rustc_allow_incoherent_impl]
|
| 2168 | |
| 2169 | pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
|
| 2170 | if self.is::<T>() {
|
| 2171 | unsafe {
|
| 2172 | let raw: *mut dyn Error = Box::into_raw(self);
|
| 2173 | Ok(Box::from_raw(raw as *mut T))
|
| 2174 | }
|
| 2175 | } else {
|
| 2176 | Err(self)
|
| 2177 | }
|
| 2178 | }
|
| 2179 | }
|
| 2180 |
|
| 2181 | impl dyn Error + Send {
|
| 2182 | #[inline]
|
| 2183 | #[stable(feature = "error_downcast", since = "1.3.0")]
|
| 2184 | #[rustc_allow_incoherent_impl]
|
| 2185 | |
| 2186 | pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error + Send>> {
|
| 2187 | let err: Box<dyn Error> = self;
|
| 2188 | <dyn Error>::downcast(err).map_err(|s| unsafe {
|
| 2189 | |
| 2190 | Box::from_raw(Box::into_raw(s) as *mut (dyn Error + Send))
|
| 2191 | })
|
| 2192 | }
|
| 2193 | }
|
| 2194 |
|
| 2195 | impl dyn Error + Send + Sync {
|
| 2196 | #[inline]
|
| 2197 | #[stable(feature = "error_downcast", since = "1.3.0")]
|
| 2198 | #[rustc_allow_incoherent_impl]
|
| 2199 | |
| 2200 | pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
|
| 2201 | let err: Box<dyn Error> = self;
|
| 2202 | <dyn Error>::downcast(err).map_err(|s| unsafe {
|
| 2203 | |
| 2204 | Box::from_raw(Box::into_raw(s) as *mut (dyn Error + Send + Sync))
|
| 2205 | })
|
| 2206 | }
|
| 2207 | }
|
| 2208 |
|
| 2209 | #[cfg(not(no_global_oom_handling))]
|
| 2210 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2211 | impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
|
| 2212 | |
| 2213 | |
| 2214 | |
| 2215 | |
| 2216 | |
| 2217 | |
| 2218 | |
| 2219 | |
| 2220 | |
| 2221 | |
| 2222 | |
| 2223 | |
| 2224 | |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | |
| 2229 | |
| 2230 | |
| 2231 | |
| 2232 | |
| 2233 | |
| 2234 | |
| 2235 | |
| 2236 | |
| 2237 | fn from(err: E) -> Box<dyn Error + 'a> {
|
| 2238 | Box::new(err)
|
| 2239 | }
|
| 2240 | }
|
| 2241 |
|
| 2242 | #[cfg(not(no_global_oom_handling))]
|
| 2243 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2244 | impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
|
| 2245 | |
| 2246 | |
| 2247 | |
| 2248 | |
| 2249 | |
| 2250 | |
| 2251 | |
| 2252 | |
| 2253 | |
| 2254 | |
| 2255 | |
| 2256 | |
| 2257 | |
| 2258 | |
| 2259 | |
| 2260 | |
| 2261 | |
| 2262 | |
| 2263 | |
| 2264 | |
| 2265 | |
| 2266 | |
| 2267 | |
| 2268 | |
| 2269 | |
| 2270 | |
| 2271 | |
| 2272 | |
| 2273 | |
| 2274 | |
| 2275 | |
| 2276 | fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
|
| 2277 | Box::new(err)
|
| 2278 | }
|
| 2279 | }
|
| 2280 |
|
| 2281 | #[cfg(not(no_global_oom_handling))]
|
| 2282 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2283 | impl From<String> for Box<dyn Error + Send + Sync> {
|
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | |
| 2288 | |
| 2289 | |
| 2290 | |
| 2291 | |
| 2292 | |
| 2293 | |
| 2294 | |
| 2295 | |
| 2296 | |
| 2297 | #[inline]
|
| 2298 | fn from(err: String) -> Box<dyn Error + Send + Sync> {
|
| 2299 | struct StringError(String);
|
| 2300 |
|
| 2301 | impl Error for StringError {
|
| 2302 | #[allow(deprecated)]
|
| 2303 | fn description(&self) -> &str {
|
| 2304 | &self.0
|
| 2305 | }
|
| 2306 | }
|
| 2307 |
|
| 2308 | impl fmt::Display for StringError {
|
| 2309 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 2310 | fmt::Display::fmt(&self.0, f)
|
| 2311 | }
|
| 2312 | }
|
| 2313 |
|
| 2314 | |
| 2315 | impl fmt::Debug for StringError {
|
| 2316 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 2317 | fmt::Debug::fmt(&self.0, f)
|
| 2318 | }
|
| 2319 | }
|
| 2320 |
|
| 2321 | Box::new(StringError(err))
|
| 2322 | }
|
| 2323 | }
|
| 2324 |
|
| 2325 | #[cfg(not(no_global_oom_handling))]
|
| 2326 | #[stable(feature = "string_box_error", since = "1.6.0")]
|
| 2327 | impl From<String> for Box<dyn Error> {
|
| 2328 | |
| 2329 | |
| 2330 | |
| 2331 | |
| 2332 | |
| 2333 | |
| 2334 | |
| 2335 | |
| 2336 | |
| 2337 | |
| 2338 | |
| 2339 | |
| 2340 | fn from(str_err: String) -> Box<dyn Error> {
|
| 2341 | let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
|
| 2342 | let err2: Box<dyn Error> = err1;
|
| 2343 | err2
|
| 2344 | }
|
| 2345 | }
|
| 2346 |
|
| 2347 | #[cfg(not(no_global_oom_handling))]
|
| 2348 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2349 | impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
|
| 2350 | |
| 2351 | |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | |
| 2356 | |
| 2357 | |
| 2358 | |
| 2359 | |
| 2360 | |
| 2361 | |
| 2362 | |
| 2363 | |
| 2364 | |
| 2365 | #[inline]
|
| 2366 | fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
|
| 2367 | From::from(String::from(err))
|
| 2368 | }
|
| 2369 | }
|
| 2370 |
|
| 2371 | #[cfg(not(no_global_oom_handling))]
|
| 2372 | #[stable(feature = "string_box_error", since = "1.6.0")]
|
| 2373 | impl From<&str> for Box<dyn Error> {
|
| 2374 | |
| 2375 | |
| 2376 | |
| 2377 | |
| 2378 | |
| 2379 | |
| 2380 | |
| 2381 | |
| 2382 | |
| 2383 | |
| 2384 | |
| 2385 | |
| 2386 | |
| 2387 | |
| 2388 | fn from(err: &str) -> Box<dyn Error> {
|
| 2389 | From::from(String::from(err))
|
| 2390 | }
|
| 2391 | }
|
| 2392 |
|
| 2393 | #[cfg(not(no_global_oom_handling))]
|
| 2394 | #[stable(feature = "cow_box_error", since = "1.22.0")]
|
| 2395 | impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
|
| 2396 | |
| 2397 | |
| 2398 | |
| 2399 | |
| 2400 | |
| 2401 | |
| 2402 | |
| 2403 | |
| 2404 | |
| 2405 | |
| 2406 | |
| 2407 | |
| 2408 | |
| 2409 | |
| 2410 | fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
|
| 2411 | From::from(String::from(err))
|
| 2412 | }
|
| 2413 | }
|
| 2414 |
|
| 2415 | #[cfg(not(no_global_oom_handling))]
|
| 2416 | #[stable(feature = "cow_box_error", since = "1.22.0")]
|
| 2417 | impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
|
| 2418 | |
| 2419 | |
| 2420 | |
| 2421 | |
| 2422 | |
| 2423 | |
| 2424 | |
| 2425 | |
| 2426 | |
| 2427 | |
| 2428 | |
| 2429 | |
| 2430 | |
| 2431 | fn from(err: Cow<'a, str>) -> Box<dyn Error> {
|
| 2432 | From::from(String::from(err))
|
| 2433 | }
|
| 2434 | }
|
| 2435 |
|
| 2436 | #[stable(feature = "box_error", since = "1.8.0")]
|
| 2437 | impl<T: core::error::Error> core::error::Error for Box<T> {
|
| 2438 | #[allow(deprecated, deprecated_in_future)]
|
| 2439 | fn description(&self) -> &str {
|
| 2440 | core::error::Error::description(&**self)
|
| 2441 | }
|
| 2442 |
|
| 2443 | #[allow(deprecated)]
|
| 2444 | fn cause(&self) -> Option<&dyn core::error::Error> {
|
| 2445 | core::error::Error::cause(&**self)
|
| 2446 | }
|
| 2447 |
|
| 2448 | fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
|
| 2449 | core::error::Error::source(&**self)
|
| 2450 | }
|
| 2451 | }
|