| 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 | #![stable(feature = "rust1", since = "1.0.0")]
|
| 57 |
|
| 58 | #[cfg(not(no_global_oom_handling))]
|
| 59 | use core::cmp;
|
| 60 | use core::cmp::Ordering;
|
| 61 | use core::convert::TryFrom;
|
| 62 | use core::fmt;
|
| 63 | use core::hash::{Hash, Hasher};
|
| 64 | use core::intrinsics::{arith_offset, assume};
|
| 65 | use core::iter;
|
| 66 | #[cfg(not(no_global_oom_handling))]
|
| 67 | use core::iter::FromIterator;
|
| 68 | use core::marker::PhantomData;
|
| 69 | use core::mem::{self, ManuallyDrop, MaybeUninit};
|
| 70 | use core::ops::{self, Index, IndexMut, Range, RangeBounds};
|
| 71 | use core::ptr::{self, NonNull};
|
| 72 | use core::slice::{self, SliceIndex};
|
| 73 |
|
| 74 | use crate::alloc::{Allocator, Global};
|
| 75 | #[cfg(not(no_borrow))]
|
| 76 | use crate::borrow::{Cow, ToOwned};
|
| 77 | use crate::boxed::Box;
|
| 78 | use crate::collections::TryReserveError;
|
| 79 | use crate::raw_vec::RawVec;
|
| 80 |
|
| 81 | #[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")]
|
| 82 | pub use self::drain_filter::DrainFilter;
|
| 83 |
|
| 84 | mod drain_filter;
|
| 85 |
|
| 86 | #[cfg(not(no_global_oom_handling))]
|
| 87 | #[stable(feature = "vec_splice", since = "1.21.0")]
|
| 88 | pub use self::splice::Splice;
|
| 89 |
|
| 90 | #[cfg(not(no_global_oom_handling))]
|
| 91 | mod splice;
|
| 92 |
|
| 93 | #[stable(feature = "drain", since = "1.6.0")]
|
| 94 | pub use self::drain::Drain;
|
| 95 |
|
| 96 | mod drain;
|
| 97 |
|
| 98 | #[cfg(not(no_borrow))]
|
| 99 | #[cfg(not(no_global_oom_handling))]
|
| 100 | mod cow;
|
| 101 |
|
| 102 | #[cfg(not(no_global_oom_handling))]
|
| 103 | pub(crate) use self::in_place_collect::AsVecIntoIter;
|
| 104 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 105 | pub use self::into_iter::IntoIter;
|
| 106 |
|
| 107 | mod into_iter;
|
| 108 |
|
| 109 | #[cfg(not(no_global_oom_handling))]
|
| 110 | use self::is_zero::IsZero;
|
| 111 |
|
| 112 | mod is_zero;
|
| 113 |
|
| 114 | #[cfg(not(no_global_oom_handling))]
|
| 115 | mod in_place_collect;
|
| 116 |
|
| 117 | mod partial_eq;
|
| 118 |
|
| 119 | #[cfg(not(no_global_oom_handling))]
|
| 120 | use self::spec_from_elem::SpecFromElem;
|
| 121 |
|
| 122 | #[cfg(not(no_global_oom_handling))]
|
| 123 | mod spec_from_elem;
|
| 124 |
|
| 125 | #[cfg(not(no_global_oom_handling))]
|
| 126 | use self::set_len_on_drop::SetLenOnDrop;
|
| 127 |
|
| 128 | #[cfg(not(no_global_oom_handling))]
|
| 129 | mod set_len_on_drop;
|
| 130 |
|
| 131 | #[cfg(not(no_global_oom_handling))]
|
| 132 | use self::in_place_drop::InPlaceDrop;
|
| 133 |
|
| 134 | #[cfg(not(no_global_oom_handling))]
|
| 135 | mod in_place_drop;
|
| 136 |
|
| 137 | #[cfg(not(no_global_oom_handling))]
|
| 138 | use self::spec_from_iter_nested::SpecFromIterNested;
|
| 139 |
|
| 140 | #[cfg(not(no_global_oom_handling))]
|
| 141 | mod spec_from_iter_nested;
|
| 142 |
|
| 143 | #[cfg(not(no_global_oom_handling))]
|
| 144 | use self::spec_from_iter::SpecFromIter;
|
| 145 |
|
| 146 | #[cfg(not(no_global_oom_handling))]
|
| 147 | mod spec_from_iter;
|
| 148 |
|
| 149 | #[cfg(not(no_global_oom_handling))]
|
| 150 | use self::spec_extend::SpecExtend;
|
| 151 |
|
| 152 | #[cfg(not(no_global_oom_handling))]
|
| 153 | mod spec_extend;
|
| 154 |
|
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 402 | #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")]
|
| 403 | #[rustc_insignificant_dtor]
|
| 404 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
|
| 405 | buf: RawVec<T, A>,
|
| 406 | len: usize,
|
| 407 | }
|
| 408 |
|
| 409 | |
| 410 | |
| 411 | |
| 412 |
|
| 413 | impl<T> Vec<T> {
|
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | #[inline]
|
| 425 | #[rustc_const_stable(feature = "const_vec_new", since = "1.39.0")]
|
| 426 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 427 | #[must_use]
|
| 428 | pub const fn new() -> Self {
|
| 429 | Vec { buf: RawVec::NEW, len: 0 }
|
| 430 | }
|
| 431 |
|
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 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 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | #[cfg(not(no_global_oom_handling))]
|
| 470 | #[inline]
|
| 471 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 472 | #[must_use]
|
| 473 | pub fn with_capacity(capacity: usize) -> Self {
|
| 474 | Self::with_capacity_in(capacity, Global)
|
| 475 | }
|
| 476 |
|
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | #[inline]
|
| 514 | #[stable(feature = "kernel", since = "1.0.0")]
|
| 515 | pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
|
| 516 | Self::try_with_capacity_in(capacity, Global)
|
| 517 | }
|
| 518 |
|
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 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 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | #[inline]
|
| 588 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 589 | pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
|
| 590 | unsafe { Self::from_raw_parts_in(ptr, length, capacity, Global) }
|
| 591 | }
|
| 592 | }
|
| 593 |
|
| 594 | impl<T, A: Allocator> Vec<T, A> {
|
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | #[inline]
|
| 610 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 611 | pub const fn new_in(alloc: A) -> Self {
|
| 612 | Vec { buf: RawVec::new_in(alloc), len: 0 }
|
| 613 | }
|
| 614 |
|
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | #[cfg(not(no_global_oom_handling))]
|
| 658 | #[inline]
|
| 659 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 660 | pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
|
| 661 | Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 }
|
| 662 | }
|
| 663 |
|
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | #[inline]
|
| 706 | #[stable(feature = "kernel", since = "1.0.0")]
|
| 707 | pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
|
| 708 | Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 })
|
| 709 | }
|
| 710 |
|
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | #[inline]
|
| 782 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 783 | pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self {
|
| 784 | unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } }
|
| 785 | }
|
| 786 |
|
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
|
| 820 | pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
|
| 821 | let mut me = ManuallyDrop::new(self);
|
| 822 | (me.as_mut_ptr(), me.len(), me.capacity())
|
| 823 | }
|
| 824 |
|
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 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 | |
| 861 | |
| 862 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 863 | |
| 864 | pub fn into_raw_parts_with_alloc(self) -> (*mut T, usize, usize, A) {
|
| 865 | let mut me = ManuallyDrop::new(self);
|
| 866 | let len = me.len();
|
| 867 | let capacity = me.capacity();
|
| 868 | let ptr = me.as_mut_ptr();
|
| 869 | let alloc = unsafe { ptr::read(me.allocator()) };
|
| 870 | (ptr, len, capacity, alloc)
|
| 871 | }
|
| 872 |
|
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | #[inline]
|
| 883 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 884 | pub fn capacity(&self) -> usize {
|
| 885 | self.buf.capacity()
|
| 886 | }
|
| 887 |
|
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | #[cfg(not(no_global_oom_handling))]
|
| 906 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 907 | pub fn reserve(&mut self, additional: usize) {
|
| 908 | self.buf.reserve(self.len, additional);
|
| 909 | }
|
| 910 |
|
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | #[cfg(not(no_global_oom_handling))]
|
| 934 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 935 | pub fn reserve_exact(&mut self, additional: usize) {
|
| 936 | self.buf.reserve_exact(self.len, additional);
|
| 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 | #[stable(feature = "try_reserve", since = "1.57.0")]
|
| 971 | pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
|
| 972 | self.buf.try_reserve(self.len, additional)
|
| 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 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | #[stable(feature = "try_reserve", since = "1.57.0")]
|
| 1013 | pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
|
| 1014 | self.buf.try_reserve_exact(self.len, additional)
|
| 1015 | }
|
| 1016 |
|
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | #[cfg(not(no_global_oom_handling))]
|
| 1032 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1033 | pub fn shrink_to_fit(&mut self) {
|
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | if self.capacity() > self.len {
|
| 1038 | self.buf.shrink_to_fit(self.len);
|
| 1039 | }
|
| 1040 | }
|
| 1041 |
|
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | #[cfg(not(no_global_oom_handling))]
|
| 1061 | #[stable(feature = "shrink_to", since = "1.56.0")]
|
| 1062 | pub fn shrink_to(&mut self, min_capacity: usize) {
|
| 1063 | if self.capacity() > min_capacity {
|
| 1064 | self.buf.shrink_to_fit(cmp::max(self.len, min_capacity));
|
| 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 | #[cfg(not(no_global_oom_handling))]
|
| 1093 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1094 | pub fn into_boxed_slice(mut self) -> Box<[T], A> {
|
| 1095 | unsafe {
|
| 1096 | self.shrink_to_fit();
|
| 1097 | let me = ManuallyDrop::new(self);
|
| 1098 | let buf = ptr::read(&me.buf);
|
| 1099 | let len = me.len();
|
| 1100 | buf.into_box(len).assume_init()
|
| 1101 | }
|
| 1102 | }
|
| 1103 |
|
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | |
| 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 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1147 | pub fn truncate(&mut self, len: usize) {
|
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | unsafe {
|
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | if len > self.len {
|
| 1160 | return;
|
| 1161 | }
|
| 1162 | let remaining_len = self.len - len;
|
| 1163 | let s = ptr::slice_from_raw_parts_mut(self.as_mut_ptr().add(len), remaining_len);
|
| 1164 | self.len = len;
|
| 1165 | ptr::drop_in_place(s);
|
| 1166 | }
|
| 1167 | }
|
| 1168 |
|
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | #[inline]
|
| 1181 | #[stable(feature = "vec_as_slice", since = "1.7.0")]
|
| 1182 | pub fn as_slice(&self) -> &[T] {
|
| 1183 | self
|
| 1184 | }
|
| 1185 |
|
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | #[inline]
|
| 1198 | #[stable(feature = "vec_as_slice", since = "1.7.0")]
|
| 1199 | pub fn as_mut_slice(&mut self) -> &mut [T] {
|
| 1200 | self
|
| 1201 | }
|
| 1202 |
|
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | #[stable(feature = "vec_as_ptr", since = "1.37.0")]
|
| 1229 | #[inline]
|
| 1230 | pub fn as_ptr(&self) -> *const T {
|
| 1231 | |
| 1232 | |
| 1233 | let ptr = self.buf.ptr();
|
| 1234 | unsafe {
|
| 1235 | assume(!ptr.is_null());
|
| 1236 | }
|
| 1237 | ptr
|
| 1238 | }
|
| 1239 |
|
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | #[stable(feature = "vec_as_ptr", since = "1.37.0")]
|
| 1265 | #[inline]
|
| 1266 | pub fn as_mut_ptr(&mut self) -> *mut T {
|
| 1267 | |
| 1268 | |
| 1269 | let ptr = self.buf.ptr();
|
| 1270 | unsafe {
|
| 1271 | assume(!ptr.is_null());
|
| 1272 | }
|
| 1273 | ptr
|
| 1274 | }
|
| 1275 |
|
| 1276 | |
| 1277 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 1278 | #[inline]
|
| 1279 | pub fn allocator(&self) -> &A {
|
| 1280 | self.buf.allocator()
|
| 1281 | }
|
| 1282 |
|
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | #[inline]
|
| 1362 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1363 | pub unsafe fn set_len(&mut self, new_len: usize) {
|
| 1364 | debug_assert!(new_len <= self.capacity());
|
| 1365 |
|
| 1366 | self.len = new_len;
|
| 1367 | }
|
| 1368 |
|
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | |
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | #[inline]
|
| 1394 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1395 | pub fn swap_remove(&mut self, index: usize) -> T {
|
| 1396 | #[cold]
|
| 1397 | #[inline(never)]
|
| 1398 | fn assert_failed(index: usize, len: usize) -> ! {
|
| 1399 | panic!("swap_remove index (is {index}) should be < len (is {len})");
|
| 1400 | }
|
| 1401 |
|
| 1402 | let len = self.len();
|
| 1403 | if index >= len {
|
| 1404 | assert_failed(index, len);
|
| 1405 | }
|
| 1406 | unsafe {
|
| 1407 | |
| 1408 | |
| 1409 | |
| 1410 | let value = ptr::read(self.as_ptr().add(index));
|
| 1411 | let base_ptr = self.as_mut_ptr();
|
| 1412 | ptr::copy(base_ptr.add(len - 1), base_ptr.add(index), 1);
|
| 1413 | self.set_len(len - 1);
|
| 1414 | value
|
| 1415 | }
|
| 1416 | }
|
| 1417 |
|
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | #[cfg(not(no_global_oom_handling))]
|
| 1435 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1436 | pub fn insert(&mut self, index: usize, element: T) {
|
| 1437 | #[cold]
|
| 1438 | #[inline(never)]
|
| 1439 | fn assert_failed(index: usize, len: usize) -> ! {
|
| 1440 | panic!("insertion index (is {index}) should be <= len (is {len})");
|
| 1441 | }
|
| 1442 |
|
| 1443 | let len = self.len();
|
| 1444 | if index > len {
|
| 1445 | assert_failed(index, len);
|
| 1446 | }
|
| 1447 |
|
| 1448 | |
| 1449 | if len == self.buf.capacity() {
|
| 1450 | self.reserve(1);
|
| 1451 | }
|
| 1452 |
|
| 1453 | unsafe {
|
| 1454 | |
| 1455 | |
| 1456 | {
|
| 1457 | let p = self.as_mut_ptr().add(index);
|
| 1458 | |
| 1459 | |
| 1460 | ptr::copy(p, p.offset(1), len - index);
|
| 1461 | |
| 1462 | |
| 1463 | ptr::write(p, element);
|
| 1464 | }
|
| 1465 | self.set_len(len + 1);
|
| 1466 | }
|
| 1467 | }
|
| 1468 |
|
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 | |
| 1478 | |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1493 | #[track_caller]
|
| 1494 | pub fn remove(&mut self, index: usize) -> T {
|
| 1495 | #[cold]
|
| 1496 | #[inline(never)]
|
| 1497 | #[track_caller]
|
| 1498 | fn assert_failed(index: usize, len: usize) -> ! {
|
| 1499 | panic!("removal index (is {index}) should be < len (is {len})");
|
| 1500 | }
|
| 1501 |
|
| 1502 | let len = self.len();
|
| 1503 | if index >= len {
|
| 1504 | assert_failed(index, len);
|
| 1505 | }
|
| 1506 | unsafe {
|
| 1507 | |
| 1508 | let ret;
|
| 1509 | {
|
| 1510 | |
| 1511 | let ptr = self.as_mut_ptr().add(index);
|
| 1512 | |
| 1513 | |
| 1514 | ret = ptr::read(ptr);
|
| 1515 |
|
| 1516 | |
| 1517 | ptr::copy(ptr.offset(1), ptr, len - index - 1);
|
| 1518 | }
|
| 1519 | self.set_len(len - 1);
|
| 1520 | ret
|
| 1521 | }
|
| 1522 | }
|
| 1523 |
|
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | |
| 1545 | |
| 1546 | |
| 1547 | |
| 1548 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1549 | pub fn retain<F>(&mut self, mut f: F)
|
| 1550 | where
|
| 1551 | F: FnMut(&T) -> bool,
|
| 1552 | {
|
| 1553 | self.retain_mut(|elem| f(elem));
|
| 1554 | }
|
| 1555 |
|
| 1556 | |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | |
| 1572 | |
| 1573 | |
| 1574 | #[stable(feature = "vec_retain_mut", since = "1.61.0")]
|
| 1575 | pub fn retain_mut<F>(&mut self, mut f: F)
|
| 1576 | where
|
| 1577 | F: FnMut(&mut T) -> bool,
|
| 1578 | {
|
| 1579 | let original_len = self.len();
|
| 1580 | |
| 1581 | |
| 1582 | unsafe { self.set_len(0) };
|
| 1583 |
|
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | struct BackshiftOnDrop<'a, T, A: Allocator> {
|
| 1596 | v: &'a mut Vec<T, A>,
|
| 1597 | processed_len: usize,
|
| 1598 | deleted_cnt: usize,
|
| 1599 | original_len: usize,
|
| 1600 | }
|
| 1601 |
|
| 1602 | impl<T, A: Allocator> Drop for BackshiftOnDrop<'_, T, A> {
|
| 1603 | fn drop(&mut self) {
|
| 1604 | if self.deleted_cnt > 0 {
|
| 1605 | |
| 1606 | unsafe {
|
| 1607 | ptr::copy(
|
| 1608 | self.v.as_ptr().add(self.processed_len),
|
| 1609 | self.v.as_mut_ptr().add(self.processed_len - self.deleted_cnt),
|
| 1610 | self.original_len - self.processed_len,
|
| 1611 | );
|
| 1612 | }
|
| 1613 | }
|
| 1614 | |
| 1615 | unsafe {
|
| 1616 | self.v.set_len(self.original_len - self.deleted_cnt);
|
| 1617 | }
|
| 1618 | }
|
| 1619 | }
|
| 1620 |
|
| 1621 | let mut g = BackshiftOnDrop { v: self, processed_len: 0, deleted_cnt: 0, original_len };
|
| 1622 |
|
| 1623 | fn process_loop<F, T, A: Allocator, const DELETED: bool>(
|
| 1624 | original_len: usize,
|
| 1625 | f: &mut F,
|
| 1626 | g: &mut BackshiftOnDrop<'_, T, A>,
|
| 1627 | ) where
|
| 1628 | F: FnMut(&mut T) -> bool,
|
| 1629 | {
|
| 1630 | while g.processed_len != original_len {
|
| 1631 | |
| 1632 | let cur = unsafe { &mut *g.v.as_mut_ptr().add(g.processed_len) };
|
| 1633 | if !f(cur) {
|
| 1634 | |
| 1635 | g.processed_len += 1;
|
| 1636 | g.deleted_cnt += 1;
|
| 1637 | |
| 1638 | unsafe { ptr::drop_in_place(cur) };
|
| 1639 | |
| 1640 | if DELETED {
|
| 1641 | continue;
|
| 1642 | } else {
|
| 1643 | break;
|
| 1644 | }
|
| 1645 | }
|
| 1646 | if DELETED {
|
| 1647 | |
| 1648 | |
| 1649 | unsafe {
|
| 1650 | let hole_slot = g.v.as_mut_ptr().add(g.processed_len - g.deleted_cnt);
|
| 1651 | ptr::copy_nonoverlapping(cur, hole_slot, 1);
|
| 1652 | }
|
| 1653 | }
|
| 1654 | g.processed_len += 1;
|
| 1655 | }
|
| 1656 | }
|
| 1657 |
|
| 1658 | |
| 1659 | process_loop::<F, T, A, false>(original_len, &mut f, &mut g);
|
| 1660 |
|
| 1661 | |
| 1662 | process_loop::<F, T, A, true>(original_len, &mut f, &mut g);
|
| 1663 |
|
| 1664 | |
| 1665 | drop(g);
|
| 1666 | }
|
| 1667 |
|
| 1668 | |
| 1669 | |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | |
| 1679 | |
| 1680 | |
| 1681 | |
| 1682 | #[stable(feature = "dedup_by", since = "1.16.0")]
|
| 1683 | #[inline]
|
| 1684 | pub fn dedup_by_key<F, K>(&mut self, mut key: F)
|
| 1685 | where
|
| 1686 | F: FnMut(&mut T) -> K,
|
| 1687 | K: PartialEq,
|
| 1688 | {
|
| 1689 | self.dedup_by(|a, b| key(a) == key(b))
|
| 1690 | }
|
| 1691 |
|
| 1692 | |
| 1693 | |
| 1694 | |
| 1695 | |
| 1696 | |
| 1697 | |
| 1698 | |
| 1699 | |
| 1700 | |
| 1701 | |
| 1702 | |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | #[stable(feature = "dedup_by", since = "1.16.0")]
|
| 1711 | pub fn dedup_by<F>(&mut self, mut same_bucket: F)
|
| 1712 | where
|
| 1713 | F: FnMut(&mut T, &mut T) -> bool,
|
| 1714 | {
|
| 1715 | let len = self.len();
|
| 1716 | if len <= 1 {
|
| 1717 | return;
|
| 1718 | }
|
| 1719 |
|
| 1720 |
|
| 1721 | struct FillGapOnDrop<'a, T, A: core::alloc::Allocator> {
|
| 1722 |
|
| 1723 | read: usize,
|
| 1724 |
|
| 1725 | |
| 1726 | * when we find it.
|
| 1727 | write: usize,
|
| 1728 |
|
| 1729 |
|
| 1730 | vec: &'a mut Vec<T, A>,
|
| 1731 | }
|
| 1732 |
|
| 1733 | impl<'a, T, A: core::alloc::Allocator> Drop for FillGapOnDrop<'a, T, A> {
|
| 1734 | fn drop(&mut self) {
|
| 1735 |
|
| 1736 |
|
| 1737 | |
| 1738 | * and `len - read` never overflow and that the copy is always
|
| 1739 | * in-bounds.
|
| 1740 | unsafe {
|
| 1741 | let ptr = self.vec.as_mut_ptr();
|
| 1742 | let len = self.vec.len();
|
| 1743 |
|
| 1744 | |
| 1745 | * Basically vec[read..].len()
|
| 1746 | let items_left = len.wrapping_sub(self.read);
|
| 1747 |
|
| 1748 |
|
| 1749 | let dropped_ptr = ptr.add(self.write);
|
| 1750 |
|
| 1751 | let valid_ptr = ptr.add(self.read);
|
| 1752 |
|
| 1753 | |
| 1754 | * The slices can overlap, so `copy_nonoverlapping` cannot be used
|
| 1755 | ptr::copy(valid_ptr, dropped_ptr, items_left);
|
| 1756 |
|
| 1757 | |
| 1758 | * Basically vec[read..write].len()
|
| 1759 | let dropped = self.read.wrapping_sub(self.write);
|
| 1760 |
|
| 1761 | self.vec.set_len(len - dropped);
|
| 1762 | }
|
| 1763 | }
|
| 1764 | }
|
| 1765 |
|
| 1766 | let mut gap = FillGapOnDrop { read: 1, write: 1, vec: self };
|
| 1767 | let ptr = gap.vec.as_mut_ptr();
|
| 1768 |
|
| 1769 | |
| 1770 | * doing slice partition_dedup + truncate
|
| 1771 |
|
| 1772 | |
| 1773 | * are always in-bounds and read_ptr never aliases prev_ptr
|
| 1774 | unsafe {
|
| 1775 | while gap.read < len {
|
| 1776 | let read_ptr = ptr.add(gap.read);
|
| 1777 | let prev_ptr = ptr.add(gap.write.wrapping_sub(1));
|
| 1778 |
|
| 1779 | if same_bucket(&mut *read_ptr, &mut *prev_ptr) {
|
| 1780 | |
| 1781 | gap.read += 1;
|
| 1782 |
|
| 1783 | ptr::drop_in_place(read_ptr);
|
| 1784 | } else {
|
| 1785 | let write_ptr = ptr.add(gap.write);
|
| 1786 |
|
| 1787 | |
| 1788 | * have to use `copy` or conditional `copy_nonoverlapping`.
|
| 1789 | * Looks like the first option is faster.
|
| 1790 | ptr::copy(read_ptr, write_ptr, 1);
|
| 1791 |
|
| 1792 |
|
| 1793 | gap.write += 1;
|
| 1794 | gap.read += 1;
|
| 1795 | }
|
| 1796 | }
|
| 1797 |
|
| 1798 | |
| 1799 | * when `same_bucket` is guaranteed to not panic, this bloats a little
|
| 1800 | * the codegen, so we just do it manually
|
| 1801 | gap.vec.set_len(gap.write);
|
| 1802 | mem::forget(gap);
|
| 1803 | }
|
| 1804 | }
|
| 1805 |
|
| 1806 | |
| 1807 | |
| 1808 | |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | |
| 1813 | |
| 1814 | |
| 1815 | |
| 1816 | |
| 1817 | |
| 1818 | |
| 1819 | #[cfg(not(no_global_oom_handling))]
|
| 1820 | #[inline]
|
| 1821 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1822 | pub fn push(&mut self, value: T) {
|
| 1823 | |
| 1824 | |
| 1825 | if self.len == self.buf.capacity() {
|
| 1826 | self.buf.reserve_for_push(self.len);
|
| 1827 | }
|
| 1828 | unsafe {
|
| 1829 | let end = self.as_mut_ptr().add(self.len);
|
| 1830 | ptr::write(end, value);
|
| 1831 | self.len += 1;
|
| 1832 | }
|
| 1833 | }
|
| 1834 |
|
| 1835 | |
| 1836 | |
| 1837 | |
| 1838 | |
| 1839 | |
| 1840 | |
| 1841 | |
| 1842 | |
| 1843 | |
| 1844 | #[inline]
|
| 1845 | #[stable(feature = "kernel", since = "1.0.0")]
|
| 1846 | pub fn try_push(&mut self, value: T) -> Result<(), TryReserveError> {
|
| 1847 | if self.len == self.buf.capacity() {
|
| 1848 | self.buf.try_reserve_for_push(self.len)?;
|
| 1849 | }
|
| 1850 | unsafe {
|
| 1851 | let end = self.as_mut_ptr().add(self.len);
|
| 1852 | ptr::write(end, value);
|
| 1853 | self.len += 1;
|
| 1854 | }
|
| 1855 | Ok(())
|
| 1856 | }
|
| 1857 |
|
| 1858 | |
| 1859 | |
| 1860 | |
| 1861 | |
| 1862 | |
| 1863 | |
| 1864 | |
| 1865 | |
| 1866 | |
| 1867 | |
| 1868 | |
| 1869 | |
| 1870 | |
| 1871 | |
| 1872 | |
| 1873 | #[inline]
|
| 1874 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 1875 | pub fn pop(&mut self) -> Option<T> {
|
| 1876 | if self.len == 0 {
|
| 1877 | None
|
| 1878 | } else {
|
| 1879 | unsafe {
|
| 1880 | self.len -= 1;
|
| 1881 | Some(ptr::read(self.as_ptr().add(self.len())))
|
| 1882 | }
|
| 1883 | }
|
| 1884 | }
|
| 1885 |
|
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | |
| 1897 | |
| 1898 | |
| 1899 | |
| 1900 | |
| 1901 | #[cfg(not(no_global_oom_handling))]
|
| 1902 | #[inline]
|
| 1903 | #[stable(feature = "append", since = "1.4.0")]
|
| 1904 | pub fn append(&mut self, other: &mut Self) {
|
| 1905 | unsafe {
|
| 1906 | self.append_elements(other.as_slice() as _);
|
| 1907 | other.set_len(0);
|
| 1908 | }
|
| 1909 | }
|
| 1910 |
|
| 1911 | |
| 1912 | #[cfg(not(no_global_oom_handling))]
|
| 1913 | #[inline]
|
| 1914 | unsafe fn append_elements(&mut self, other: *const [T]) {
|
| 1915 | let count = unsafe { (*other).len() };
|
| 1916 | self.reserve(count);
|
| 1917 | let len = self.len();
|
| 1918 | unsafe { ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count) };
|
| 1919 | self.len += count;
|
| 1920 | }
|
| 1921 |
|
| 1922 | |
| 1923 | |
| 1924 | |
| 1925 | |
| 1926 | |
| 1927 | |
| 1928 | |
| 1929 | |
| 1930 | |
| 1931 | |
| 1932 | |
| 1933 | |
| 1934 | |
| 1935 | |
| 1936 | |
| 1937 | |
| 1938 | |
| 1939 | |
| 1940 | |
| 1941 | |
| 1942 | |
| 1943 | |
| 1944 | |
| 1945 | |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | |
| 1950 | |
| 1951 | |
| 1952 | #[stable(feature = "drain", since = "1.6.0")]
|
| 1953 | pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A>
|
| 1954 | where
|
| 1955 | R: RangeBounds<usize>,
|
| 1956 | {
|
| 1957 | |
| 1958 | |
| 1959 | |
| 1960 | |
| 1961 | |
| 1962 | |
| 1963 | |
| 1964 | |
| 1965 | |
| 1966 | |
| 1967 | let len = self.len();
|
| 1968 | let Range { start, end } = slice::range(range, ..len);
|
| 1969 |
|
| 1970 | unsafe {
|
| 1971 | |
| 1972 | self.set_len(start);
|
| 1973 | |
| 1974 | |
| 1975 | let range_slice = slice::from_raw_parts_mut(self.as_mut_ptr().add(start), end - start);
|
| 1976 | Drain {
|
| 1977 | tail_start: end,
|
| 1978 | tail_len: len - end,
|
| 1979 | iter: range_slice.iter(),
|
| 1980 | vec: NonNull::from(self),
|
| 1981 | }
|
| 1982 | }
|
| 1983 | }
|
| 1984 |
|
| 1985 | |
| 1986 | |
| 1987 | |
| 1988 | |
| 1989 | |
| 1990 | |
| 1991 | |
| 1992 | |
| 1993 | |
| 1994 | |
| 1995 | |
| 1996 | |
| 1997 | |
| 1998 | |
| 1999 | #[inline]
|
| 2000 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2001 | pub fn clear(&mut self) {
|
| 2002 | let elems: *mut [T] = self.as_mut_slice();
|
| 2003 |
|
| 2004 | |
| 2005 | |
| 2006 | |
| 2007 | |
| 2008 | |
| 2009 | |
| 2010 | unsafe {
|
| 2011 | self.len = 0;
|
| 2012 | ptr::drop_in_place(elems);
|
| 2013 | }
|
| 2014 | }
|
| 2015 |
|
| 2016 | |
| 2017 | |
| 2018 | |
| 2019 | |
| 2020 | |
| 2021 | |
| 2022 | |
| 2023 | |
| 2024 | |
| 2025 | #[inline]
|
| 2026 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2027 | pub fn len(&self) -> usize {
|
| 2028 | self.len
|
| 2029 | }
|
| 2030 |
|
| 2031 | |
| 2032 | |
| 2033 | |
| 2034 | |
| 2035 | |
| 2036 | |
| 2037 | |
| 2038 | |
| 2039 | |
| 2040 | |
| 2041 | |
| 2042 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2043 | pub fn is_empty(&self) -> bool {
|
| 2044 | self.len() == 0
|
| 2045 | }
|
| 2046 |
|
| 2047 | |
| 2048 | |
| 2049 | |
| 2050 | |
| 2051 | |
| 2052 | |
| 2053 | |
| 2054 | |
| 2055 | |
| 2056 | |
| 2057 | |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | |
| 2062 | |
| 2063 | |
| 2064 | |
| 2065 | #[cfg(not(no_global_oom_handling))]
|
| 2066 | #[inline]
|
| 2067 | #[must_use = "use `.truncate()` if you don't need the other half"]
|
| 2068 | #[stable(feature = "split_off", since = "1.4.0")]
|
| 2069 | pub fn split_off(&mut self, at: usize) -> Self
|
| 2070 | where
|
| 2071 | A: Clone,
|
| 2072 | {
|
| 2073 | #[cold]
|
| 2074 | #[inline(never)]
|
| 2075 | fn assert_failed(at: usize, len: usize) -> ! {
|
| 2076 | panic!("`at` split index (is {at}) should be <= len (is {len})");
|
| 2077 | }
|
| 2078 |
|
| 2079 | if at > self.len() {
|
| 2080 | assert_failed(at, self.len());
|
| 2081 | }
|
| 2082 |
|
| 2083 | if at == 0 {
|
| 2084 | |
| 2085 | return mem::replace(
|
| 2086 | self,
|
| 2087 | Vec::with_capacity_in(self.capacity(), self.allocator().clone()),
|
| 2088 | );
|
| 2089 | }
|
| 2090 |
|
| 2091 | let other_len = self.len - at;
|
| 2092 | let mut other = Vec::with_capacity_in(other_len, self.allocator().clone());
|
| 2093 |
|
| 2094 | |
| 2095 | unsafe {
|
| 2096 | self.set_len(at);
|
| 2097 | other.set_len(other_len);
|
| 2098 |
|
| 2099 | ptr::copy_nonoverlapping(self.as_ptr().add(at), other.as_mut_ptr(), other.len());
|
| 2100 | }
|
| 2101 | other
|
| 2102 | }
|
| 2103 |
|
| 2104 | |
| 2105 | |
| 2106 | |
| 2107 | |
| 2108 | |
| 2109 | |
| 2110 | |
| 2111 | |
| 2112 | |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | |
| 2117 | |
| 2118 | |
| 2119 | |
| 2120 | |
| 2121 | |
| 2122 | |
| 2123 | |
| 2124 | |
| 2125 | |
| 2126 | |
| 2127 | |
| 2128 | |
| 2129 | |
| 2130 | #[cfg(not(no_global_oom_handling))]
|
| 2131 | #[stable(feature = "vec_resize_with", since = "1.33.0")]
|
| 2132 | pub fn resize_with<F>(&mut self, new_len: usize, f: F)
|
| 2133 | where
|
| 2134 | F: FnMut() -> T,
|
| 2135 | {
|
| 2136 | let len = self.len();
|
| 2137 | if new_len > len {
|
| 2138 | self.extend_with(new_len - len, ExtendFunc(f));
|
| 2139 | } else {
|
| 2140 | self.truncate(new_len);
|
| 2141 | }
|
| 2142 | }
|
| 2143 |
|
| 2144 | |
| 2145 | |
| 2146 | |
| 2147 | |
| 2148 | |
| 2149 | |
| 2150 | |
| 2151 | |
| 2152 | |
| 2153 | |
| 2154 | |
| 2155 | |
| 2156 | |
| 2157 | |
| 2158 | |
| 2159 | |
| 2160 | |
| 2161 | |
| 2162 | |
| 2163 | |
| 2164 | |
| 2165 | |
| 2166 | |
| 2167 | #[cfg(not(no_global_oom_handling))]
|
| 2168 | #[stable(feature = "vec_leak", since = "1.47.0")]
|
| 2169 | #[inline]
|
| 2170 | pub fn leak<'a>(self) -> &'a mut [T]
|
| 2171 | where
|
| 2172 | A: 'a,
|
| 2173 | {
|
| 2174 | let mut me = ManuallyDrop::new(self);
|
| 2175 | unsafe { slice::from_raw_parts_mut(me.as_mut_ptr(), me.len) }
|
| 2176 | }
|
| 2177 |
|
| 2178 | |
| 2179 | |
| 2180 | |
| 2181 | |
| 2182 | |
| 2183 | |
| 2184 | |
| 2185 | |
| 2186 | |
| 2187 | |
| 2188 | |
| 2189 | |
| 2190 | |
| 2191 | |
| 2192 | |
| 2193 | |
| 2194 | |
| 2195 | |
| 2196 | |
| 2197 | |
| 2198 | |
| 2199 | |
| 2200 | |
| 2201 | |
| 2202 | |
| 2203 | |
| 2204 | |
| 2205 | |
| 2206 | #[stable(feature = "vec_spare_capacity", since = "1.60.0")]
|
| 2207 | #[inline]
|
| 2208 | pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
|
| 2209 | |
| 2210 | |
| 2211 | |
| 2212 | unsafe {
|
| 2213 | slice::from_raw_parts_mut(
|
| 2214 | self.as_mut_ptr().add(self.len) as *mut MaybeUninit<T>,
|
| 2215 | self.buf.capacity() - self.len,
|
| 2216 | )
|
| 2217 | }
|
| 2218 | }
|
| 2219 |
|
| 2220 | |
| 2221 | |
| 2222 | |
| 2223 | |
| 2224 | |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | |
| 2229 | |
| 2230 | |
| 2231 | |
| 2232 | |
| 2233 | |
| 2234 | |
| 2235 | |
| 2236 | |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | |
| 2243 | |
| 2244 | |
| 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 | #[unstable(feature = "vec_split_at_spare", issue = "81944")]
|
| 2272 | #[inline]
|
| 2273 | pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
|
| 2274 | |
| 2275 | |
| 2276 | let (init, spare, _) = unsafe { self.split_at_spare_mut_with_len() };
|
| 2277 | (init, spare)
|
| 2278 | }
|
| 2279 |
|
| 2280 | |
| 2281 | |
| 2282 | |
| 2283 | unsafe fn split_at_spare_mut_with_len(
|
| 2284 | &mut self,
|
| 2285 | ) -> (&mut [T], &mut [MaybeUninit<T>], &mut usize) {
|
| 2286 | let ptr = self.as_mut_ptr();
|
| 2287 | |
| 2288 | |
| 2289 | |
| 2290 | |
| 2291 | let spare_ptr = unsafe { ptr.add(self.len) };
|
| 2292 | let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
|
| 2293 | let spare_len = self.buf.capacity() - self.len;
|
| 2294 |
|
| 2295 | |
| 2296 | |
| 2297 | |
| 2298 | unsafe {
|
| 2299 | let initialized = slice::from_raw_parts_mut(ptr, self.len);
|
| 2300 | let spare = slice::from_raw_parts_mut(spare_ptr, spare_len);
|
| 2301 |
|
| 2302 | (initialized, spare, &mut self.len)
|
| 2303 | }
|
| 2304 | }
|
| 2305 | }
|
| 2306 |
|
| 2307 | impl<T: Clone, A: Allocator> Vec<T, A> {
|
| 2308 | |
| 2309 | |
| 2310 | |
| 2311 | |
| 2312 | |
| 2313 | |
| 2314 | |
| 2315 | |
| 2316 | |
| 2317 | |
| 2318 | |
| 2319 | |
| 2320 | |
| 2321 | |
| 2322 | |
| 2323 | |
| 2324 | |
| 2325 | |
| 2326 | |
| 2327 | |
| 2328 | |
| 2329 | |
| 2330 | |
| 2331 | #[cfg(not(no_global_oom_handling))]
|
| 2332 | #[stable(feature = "vec_resize", since = "1.5.0")]
|
| 2333 | pub fn resize(&mut self, new_len: usize, value: T) {
|
| 2334 | let len = self.len();
|
| 2335 |
|
| 2336 | if new_len > len {
|
| 2337 | self.extend_with(new_len - len, ExtendElement(value))
|
| 2338 | } else {
|
| 2339 | self.truncate(new_len);
|
| 2340 | }
|
| 2341 | }
|
| 2342 |
|
| 2343 | |
| 2344 | |
| 2345 | |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | |
| 2350 | |
| 2351 | |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | |
| 2356 | |
| 2357 | |
| 2358 | |
| 2359 | |
| 2360 | |
| 2361 | |
| 2362 | #[cfg(not(no_global_oom_handling))]
|
| 2363 | #[stable(feature = "vec_extend_from_slice", since = "1.6.0")]
|
| 2364 | pub fn extend_from_slice(&mut self, other: &[T]) {
|
| 2365 | self.spec_extend(other.iter())
|
| 2366 | }
|
| 2367 |
|
| 2368 | |
| 2369 | |
| 2370 | |
| 2371 | |
| 2372 | |
| 2373 | |
| 2374 | |
| 2375 | |
| 2376 | |
| 2377 | |
| 2378 | |
| 2379 | |
| 2380 | |
| 2381 | |
| 2382 | |
| 2383 | |
| 2384 | |
| 2385 | |
| 2386 | |
| 2387 | |
| 2388 | |
| 2389 | #[cfg(not(no_global_oom_handling))]
|
| 2390 | #[stable(feature = "vec_extend_from_within", since = "1.53.0")]
|
| 2391 | pub fn extend_from_within<R>(&mut self, src: R)
|
| 2392 | where
|
| 2393 | R: RangeBounds<usize>,
|
| 2394 | {
|
| 2395 | let range = slice::range(src, ..self.len());
|
| 2396 | self.reserve(range.len());
|
| 2397 |
|
| 2398 | |
| 2399 | |
| 2400 | unsafe {
|
| 2401 | self.spec_extend_from_within(range);
|
| 2402 | }
|
| 2403 | }
|
| 2404 | }
|
| 2405 |
|
| 2406 | impl<T, A: Allocator, const N: usize> Vec<[T; N], A> {
|
| 2407 | |
| 2408 | |
| 2409 | |
| 2410 | |
| 2411 | |
| 2412 | |
| 2413 | |
| 2414 | |
| 2415 | |
| 2416 | |
| 2417 | |
| 2418 | |
| 2419 | |
| 2420 | |
| 2421 | |
| 2422 | |
| 2423 | |
| 2424 | |
| 2425 | |
| 2426 | |
| 2427 | |
| 2428 | #[unstable(feature = "slice_flatten", issue = "95629")]
|
| 2429 | pub fn into_flattened(self) -> Vec<T, A> {
|
| 2430 | let (ptr, len, cap, alloc) = self.into_raw_parts_with_alloc();
|
| 2431 | let (new_len, new_cap) = if mem::size_of::<T>() == 0 {
|
| 2432 | (len.checked_mul(N).expect("vec len overflow"), usize::MAX)
|
| 2433 | } else {
|
| 2434 | |
| 2435 | |
| 2436 | |
| 2437 | |
| 2438 | |
| 2439 | unsafe { (len.unchecked_mul(N), cap.unchecked_mul(N)) }
|
| 2440 | };
|
| 2441 | |
| 2442 | |
| 2443 | |
| 2444 | |
| 2445 | |
| 2446 | |
| 2447 | unsafe { Vec::<T, A>::from_raw_parts_in(ptr.cast(), new_len, new_cap, alloc) }
|
| 2448 | }
|
| 2449 | }
|
| 2450 |
|
| 2451 | |
| 2452 | trait ExtendWith<T> {
|
| 2453 | fn next(&mut self) -> T;
|
| 2454 | fn last(self) -> T;
|
| 2455 | }
|
| 2456 |
|
| 2457 | struct ExtendElement<T>(T);
|
| 2458 | impl<T: Clone> ExtendWith<T> for ExtendElement<T> {
|
| 2459 | fn next(&mut self) -> T {
|
| 2460 | self.0.clone()
|
| 2461 | }
|
| 2462 | fn last(self) -> T {
|
| 2463 | self.0
|
| 2464 | }
|
| 2465 | }
|
| 2466 |
|
| 2467 | struct ExtendFunc<F>(F);
|
| 2468 | impl<T, F: FnMut() -> T> ExtendWith<T> for ExtendFunc<F> {
|
| 2469 | fn next(&mut self) -> T {
|
| 2470 | (self.0)()
|
| 2471 | }
|
| 2472 | fn last(mut self) -> T {
|
| 2473 | (self.0)()
|
| 2474 | }
|
| 2475 | }
|
| 2476 |
|
| 2477 | impl<T, A: Allocator> Vec<T, A> {
|
| 2478 | #[cfg(not(no_global_oom_handling))]
|
| 2479 | |
| 2480 | fn extend_with<E: ExtendWith<T>>(&mut self, n: usize, mut value: E) {
|
| 2481 | self.reserve(n);
|
| 2482 |
|
| 2483 | unsafe {
|
| 2484 | let mut ptr = self.as_mut_ptr().add(self.len());
|
| 2485 | |
| 2486 | |
| 2487 | |
| 2488 | let mut local_len = SetLenOnDrop::new(&mut self.len);
|
| 2489 |
|
| 2490 | |
| 2491 | for _ in 1..n {
|
| 2492 | ptr::write(ptr, value.next());
|
| 2493 | ptr = ptr.offset(1);
|
| 2494 | |
| 2495 | local_len.increment_len(1);
|
| 2496 | }
|
| 2497 |
|
| 2498 | if n > 0 {
|
| 2499 | |
| 2500 | ptr::write(ptr, value.last());
|
| 2501 | local_len.increment_len(1);
|
| 2502 | }
|
| 2503 |
|
| 2504 | |
| 2505 | }
|
| 2506 | }
|
| 2507 | }
|
| 2508 |
|
| 2509 | impl<T: PartialEq, A: Allocator> Vec<T, A> {
|
| 2510 | |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | |
| 2518 | |
| 2519 | |
| 2520 | |
| 2521 | |
| 2522 | |
| 2523 | |
| 2524 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2525 | #[inline]
|
| 2526 | pub fn dedup(&mut self) {
|
| 2527 | self.dedup_by(|a, b| a == b)
|
| 2528 | }
|
| 2529 | }
|
| 2530 |
|
| 2531 | |
| 2532 | |
| 2533 | |
| 2534 |
|
| 2535 | #[doc(hidden)]
|
| 2536 | #[cfg(not(no_global_oom_handling))]
|
| 2537 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2538 | pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
|
| 2539 | <T as SpecFromElem>::from_elem(elem, n, Global)
|
| 2540 | }
|
| 2541 |
|
| 2542 | #[doc(hidden)]
|
| 2543 | #[cfg(not(no_global_oom_handling))]
|
| 2544 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 2545 | pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
|
| 2546 | <T as SpecFromElem>::from_elem(elem, n, alloc)
|
| 2547 | }
|
| 2548 |
|
| 2549 | trait ExtendFromWithinSpec {
|
| 2550 | |
| 2551 | |
| 2552 | |
| 2553 | |
| 2554 | unsafe fn spec_extend_from_within(&mut self, src: Range<usize>);
|
| 2555 | }
|
| 2556 |
|
| 2557 | impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
|
| 2558 | default unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) {
|
| 2559 | |
| 2560 | |
| 2561 | let (this, spare, len) = unsafe { self.split_at_spare_mut_with_len() };
|
| 2562 |
|
| 2563 | |
| 2564 | |
| 2565 | let to_clone = unsafe { this.get_unchecked(src) };
|
| 2566 |
|
| 2567 | iter::zip(to_clone, spare)
|
| 2568 | .map(|(src, dst)| dst.write(src.clone()))
|
| 2569 | |
| 2570 | |
| 2571 | |
| 2572 | .for_each(|_| *len += 1);
|
| 2573 | }
|
| 2574 | }
|
| 2575 |
|
| 2576 | impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
|
| 2577 | unsafe fn spec_extend_from_within(&mut self, src: Range<usize>) {
|
| 2578 | let count = src.len();
|
| 2579 | {
|
| 2580 | let (init, spare) = self.split_at_spare_mut();
|
| 2581 |
|
| 2582 | |
| 2583 | |
| 2584 | let source = unsafe { init.get_unchecked(src) };
|
| 2585 |
|
| 2586 | |
| 2587 | |
| 2588 | |
| 2589 | |
| 2590 | |
| 2591 | |
| 2592 | |
| 2593 | |
| 2594 | |
| 2595 | unsafe { ptr::copy_nonoverlapping(source.as_ptr(), spare.as_mut_ptr() as _, count) };
|
| 2596 | }
|
| 2597 |
|
| 2598 | |
| 2599 | |
| 2600 | self.len += count;
|
| 2601 | }
|
| 2602 | }
|
| 2603 |
|
| 2604 | |
| 2605 | |
| 2606 | |
| 2607 |
|
| 2608 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2609 | impl<T, A: Allocator> ops::Deref for Vec<T, A> {
|
| 2610 | type Target = [T];
|
| 2611 |
|
| 2612 | fn deref(&self) -> &[T] {
|
| 2613 | unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
|
| 2614 | }
|
| 2615 | }
|
| 2616 |
|
| 2617 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2618 | impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
|
| 2619 | fn deref_mut(&mut self) -> &mut [T] {
|
| 2620 | unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
|
| 2621 | }
|
| 2622 | }
|
| 2623 |
|
| 2624 | #[cfg(not(no_global_oom_handling))]
|
| 2625 | trait SpecCloneFrom {
|
| 2626 | fn clone_from(this: &mut Self, other: &Self);
|
| 2627 | }
|
| 2628 |
|
| 2629 | #[cfg(not(no_global_oom_handling))]
|
| 2630 | impl<T: Clone, A: Allocator> SpecCloneFrom for Vec<T, A> {
|
| 2631 | default fn clone_from(this: &mut Self, other: &Self) {
|
| 2632 | |
| 2633 | this.truncate(other.len());
|
| 2634 |
|
| 2635 | |
| 2636 | |
| 2637 | let (init, tail) = other.split_at(this.len());
|
| 2638 |
|
| 2639 | |
| 2640 | this.clone_from_slice(init);
|
| 2641 | this.extend_from_slice(tail);
|
| 2642 | }
|
| 2643 | }
|
| 2644 |
|
| 2645 | #[cfg(not(no_global_oom_handling))]
|
| 2646 | impl<T: Copy, A: Allocator> SpecCloneFrom for Vec<T, A> {
|
| 2647 | fn clone_from(this: &mut Self, other: &Self) {
|
| 2648 | this.clear();
|
| 2649 | this.extend_from_slice(other);
|
| 2650 | }
|
| 2651 | }
|
| 2652 |
|
| 2653 | #[cfg(not(no_global_oom_handling))]
|
| 2654 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2655 | impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> {
|
| 2656 | #[cfg(not(test))]
|
| 2657 | fn clone(&self) -> Self {
|
| 2658 | let alloc = self.allocator().clone();
|
| 2659 | <[T]>::to_vec_in(&**self, alloc)
|
| 2660 | }
|
| 2661 |
|
| 2662 | |
| 2663 | |
| 2664 | |
| 2665 | |
| 2666 | #[cfg(test)]
|
| 2667 | fn clone(&self) -> Self {
|
| 2668 | let alloc = self.allocator().clone();
|
| 2669 | crate::slice::to_vec(&**self, alloc)
|
| 2670 | }
|
| 2671 |
|
| 2672 | fn clone_from(&mut self, other: &Self) {
|
| 2673 | SpecCloneFrom::clone_from(self, other)
|
| 2674 | }
|
| 2675 | }
|
| 2676 |
|
| 2677 | |
| 2678 | |
| 2679 | |
| 2680 | |
| 2681 | |
| 2682 | |
| 2683 | |
| 2684 | |
| 2685 | |
| 2686 | |
| 2687 | |
| 2688 | |
| 2689 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2690 | impl<T: Hash, A: Allocator> Hash for Vec<T, A> {
|
| 2691 | #[inline]
|
| 2692 | fn hash<H: Hasher>(&self, state: &mut H) {
|
| 2693 | Hash::hash(&**self, state)
|
| 2694 | }
|
| 2695 | }
|
| 2696 |
|
| 2697 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2698 | #[rustc_on_unimplemented(
|
| 2699 | message = "vector indices are of type `usize` or ranges of `usize`",
|
| 2700 | label = "vector indices are of type `usize` or ranges of `usize`"
|
| 2701 | )]
|
| 2702 | impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> {
|
| 2703 | type Output = I::Output;
|
| 2704 |
|
| 2705 | #[inline]
|
| 2706 | fn index(&self, index: I) -> &Self::Output {
|
| 2707 | Index::index(&**self, index)
|
| 2708 | }
|
| 2709 | }
|
| 2710 |
|
| 2711 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2712 | #[rustc_on_unimplemented(
|
| 2713 | message = "vector indices are of type `usize` or ranges of `usize`",
|
| 2714 | label = "vector indices are of type `usize` or ranges of `usize`"
|
| 2715 | )]
|
| 2716 | impl<T, I: SliceIndex<[T]>, A: Allocator> IndexMut<I> for Vec<T, A> {
|
| 2717 | #[inline]
|
| 2718 | fn index_mut(&mut self, index: I) -> &mut Self::Output {
|
| 2719 | IndexMut::index_mut(&mut **self, index)
|
| 2720 | }
|
| 2721 | }
|
| 2722 |
|
| 2723 | #[cfg(not(no_global_oom_handling))]
|
| 2724 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2725 | impl<T> FromIterator<T> for Vec<T> {
|
| 2726 | #[inline]
|
| 2727 | fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> {
|
| 2728 | <Self as SpecFromIter<T, I::IntoIter>>::from_iter(iter.into_iter())
|
| 2729 | }
|
| 2730 | }
|
| 2731 |
|
| 2732 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2733 | impl<T, A: Allocator> IntoIterator for Vec<T, A> {
|
| 2734 | type Item = T;
|
| 2735 | type IntoIter = IntoIter<T, A>;
|
| 2736 |
|
| 2737 | |
| 2738 | |
| 2739 | |
| 2740 | |
| 2741 | |
| 2742 | |
| 2743 | |
| 2744 | |
| 2745 | |
| 2746 | |
| 2747 | |
| 2748 | |
| 2749 | |
| 2750 | #[inline]
|
| 2751 | fn into_iter(self) -> IntoIter<T, A> {
|
| 2752 | unsafe {
|
| 2753 | let mut me = ManuallyDrop::new(self);
|
| 2754 | let alloc = ManuallyDrop::new(ptr::read(me.allocator()));
|
| 2755 | let begin = me.as_mut_ptr();
|
| 2756 | let end = if mem::size_of::<T>() == 0 {
|
| 2757 | arith_offset(begin as *const i8, me.len() as isize) as *const T
|
| 2758 | } else {
|
| 2759 | begin.add(me.len()) as *const T
|
| 2760 | };
|
| 2761 | let cap = me.buf.capacity();
|
| 2762 | IntoIter {
|
| 2763 | buf: NonNull::new_unchecked(begin),
|
| 2764 | phantom: PhantomData,
|
| 2765 | cap,
|
| 2766 | alloc,
|
| 2767 | ptr: begin,
|
| 2768 | end,
|
| 2769 | }
|
| 2770 | }
|
| 2771 | }
|
| 2772 | }
|
| 2773 |
|
| 2774 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2775 | impl<'a, T, A: Allocator> IntoIterator for &'a Vec<T, A> {
|
| 2776 | type Item = &'a T;
|
| 2777 | type IntoIter = slice::Iter<'a, T>;
|
| 2778 |
|
| 2779 | fn into_iter(self) -> slice::Iter<'a, T> {
|
| 2780 | self.iter()
|
| 2781 | }
|
| 2782 | }
|
| 2783 |
|
| 2784 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2785 | impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> {
|
| 2786 | type Item = &'a mut T;
|
| 2787 | type IntoIter = slice::IterMut<'a, T>;
|
| 2788 |
|
| 2789 | fn into_iter(self) -> slice::IterMut<'a, T> {
|
| 2790 | self.iter_mut()
|
| 2791 | }
|
| 2792 | }
|
| 2793 |
|
| 2794 | #[cfg(not(no_global_oom_handling))]
|
| 2795 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2796 | impl<T, A: Allocator> Extend<T> for Vec<T, A> {
|
| 2797 | #[inline]
|
| 2798 | fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
|
| 2799 | <Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter())
|
| 2800 | }
|
| 2801 |
|
| 2802 | #[inline]
|
| 2803 | fn extend_one(&mut self, item: T) {
|
| 2804 | self.push(item);
|
| 2805 | }
|
| 2806 |
|
| 2807 | #[inline]
|
| 2808 | fn extend_reserve(&mut self, additional: usize) {
|
| 2809 | self.reserve(additional);
|
| 2810 | }
|
| 2811 | }
|
| 2812 |
|
| 2813 | impl<T, A: Allocator> Vec<T, A> {
|
| 2814 | |
| 2815 | |
| 2816 | #[cfg(not(no_global_oom_handling))]
|
| 2817 | fn extend_desugared<I: Iterator<Item = T>>(&mut self, mut iterator: I) {
|
| 2818 | |
| 2819 | |
| 2820 | |
| 2821 | |
| 2822 | |
| 2823 | |
| 2824 | |
| 2825 | while let Some(element) = iterator.next() {
|
| 2826 | let len = self.len();
|
| 2827 | if len == self.capacity() {
|
| 2828 | let (lower, _) = iterator.size_hint();
|
| 2829 | self.reserve(lower.saturating_add(1));
|
| 2830 | }
|
| 2831 | unsafe {
|
| 2832 | ptr::write(self.as_mut_ptr().add(len), element);
|
| 2833 | |
| 2834 | |
| 2835 | |
| 2836 | self.set_len(len + 1);
|
| 2837 | }
|
| 2838 | }
|
| 2839 | }
|
| 2840 |
|
| 2841 | |
| 2842 | |
| 2843 | |
| 2844 | |
| 2845 | |
| 2846 | |
| 2847 | |
| 2848 | |
| 2849 | |
| 2850 | |
| 2851 | |
| 2852 | |
| 2853 | |
| 2854 | |
| 2855 | |
| 2856 | |
| 2857 | |
| 2858 | |
| 2859 | |
| 2860 | |
| 2861 | |
| 2862 | |
| 2863 | |
| 2864 | |
| 2865 | |
| 2866 | |
| 2867 | |
| 2868 | |
| 2869 | |
| 2870 | |
| 2871 | |
| 2872 | |
| 2873 | |
| 2874 | #[cfg(not(no_global_oom_handling))]
|
| 2875 | #[inline]
|
| 2876 | #[stable(feature = "vec_splice", since = "1.21.0")]
|
| 2877 | pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, A>
|
| 2878 | where
|
| 2879 | R: RangeBounds<usize>,
|
| 2880 | I: IntoIterator<Item = T>,
|
| 2881 | {
|
| 2882 | Splice { drain: self.drain(range), replace_with: replace_with.into_iter() }
|
| 2883 | }
|
| 2884 |
|
| 2885 | |
| 2886 | |
| 2887 | |
| 2888 | |
| 2889 | |
| 2890 | |
| 2891 | |
| 2892 | |
| 2893 | |
| 2894 | |
| 2895 | |
| 2896 | |
| 2897 | |
| 2898 | |
| 2899 | |
| 2900 | |
| 2901 | |
| 2902 | |
| 2903 | |
| 2904 | |
| 2905 | |
| 2906 | |
| 2907 | |
| 2908 | |
| 2909 | |
| 2910 | |
| 2911 | |
| 2912 | |
| 2913 | |
| 2914 | |
| 2915 | |
| 2916 | |
| 2917 | |
| 2918 | |
| 2919 | |
| 2920 | |
| 2921 | |
| 2922 | |
| 2923 | |
| 2924 | |
| 2925 | |
| 2926 | |
| 2927 | |
| 2928 | |
| 2929 | #[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")]
|
| 2930 | pub fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, T, F, A>
|
| 2931 | where
|
| 2932 | F: FnMut(&mut T) -> bool,
|
| 2933 | {
|
| 2934 | let old_len = self.len();
|
| 2935 |
|
| 2936 | |
| 2937 | unsafe {
|
| 2938 | self.set_len(0);
|
| 2939 | }
|
| 2940 |
|
| 2941 | DrainFilter { vec: self, idx: 0, del: 0, old_len, pred: filter, panic_flag: false }
|
| 2942 | }
|
| 2943 | }
|
| 2944 |
|
| 2945 | |
| 2946 | |
| 2947 | |
| 2948 | |
| 2949 | |
| 2950 | |
| 2951 | #[cfg(not(no_global_oom_handling))]
|
| 2952 | #[stable(feature = "extend_ref", since = "1.2.0")]
|
| 2953 | impl<'a, T: Copy + 'a, A: Allocator + 'a> Extend<&'a T> for Vec<T, A> {
|
| 2954 | fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
|
| 2955 | self.spec_extend(iter.into_iter())
|
| 2956 | }
|
| 2957 |
|
| 2958 | #[inline]
|
| 2959 | fn extend_one(&mut self, &item: &'a T) {
|
| 2960 | self.push(item);
|
| 2961 | }
|
| 2962 |
|
| 2963 | #[inline]
|
| 2964 | fn extend_reserve(&mut self, additional: usize) {
|
| 2965 | self.reserve(additional);
|
| 2966 | }
|
| 2967 | }
|
| 2968 |
|
| 2969 | |
| 2970 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2971 | impl<T: PartialOrd, A: Allocator> PartialOrd for Vec<T, A> {
|
| 2972 | #[inline]
|
| 2973 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
| 2974 | PartialOrd::partial_cmp(&**self, &**other)
|
| 2975 | }
|
| 2976 | }
|
| 2977 |
|
| 2978 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2979 | impl<T: Eq, A: Allocator> Eq for Vec<T, A> {}
|
| 2980 |
|
| 2981 | |
| 2982 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2983 | impl<T: Ord, A: Allocator> Ord for Vec<T, A> {
|
| 2984 | #[inline]
|
| 2985 | fn cmp(&self, other: &Self) -> Ordering {
|
| 2986 | Ord::cmp(&**self, &**other)
|
| 2987 | }
|
| 2988 | }
|
| 2989 |
|
| 2990 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 2991 | unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
|
| 2992 | fn drop(&mut self) {
|
| 2993 | unsafe {
|
| 2994 | |
| 2995 | |
| 2996 | |
| 2997 | ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len))
|
| 2998 | }
|
| 2999 | |
| 3000 | }
|
| 3001 | }
|
| 3002 |
|
| 3003 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 3004 | #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
|
| 3005 | impl<T> const Default for Vec<T> {
|
| 3006 | |
| 3007 | fn default() -> Vec<T> {
|
| 3008 | Vec::new()
|
| 3009 | }
|
| 3010 | }
|
| 3011 |
|
| 3012 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 3013 | impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> {
|
| 3014 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 3015 | fmt::Debug::fmt(&**self, f)
|
| 3016 | }
|
| 3017 | }
|
| 3018 |
|
| 3019 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 3020 | impl<T, A: Allocator> AsRef<Vec<T, A>> for Vec<T, A> {
|
| 3021 | fn as_ref(&self) -> &Vec<T, A> {
|
| 3022 | self
|
| 3023 | }
|
| 3024 | }
|
| 3025 |
|
| 3026 | #[stable(feature = "vec_as_mut", since = "1.5.0")]
|
| 3027 | impl<T, A: Allocator> AsMut<Vec<T, A>> for Vec<T, A> {
|
| 3028 | fn as_mut(&mut self) -> &mut Vec<T, A> {
|
| 3029 | self
|
| 3030 | }
|
| 3031 | }
|
| 3032 |
|
| 3033 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 3034 | impl<T, A: Allocator> AsRef<[T]> for Vec<T, A> {
|
| 3035 | fn as_ref(&self) -> &[T] {
|
| 3036 | self
|
| 3037 | }
|
| 3038 | }
|
| 3039 |
|
| 3040 | #[stable(feature = "vec_as_mut", since = "1.5.0")]
|
| 3041 | impl<T, A: Allocator> AsMut<[T]> for Vec<T, A> {
|
| 3042 | fn as_mut(&mut self) -> &mut [T] {
|
| 3043 | self
|
| 3044 | }
|
| 3045 | }
|
| 3046 |
|
| 3047 | #[cfg(not(no_global_oom_handling))]
|
| 3048 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 3049 | impl<T: Clone> From<&[T]> for Vec<T> {
|
| 3050 | |
| 3051 | |
| 3052 | |
| 3053 | |
| 3054 | |
| 3055 | |
| 3056 | |
| 3057 | #[cfg(not(test))]
|
| 3058 | fn from(s: &[T]) -> Vec<T> {
|
| 3059 | s.to_vec()
|
| 3060 | }
|
| 3061 | #[cfg(test)]
|
| 3062 | fn from(s: &[T]) -> Vec<T> {
|
| 3063 | crate::slice::to_vec(s, Global)
|
| 3064 | }
|
| 3065 | }
|
| 3066 |
|
| 3067 | #[cfg(not(no_global_oom_handling))]
|
| 3068 | #[stable(feature = "vec_from_mut", since = "1.19.0")]
|
| 3069 | impl<T: Clone> From<&mut [T]> for Vec<T> {
|
| 3070 | |
| 3071 | |
| 3072 | |
| 3073 | |
| 3074 | |
| 3075 | |
| 3076 | |
| 3077 | #[cfg(not(test))]
|
| 3078 | fn from(s: &mut [T]) -> Vec<T> {
|
| 3079 | s.to_vec()
|
| 3080 | }
|
| 3081 | #[cfg(test)]
|
| 3082 | fn from(s: &mut [T]) -> Vec<T> {
|
| 3083 | crate::slice::to_vec(s, Global)
|
| 3084 | }
|
| 3085 | }
|
| 3086 |
|
| 3087 | #[cfg(not(no_global_oom_handling))]
|
| 3088 | #[stable(feature = "vec_from_array", since = "1.44.0")]
|
| 3089 | impl<T, const N: usize> From<[T; N]> for Vec<T> {
|
| 3090 | |
| 3091 | |
| 3092 | |
| 3093 | |
| 3094 | |
| 3095 | |
| 3096 | |
| 3097 | #[cfg(not(test))]
|
| 3098 | fn from(s: [T; N]) -> Vec<T> {
|
| 3099 | <[T]>::into_vec(box s)
|
| 3100 | }
|
| 3101 |
|
| 3102 | #[cfg(test)]
|
| 3103 | fn from(s: [T; N]) -> Vec<T> {
|
| 3104 | crate::slice::into_vec(box s)
|
| 3105 | }
|
| 3106 | }
|
| 3107 |
|
| 3108 | #[cfg(not(no_borrow))]
|
| 3109 | #[stable(feature = "vec_from_cow_slice", since = "1.14.0")]
|
| 3110 | impl<'a, T> From<Cow<'a, [T]>> for Vec<T>
|
| 3111 | where
|
| 3112 | [T]: ToOwned<Owned = Vec<T>>,
|
| 3113 | {
|
| 3114 | |
| 3115 | |
| 3116 | |
| 3117 | |
| 3118 | |
| 3119 | |
| 3120 | |
| 3121 | |
| 3122 | |
| 3123 | |
| 3124 | |
| 3125 | |
| 3126 | |
| 3127 | |
| 3128 | fn from(s: Cow<'a, [T]>) -> Vec<T> {
|
| 3129 | s.into_owned()
|
| 3130 | }
|
| 3131 | }
|
| 3132 |
|
| 3133 | |
| 3134 | #[cfg(not(test))]
|
| 3135 | #[stable(feature = "vec_from_box", since = "1.18.0")]
|
| 3136 | impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> {
|
| 3137 | |
| 3138 | |
| 3139 | |
| 3140 | |
| 3141 | |
| 3142 | |
| 3143 | |
| 3144 | |
| 3145 | |
| 3146 | fn from(s: Box<[T], A>) -> Self {
|
| 3147 | s.into_vec()
|
| 3148 | }
|
| 3149 | }
|
| 3150 |
|
| 3151 | |
| 3152 | #[cfg(not(no_global_oom_handling))]
|
| 3153 | #[cfg(not(test))]
|
| 3154 | #[stable(feature = "box_from_vec", since = "1.20.0")]
|
| 3155 | impl<T, A: Allocator> From<Vec<T, A>> for Box<[T], A> {
|
| 3156 | |
| 3157 | |
| 3158 | |
| 3159 | |
| 3160 | |
| 3161 | |
| 3162 | |
| 3163 | |
| 3164 | |
| 3165 | |
| 3166 | fn from(v: Vec<T, A>) -> Self {
|
| 3167 | v.into_boxed_slice()
|
| 3168 | }
|
| 3169 | }
|
| 3170 |
|
| 3171 | #[cfg(not(no_global_oom_handling))]
|
| 3172 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 3173 | impl From<&str> for Vec<u8> {
|
| 3174 | |
| 3175 | |
| 3176 | |
| 3177 | |
| 3178 | |
| 3179 | |
| 3180 | |
| 3181 | fn from(s: &str) -> Vec<u8> {
|
| 3182 | From::from(s.as_bytes())
|
| 3183 | }
|
| 3184 | }
|
| 3185 |
|
| 3186 | #[stable(feature = "array_try_from_vec", since = "1.48.0")]
|
| 3187 | impl<T, A: Allocator, const N: usize> TryFrom<Vec<T, A>> for [T; N] {
|
| 3188 | type Error = Vec<T, A>;
|
| 3189 |
|
| 3190 | |
| 3191 | |
| 3192 | |
| 3193 | |
| 3194 | |
| 3195 | |
| 3196 | |
| 3197 | |
| 3198 | |
| 3199 | |
| 3200 | |
| 3201 | |
| 3202 | |
| 3203 | |
| 3204 | |
| 3205 | |
| 3206 | |
| 3207 | |
| 3208 | |
| 3209 | |
| 3210 | |
| 3211 | |
| 3212 | |
| 3213 | |
| 3214 | |
| 3215 | |
| 3216 | fn try_from(mut vec: Vec<T, A>) -> Result<[T; N], Vec<T, A>> {
|
| 3217 | if vec.len() != N {
|
| 3218 | return Err(vec);
|
| 3219 | }
|
| 3220 |
|
| 3221 | |
| 3222 | unsafe { vec.set_len(0) };
|
| 3223 |
|
| 3224 | |
| 3225 | |
| 3226 | |
| 3227 | |
| 3228 | |
| 3229 | let array = unsafe { ptr::read(vec.as_ptr() as *const [T; N]) };
|
| 3230 | Ok(array)
|
| 3231 | }
|
| 3232 | }
|