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 150 151 152 153 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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 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 470 471 472 473 474 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 514 515 516 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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 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 658 659 660 661 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 706 707 708 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 782 783 784 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 820 821 822 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 863 864 865 866 867 868 869 870 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 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 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 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 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 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 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 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 1177 1178 1179 1180 1181 1182 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 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 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 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 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 1362 1363 1364 1365 1366 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 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 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 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 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 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 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 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 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 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 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 1953 1954
use core::{
borrow::{Borrow, BorrowMut},
fmt::Debug,
hash::Hash,
iter,
mem::{ManuallyDrop, MaybeUninit},
num::NonZeroUsize,
ops::{Deref, DerefMut, Index, IndexMut, RangeBounds},
panic::{RefUnwindSafe, UnwindSafe},
ptr::{self, NonNull},
slice::SliceIndex,
};
use crate::{
bump_down, error_behavior_generic_methods_allocation_failure,
polyfill::{nonnull, pointer, slice},
up_align_usize_unchecked, BaseAllocator, BumpBox, BumpScope, Drain, ErrorBehavior, ExtractIf, FixedBumpVec,
GuaranteedAllocatedStats, IntoIter, MinimumAlignment, NoDrop, SetLenOnDropByPtr, SizedTypeProperties, Stats,
SupportedMinimumAlignment,
};
/// This is like [`vec!`] but allocates inside a `Bump` or `BumpScope`, returning a [`BumpVec`].
///
/// `$bump` can be a [`Bump`](crate::Bump) or [`BumpScope`] (anything where `$bump.as_scope()` returns a `&BumpScope`).
///
/// # Panics
/// If used without `try`, panics on allocation failure.
///
/// # Errors
/// If used with `try`, errors on allocation failure.
///
/// # Examples
///
/// There are three forms of this macro:
///
/// - Create an empty [`BumpVec`]:
/// ```
/// # use bump_scope::{ bump_vec, Bump, BumpVec };
/// # let bump: Bump = Bump::new();
/// let vec: BumpVec<i32> = bump_vec![in bump];
/// assert!(vec.is_empty());
/// ```
///
/// - Create a [`BumpVec`] containing a given list of elements:
///
/// ```
/// # use bump_scope::{ bump_vec, Bump };
/// # let bump: Bump = Bump::new();
/// let vec = bump_vec![in bump; 1, 2, 3];
/// assert_eq!(vec[0], 1);
/// assert_eq!(vec[1], 2);
/// assert_eq!(vec[2], 3);
/// ```
///
/// - Create a [`BumpVec`] from a given element and size:
///
/// ```
/// # use bump_scope::{ bump_vec, Bump };
/// # let bump: Bump = Bump::new();
/// let vec = bump_vec![in bump; 1; 3];
/// assert_eq!(vec, [1, 1, 1]);
/// ```
///
/// Note that unlike array expressions this syntax supports all elements
/// which implement [`Clone`] and the number of elements doesn't have to be
/// a constant.
///
/// This will use `clone` to duplicate an expression, so one should be careful
/// using this with types having a nonstandard `Clone` implementation. For
/// example, `bump_vec![in bump; Rc::new(1); 5]` will create a vector of five references
/// to the same boxed integer value, not five references pointing to independently
/// boxed integers.
///
/// Also, note that `bump_vec![in bump; expr; 0]` is allowed, and produces an empty vector.
/// This will still evaluate `expr`, however, and immediately drop the resulting value, so
/// be mindful of side effects.
#[macro_export]
macro_rules! bump_vec {
[in $bump:expr] => {
$crate::BumpVec::new_in($bump.as_scope())
};
[in $bump:expr; $($values:expr),* $(,)?] => {
$crate::BumpVec::from_array_in([$($values),*], $bump.as_scope())
};
[in $bump:expr; $value:expr; $count:expr] => {
$crate::BumpVec::from_elem_in($value, $count, $bump.as_scope())
};
[try in $bump:expr] => {
Ok::<_, $crate::allocator_api2::alloc::AllocError>($crate::BumpVec::new_in($bump.as_scope()))
};
[try in $bump:expr; $($values:expr),* $(,)?] => {
$crate::BumpVec::try_from_array_in([$($values),*], $bump.as_scope())
};
[try in $bump:expr; $value:expr; $count:expr] => {
$crate::BumpVec::try_from_elem_in($value, $count, $bump.as_scope())
};
}
macro_rules! bump_vec_declaration {
($($allocator_parameter:tt)*) => {
/// A bump allocated `Vec`.
///
/// This type can be used to allocate a slice, when `alloc_*` methods are too limiting:
/// ```
/// use bump_scope::{ Bump, BumpVec };
/// let bump: Bump = Bump::new();
/// let mut vec = BumpVec::new_in(&bump);
///
/// vec.push(1);
/// vec.push(2);
/// vec.push(3);
///
/// let slice: &[i32] = vec.into_slice();
///
/// assert_eq!(slice, [1, 2, 3]);
/// ```
///
/// ## Why not just use a [`Vec`]?
///
/// You can use a `Vec` (from the standard library or from allocator-api2) in mostly the same way.
/// The main difference is that a `BumpVec` can be turned into a slice that is live for `'a` of `BumpScope<'a>` instead of just `'b` of `&'b BumpScope`.
/// This enables such a slice to be live while entering new scopes. This would not be possible with `Vec`:
/// ```
/// # use bump_scope::{ Bump, BumpVec };
/// # let mut bump: Bump = Bump::new();
/// let bump = bump.as_mut_scope();
///
/// let slice = {
/// let mut vec = BumpVec::new_in(&*bump);
///
/// vec.push(1);
/// vec.push(2);
/// vec.push(3);
///
/// vec.into_slice()
/// };
///
/// bump.scoped(|bump| {
/// // allocate more things
/// });
///
/// assert_eq!(slice, [1, 2, 3]);
/// ```
pub struct BumpVec<
'b,
'a: 'b,
T,
$($allocator_parameter)*,
const MIN_ALIGN: usize = 1,
const UP: bool = true,
const GUARANTEED_ALLOCATED: bool = true,
> {
pub(crate) fixed: FixedBumpVec<'a, T>,
pub(crate) bump: &'b BumpScope<'a, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>,
}
};
}
crate::maybe_default_allocator!(bump_vec_declaration);
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> UnwindSafe
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: UnwindSafe,
A: UnwindSafe,
{
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> RefUnwindSafe
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: RefUnwindSafe,
A: RefUnwindSafe,
{
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> Deref
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
type Target = [T];
fn deref(&self) -> &Self::Target {
&self.fixed
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> DerefMut
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.fixed
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool>
BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
A: BaseAllocator<GUARANTEED_ALLOCATED>,
{
/// Constructs a new empty `BumpVec<T>`.
///
/// The vector will not allocate until elements are pushed onto it.
///
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, BumpVec };
/// # let bump: Bump = Bump::new();
/// # #[allow(unused_mut)]
/// let mut vec = BumpVec::<i32>::new_in(&bump);
/// ```
#[inline]
pub fn new_in(bump: impl Into<&'b BumpScope<'a, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>>) -> Self {
Self {
fixed: FixedBumpVec::EMPTY,
bump: bump.into(),
}
}
error_behavior_generic_methods_allocation_failure! {
#[doc = include_str!("docs/vec/with_capacity.md")]
impl
for pub fn with_capacity_in
for pub fn try_with_capacity_in
fn generic_with_capacity_in(capacity: usize, bump: impl Into<&'b BumpScope<'a, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>>) -> Self {
let bump = bump.into();
if T::IS_ZST {
return Ok(Self {
fixed: FixedBumpVec::EMPTY,
bump,
});
}
if capacity == 0 {
return Ok(Self {
fixed: FixedBumpVec::EMPTY,
bump,
});
}
Ok(Self {
fixed: bump.generic_alloc_fixed_vec(capacity)?,
bump,
})
}
/// Constructs a new `BumpVec<T>` and pushes `value` `count` times.
impl
for pub fn from_elem_in
for pub fn try_from_elem_in
fn generic_from_elem_in(value: T, count: usize, bump: impl Into<&'b BumpScope<'a, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>>) -> Self
where {
T: Clone
} in {
let mut vec = Self::generic_with_capacity_in(count, bump)?;
unsafe {
if count != 0 {
for _ in 0..(count - 1) {
vec.unchecked_push_with(|| value.clone());
}
vec.unchecked_push_with(|| value);
}
}
Ok(vec)
}
/// Constructs a new `BumpVec<T>` from a `[T; N]`.
impl
for pub fn from_array_in
for pub fn try_from_array_in
fn generic_from_array_in<{const N: usize}>(array: [T; N], bump: impl Into<&'b BumpScope<'a, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>>) -> Self {
#![allow(clippy::needless_pass_by_value)]
#![allow(clippy::needless_pass_by_ref_mut)]
let array = ManuallyDrop::new(array);
let bump = bump.into();
if T::IS_ZST {
return Ok(Self {
fixed: FixedBumpVec { initialized: unsafe { BumpBox::from_raw(nonnull::slice_from_raw_parts(NonNull::dangling(), N)) }, capacity: usize::MAX },
bump,
});
}
if N == 0 {
return Ok(Self {
fixed: FixedBumpVec::EMPTY,
bump,
});
}
let mut fixed = bump.generic_alloc_fixed_vec(N)?;
let src = array.as_ptr();
let dst = fixed.as_mut_ptr();
unsafe {
ptr::copy_nonoverlapping(src, dst, N);
fixed.set_len(N);
}
Ok(Self { fixed, bump })
}
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool>
BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
#[doc = include_str!("docs/vec/capacity.md")]
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, BumpVec };
/// # let bump: Bump = Bump::new();
/// let vec = BumpVec::<i32>::with_capacity_in(2048, &bump);
/// assert!(vec.capacity() >= 2048);
/// ```
#[must_use]
#[inline(always)]
pub const fn capacity(&self) -> usize {
self.fixed.capacity()
}
#[doc = include_str!("docs/vec/len.md")]
#[must_use]
#[inline(always)]
pub const fn len(&self) -> usize {
self.fixed.len()
}
#[doc = include_str!("docs/vec/is_empty.md")]
#[must_use]
#[inline(always)]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
#[doc = include_str!("docs/vec/pop.md")]
#[inline(always)]
pub fn pop(&mut self) -> Option<T> {
self.fixed.pop()
}
#[doc = include_str!("docs/vec/clear.md")]
/// # Examples
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.clear();
/// assert!(vec.is_empty());
/// ```
#[inline(always)]
pub fn clear(&mut self) {
self.fixed.clear();
}
#[doc = include_str!("docs/vec/truncate.md")]
/// # Examples
///
/// Truncating a five element vector to two elements:
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; 1, 2, 3, 4, 5];
/// vec.truncate(2);
/// assert_eq!(vec, [1, 2]);
/// ```
///
/// No truncation occurs when `len` is greater than the vector's current
/// length:
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.truncate(8);
/// assert_eq!(vec, [1, 2, 3]);
/// ```
///
/// Truncating when `len == 0` is equivalent to calling the [`clear`]
/// method.
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.truncate(0);
/// assert_eq!(vec, []);
/// ```
///
/// [`clear`]: BumpVec::clear
/// [`drain`]: BumpVec::drain
pub fn truncate(&mut self, len: usize) {
self.fixed.truncate(len);
}
#[doc = include_str!("docs/vec/remove.md")]
/// # Examples
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// let mut v = bump_vec![in bump; 1, 2, 3];
/// assert_eq!(v.remove(1), 2);
/// assert_eq!(v, [1, 3]);
/// ```
#[track_caller]
pub fn remove(&mut self, index: usize) -> T {
self.fixed.remove(index)
}
#[doc = include_str!("docs/vec/swap_remove.md")]
/// # Examples
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut v = bump_vec![in bump; "foo", "bar", "baz", "qux"];
///
/// assert_eq!(v.swap_remove(1), "bar");
/// assert_eq!(v, ["foo", "qux", "baz"]);
///
/// assert_eq!(v.swap_remove(0), "foo");
/// assert_eq!(v, ["baz", "qux"]);
/// ```
#[inline]
pub fn swap_remove(&mut self, index: usize) -> T {
self.fixed.swap_remove(index)
}
/// Extracts a slice containing the entire vector.
///
/// Equivalent to `&s[..]`.
#[must_use]
#[inline(always)]
pub const fn as_slice(&self) -> &[T] {
self.fixed.as_slice()
}
/// Extracts a mutable slice containing the entire vector.
///
/// Equivalent to `&mut s[..]`.
#[must_use]
#[inline(always)]
pub fn as_mut_slice(&mut self) -> &mut [T] {
self.fixed.as_mut_slice()
}
/// Returns a raw pointer to the slice, or a dangling raw pointer
/// valid for zero sized reads.
#[inline]
#[must_use]
pub fn as_ptr(&self) -> *const T {
self.fixed.as_ptr()
}
/// Returns an unsafe mutable pointer to slice, or a dangling
/// raw pointer valid for zero sized reads.
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
self.fixed.as_mut_ptr()
}
/// Returns a raw nonnull pointer to the slice, or a dangling raw pointer
/// valid for zero sized reads.
#[must_use]
#[inline(always)]
pub fn as_non_null_ptr(&self) -> NonNull<T> {
self.fixed.as_non_null_ptr()
}
/// Returns a raw nonnull pointer to the slice, or a dangling raw pointer
/// valid for zero sized reads.
#[must_use]
#[inline(always)]
pub fn as_non_null_slice(&self) -> NonNull<[T]> {
self.fixed.as_non_null_slice()
}
/// Appends an element to the back of the collection.
///
/// # Safety
/// Vector must not be full.
#[inline(always)]
pub unsafe fn unchecked_push(&mut self, value: T) {
self.fixed.unchecked_push(value);
}
/// Appends an element to the back of the collection.
///
/// # Safety
/// Vector must not be full.
#[inline(always)]
pub unsafe fn unchecked_push_with(&mut self, f: impl FnOnce() -> T) {
self.fixed.unchecked_push_with(f);
}
/// Forces the length of the vector to `new_len`.
///
/// This is a low-level operation that maintains none of the normal
/// invariants of the type. Normally changing the length of a vector
/// is done using one of the safe operations instead, such as
/// [`truncate`] or [`clear`].
///
/// # Safety
/// - `new_len` must be less than or equal to the [`capacity`] (capacity is not tracked by this type).
/// - The elements at `old_len..new_len` must be initialized.
///
/// [`truncate`]: BumpVec::truncate
/// [`clear`]: BumpVec::clear
/// [`capacity`]: BumpVec::capacity
#[inline]
pub unsafe fn set_len(&mut self, new_len: usize) {
self.fixed.set_len(new_len);
}
#[inline]
pub(crate) unsafe fn inc_len(&mut self, amount: usize) {
self.fixed.inc_len(amount);
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool>
BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
A: BaseAllocator<GUARANTEED_ALLOCATED>,
{
error_behavior_generic_methods_allocation_failure! {
/// Appends an element to the back of a collection.
impl
/// # Examples
///
/// ```
/// # use bump_scope::{ bump_vec, Bump };
/// # let bump: Bump = Bump::new();
/// let mut vec = bump_vec![in bump; 1, 2];
/// vec.push(3);
/// assert_eq!(vec, [1, 2, 3]);
/// ```
for pub fn push
for pub fn try_push
fn generic_push(&mut self, value: T) {
self.generic_push_with(|| value)
}
/// Appends an element to the back of a collection.
impl
for pub fn push_with
for pub fn try_push_with
fn generic_push_with(&mut self, f: impl FnOnce() -> T) {
self.generic_reserve_one()?;
unsafe {
self.unchecked_push_with(f);
}
Ok(())
}
/// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
do panics
/// Panics if `index > len`.
do examples
/// ```
/// # use bump_scope::{ bump_vec, Bump, BumpVec };
/// # let bump: Bump = Bump::new();
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.insert(1, 4);
/// assert_eq!(vec, [1, 4, 2, 3]);
/// vec.insert(4, 5);
/// assert_eq!(vec, [1, 4, 2, 3, 5]);
/// ```
impl
for pub fn insert
for pub fn try_insert
fn generic_insert(&mut self, index: usize, element: T) {
#[cold]
#[inline(never)]
fn assert_failed(index: usize, len: usize) -> ! {
panic!("insertion index (is {index}) should be <= len (is {len})");
}
if index > self.len() {
assert_failed(index, self.len());
}
self.generic_reserve_one()?;
unsafe {
let pos = self.as_mut_ptr().add(index);
if index != self.len() {
let len = self.len() - index;
ptr::copy(pos, pos.add(1), len);
}
pos.write(element);
self.inc_len(1);
}
Ok(())
}
/// Copies and appends all elements in a slice to the `BumpVec`.
///
/// Iterates over the `slice`, copies each element, and then appends
/// it to this `BumpVec`. The `slice` is traversed in-order.
///
/// Note that this function is same as [`extend`] except that it is
/// specialized to work with copyable slices instead.
///
/// [`extend`]: BumpVec::extend
impl
for pub fn extend_from_slice_copy
for pub fn try_extend_from_slice_copy
fn generic_extend_from_slice_copy(&mut self, slice: &[T])
where {
T: Copy
} in {
unsafe { self.extend_by_copy_nonoverlapping(slice) }
}
/// Clones and appends all elements in a slice to the `BumpVec`.
///
/// Iterates over the `slice`, clones each element, and then appends
/// it to this `BumpVec`. The `slice` is traversed in-order.
///
/// Note that this function is same as [`extend`] except that it is
/// specialized to work with slices instead.
///
/// [`extend`]: BumpVec::extend
impl
for pub fn extend_from_slice_clone
for pub fn try_extend_from_slice_clone
fn generic_extend_from_slice_clone(&mut self, slice: &[T])
where {
T: Clone
} in {
self.generic_reserve(slice.len())?;
unsafe {
let mut ptr = self.as_mut_ptr().add(self.len());
for value in slice {
pointer::write_with(ptr, || value.clone());
ptr = ptr.add(1);
self.inc_len(1);
}
}
Ok(())
}
/// Appends all elements in an array to the `BumpVec`.
///
/// Iterates over the `array`, copies each element, and then appends
/// it to this `BumpVec`. The `array` is traversed in-order.
///
/// Note that this function is same as [`extend`] except that it is
/// specialized to work with arrays instead.
///
/// [`extend`]: BumpVec::extend
#[allow(clippy::needless_pass_by_value)]
impl
for pub fn extend_from_array
for pub fn try_extend_from_array
fn generic_extend_from_array<{const N: usize}>(&mut self, array: [T; N]) {
unsafe { self.extend_by_copy_nonoverlapping(&array) }
}
/// Copies elements from `src` range to the end of the vector.
do panics
/// Panics if the starting point is greater than the end point or if
/// the end point is greater than the length of the vector.
do examples
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; 0, 1, 2, 3, 4];
///
/// vec.extend_from_within_copy(2..);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
///
/// vec.extend_from_within_copy(..2);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
///
/// vec.extend_from_within_copy(4..8);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
/// ```
impl
for pub fn extend_from_within_copy
for pub fn try_extend_from_within_copy
fn generic_extend_from_within_copy<{R}>(&mut self, src: R)
where {
T: Copy,
R: RangeBounds<usize>,
} in {
let range = slice::range(src, ..self.len());
let count = range.len();
self.generic_reserve(count)?;
// SAFETY:
// - `slice::range` guarantees that the given range is valid for indexing self
unsafe {
let ptr = self.as_mut_ptr();
let src = ptr.add(range.start);
let dst = ptr.add(self.len());
ptr::copy_nonoverlapping(src, dst, count);
self.inc_len(count);
Ok(())
}
}
/// Clones elements from `src` range to the end of the vector.
///
/// # Panics
///
/// Panics if the starting point is greater than the end point or if
/// the end point is greater than the length of the vector.
///
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; 0, 1, 2, 3, 4];
///
/// vec.extend_from_within_clone(2..);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
///
/// vec.extend_from_within_clone(..2);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
///
/// vec.extend_from_within_clone(4..8);
/// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
/// ```
impl
for pub fn extend_from_within_clone
for pub fn try_extend_from_within_clone
fn generic_extend_from_within_clone<{R}>(&mut self, src: R)
where {
T: Clone,
R: RangeBounds<usize>,
} in {
let range = slice::range(src, ..self.len());
let count = range.len();
self.generic_reserve(count)?;
if T::IS_ZST {
unsafe {
// We can materialize ZST's from nothing.
#[allow(clippy::uninit_assumed_init)]
let fake = ManuallyDrop::new(MaybeUninit::<T>::uninit().assume_init());
for _ in 0..count {
self.unchecked_push((*fake).clone());
}
return Ok(());
}
}
// SAFETY:
// - `slice::range` guarantees that the given range is valid for indexing self
unsafe {
let ptr = self.as_mut_ptr();
let mut src = ptr.add(range.start);
let mut dst = ptr.add(self.len());
let src_end = src.add(count);
while src != src_end {
dst.write((*src).clone());
src = src.add(1);
dst = dst.add(1);
self.inc_len(1);
}
}
Ok(())
}
#[cfg(feature = "zerocopy")]
/// Extends this vector by pushing `additional` new items onto the end.
/// The new items are initialized with zeroes.
impl
do examples
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.extend_zeroed(2);
/// assert_eq!(vec, [1, 2, 3, 0, 0]);
/// ```
for pub fn extend_zeroed
for pub fn try_extend_zeroed
fn generic_extend_zeroed(&mut self, additional: usize)
where {
T: zerocopy::FromZeroes
} in {
self.generic_reserve(additional)?;
unsafe {
let ptr = self.as_mut_ptr();
let len = self.len();
ptr.add(len).write_bytes(0, additional);
self.set_len(len + additional);
}
Ok(())
}
/// Reserves capacity for at least `additional` more elements to be inserted
/// in the given `BumpVec<T>`. The collection may reserve more space to
/// speculatively avoid frequent reallocations. After calling `reserve`,
/// capacity will be greater than or equal to `self.len() + additional`.
/// Does nothing if capacity is already sufficient.
impl
for pub fn reserve
for pub fn try_reserve
fn generic_reserve(&mut self, additional: usize) {
if additional > (self.capacity() - self.len()) {
self.generic_grow_cold(additional)?;
}
Ok(())
}
/// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `BumpVec` is extended by the
/// difference, with each additional slot filled with `value`.
/// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
///
/// This method requires `T` to implement [`Clone`],
/// in order to be able to clone the passed value.
/// If you need more flexibility (or want to rely on [`Default`] instead of
/// [`Clone`]), use [`resize_with`].
/// If you only need to resize to a smaller size, use [`truncate`].
///
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; "hello"];
/// vec.resize(3, "world");
/// assert_eq!(vec, ["hello", "world", "world"]);
/// drop(vec);
///
/// let mut vec = bump_vec![in bump; 1, 2, 3, 4];
/// vec.resize(2, 0);
/// assert_eq!(vec, [1, 2]);
/// ```
///
/// [`resize_with`]: BumpVec::resize_with
/// [`truncate`]: BumpBox::truncate
impl
for pub fn resize
for pub fn try_resize
fn generic_resize(&mut self, new_len: usize, value: T)
where { T: Clone } in
{
let len = self.len();
if new_len > len {
self.extend_with(new_len - len, value)
} else {
self.truncate(new_len);
Ok(())
}
}
/// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `BumpVec` is extended by the
/// difference, with each additional slot filled with the result of
/// calling the closure `f`. The return values from `f` will end up
/// in the `BumpVec` in the order they have been generated.
///
/// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
///
/// This method uses a closure to create new values on every push. If
/// you'd rather [`Clone`] a given value, use [`BumpVec::resize`]. If you
/// want to use the [`Default`] trait to generate values, you can
/// pass [`Default::default`] as the second argument.
///
do examples
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.resize_with(5, Default::default);
/// assert_eq!(vec, [1, 2, 3, 0, 0]);
/// drop(vec);
///
/// let mut vec = bump_vec![in bump];
/// let mut p = 1;
/// vec.resize_with(4, || { p *= 2; p });
/// assert_eq!(vec, [2, 4, 8, 16]);
/// ```
impl
for pub fn resize_with
for pub fn try_resize_with
fn generic_resize_with<{F}>(&mut self, new_len: usize, f: F)
where {
F: FnMut() -> T,
} in {
let len = self.len();
if new_len > len {
unsafe { self.extend_trusted(iter::repeat_with(f).take(new_len - len)) }
} else {
self.truncate(new_len);
Ok(())
}
}
#[cfg(feature = "zerocopy")]
/// Resizes this vector in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the vector is extended by the
/// difference, with each additional slot filled with `value`.
/// If `new_len` is less than `len`, the vector is simply truncated.
impl
do examples
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.resize_zeroed(5);
/// assert_eq!(vec, [1, 2, 3, 0, 0]);
///
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.resize_zeroed(2);
/// assert_eq!(vec, [1, 2]);
/// ```
for pub fn resize_zeroed
for pub fn try_resize_zeroed
fn generic_resize_zeroed(&mut self, new_len: usize)
where {
T: zerocopy::FromZeroes
} in {
let len = self.len();
if new_len > len {
self.generic_extend_zeroed(new_len - len)
} else {
self.truncate(new_len);
Ok(())
}
}
/// Moves all the elements of `other` into `self`, leaving `other` empty.
do examples
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// // needs a scope because of lifetime shenanigans
/// let bump = bump.as_scope();
/// let mut slice = bump.alloc_slice_copy(&[4, 5, 6]);
/// let mut vec = bump_vec![in bump; 1, 2, 3];
/// vec.append(&mut slice);
/// assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
/// assert_eq!(slice, []);
/// ```
impl
for pub fn append
for pub fn try_append
fn generic_append(&mut self, other: &mut BumpBox<[T]>) {
unsafe {
self.extend_by_copy_nonoverlapping(other.as_slice())?;
other.set_len(0);
Ok(())
}
}
}
/// Extend the vector by `n` clones of value.
fn extend_with<B: ErrorBehavior>(&mut self, n: usize, value: T) -> Result<(), B>
where
T: Clone,
{
self.generic_reserve(n)?;
unsafe {
self.fixed.extend_with_unchecked(n, value);
}
Ok(())
}
#[inline(always)]
unsafe fn extend_by_copy_nonoverlapping<E: ErrorBehavior>(&mut self, other: *const [T]) -> Result<(), E> {
let len = pointer::len(other);
self.generic_reserve(len)?;
let src = other.cast::<T>();
let dst = self.as_mut_ptr().add(self.len());
ptr::copy_nonoverlapping(src, dst, len);
self.inc_len(len);
Ok(())
}
#[inline]
fn generic_reserve_one<E: ErrorBehavior>(&mut self) -> Result<(), E> {
if self.capacity() == self.len() {
self.generic_grow_cold::<E>(1)?;
}
Ok(())
}
#[cold]
#[inline(never)]
fn generic_grow_cold<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
let required_cap = match self.len().checked_add(additional) {
Some(required_cap) => required_cap,
None => return Err(E::capacity_overflow())?,
};
if T::IS_ZST {
return Ok(());
}
if self.capacity() == 0 {
self.fixed = self.bump.generic_alloc_fixed_vec(required_cap)?;
return Ok(());
}
let old_ptr = self.as_non_null_ptr();
let new_cap = self.capacity().checked_mul(2).unwrap_or(required_cap).max(required_cap);
let old_size = self.fixed.capacity * T::SIZE; // we already allocated that amount so this can't overflow
let new_size = new_cap.checked_mul(T::SIZE).ok_or_else(|| E::capacity_overflow())?;
unsafe {
if UP {
let is_last = nonnull::byte_add(old_ptr, old_size).cast() == self.bump.chunk.get().pos();
if is_last {
let chunk_end = self.bump.chunk.get().content_end();
let remaining = nonnull::addr(chunk_end).get() - nonnull::addr(old_ptr).get();
if new_size <= remaining {
// There is enough space! We will grow in place. Just need to update the bump pointer.
let old_addr = nonnull::addr(old_ptr);
let new_end = old_addr.get() + new_size;
// Up-aligning a pointer inside a chunks content by `MIN_ALIGN` never overflows.
let new_pos = up_align_usize_unchecked(new_end, MIN_ALIGN);
self.bump.chunk.get().set_pos_addr(new_pos);
} else {
// The current chunk doesn't have enough space to allocate this layout. We need to allocate in another chunk.
let new_ptr = self.bump.do_alloc_slice_in_another_chunk::<E, T>(new_cap)?.cast();
nonnull::copy_nonoverlapping::<u8>(old_ptr.cast(), new_ptr.cast(), old_size);
self.fixed.initialized.set_ptr(new_ptr);
}
} else {
let new_ptr = self.bump.do_alloc_slice::<E, T>(new_cap)?.cast();
nonnull::copy_nonoverlapping::<u8>(old_ptr.cast(), new_ptr.cast(), old_size);
self.fixed.initialized.set_ptr(new_ptr);
}
} else {
let is_last = old_ptr.cast() == self.bump.chunk.get().pos();
if is_last {
// We may be able to reuse the currently allocated space. Just need to check if the current chunk has enough space for that.
let additional_size = new_size - old_size;
let old_addr = nonnull::addr(old_ptr);
let new_addr = bump_down(old_addr, additional_size, T::ALIGN.max(MIN_ALIGN));
let very_start = nonnull::addr(self.bump.chunk.get().content_start());
if new_addr >= very_start.get() {
// There is enough space in the current chunk! We will reuse the allocated space.
let new_addr = NonZeroUsize::new_unchecked(new_addr);
let new_addr_end = new_addr.get() + new_size;
let new_ptr = nonnull::with_addr(old_ptr, new_addr);
// Check if the regions don't overlap so we may use the faster `copy_nonoverlapping`.
if new_addr_end < old_addr.get() {
nonnull::copy_nonoverlapping::<u8>(old_ptr.cast(), new_ptr.cast(), old_size);
} else {
nonnull::copy::<u8>(old_ptr.cast(), new_ptr.cast(), old_size);
}
self.bump.chunk.get().set_pos_addr(new_addr.get());
self.fixed.initialized.set_ptr(new_ptr);
} else {
// The current chunk doesn't have enough space to allocate this layout. We need to allocate in another chunk.
let new_ptr = self.bump.do_alloc_slice_in_another_chunk::<E, T>(new_cap)?.cast();
nonnull::copy_nonoverlapping::<u8>(old_ptr.cast(), new_ptr.cast(), old_size);
self.fixed.initialized.set_ptr(new_ptr);
}
} else {
let new_ptr = self.bump.do_alloc_slice::<E, T>(new_cap)?.cast();
nonnull::copy_nonoverlapping::<u8>(old_ptr.cast(), new_ptr.cast(), old_size);
self.fixed.initialized.set_ptr(new_ptr);
}
}
}
self.fixed.capacity = new_cap;
Ok(())
}
/// Shrinks the capacity of the vector as much as possible.
///
/// This will also free space for future bump allocations iff this is the most recent allocation.
///
/// # Examples
/// ```
/// # use bump_scope::{ Bump, BumpVec };
/// # let bump: Bump = Bump::new();
/// let mut vec = BumpVec::with_capacity_in(10, &bump);
/// vec.extend([1, 2, 3]);
/// assert!(vec.capacity() == 10);
/// assert_eq!(bump.stats().allocated(), 10 * 4);
/// vec.shrink_to_fit();
/// assert!(vec.capacity() == 3);
/// assert_eq!(bump.stats().allocated(), 3 * 4);
/// ```
pub fn shrink_to_fit(&mut self) {
let old_ptr = self.as_non_null_ptr();
let old_size = self.fixed.capacity * T::SIZE; // we already allocated that amount so this can't overflow
let new_size = self.len() * T::SIZE; // its less than the capacity so this can't overflow
unsafe {
let is_last = if UP {
nonnull::byte_add(old_ptr, old_size).cast() == self.bump.chunk.get().pos()
} else {
old_ptr.cast() == self.bump.chunk.get().pos()
};
if is_last {
// we can only do something if this is the last allocation
if UP {
let end = nonnull::addr(old_ptr).get() + new_size;
// Up-aligning a pointer inside a chunk by `MIN_ALIGN` never overflows.
let new_pos = up_align_usize_unchecked(end, MIN_ALIGN);
self.bump.chunk.get().set_pos_addr(new_pos);
} else {
let old_addr = nonnull::addr(old_ptr);
let old_addr_old_end = NonZeroUsize::new_unchecked(old_addr.get() + old_size);
let new_addr = bump_down(old_addr_old_end, new_size, T::ALIGN.max(MIN_ALIGN));
let new_addr = NonZeroUsize::new_unchecked(new_addr);
let old_addr_new_end = NonZeroUsize::new_unchecked(old_addr.get() + new_size);
let new_ptr = nonnull::with_addr(old_ptr, new_addr);
let overlaps = old_addr_new_end > new_addr;
if overlaps {
nonnull::copy::<u8>(old_ptr.cast(), new_ptr.cast(), new_size);
} else {
nonnull::copy_nonoverlapping::<u8>(old_ptr.cast(), new_ptr.cast(), new_size);
}
self.bump.chunk.get().set_pos(new_ptr.cast());
self.fixed.initialized.set_ptr(new_ptr);
}
self.fixed.capacity = self.len();
}
}
}
/// Turns this `BumpVec<T>` into a `FixedBumpVec<T>`.
#[must_use]
#[inline(always)]
pub fn into_fixed_vec(self) -> FixedBumpVec<'a, T> {
self.fixed
}
/// Turns this `BumpVec<T>` into a `BumpBox<[T]>`.
///
/// You may want to call [`shrink_to_fit`](Self::shrink_to_fit) before this, so the unused capacity does not take up space.
#[must_use]
#[inline(always)]
pub fn into_boxed_slice(self) -> BumpBox<'a, [T]> {
self.fixed.into_boxed_slice()
}
/// Turns this `BumpVec<T>` into a `&[T]` that is live for the entire bump scope.
///
/// You may want to call [`shrink_to_fit`](Self::shrink_to_fit) before this, so the unused capacity does not take up space.
///
/// This is only available for [`NoDrop`] types so you don't omit dropping a value for which it matters.
///
/// `!NoDrop` types can still be turned into slices via <code>BumpBox::[leak](BumpBox::leak)(vec.[into_boxed_slice](Self::into_boxed_slice)())</code>.
#[must_use]
#[inline(always)]
pub fn into_slice(self) -> &'a mut [T]
where
[T]: NoDrop,
{
self.into_boxed_slice().into_mut()
}
/// # Safety
///
/// `iterator` must satisfy the invariants of nightly's `TrustedLen`.
unsafe fn extend_trusted<B: ErrorBehavior>(&mut self, iterator: impl Iterator<Item = T>) -> Result<(), B> {
let (low, high) = iterator.size_hint();
if let Some(additional) = high {
debug_assert_eq!(
low,
additional,
"TrustedLen iterator's size hint is not exact: {:?}",
(low, high)
);
self.generic_reserve(additional)?;
let ptr = self.as_mut_ptr();
let mut local_len = SetLenOnDropByPtr::new(&mut self.fixed.initialized.ptr);
iterator.for_each(move |element| {
let dst = ptr.add(local_len.current_len());
ptr::write(dst, element);
// Since the loop executes user code which can panic we have to update
// the length every step to correctly drop what we've written.
// NB can't overflow since we would have had to alloc the address space
local_len.increment_len(1);
});
Ok(())
} else {
// Per TrustedLen contract a `None` upper bound means that the iterator length
// truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway.
// Since the other branch already panics eagerly (via `reserve()`) we do the same here.
// This avoids additional codegen for a fallback code path which would eventually
// panic anyway.
Err(B::capacity_overflow())
}
}
#[doc = include_str!("docs/retain.md")]
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; 1, 2, 3, 4];
///
/// vec.retain(|x| if *x <= 3 {
/// *x += 1;
/// true
/// } else {
/// false
/// });
///
/// assert_eq!(vec, [2, 3, 4]);
/// ```
#[allow(clippy::pedantic)]
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&mut T) -> bool,
{
self.fixed.retain(f)
}
/// Removes the specified range from the vector in bulk, returning all
/// removed elements as an iterator. If the iterator is dropped before
/// being fully consumed, it drops the remaining removed elements.
///
/// The returned iterator keeps a mutable borrow on the vector to optimize
/// its implementation.
///
/// # Panics
///
/// Panics if the starting point is greater than the end point or if
/// the end point is greater than the length of the vector.
///
/// # Leaking
///
/// If the returned iterator goes out of scope without being dropped (due to
/// [`mem::forget`](core::mem::forget), for example), the vector may have lost and leaked
/// elements arbitrarily, including elements outside the range.
///
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut v = bump_vec![in bump; 1, 2, 3];
/// let u = bump.alloc_iter(v.drain(1..));
/// assert_eq!(v, [1]);
/// assert_eq!(u, [2, 3]);
///
/// // A full range clears the vector, like `clear()` does
/// v.drain(..);
/// assert_eq!(v, []);
/// ```
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T>
where
R: RangeBounds<usize>,
{
self.fixed.drain(range)
}
/// Creates an iterator which uses a closure to determine if an element should be removed.
///
/// If the closure returns true, then the element is removed and yielded.
/// If the closure returns false, the element will remain in the vector and will not be yielded
/// by the iterator.
///
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
/// or the iteration short-circuits, then the remaining elements will be retained.
/// Use [`retain`] with a negated predicate if you do not need the returned iterator.
///
/// Using this method is equivalent to the following code:
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 6 };
/// # let bump: Bump = Bump::new();
/// # let mut vec = bump_vec![in bump; 1, 2, 3, 4, 5, 6];
/// let mut i = 0;
/// while i < vec.len() {
/// if some_predicate(&mut vec[i]) {
/// let val = vec.remove(i);
/// // your code here
/// } else {
/// i += 1;
/// }
/// }
///
/// # assert_eq!(vec, [1, 4, 5]);
/// ```
///
/// But `extract_if` is easier to use. `extract_if` is also more efficient,
/// because it can backshift the elements of the array in bulk.
///
/// Note that `extract_if` also lets you mutate every element in the filter closure,
/// regardless of whether you choose to keep or remove it.
///
/// # Examples
///
/// Splitting an array into evens and odds, reusing the original allocation:
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// let mut numbers = bump_vec![in bump; 1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15];
///
/// let evens = numbers.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
/// let odds = numbers;
///
/// assert_eq!(evens, [2, 4, 6, 8, 14]);
/// assert_eq!(odds, [1, 3, 5, 9, 11, 13, 15]);
/// ```
///
/// [`retain`]: Self::retain
pub fn extract_if<F>(&mut self, filter: F) -> ExtractIf<T, F>
where
F: FnMut(&mut T) -> bool,
{
self.fixed.extract_if(filter)
}
/// Removes consecutive repeated elements in the vector according to the
/// [`PartialEq`] trait implementation.
///
/// If the vector is sorted, this removes all duplicates.
///
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// let mut vec = bump_vec![in bump; 1, 2, 2, 3, 2];
///
/// vec.dedup();
///
/// assert_eq!(vec, [1, 2, 3, 2]);
/// ```
#[inline]
pub fn dedup(&mut self)
where
T: PartialEq,
{
self.fixed.dedup();
}
/// Removes all but the first of consecutive elements in the vector that resolve to the same
/// key.
///
/// If the vector is sorted, this removes all duplicates.
///
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// let mut vec = bump_vec![in bump; 10, 20, 21, 30, 20];
///
/// vec.dedup_by_key(|i| *i / 10);
///
/// assert_eq!(vec, [10, 20, 30, 20]);
/// ```
#[inline]
pub fn dedup_by_key<F, K>(&mut self, key: F)
where
F: FnMut(&mut T) -> K,
K: PartialEq,
{
self.fixed.dedup_by_key(key);
}
/// Removes all but the first of consecutive elements in the vector satisfying a given equality
/// relation.
///
/// The `same_bucket` function is passed references to two elements from the vector and
/// must determine if the elements compare equal. The elements are passed in opposite order
/// from their order in the vector, so if `same_bucket(a, b)` returns `true`, `a` is removed.
///
/// If the vector is sorted, this removes all duplicates.
///
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// let mut vec = bump_vec![in bump; "foo", "bar", "Bar", "baz", "bar"];
///
/// vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
///
/// assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
/// ```
pub fn dedup_by<F>(&mut self, same_bucket: F)
where
F: FnMut(&mut T, &mut T) -> bool,
{
self.fixed.dedup_by(same_bucket);
}
/// Returns vector content as a slice of `T`, along with the remaining spare
/// capacity of the vector as a slice of `MaybeUninit<T>`.
///
/// The returned spare capacity slice can be used to fill the vector with data
/// (e.g. by reading from a file) before marking the data as initialized using
/// the [`set_len`] method.
///
/// [`set_len`]: BumpBox::set_len
///
/// Note that this is a low-level API, which should be used with care for
/// optimization purposes. If you need to append data to a `BumpVec`
/// you can use [`push`], [`extend`], `extend_from_slice`[`_copy`](BumpVec::extend_from_slice_copy)`/`[`_clone`](BumpVec::extend_from_within_clone),
/// `extend_from_within`[`_copy`](BumpVec::extend_from_within_copy)`/`[`_clone`](BumpVec::extend_from_within_clone), [`insert`], [`resize`] or
/// [`resize_with`], depending on your exact needs.
///
/// [`push`]: BumpVec::push
/// [`extend`]: BumpVec::extend
/// [`insert`]: BumpVec::insert
/// [`append`]: BumpVec::append
/// [`resize`]: BumpVec::resize
/// [`resize_with`]: BumpVec::resize_with
#[inline]
pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
let ptr = self.as_mut_ptr();
// SAFETY:
// - `ptr` is [guaranteed allocated]o be valid for `self.len` elements
// - but the allocation extends out to `self.buf.capacity()` elements, possibly
// uninitialized
let spare_ptr = unsafe { ptr.add(self.len()) };
let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
let spare_len = self.capacity() - self.len();
// SAFETY:
// - `ptr` is [guaranteed allocated]o be valid for `self.len` elements
// - `spare_ptr` is pointing one element past the buffer, so it doesn't overlap with `initialized`
unsafe {
let initialized = slice::from_raw_parts_mut(ptr, self.len());
let spare = slice::from_raw_parts_mut(spare_ptr, spare_len);
(initialized, spare)
}
}
#[doc = include_str!("docs/allocator.md")]
#[must_use]
#[inline(always)]
pub fn allocator(&self) -> &A {
self.bump.allocator()
}
#[doc = include_str!("docs/bump.md")]
#[must_use]
#[inline(always)]
pub fn bump(&self) -> &'b BumpScope<'a, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED> {
self.bump
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool>
BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
A: BaseAllocator<GUARANTEED_ALLOCATED>,
{
#[doc = include_str!("docs/stats.md")]
#[must_use]
#[inline(always)]
pub fn stats(&self) -> Stats<'a, UP> {
self.bump.stats()
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool> BumpVec<'b, 'a, T, A, MIN_ALIGN, UP>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
A: BaseAllocator,
{
#[doc = include_str!("docs/stats.md")]
#[must_use]
#[inline(always)]
pub fn guaranteed_allocated_stats(&self) -> GuaranteedAllocatedStats<'a, UP> {
self.bump.guaranteed_allocated_stats()
}
}
impl<'b, 'a: 'b, T, const N: usize, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A>
BumpVec<'b, 'a, [T; N], A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
A: BaseAllocator<GUARANTEED_ALLOCATED>,
{
/// Takes a `BumpVec<[T; N]>` and flattens it into a `BumpVec<T>`.
///
/// # Panics
///
/// Panics if the length of the resulting vector would overflow a `usize`.
///
/// This is only possible when flattening a vector of arrays of zero-sized
/// types, and thus tends to be irrelevant in practice. If
/// `size_of::<T>() > 0`, this will never panic.
///
/// # Examples
///
/// ```
/// # use bump_scope::{ Bump, bump_vec };
/// # let bump: Bump = Bump::new();
/// #
/// let mut vec = bump_vec![in bump; [1, 2, 3], [4, 5, 6], [7, 8, 9]];
/// assert_eq!(vec.pop(), Some([7, 8, 9]));
///
/// let mut flattened = vec.into_flattened();
/// assert_eq!(flattened.pop(), Some(6));
/// ```
#[must_use]
pub fn into_flattened(self) -> BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED> {
let Self { fixed, bump } = self;
let fixed = fixed.into_flattened();
BumpVec { fixed, bump }
}
}
impl<'b, 'a: 'b, T: Debug, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A> Debug
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Debug::fmt(self.as_slice(), f)
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, I: SliceIndex<[T]>> Index<I>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
type Output = I::Output;
#[inline(always)]
fn index(&self, index: I) -> &Self::Output {
Index::index(self.as_slice(), index)
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, I: SliceIndex<[T]>>
IndexMut<I> for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
#[inline(always)]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
IndexMut::index_mut(self.as_mut_slice(), index)
}
}
#[cfg(not(no_global_oom_handling))]
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> Extend<T>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
A: BaseAllocator<GUARANTEED_ALLOCATED> + 'a,
{
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
let iter = iter.into_iter();
self.reserve(iter.size_hint().0);
for value in iter {
self.push(value);
}
}
}
#[cfg(not(no_global_oom_handling))]
impl<'b, 'a: 'b, 't, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> Extend<&'t T>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
A: BaseAllocator<GUARANTEED_ALLOCATED> + 'a,
T: Clone + 't,
{
#[inline]
fn extend<I: IntoIterator<Item = &'t T>>(&mut self, iter: I) {
let iter = iter.into_iter();
self.reserve(iter.size_hint().0);
for value in iter {
self.push(value.clone());
}
}
}
impl<'b0, 'a0: 'b0, 'b1, 'a1: 'b1, T, U, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A>
PartialEq<BumpVec<'b1, 'a1, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>>
for BumpVec<'b0, 'a0, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &BumpVec<'b1, 'a1, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>) -> bool {
<[T] as PartialEq<[U]>>::eq(self, other)
}
#[inline(always)]
fn ne(&self, other: &BumpVec<'b1, 'a1, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>) -> bool {
<[T] as PartialEq<[U]>>::ne(self, other)
}
}
impl<'b, 'a: 'b, T, U, const N: usize, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A>
PartialEq<[U; N]> for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &[U; N]) -> bool {
<[T] as PartialEq<[U]>>::eq(self, other)
}
#[inline(always)]
fn ne(&self, other: &[U; N]) -> bool {
<[T] as PartialEq<[U]>>::ne(self, other)
}
}
impl<'b, 'a: 'b, T, U, const N: usize, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A>
PartialEq<&[U; N]> for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &&[U; N]) -> bool {
<[T] as PartialEq<[U]>>::eq(self, *other)
}
#[inline(always)]
fn ne(&self, other: &&[U; N]) -> bool {
<[T] as PartialEq<[U]>>::ne(self, *other)
}
}
impl<'b, 'a: 'b, T, U, const N: usize, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A>
PartialEq<&mut [U; N]> for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &&mut [U; N]) -> bool {
<[T] as PartialEq<[U]>>::eq(self, *other)
}
#[inline(always)]
fn ne(&self, other: &&mut [U; N]) -> bool {
<[T] as PartialEq<[U]>>::ne(self, *other)
}
}
impl<'b, 'a: 'b, T, U, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A> PartialEq<[U]>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &[U]) -> bool {
<[T] as PartialEq<[U]>>::eq(self, other)
}
#[inline(always)]
fn ne(&self, other: &[U]) -> bool {
<[T] as PartialEq<[U]>>::ne(self, other)
}
}
impl<'b, 'a: 'b, T, U, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A> PartialEq<&[U]>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &&[U]) -> bool {
<[T] as PartialEq<[U]>>::eq(self, other)
}
#[inline(always)]
fn ne(&self, other: &&[U]) -> bool {
<[T] as PartialEq<[U]>>::ne(self, other)
}
}
impl<'b, 'a: 'b, T, U, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A> PartialEq<&mut [U]>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &&mut [U]) -> bool {
<[T] as PartialEq<[U]>>::eq(self, other)
}
#[inline(always)]
fn ne(&self, other: &&mut [U]) -> bool {
<[T] as PartialEq<[U]>>::ne(self, other)
}
}
impl<'b, 'a: 'b, T, U, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A>
PartialEq<BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>> for [T]
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>) -> bool {
<[T] as PartialEq<[U]>>::eq(self, other)
}
#[inline(always)]
fn ne(&self, other: &BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>) -> bool {
<[T] as PartialEq<[U]>>::ne(self, other)
}
}
impl<'b, 'a: 'b, T, U, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A>
PartialEq<BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>> for &[T]
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>) -> bool {
<[T] as PartialEq<[U]>>::eq(self, other)
}
#[inline(always)]
fn ne(&self, other: &BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>) -> bool {
<[T] as PartialEq<[U]>>::ne(self, other)
}
}
impl<'b, 'a: 'b, T, U, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A>
PartialEq<BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>> for &mut [T]
where
T: PartialEq<U>,
{
#[inline(always)]
fn eq(&self, other: &BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>) -> bool {
<[T] as PartialEq<[U]>>::eq(self, other)
}
#[inline(always)]
fn ne(&self, other: &BumpVec<'b, 'a, U, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>) -> bool {
<[T] as PartialEq<[U]>>::ne(self, other)
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> IntoIterator
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
{
type Item = T;
type IntoIter = IntoIter<'a, T>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.fixed.into_iter()
}
}
impl<'c, 'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> IntoIterator
for &'c BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
type Item = &'c T;
type IntoIter = slice::Iter<'c, T>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.as_slice().iter()
}
}
impl<'c, 'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> IntoIterator
for &'c mut BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
type Item = &'c mut T;
type IntoIter = slice::IterMut<'c, T>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.as_mut_slice().iter_mut()
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> AsRef<[T]>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
#[inline(always)]
fn as_ref(&self) -> &[T] {
self
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> AsMut<[T]>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
#[inline(always)]
fn as_mut(&mut self) -> &mut [T] {
self
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> Borrow<[T]>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
#[inline(always)]
fn borrow(&self) -> &[T] {
self
}
}
impl<'b, 'a: 'b, T, A, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool> BorrowMut<[T]>
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
#[inline(always)]
fn borrow_mut(&mut self) -> &mut [T] {
self
}
}
impl<'b, 'a: 'b, T: Hash, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A> Hash
for BumpVec<'b, 'a, T, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
{
#[inline(always)]
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.as_slice().hash(state);
}
}
/// Returns [`ErrorKind::OutOfMemory`](std::io::ErrorKind::OutOfMemory) when allocations fail.
#[cfg(feature = "std")]
impl<'b, 'a: 'b, const MIN_ALIGN: usize, const UP: bool, const GUARANTEED_ALLOCATED: bool, A> std::io::Write
for BumpVec<'b, 'a, u8, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED>
where
MinimumAlignment<MIN_ALIGN>: SupportedMinimumAlignment,
A: BaseAllocator<GUARANTEED_ALLOCATED> + 'a,
{
#[inline(always)]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.try_extend_from_slice_copy(buf).is_err() {
return Err(std::io::ErrorKind::OutOfMemory.into());
}
Ok(buf.len())
}
#[inline(always)]
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
#[inline]
fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
let len = bufs.iter().map(|b| b.len()).sum();
if self.try_reserve(len).is_err() {
return Err(std::io::ErrorKind::OutOfMemory.into());
}
unsafe {
let mut dst = self.as_mut_ptr().add(self.len());
for buf in bufs {
buf.as_ptr().copy_to_nonoverlapping(dst, buf.len());
dst = dst.add(buf.len());
}
self.inc_len(len);
}
Ok(len)
}
#[inline(always)]
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
if self.try_extend_from_slice_copy(buf).is_err() {
return Err(std::io::ErrorKind::OutOfMemory.into());
}
Ok(())
}
}