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
//! This crate implements a [circular buffer], also known as cyclic buffer, circular queue or ring.
//!
//! The main struct is [`CircularBuffer`]. It can live on the stack and does not require any heap
//! memory allocation. A `CircularBuffer` is sequence of elements with a maximum capacity: elements
//! can be added to the buffer, and once the maximum capacity is reached, the elements at the start
//! of the buffer are discarded and overwritten.
//!
//! [circular buffer]: https://en.wikipedia.org/wiki/Circular_buffer
//!
//! # Examples
//!
//! ```
//! use circular_buffer::CircularBuffer;
//!
//! // Initialize a new, empty circular buffer with a capacity of 5 elements
//! let mut buf = CircularBuffer::<5, u32>::new();
//!
//! // Add a few elements
//! buf.push_back(1);
//! buf.push_back(2);
//! buf.push_back(3);
//! assert_eq!(buf.to_vec(), vec![1, 2, 3]);
//!
//! // Add more elements to fill the buffer capacity completely
//! buf.push_back(4);
//! buf.push_back(5);
//! assert_eq!(buf.to_vec(), vec![1, 2, 3, 4, 5]);
//!
//! // Adding more elements than the buffer can contain causes the front elements to be
//! // automatically dropped
//! buf.push_back(6);
//! assert_eq!(buf.to_vec(), vec![2, 3, 4, 5, 6]); // `1` got dropped to make room for `6`
//! ```
//!
//! # Interface
//!
//! [`CircularBuffer`] provides methods akin the ones for the standard
//! [`VecDeque`](std::collections::VecDeque) and [`LinkedList`](std::collections::LinkedList). The
//! list below includes the most common methods, but see the
//! [`CircularBuffer` struct documentation](CircularBuffer) to see more.
//!
//! ## Adding/removing elements
//!
//! * [`push_back()`](CircularBuffer::push_back)
//! * [`push_front()`](CircularBuffer::push_front)
//! * [`pop_back()`](CircularBuffer::pop_back)
//! * [`pop_front()`](CircularBuffer::pop_front)
//! * [`swap_remove_back()`](CircularBuffer::swap_remove_back)
//! * [`swap_remove_front()`](CircularBuffer::swap_remove_front)
//!
//! ## Getting/mutating elements
//!
//! * [`front()`](CircularBuffer::front), [`front_mut()`](CircularBuffer::front_mut)
//! * [`back()`](CircularBuffer::back), [`back_mut()`](CircularBuffer::back_mut)
//! * [`get()`](CircularBuffer::get), [`get_mut()`](CircularBuffer::get_mut)
//!
//! ## Iterators
//!
//! * [`into_iter()`](CircularBuffer::into_iter)
//! * [`iter()`](CircularBuffer::iter), [`iter_mut()`](CircularBuffer::iter_mut)
//! * [`range()`](CircularBuffer::range), [`range_mut()`](CircularBuffer::range_mut)
//!
//! ## Writing/reading bytes
//!
//! For the special case of a `CircularBuffer` containing `u8` elements, bytes can be written and
//! read using the standard [`Write`](std::io::Write) and [`Read`](std::io::Read) traits. Writing
//! past the buffer capacity will overwrite the bytes at the start of the buffer, and reading
//! elements will consume elements from the buffer.
//!
//! ```
//! use circular_buffer::CircularBuffer;
//! use std::io::Read;
//! use std::io::Write;
//!
//! let mut buf = CircularBuffer::<5, u8>::new();
//! assert_eq!(buf, b"");
//!
//! write!(buf, "hello");
//! assert_eq!(buf, b"hello");
//!
//! write!(buf, "this string will overflow the buffer and wrap around");
//! assert_eq!(buf, b"round");
//!
//! let mut s = String::new();
//! buf.read_to_string(&mut s).expect("failed to read from buffer");
//! assert_eq!(s, "round");
//! assert_eq!(buf, b"");
//! ```
//!
//! # Time complexity
//!
//! Most of the methods implemented by [`CircularBuffer`] run in constant time. Some of the methods
//! may run in linear time if the type of the elements implements [`Drop`], as each element needs
//! to be deallocated one-by-one.
//!
//! | Method | Complexity |
//! |--------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------|
//! | [`push_back()`](CircularBuffer::push_back), [`push_front()`](CircularBuffer::push_front) | *O*(1) |
//! | [`pop_back()`](CircularBuffer::pop_back), [`pop_front()`](CircularBuffer::pop_front) | *O*(1) |
//! | [`remove(i)`](CircularBuffer::remove) | *O*(*n* − *i*) |
//! | [`truncate_back(i)`](CircularBuffer::truncate_back), [`truncate_front(i)`](CircularBuffer::truncate_front) | *O*(*n* − *i*) for types that implement [`Drop`], *O*(1) otherwise |
//! | [`clear()`](CircularBuffer::clear) | *O*(*n*) for types that implement [`Drop`], *O*(1) otherwise |
//! | [`front()`](CircularBuffer::front), [`back()`](CircularBuffer::back), [`get()`](CircularBuffer::get) | *O*(1) |
//! | [`swap()`](CircularBuffer::swap), [`swap_remove_front()`](CircularBuffer::swap_remove_front), [`swap_remove_back()`](CircularBuffer::swap_remove_back) | *O*(1) |
//! | [`as_slices()`](CircularBuffer::as_slices), [`as_mut_slices()`](CircularBuffer::as_mut_slices) | *O*(1) |
//! | [`len()`](CircularBuffer::len), [`capacity()`](CircularBuffer::capacity) | *O*(1) |
//!
//! # Stack vs heap
//!
//! The [`CircularBuffer`] struct is compact and has a fixed size, so it may live on the stack.
//! This can provide optimal performance for small buffers as memory allocation can be avoided.
//!
//! For large buffers, or for buffers that need to be passed around often, it can be useful to
//! allocate the buffer on the heap. Use a [`Box`](std::boxed) for that:
//!
//! ```
//! use circular_buffer::CircularBuffer;
//!
//! let mut buf = CircularBuffer::<4096, u32>::boxed();
//! assert_eq!(buf.len(), 0);
//!
//! for i in 0..1024 {
//! buf.push_back(i);
//! }
//! assert_eq!(buf.len(), 1024);
//!
//! buf.truncate_back(128);
//! assert_eq!(buf.len(), 128);
//! ```
//!
//! # `no_std`
//!
//! This crate can be used in a [`no_std` environment], although the I/O features and
//! heap-allocation features won't be available in `no_std` mode. By default, this crate uses
//! `std`; to use this crate in `no_std` mode, disable the default features for this crate in your
//! `Cargo.toml`:
//!
//! ```text
//! [dependencies]
//! circular-buffer = { version = "0.1", features = [] }
//! ```
//!
//! [`no_std` environment]: https://docs.rust-embedded.org/book/intro/no-std.html
#![cfg_attr(not(feature = "use_std"), no_std)]
#![cfg_attr(feature = "unstable", feature(const_maybe_uninit_assume_init))]
#![cfg_attr(feature = "unstable", feature(const_maybe_uninit_uninit_array))]
#![cfg_attr(feature = "unstable", feature(const_mut_refs))]
#![cfg_attr(feature = "unstable", feature(const_slice_index))]
#![cfg_attr(feature = "unstable", feature(const_slice_split_at_mut))]
#![cfg_attr(feature = "unstable", feature(const_slice_split_at_not_mut))]
#![cfg_attr(feature = "unstable", feature(const_trait_impl))]
#![cfg_attr(feature = "unstable", feature(maybe_uninit_slice))]
#![cfg_attr(feature = "unstable", feature(maybe_uninit_uninit_array))]
#![cfg_attr(feature = "unstable", feature(maybe_uninit_write_slice))]
#![cfg_attr(feature = "unstable", feature(one_sided_range))]
#![cfg_attr(feature = "unstable", feature(slice_take))]
#![cfg_attr(all(feature = "unstable", feature = "use_std"), feature(new_uninit))]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(pointer_structural_match)]
#![warn(unreachable_pub)]
#![warn(unused_qualifications)]
mod iter;
#[cfg(feature = "use_std")]
mod io;
#[cfg(test)]
mod tests;
use core::cmp::Ordering;
use core::fmt;
use core::hash::Hash;
use core::hash::Hasher;
use core::mem::MaybeUninit;
use core::mem;
use core::ops::Range;
use core::ops::RangeBounds;
use core::ptr;
pub use crate::iter::IntoIter;
pub use crate::iter::Iter;
pub use crate::iter::IterMut;
macro_rules! unstable_const_fn {
(
$( #[ $meta:meta ] )*
$vis:vis const fn $fn:ident $( <{ $( $generics:tt )* }> )? ( $( $arg:tt )* )
$( -> $out:ty )? { $( $tt:tt )* }
) => {
#[cfg(feature = "unstable")]
$(#[$meta])*
$vis const fn $fn $(<$($generics)*>)? ($($arg)*) $(-> $out)? { $($tt)* }
#[cfg(not(feature = "unstable"))]
$(#[$meta])*
$vis fn $fn $(<$($generics)*>)? ($($arg)*) $(-> $out)? { $($tt)* }
}
}
#[cfg(feature = "unstable")]
macro_rules! unstable_const_impl {
(
$( #[ $meta:meta ] )*
impl $( <{ $( $generics:tt )* }> )? const $trait:ident for $type:ty { $( $tt:tt )* }
) => {
$(#[$meta])*
impl $(<$($generics)*>)? const $trait for $type { $($tt)* }
}
}
#[cfg(not(feature = "unstable"))]
macro_rules! unstable_const_impl {
(
$( #[ $meta:meta ] )*
impl $( <{ $( $generics:tt )* }> )? const $trait:ident for $type:ty { $( $tt:tt )* }
) => {
$(#[$meta])*
impl $(<$($generics)*>)? $trait for $type { $($tt)* }
}
}
use unstable_const_fn;
/// Returns `(x + y) % m` without risk of overflows if `x + y` cannot fit in `usize`.
///
/// `x` and `y` are expected to be less than, or equal to `m`.
#[inline]
const fn add_mod(x: usize, y: usize, m: usize) -> usize {
debug_assert!(m > 0);
debug_assert!(x <= m);
debug_assert!(y <= m);
let (z, overflow) = x.overflowing_add(y);
(z + (overflow as usize) * (usize::MAX % m + 1)) % m
}
/// Returns `(x - y) % m` without risk of underflows if `x - y` is negative.
///
/// `x` and `y` are expected to be less than, or equal to `m`.
#[inline]
const fn sub_mod(x: usize, y: usize, m: usize) -> usize {
debug_assert!(m > 0);
debug_assert!(x <= m);
debug_assert!(y <= m);
add_mod(x, m - y, m)
}
/// A fixed-size circular buffer.
///
/// A `CircularBuffer` may live on the stack. Wrap the `CircularBuffer` in a [`Box`](std::boxed)
/// using [`CircularBuffer::boxed()`] if you need the struct to be heap-allocated.
///
/// See the [module-level documentation](self) for more details and examples.
pub struct CircularBuffer<const N: usize, T> {
size: usize,
start: usize,
items: [MaybeUninit<T>; N],
}
impl<const N: usize, T> CircularBuffer<N, T> {
/// Returns an empty `CircularBuffer`.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
/// let buf = CircularBuffer::<16, u32>::new();
/// assert_eq!(buf, []);
/// ```
#[inline]
#[must_use]
pub const fn new() -> Self {
#[cfg(feature = "unstable")]
{
Self {
size: 0,
start: 0,
items: MaybeUninit::uninit_array(),
}
}
#[cfg(not(feature = "unstable"))]
{
Self {
size: 0,
start: 0,
items: unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() },
}
}
}
/// Returns an empty heap-allocated `CircularBuffer`.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
/// let buf = CircularBuffer::<1024, f64>::boxed();
/// assert_eq!(buf.len(), 0);
/// ```
#[must_use]
#[cfg(feature = "use_std")]
#[cfg(feature = "unstable")]
pub fn boxed() -> Box<Self> {
let mut uninit: Box<MaybeUninit<Self>> = Box::new_uninit();
let ptr = uninit.as_mut_ptr();
unsafe {
// SAFETY: the pointer contains enough memory to contain `Self` and `addr_of_mut`
// ensures that the address written to is properly aligned.
std::ptr::addr_of_mut!((*ptr).size).write(0);
std::ptr::addr_of_mut!((*ptr).start).write(0);
// SAFETY: `size` and `start` have been properly initialized to 0; `items` does not
// need to be initialized if `size` is 0
uninit.assume_init()
}
}
/// Returns an empty heap-allocated `CircularBuffer`.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
/// let buf = CircularBuffer::<1024, f64>::boxed();
/// assert_eq!(buf.len(), 0);
/// ```
#[must_use]
#[cfg(feature = "use_std")]
#[cfg(not(feature = "unstable"))]
pub fn boxed() -> Box<Self> {
// SAFETY: this is emulating the code above, just using direct allocation and raw pointers
// instead of MaybeUninit. Only `size` and `start` need to be initialized to 0; `items`
// does not need to be initialized if `size` is 0.
unsafe {
let layout = std::alloc::Layout::new::<Self>();
let ptr = std::alloc::alloc(layout) as *mut Self;
std::ptr::addr_of_mut!((*ptr).size).write(0);
std::ptr::addr_of_mut!((*ptr).start).write(0);
Box::from_raw(ptr)
}
}
/// Returns the number of elements in the buffer.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<16, u32>::new();
/// assert_eq!(buf.len(), 0);
///
/// buf.push_back(1);
/// buf.push_back(2);
/// buf.push_back(3);
/// assert_eq!(buf.len(), 3);
/// ```
#[inline]
pub const fn len(&self) -> usize {
self.size
}
/// Returns the capacity of the buffer.
///
/// This is the maximum number of elements that the buffer can hold.
///
/// This method always returns the generic const parameter `N`.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
/// let mut buf = CircularBuffer::<16, u32>::new();
/// assert_eq!(buf.capacity(), 16);
/// ```
#[inline]
pub const fn capacity(&self) -> usize {
N
}
/// Returns `true` if the buffer contains 0 elements.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<16, u32>::new();
/// assert!(buf.is_empty());
///
/// buf.push_back(1);
/// assert!(!buf.is_empty());
/// ```
#[inline]
pub const fn is_empty(&self) -> bool {
self.size == 0
}
/// Returns `true` if the number of elements in the buffer matches the buffer capacity.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<5, u32>::new();
/// assert!(!buf.is_full());
///
/// buf.push_back(1);
/// assert!(!buf.is_full());
///
/// buf.push_back(2);
/// buf.push_back(3);
/// buf.push_back(4);
/// buf.push_back(5);
/// assert!(buf.is_full());
/// ```
#[inline]
pub const fn is_full(&self) -> bool {
self.size == N
}
unstable_const_fn! {
/// Returns an iterator over the elements of the buffer.
///
/// The iterator advances from front to back. Use [`.rev()`](Iter::rev) to advance from
/// back to front.
///
/// # Examples
///
/// Iterate from front to back:
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let buf = CircularBuffer::<5, char>::from_iter("abc".chars());
/// let mut it = buf.iter();
///
/// assert_eq!(it.next(), Some(&'a'));
/// assert_eq!(it.next(), Some(&'b'));
/// assert_eq!(it.next(), Some(&'c'));
/// assert_eq!(it.next(), None);
/// ```
///
/// Iterate from back to front:
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let buf = CircularBuffer::<5, char>::from_iter("abc".chars());
/// let mut it = buf.iter().rev();
///
/// assert_eq!(it.next(), Some(&'c'));
/// assert_eq!(it.next(), Some(&'b'));
/// assert_eq!(it.next(), Some(&'a'));
/// assert_eq!(it.next(), None);
/// ```
#[inline]
#[must_use]
pub const fn iter(&self) -> Iter<'_, T> {
Iter::new(self)
}
}
unstable_const_fn! {
/// Returns an iterator over the elements of the buffer that allows modifying each value.
///
/// The iterator advances from front to back. Use [`.rev()`](Iter::rev) to advance from
/// back to front.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<5, u32>::from([1, 2, 3]);
/// for elem in buf.iter_mut() {
/// *elem += 5;
/// }
/// assert_eq!(buf, [6, 7, 8]);
/// ```
#[inline]
#[must_use]
pub const fn iter_mut(&mut self) -> IterMut<'_, T> {
IterMut::new(self)
}
}
/// Returns an iterator over the specified range of elements of the buffer.
///
/// The iterator advances from front to back. Use [`.rev()`](Iter::rev) to advance from back to
/// front.
///
/// # Panics
///
/// If the start of the range is greater than the end, or if the end is greater than the length
/// of the buffer.
///
/// # Examples
///
/// Iterate from front to back:
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let buf = CircularBuffer::<16, char>::from_iter("abcdefghi".chars());
/// let mut it = buf.range(3..6);
///
/// assert_eq!(it.next(), Some(&'d'));
/// assert_eq!(it.next(), Some(&'e'));
/// assert_eq!(it.next(), Some(&'f'));
/// assert_eq!(it.next(), None);
/// ```
///
/// Iterate from back to front:
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let buf = CircularBuffer::<16, char>::from_iter("abcdefghi".chars());
/// let mut it = buf.range(3..6).rev();
///
/// assert_eq!(it.next(), Some(&'f'));
/// assert_eq!(it.next(), Some(&'e'));
/// assert_eq!(it.next(), Some(&'d'));
/// assert_eq!(it.next(), None);
/// ```
#[inline]
#[must_use]
pub fn range<R>(&self, range: R) -> Iter<'_, T>
where R: RangeBounds<usize>
{
Iter::over_range(self, range)
}
/// Returns an iterator over the specified range of elements of the buffer that allows
/// modifying each value.
///
/// The iterator advances from front to back. Use [`.rev()`](Iter::rev) to advance from back to
/// front.
///
/// # Panics
///
/// If the start of the range is greater than the end, or if the end is greater than the length
/// of the buffer.
///
/// # Examples
///
/// Iterate from front to back:
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<16, i32>::from_iter([1, 2, 3, 4, 5, 6]);
/// for elem in buf.range_mut(..3) {
/// *elem *= -1;
/// }
/// assert_eq!(buf, [-1, -2, -3, 4, 5, 6]);
/// ```
#[inline]
#[must_use]
pub fn range_mut<R>(&mut self, range: R) -> IterMut<'_, T>
where R: RangeBounds<usize>
{
IterMut::over_range(self, range)
}
// TODO #[inline]
// TODO #[must_use]
// TODO pub const fn drain(&mut self) -> Drain<'_, N, T> {
// TODO todo!()
// TODO }
unstable_const_fn! {
/// Returns a pair of slices which contain the elements of this buffer.
///
/// The second slice may be empty if the internal buffer is contiguous.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, char>::new();
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// buf.push_back('d');
///
/// // Buffer is contiguous; second slice is empty
/// assert_eq!(buf.as_slices(), (&['a', 'b', 'c', 'd'][..], &[][..]));
///
/// buf.push_back('e');
/// buf.push_back('f');
///
/// // Buffer is disjoint; both slices are non-empty
/// assert_eq!(buf.as_slices(), (&['c', 'd'][..], &['e', 'f'][..]));
/// ```
#[inline]
pub const fn as_slices(&self) -> (&[T], &[T]) {
if N == 0 || self.size == 0 {
return (&[], &[]);
}
debug_assert!(self.start < N, "start out-of-bounds");
debug_assert!(self.size <= N, "size out-of-bounds");
let start = self.start;
let end = add_mod(self.start, self.size, N);
let (left, right) = if start < end {
(&self.items[start..end], &[][..])
} else {
let (right, left) = self.items.split_at(end);
let left = &left[start - end..];
(left, right)
};
// SAFETY: The elements in these slices are guaranteed to be initialized
#[cfg(feature = "unstable")]
unsafe {
(MaybeUninit::slice_assume_init_ref(left),
MaybeUninit::slice_assume_init_ref(right))
}
#[cfg(not(feature = "unstable"))]
unsafe {
(&*(left as *const [MaybeUninit<T>] as *const [T]),
&*(right as *const [MaybeUninit<T>] as *const [T]))
}
}
}
unstable_const_fn! {
/// Returns a pair of mutable slices which contain the elements of this buffer.
///
/// These slices can be used to modify or replace the elements in the buffer.
///
/// The second slice may be empty if the internal buffer is contiguous.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, char>::new();
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// buf.push_back('d');
/// buf.push_back('e');
/// buf.push_back('f');
///
/// assert_eq!(buf, ['c', 'd', 'e', 'f']);
///
/// let (left, right) = buf.as_mut_slices();
/// assert_eq!(left, &mut ['c', 'd'][..]);
/// assert_eq!(right, &mut ['e', 'f'][..]);
///
/// left[0] = 'z';
///
/// assert_eq!(buf, ['z', 'd', 'e', 'f']);
/// ```
#[inline]
pub const fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
if N == 0 || self.size == 0 {
return (&mut [][..], &mut [][..]);
}
debug_assert!(self.start < N, "start out-of-bounds");
debug_assert!(self.size <= N, "size out-of-bounds");
let start = self.start;
let end = add_mod(self.start, self.size, N);
let (left, right) = if start < end {
(&mut self.items[start..end], &mut [][..])
} else {
let (right, left) = self.items.split_at_mut(end);
let left = &mut left[start - end..];
(left, right)
};
// SAFETY: The elements in these slices are guaranteed to be initialized
#[cfg(feature = "unstable")]
unsafe {
(MaybeUninit::slice_assume_init_mut(left),
MaybeUninit::slice_assume_init_mut(right))
}
#[cfg(not(feature = "unstable"))]
unsafe {
(&mut *(left as *mut [MaybeUninit<T>] as *mut [T]),
&mut *(right as *mut [MaybeUninit<T>] as *mut [T]))
}
}
}
#[inline]
fn front_maybe_uninit_mut(&mut self) -> &mut MaybeUninit<T> {
debug_assert!(self.size > 0, "empty buffer");
debug_assert!(self.start < N, "start out-of-bounds");
&mut self.items[self.start]
}
#[inline]
const fn front_maybe_uninit(&self) -> &MaybeUninit<T> {
debug_assert!(self.size > 0, "empty buffer");
debug_assert!(self.size <= N, "size out-of-bounds");
debug_assert!(self.start < N, "start out-of-bounds");
&self.items[self.start]
}
#[inline]
const fn back_maybe_uninit(&self) -> &MaybeUninit<T> {
debug_assert!(self.size > 0, "empty buffer");
debug_assert!(self.size <= N, "size out-of-bounds");
debug_assert!(self.start < N, "start out-of-bounds");
let back = add_mod(self.start, self.size - 1, N);
&self.items[back]
}
#[inline]
fn back_maybe_uninit_mut(&mut self) -> &mut MaybeUninit<T> {
debug_assert!(self.size > 0, "empty buffer");
debug_assert!(self.size <= N, "size out-of-bounds");
debug_assert!(self.start < N, "start out-of-bounds");
let back = add_mod(self.start, self.size - 1, N);
&mut self.items[back]
}
#[inline]
const fn get_maybe_uninit(&self, index: usize) -> &MaybeUninit<T> {
debug_assert!(self.size > 0, "empty buffer");
debug_assert!(index < N, "index out-of-bounds");
debug_assert!(self.start < N, "start out-of-bounds");
let index = add_mod(self.start, index, N);
&self.items[index]
}
#[inline]
fn get_maybe_uninit_mut(&mut self, index: usize) -> &mut MaybeUninit<T> {
debug_assert!(self.size > 0, "empty buffer");
debug_assert!(index < N, "index out-of-bounds");
debug_assert!(self.start < N, "start out-of-bounds");
let index = add_mod(self.start, index, N);
&mut self.items[index]
}
#[inline]
fn slices_uninit_mut(&mut self) -> (&mut [MaybeUninit<T>], &mut [MaybeUninit<T>]) {
if N == 0 {
return (&mut [][..], &mut [][..]);
}
debug_assert!(self.start < N, "start out-of-bounds");
debug_assert!(self.size <= N, "size out-of-bounds");
let start = self.start;
let end = add_mod(start, self.size, N);
if end < start {
(&mut self.items[end..start], &mut [][..])
} else {
let (left, right) = self.items.split_at_mut(end);
let left = &mut left[..start];
(right, left)
}
}
#[inline]
fn inc_start(&mut self) {
debug_assert!(self.start < N, "start out-of-bounds");
self.start = add_mod(self.start, 1, N);
}
#[inline]
fn dec_start(&mut self) {
debug_assert!(self.start < N, "start out-of-bounds");
self.start = sub_mod(self.start, 1, N);
}
#[inline]
fn inc_size(&mut self) {
debug_assert!(self.size <= N, "size out-of-bounds");
self.size += 1;
debug_assert!(self.size <= N, "size exceeding capacity");
}
#[inline]
fn dec_size(&mut self) {
debug_assert!(self.size > 0, "size is 0");
self.size -= 1;
}
#[inline]
unsafe fn drop_range(&mut self, range: Range<usize>) {
if range.is_empty() {
return;
}
debug_assert!(self.start < N, "start out-of-bounds");
debug_assert!(self.size <= N, "size out-of-bounds");
debug_assert!(range.end <= self.size, "end of range out-of-bounds");
debug_assert!(range.start == 0 || range.end == self.size,
"range does not include boundary of the buffer");
// Drops all the items in the slice when dropped. This is needed to ensure that all
// elements are dropped in case a panic occurs during the drop of a single element.
struct Dropper<'a, T>(&'a mut [MaybeUninit<T>]);
impl<'a, T> Drop for Dropper<'a, T> {
#[inline]
fn drop(&mut self) {
// SAFETY: the caller of `drop_range` is responsible to check that this slice was
// initialized.
#[cfg(feature = "unstable")]
unsafe { ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(self.0)); }
#[cfg(not(feature = "unstable"))]
unsafe { ptr::drop_in_place(&mut *(self.0 as *mut [MaybeUninit<T>] as *mut [T])); }
}
}
let drop_from = add_mod(self.start, range.start, N);
let drop_to = add_mod(self.start, range.end, N);
let (right, left) = if drop_from < drop_to {
(&mut self.items[drop_from..drop_to], &mut [][..])
} else {
let (left, right) = self.items.split_at_mut(drop_from);
let left = &mut left[..drop_to];
(right, left)
};
let _left = Dropper(left);
let _right = Dropper(right);
}
/// Returns a reference to the back element, or `None` if the buffer is empty.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, char>::new();
/// assert_eq!(buf.back(), None);
///
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// assert_eq!(buf.back(), Some(&'c'));
/// ```
#[inline]
pub fn back(&self) -> Option<&T> {
if N == 0 || self.size == 0 {
// Nothing to do
return None;
}
// SAFETY: `size` is non-zero; back element is guaranteed to be initialized
Some(unsafe { self.back_maybe_uninit().assume_init_ref() })
}
/// Returns a mutable reference to the back element, or `None` if the buffer is empty.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, char>::new();
/// assert_eq!(buf.back_mut(), None);
///
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// match buf.back_mut() {
/// None => (),
/// Some(x) => *x = 'z',
/// }
/// assert_eq!(buf, ['a', 'b', 'z']);
/// ```
#[inline]
pub fn back_mut(&mut self) -> Option<&mut T> {
if N == 0 || self.size == 0 {
// Nothing to do
return None;
}
// SAFETY: `size` is non-zero; back element is guaranteed to be initialized
Some(unsafe { self.back_maybe_uninit_mut().assume_init_mut() })
}
/// Returns a reference to the front element, or `None` if the buffer is empty.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, char>::new();
/// assert_eq!(buf.front(), None);
///
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// assert_eq!(buf.front(), Some(&'a'));
/// ```
#[inline]
pub fn front(&self) -> Option<&T> {
if N == 0 || self.size == 0 {
// Nothing to do
return None;
}
// SAFETY: `size` is non-zero; front element is guaranteed to be initialized
Some(unsafe { self.front_maybe_uninit().assume_init_ref() })
}
/// Returns a mutable reference to the front element, or `None` if the buffer is empty.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, char>::new();
/// assert_eq!(buf.front_mut(), None);
///
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// match buf.front_mut() {
/// None => (),
/// Some(x) => *x = 'z',
/// }
/// assert_eq!(buf, ['z', 'b', 'c']);
/// ```
#[inline]
pub fn front_mut(&mut self) -> Option<&mut T> {
if N == 0 || self.size == 0 {
// Nothing to do
return None;
}
// SAFETY: `size` is non-zero; front element is guaranteed to be initialized
Some(unsafe { self.front_maybe_uninit_mut().assume_init_mut() })
}
/// Returns a reference to the element at the given index, or `None` if the element does not
/// exist.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, char>::new();
/// assert_eq!(buf.get(1), None);
///
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// assert_eq!(buf.get(1), Some(&'b'));
/// ```
#[inline]
pub fn get(&self, index: usize) -> Option<&T> {
if N == 0 || index >= self.size {
// Nothing to do
return None;
}
// SAFETY: `index` is in a valid range; it is guaranteed to point to an initialized element
Some(unsafe { self.get_maybe_uninit(index).assume_init_ref() })
}
/// Returns a mutable reference to the element at the given index, or `None` if the element
/// does not exist.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, char>::new();
/// assert_eq!(buf.get_mut(1), None);
///
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// match buf.get_mut(1) {
/// None => (),
/// Some(x) => *x = 'z',
/// }
/// assert_eq!(buf, ['a', 'z', 'c']);
/// ```
#[inline]
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
if N == 0 || index >= self.size {
// Nothing to do
return None;
}
// SAFETY: `index` is in a valid range; it is guaranteed to point to an initialized element
Some(unsafe { self.get_maybe_uninit_mut(index).assume_init_mut() })
}
/// Appends an element to the back of the buffer.
///
/// If the buffer is full, the element at the front of the buffer is automatically dropped.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<3, char>::new();
///
/// buf.push_back('a'); assert_eq!(buf, ['a']);
/// buf.push_back('b'); assert_eq!(buf, ['a', 'b']);
/// buf.push_back('c'); assert_eq!(buf, ['a', 'b', 'c']);
/// // The buffer is now full; adding more values causes the front elements to be dropped
/// buf.push_back('d'); assert_eq!(buf, ['b', 'c', 'd']);
/// buf.push_back('e'); assert_eq!(buf, ['c', 'd', 'e']);
/// buf.push_back('f'); assert_eq!(buf, ['d', 'e', 'f']);
/// ```
pub fn push_back(&mut self, item: T) {
if N == 0 {
// Nothing to do
return;
}
if self.size >= N {
// At capacity; need to replace the front item
//
// SAFETY: if size is greater than 0, the front item is guaranteed to be initialized.
unsafe { ptr::drop_in_place(self.front_maybe_uninit_mut().as_mut_ptr()); }
self.front_maybe_uninit_mut().write(item);
self.inc_start();
} else {
// Some uninitialized slots left; append at the end
self.inc_size();
self.back_maybe_uninit_mut().write(item);
}
}
/// Appends an element to the front of the buffer.
///
/// If the buffer is full, the element at the back of the buffer is automatically dropped.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<3, char>::new();
///
/// buf.push_front('a'); assert_eq!(buf, ['a']);
/// buf.push_front('b'); assert_eq!(buf, ['b', 'a']);
/// buf.push_front('c'); assert_eq!(buf, ['c', 'b', 'a']);
/// // The buffer is now full; adding more values causes the back elements to be dropped
/// buf.push_front('d'); assert_eq!(buf, ['d', 'c', 'b']);
/// buf.push_front('e'); assert_eq!(buf, ['e', 'd', 'c']);
/// buf.push_front('f'); assert_eq!(buf, ['f', 'e', 'd']);
/// ```
pub fn push_front(&mut self, item: T) {
if N == 0 {
// Nothing to do
return;
}
if self.size >= N {
// At capacity; need to replace the back item
//
// SAFETY: if size is greater than 0, the front item is guaranteed to be initialized.
unsafe { ptr::drop_in_place(self.back_maybe_uninit_mut().as_mut_ptr()); }
self.back_maybe_uninit_mut().write(item);
self.dec_start();
} else {
// Some uninitialized slots left; insert at the start
self.inc_size();
self.dec_start();
self.front_maybe_uninit_mut().write(item);
}
}
/// Removes and returns an element from the back of the buffer.
///
/// If the buffer is empty, `None` is returned.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<3, char>::from(['a', 'b', 'c']);
///
/// assert_eq!(buf.pop_back(), Some('c'));
/// assert_eq!(buf.pop_back(), Some('b'));
/// assert_eq!(buf.pop_back(), Some('a'));
/// assert_eq!(buf.pop_back(), None);
/// ```
pub fn pop_back(&mut self) -> Option<T> {
if N == 0 || self.size == 0 {
// Nothing to do
return None;
}
// SAFETY: if size is greater than 0, the back item is guaranteed to be initialized.
let back = unsafe { self.back_maybe_uninit().assume_init_read() };
self.dec_size();
Some(back)
}
/// Removes and returns an element from the front of the buffer.
///
/// If the buffer is empty, `None` is returned.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<3, char>::from(['a', 'b', 'c']);
///
/// assert_eq!(buf.pop_front(), Some('a'));
/// assert_eq!(buf.pop_front(), Some('b'));
/// assert_eq!(buf.pop_front(), Some('c'));
/// assert_eq!(buf.pop_front(), None);
/// ```
pub fn pop_front(&mut self) -> Option<T> {
if N == 0 || self.size == 0 {
// Nothing to do
return None;
}
// SAFETY: if size is greater than 0, the front item is guaranteed to be initialized.
let back = unsafe { self.front_maybe_uninit().assume_init_read() };
self.dec_size();
self.inc_start();
Some(back)
}
/// Removes and returns an element at the specified index.
///
/// If the index is out of bounds, `None` is returned.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<3, char>::from(['a', 'b', 'c']);
///
/// assert_eq!(buf.remove(1), Some('b'));
/// assert_eq!(buf, ['a', 'c']);
///
/// assert_eq!(buf.remove(5), None);
/// ```
pub fn remove(&mut self, index: usize) -> Option<T> {
if N == 0 || index >= self.size {
return None;
}
let index = add_mod(self.start, index, N);
let back_index = add_mod(self.start, self.size - 1, N);
// SAFETY: `index` is in a valid range; the element is guaranteed to be initialized
let item = unsafe { self.items[index].assume_init_read() };
// SAFETY: the pointers being moved are in a valid range; the elements behind those
// pointers are guaranteed to be initialized
unsafe {
let ptr = self.items.as_mut_ptr();
if back_index >= index {
// Move the values at the right of `index` by 1 position to the left
ptr::copy(ptr.add(index).add(1), ptr.add(index), back_index - index);
} else {
// Move the values at the right of `index` by 1 position to the left
ptr::copy(ptr.add(index).add(1), ptr.add(index), N - index);
// Move the leftmost value to the end of the array
ptr::copy(ptr, ptr.add(N - 1), 1);
// Move the values at the left of `back_index` by 1 position to the left
ptr::copy(ptr.add(1), ptr, back_index);
}
}
self.dec_size();
Some(item)
}
/// Swap the element at index `i` with the element at index `j`.
///
/// # Panics
///
/// If either `i` or `j` is out of bounds.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<5, char>::from(['a', 'b', 'c', 'd']);
/// assert_eq!(buf, ['a', 'b', 'c', 'd']);
///
/// buf.swap(0, 3);
/// assert_eq!(buf, ['d', 'b', 'c', 'a']);
/// ```
///
/// Trying to swap an invalid index panics:
///
/// ```should_panic
/// use circular_buffer::CircularBuffer;
/// let mut buf = CircularBuffer::<5, char>::from(['a', 'b', 'c', 'd']);
/// buf.swap(0, 7);
/// ```
pub fn swap(&mut self, i: usize, j: usize) {
assert!(i < self.size, "i index out-of-bounds");
assert!(j < self.size, "j index out-of-bounds");
if i != j {
let i = add_mod(self.start, i, N);
let j = add_mod(self.start, j, N);
// SAFETY: these are valid pointers
unsafe { ptr::swap_nonoverlapping(&mut self.items[i], &mut self.items[j], 1) };
}
}
/// Removes the element at `index` and returns it, replacing it with the back of the buffer.
///
/// Returns `None` if `index` is out-of-bounds.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<5, char>::from(['a', 'b', 'c', 'd']);
/// assert_eq!(buf, ['a', 'b', 'c', 'd']);
///
/// assert_eq!(buf.swap_remove_back(2), Some('c'));
/// assert_eq!(buf, ['a', 'b', 'd']);
///
/// assert_eq!(buf.swap_remove_back(7), None);
/// ```
pub fn swap_remove_back(&mut self, index: usize) -> Option<T> {
if index >= self.size {
return None;
}
self.swap(index, self.size - 1);
self.pop_back()
}
/// Removes the element at `index` and returns it, replacing it with the front of the buffer.
///
/// Returns `None` if `index` is out-of-bounds.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<5, char>::from(['a', 'b', 'c', 'd']);
/// assert_eq!(buf, ['a', 'b', 'c', 'd']);
///
/// assert_eq!(buf.swap_remove_front(2), Some('c'));
/// assert_eq!(buf, ['b', 'a', 'd']);
///
/// assert_eq!(buf.swap_remove_front(7), None);
/// ```
pub fn swap_remove_front(&mut self, index: usize) -> Option<T> {
if index >= self.size {
return None;
}
self.swap(index, 0);
self.pop_front()
}
/// Shortens the buffer, keeping only the front `len` elements and dropping the rest.
///
/// If `len` is equal or greater to the buffer's current length, this has no effect.
///
/// Calling `truncate_back(0)` is equivalent to [`clear()`](Self::clear).
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, u32>::from([10, 20, 30]);
///
/// buf.truncate_back(1);
/// assert_eq!(buf, [10]);
///
/// // Truncating to a length that is greater than the buffer's length has no effect
/// buf.truncate_back(8);
/// assert_eq!(buf, [10]);
/// ```
pub fn truncate_back(&mut self, len: usize) {
if N == 0 || len >= self.size {
// Nothing to do
return;
}
let drop_range = len..self.size;
// SAFETY: `drop_range` is a valid range, so elements within are guaranteed to be
// initialized. The `size` of the buffer is shrunk before dropping, so no value will be
// dropped twice in case of panics.
unsafe { self.drop_range(drop_range) };
self.size = len;
}
/// Shortens the buffer, keeping only the back `len` elements and dropping the rest.
///
/// If `len` is equal or greater to the buffer's current length, this has no effect.
///
/// Calling `truncate_front(0)` is equivalent to [`clear()`](Self::clear).
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, u32>::from([10, 20, 30]);
///
/// buf.truncate_front(1);
/// assert_eq!(buf, [30]);
///
/// // Truncating to a length that is greater than the buffer's length has no effect
/// buf.truncate_front(8);
/// assert_eq!(buf, [30]);
/// ```
pub fn truncate_front(&mut self, len: usize) {
if N == 0 || len >= self.size {
// Nothing to do
return;
}
let drop_len = self.size - len;
let drop_range = 0..drop_len;
// SAFETY: `drop_range` is a valid range, so elements within are guaranteed to be
// initialized. The `start` of the buffer is shrunk before dropping, so no value will be
// dropped twice in case of panics.
unsafe { self.drop_range(drop_range) };
self.start = add_mod(self.start, drop_len, N);
self.size = len;
}
/// Drops all the elements in the buffer.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf = CircularBuffer::<4, u32>::from([10, 20, 30]);
/// assert_eq!(buf, [10, 20, 30]);
/// buf.clear();
/// assert_eq!(buf, []);
/// ```
#[inline]
pub fn clear(&mut self) {
self.truncate_back(0)
}
}
impl<const N: usize, T> CircularBuffer<N, T>
where T: Clone
{
/// Clones and appends all the elements from the slice to the back of the buffer.
///
/// This is an optimized version of [`extend()`](CircularBuffer::extend) for slices.
///
/// If slice contains more values than the available capacity, the elements at the front of the
/// buffer are dropped.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let mut buf: CircularBuffer<5, u32> = CircularBuffer::from([1, 2, 3]);
/// buf.extend_from_slice(&[4, 5, 6, 7]);
/// assert_eq!(buf, [3, 4, 5, 6, 7]);
/// ```
pub fn extend_from_slice(&mut self, other: &[T]) {
if N == 0 {
return;
}
debug_assert!(self.start < N, "start out-of-bounds");
debug_assert!(self.size <= N, "size out-of-bounds");
#[cfg(not(feature = "unstable"))]
fn write_uninit_slice_cloned<T: Clone>(dst: &mut [MaybeUninit<T>], src: &[T]) {
// Each call to `clone()` may panic, therefore we need to track how many elements we
// successfully cloned so that we can drop them in case of panic. This `Guard` struct
// does exactly that: it keeps track of how many items have been successfully cloned
// and drops them if the guard is dropped.
//
// This implementation was highly inspired by the implementation of
// `MaybeUninit::write_slice_cloned`
struct Guard<'a, T> {
dst: &'a mut [MaybeUninit<T>],
initialized: usize,
}
impl<'a, T> Drop for Guard<'a, T> {
fn drop(&mut self) {
let initialized = &mut self.dst[..self.initialized];
// SAFETY: this slice contain only initialized objects; `MaybeUninit<T>` has
// the same alignment and size as `T`
unsafe {
let initialized = &mut *(initialized as *mut [MaybeUninit<T>] as *mut [T]);
ptr::drop_in_place(initialized);
}
}
}
debug_assert_eq!(dst.len(), src.len());
let len = dst.len();
let mut guard = Guard { dst, initialized: 0 };
#[allow(clippy::needless_range_loop)]
for i in 0..len {
guard.dst[i].write(src[i].clone());
guard.initialized += 1;
}
// All the `clone()` calls succeded; get rid of the guard without running its `drop()`
// implementation
mem::forget(guard);
}
if other.len() < N {
// All the elements of `other` fit into the buffer
let free_size = N - self.size;
let final_size = if other.len() < free_size {
// All the elements of `other` fit at the back of the buffer
self.size + other.len()
} else {
// Some of the elements of `other` need to overwrite the front of the buffer
self.truncate_front(N - other.len());
N
};
let (right, left) = self.slices_uninit_mut();
let write_len = core::cmp::min(right.len(), other.len());
#[cfg(feature = "unstable")]
MaybeUninit::write_slice_cloned(&mut right[..write_len], &other[..write_len]);
#[cfg(not(feature = "unstable"))]
write_uninit_slice_cloned(&mut right[..write_len], &other[..write_len]);
let other = &other[write_len..];
debug_assert!(left.len() >= other.len());
let write_len = other.len();
#[cfg(feature = "unstable")]
MaybeUninit::write_slice_cloned(&mut left[..write_len], other);
#[cfg(not(feature = "unstable"))]
write_uninit_slice_cloned(&mut left[..write_len], other);
self.size = final_size;
} else {
// `other` overwrites the whole buffer; get only the last `N` elements from `other` and
// overwrite
self.clear();
self.start = 0;
let other = &other[other.len() - N..];
debug_assert_eq!(self.items.len(), other.len());
#[cfg(feature = "unstable")]
MaybeUninit::write_slice_cloned(&mut self.items, other);
#[cfg(not(feature = "unstable"))]
write_uninit_slice_cloned(&mut self.items, other);
self.size = N;
}
}
/// Clones the elements of the buffer into a new [`Vec`], leaving the buffer unchanged.
///
/// # Examples
///
/// ```
/// use circular_buffer::CircularBuffer;
///
/// let buf: CircularBuffer<5, u32> = CircularBuffer::from([1, 2, 3]);
/// let vec: Vec<u32> = buf.to_vec();
///
/// assert_eq!(buf, [1, 2, 3]);
/// assert_eq!(vec, [1, 2, 3]);
/// ```
#[must_use]
#[cfg(feature = "use_std")]
pub fn to_vec(&self) -> Vec<T> {
let mut vec = Vec::with_capacity(self.size);
vec.extend(self.iter().cloned());
debug_assert_eq!(vec.len(), self.size);
vec
}
}
unstable_const_impl! {
impl<{const N: usize, T}> const Default for CircularBuffer<N, T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
}
impl<const N: usize, const M: usize, T> From<[T; M]> for CircularBuffer<N, T> {
fn from(mut arr: [T; M]) -> Self {
#[cfg(feature = "unstable")]
let mut elems = MaybeUninit::<T>::uninit_array();
#[cfg(not(feature = "unstable"))]
let mut elems = unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() };
let arr_ptr = &arr as *const T as *const MaybeUninit<T>;
let elems_ptr = &mut elems as *mut MaybeUninit<T>;
let size = if N >= M { M } else { N };
// SAFETY:
// - `M - size` is non-negative, and `arr_ptr.add(M - size)` points to a memory location
// that contains exactly `size` elements
// - `elems_ptr` points to a memory location that contains exactly `N` elements, and `N` is
// greater than or equal to `size`
unsafe { ptr::copy_nonoverlapping(arr_ptr.add(M - size), elems_ptr, size); }
// Prevent destructors from running on those elements that we've taken ownership of; only
// destroy the elements that were discareded
//
// SAFETY: All elements in `arr` are initialized; `forget` will make sure that destructors
// are not run twice
unsafe { ptr::drop_in_place(&mut arr[..M - size]); }
mem::forget(arr);
Self { size, start: 0, items: elems }
}
}
impl<const N: usize, T> FromIterator<T> for CircularBuffer<N, T> {
fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>
{
// TODO Optimize
let mut buf = Self::new();
iter.into_iter().for_each(|item| buf.push_back(item));
buf
}
}
impl<const N: usize, T> Extend<T> for CircularBuffer<N, T> {
fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = T>
{
// TODO Optimize
iter.into_iter().for_each(|item| self.push_back(item));
}
}
impl<'a, const N: usize, T> Extend<&'a T> for CircularBuffer<N, T>
where T: Copy
{
fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = &'a T>
{
// TODO Optimize
iter.into_iter().for_each(|item| self.push_back(*item));
}
}
unstable_const_impl! {
impl<{const N: usize, T}> const IntoIterator for CircularBuffer<N, T> {
type Item = T;
type IntoIter = IntoIter<N, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self)
}
}
}
unstable_const_impl! {
impl<{'a, const N: usize, T}> const IntoIterator for &'a CircularBuffer<N, T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
Iter::new(self)
}
}
}
impl<const N: usize, const M: usize, T, U> PartialEq<CircularBuffer<M, U>> for CircularBuffer<N, T>
where T: PartialEq<U>
{
fn eq(&self, other: &CircularBuffer<M, U>) -> bool {
if self.len() != other.len() {
return false;
}
let (a_left, a_right) = self.as_slices();
let (b_left, b_right) = other.as_slices();
match a_left.len().cmp(&b_left.len()) {
Ordering::Less => {
let x = a_left.len();
let y = b_left.len() - x;
a_left[..] == b_left[..x] && a_right[..y] == b_left[x..] && a_right[y..] == b_right[..]
},
Ordering::Greater => {
let x = b_left.len();
let y = a_left.len() - x;
a_left[..x] == b_left[..] && a_right[x..] == b_left[..y] && a_right[..] == b_right[y..]
},
Ordering::Equal => {
debug_assert_eq!(a_left.len(), b_left.len());
debug_assert_eq!(a_right.len(), b_right.len());
a_left == b_left && a_right == b_right
},
}
}
}
impl<const N: usize, T> Eq for CircularBuffer<N, T> where T: Eq {}
impl<const N: usize, T, U> PartialEq<[U]> for CircularBuffer<N, T>
where T: PartialEq<U>
{
fn eq(&self, other: &[U]) -> bool {
if self.len() != other.len() {
return false;
}
let (a_left, a_right) = self.as_slices();
let (b_left, b_right) = other.split_at(a_left.len());
debug_assert_eq!(a_left.len(), b_left.len());
debug_assert_eq!(a_right.len(), b_right.len());
a_left == b_left && a_right == b_right
}
}
impl<const N: usize, const M: usize, T, U> PartialEq<[U; M]> for CircularBuffer<N, T>
where T: PartialEq<U>
{
#[inline]
fn eq(&self, other: &[U; M]) -> bool {
self == &other[..]
}
}
impl<'a, const N: usize, T, U> PartialEq<&'a [U]> for CircularBuffer<N, T>
where T: PartialEq<U>
{
#[inline]
fn eq(&self, other: &&'a [U]) -> bool {
self == *other
}
}
impl<'a, const N: usize, T, U> PartialEq<&'a mut [U]> for CircularBuffer<N, T>
where T: PartialEq<U>
{
#[inline]
fn eq(&self, other: &&'a mut [U]) -> bool {
self == *other
}
}
impl<'a, const N: usize, const M: usize, T, U> PartialEq<&'a [U; M]> for CircularBuffer<N, T>
where T: PartialEq<U>
{
#[inline]
fn eq(&self, other: &&'a [U; M]) -> bool {
self == *other
}
}
impl<'a, const N: usize, const M: usize, T, U> PartialEq<&'a mut [U; M]> for CircularBuffer<N, T>
where T: PartialEq<U>
{
#[inline]
fn eq(&self, other: &&'a mut [U; M]) -> bool {
self == *other
}
}
impl<const N: usize, const M: usize, T, U> PartialOrd<CircularBuffer<M, U>> for CircularBuffer<N, T>
where T: PartialOrd<U>
{
fn partial_cmp(&self, other: &CircularBuffer<M, U>) -> Option<Ordering> {
self.iter().partial_cmp(other.iter())
}
}
impl<const N: usize, T> Ord for CircularBuffer<N, T>
where T: Ord
{
fn cmp(&self, other: &Self) -> Ordering {
self.iter().cmp(other.iter())
}
}
impl<const N: usize, T> Hash for CircularBuffer<N, T>
where T: Hash
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.size.hash(state);
self.iter().for_each(|item| item.hash(state));
}
}
impl<const N: usize, T> Clone for CircularBuffer<N, T>
where T: Clone
{
fn clone(&self) -> Self {
// TODO Optimize
Self::from_iter(self.iter().cloned())
}
fn clone_from(&mut self, other: &Self) {
// TODO Optimize
self.clear();
self.extend(other.iter().cloned());
}
}
impl<const N: usize, T> Drop for CircularBuffer<N, T> {
#[inline]
fn drop(&mut self) {
// `clear()` will make sure that every element is dropped in a safe way
self.clear();
}
}
impl<const N: usize, T> fmt::Debug for CircularBuffer<N, T>
where T: fmt::Debug
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self).finish()
}
}