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
//! Traits, helpers, and type definitions for the collection framework.
//!
//! This module contains various tools for interacting with the collections. In summary:
//!
//! - [`Iterators`] is a trait that encapsulates all possible methods for iterating over a
//! [`Collection`].
//! - [`Collection`] is the base trait all collections must implement. Defines a series of methods
//! commonly used by almost all collections.
//! - [`FixedSizeCollection`] is a trait that extends [`Collection`] and adds the ability to define
//! collections with a fixed capacity and the possibility of expanding them. It is mainly intended
//! for collections based on arrays.
//! - [`ExpansionMode`] is a type that defines the behavior of a collection when it is full.
//! - [`check_expansion`] is a function that checks if the collection is full and if it is, expands
//! it depending on the [`ExpansionMode`]. Instead of using this function directly, the
//! [`check_expansion_add`] macro is available to wrap the behavior of a function.
//!
//! For more details, see the respective documentation of each item in the lists.
//!
//! [`check_expansion_add`]: trait_based_collection_macros::check_expansion_add
#![warn(missing_docs)]
use std::ops::{Deref, DerefMut};
pub use trait_based_collection_macros::check_expansion_add;
/// Trait that allows to implement all possible iterators over a [`Collection`]. All collections
/// must implement all iterators, allowing for default implementations across collections.
///
/// There are three different types of iterators:
///
/// - [`iter`] returns an immutable iterator over [`ItemRef`] in the [`Collection`] without
/// consuming them.
/// - [`iter_mut`] returns a mutable iterator over the [`ItemMut`] in the [`Collection`] without
/// consuming them.
/// - [`into_iter`] returns an iterator over the items in the [`Collection`] and consumes the
/// collection .
///
/// # Safety
///
/// All iterators must return the items in the same order as the other iterators. This is
/// required to ensure that the iterators are consistent with each other. This is not checked
/// by the compiler. If this is not upheld, the behavior of the [`Collection`] is undefined.
///
/// [`iter`]: Iterators::iter
/// [`ItemRef`]: Iterators::ItemRef
/// [`iter_mut`]: Iterators::iter_mut
/// [`ItemMut`]: Iterators::ItemMut
/// [`into_iter`]: IntoIterator::into_iter
pub trait Iterators<'a, T>: IntoIterator<Item = T> {
/// The type of reference the immutable iterator ([`Iter`]) iterates over the items in the
/// [`Collection`]. The reference is only valid for the duration of the iteration.
///
/// [`Iter`]: Iterators::Iter
type ItemRef: 'a + Deref<Target = T>;
/// The type of mutable reference the mutable iterator ([`IterMut`]) iterates over the items in
/// the [`Collection`]. The reference is only valid for the duration of the iteration.
///
/// [`IterMut`]: Iterators::IterMut
type ItemMut: 'a + DerefMut<Target = T>;
/// Which kind of iterator is returned by [`iter`].
///
/// [`iter`]: Iterators::iter
type Iter: Iterator<Item = Self::ItemRef>;
/// Which kind of iterator is returned by [`iter_mut`].
///
/// [`iter_mut`]: Iterators::iter_mut
type IterMut: Iterator<Item = Self::ItemMut>;
/// Creates an immutable iterator over the items in the [`Collection`] without consuming them.
///
/// If you want to be able to modify the collection , you should use the [`iter_mut`] method.
/// If you want to consume the collection , you should use the [`into_iter`] method that is
/// implemented deriving the [`IntoIterator`](trait_based_collection_macros::IntoIterator) trait.
///
/// Also, it is recommended to implement the [`IntoIterator`] trait but through reference, like
/// this example based on the one at collection :
///
/// # Examples
///
/// Example using the [`ArrayStack`]:
///
/// ```
/// use trait_based_collection::{ArrayStack, prelude::*};
///
/// let mut collection = ArrayStack::with_capacity(10);
/// for i in 0..10 {
/// collection.add(i);
/// }
/// for item in collection.iter() {
/// println!("{}", item);
/// }
/// ```
///
/// Example if the `IntoIterator` is implemented through reference:
///
/// ```
/// use trait_based_collection::{ArrayStack, prelude::*};
///
/// let mut collection = ArrayStack::with_capacity(10);
/// for i in 0..10 {
/// collection.add(i);
/// }
/// for item in &collection {
/// println!("{}", item);
/// }
/// ```
///
/// [`iter_mut`]: Iterators::iter_mut
/// [`into_iter`]: IntoIterator::into_iter
/// [`ArrayStack`]: crate::stack::ArrayStack
fn iter(&'a self) -> Self::Iter;
/// Creates a mutable iterator over the items in the [`Collection`] without consuming them.
///
/// If you don't want to be able to modify the collection , you should use the [`iter`] method.
/// If you want to consume the collection , you should use the [`into_iter`] method that is
/// implemented deriving the [`IntoIterator`](trait_based_collection_macros::IntoIterator) trait.
///
/// Also, it is recommended to implement the [`IntoIterator`] trait but through reference, like
/// this example based on the one at collection :
///
/// # Examples
///
/// Example using the [`ArrayStack`]:
///
/// ```
/// use trait_based_collection::{ArrayStack, prelude::*};
///
/// let mut collection = ArrayStack::with_capacity(10);
/// for i in 0..10 {
/// collection.add(i);
/// }
/// for item in collection.iter_mut() {
/// println!("{}", item);
/// }
/// ```
///
/// Example if the `IntoIterator` is implemented through reference:
///
/// ```
/// use trait_based_collection::{ArrayStack, prelude::*};
///
/// let mut collection = ArrayStack::with_capacity(10);
/// for i in 0..10 {
/// collection.add(i);
/// }
/// for item in &collection {
/// println!("{}", item);
/// }
/// ```
///
/// [`iter`]: Iterators::iter
/// [`into_iter`]: IntoIterator::into_iter
/// [`ArrayStack`]: crate::stack::ArrayStack
fn iter_mut(&'a mut self) -> Self::IterMut;
}
/// This is the trait that all implementations of a collection must implement. A [`Collection`] is a
/// data structure that can be used to store a collection of items.
///
/// The [`Collection`] is generic over any type without the need of any extra traits. This allows
/// the user to create a collection of any type that they want.
///
/// The trait provides a number of methods that can be used to create, manipulate, and retrieve
/// items from the [`Collection`]. The methods are divided into three groups:
///
/// - **Creation:** These methods are used to create a new [`Collection`] and add items to it.
/// There are the following methods:
/// - [`new_default`]: Creates a new [`Collection`] with a default capacity. Normally, this
/// means that the collection will be expandable.
/// - [`with_capacity`]: Creates a new [`Collection`] with a specific capacity. This method is
/// useful if you want to avoid the expense of resizing the collection when adding items.
/// - [`with_approximate_capacity`]: Creates a new [`Collection`] with a capacity that is close
/// to the specified capacity but could be larger. This method is useful if you want to avoid
/// some of the expense of resizing the collection when adding items.
///
/// For more information about capacity, see the trait [`FixedSizeCollection`] which is used to
/// create collections with a fixed capacity (i.e. the collection will not be easily expandable).
///
/// - **Manipulation:** These methods are used to add, remove, and retrieve items from the
/// [`Collection`]. There are the following methods:
/// - [`add`]: Adds an item to the [`Collection`].
/// - [`remove`]: Removes an item from the [`Collection`] and returns the ownership of the item.
/// - [`clear`]: Removes all items from the [`Collection`].
///
/// - **Retrieval:** These methods are used to retrieve items or information from the
/// [`Collection`]. There are the following methods:
/// - [`peek`]: Returns a [`ItemRef`] to the an item in the [`Collection`]. The item should be
/// the same as the one returned by [`remove`].
/// - [`peek_mut`]: Returns a [`ItemMut`] to the an item in the [`Collection`]. The item should
/// be the same as the one returned by [`remove`].
/// - [`get`]: Returns a [`ItemRef`] to the an item at the specified index in the
/// [`Collection`].
/// - [`get_mut`]: Returns a [`ItemMut`] to the an item at the specified index in the
/// [`Collection`].
/// - [`find`]: Returns an index to the an item in the [`Collection`] that matches the
/// specified item.
/// - [`contains`]: Returns true if the [`Collection`] contains the specified item.
/// - [`len`]: Returns the number of items in the [`Collection`].
/// - [`is_empty`]: Returns `true` if the [`Collection`] is empty.
///
/// As briefly mentioned above, the [`Collection`] is intended for all types of data structures.
/// However, if the the amount of items in the collection is known, it is possible to create a
/// [`FixedSizeCollection`] which can be used to create a collection with a fixed capacity. This is
/// mainly for implementation of data structures using arrays.
///
/// # Examples
///
/// A simple example of creating a [`Collection`] by using a wrapper around the [`Vec`] data
/// structure with the minimum amount of code (by using the default implementation of the
/// different methods):
///
/// ```
/// use trait_based_collection::{prelude::*, macros::All};
///
/// #[derive(All)]
/// struct MyCollection<T> {
/// data: Vec<T>,
/// }
///
/// impl<'a, T: 'a> Iterators<'a, T> for MyCollection<T> {
/// type ItemRef = &'a T;
/// type ItemMut = &'a mut T;
///
/// type Iter = std::slice::Iter<'a, T>;
/// type IterMut = std::slice::IterMut<'a, T>;
///
/// fn iter(&'a self) -> Self::Iter {
/// self.data.iter()
/// }
///
/// fn iter_mut(&'a mut self) -> Self::IterMut {
/// self.data.iter_mut()
/// }
/// }
///
/// impl<'a, T: 'a> Collection<'a, T> for MyCollection<T> {
/// fn new_default() -> Self where Self: Sized {
/// MyCollection {
/// data: Vec::new(),
/// }
/// }
///
/// fn add(&mut self, value: T) {
/// self.data.push(value);
/// }
///
/// fn remove(&mut self) -> Option<T> {
/// self.data.pop()
/// }
///
/// fn len(&self) -> usize {
/// self.data.len()
/// }
/// }
/// ```
///
///
/// # Derivable Traits
///
/// The [`Collection`] trait allows the easy implementation of several traits that can be derived
/// through the `derive` macro. These traits can be generically implemented by using the methods
/// in the [`Collection`] trait. Currently, the following traits are derivable:
///
/// - [`FromIterator`]: Creates a new [`Collection`] from an iterator.
/// - [`IntoIterator`]: Creates an iterator from a [`Collection`].
/// - [`Default`]: Creates a new [`Collection`] with a default capacity. Uses the [`new_default`]
/// method.
/// - [`Extend`]: Extends a [`Collection`] with items from an iterator. Uses the [`add`] method.
/// - [`Display`]: Converts a [`Collection`] to a string. Uses the [`iter`] method.
/// - [`NewMacro`]: Adds a macro for easy creation of a new [`Collection`] with the same syntax as
/// the array creation syntax.
/// - [`Drop`]: Drops the [`Collection`] while avoiding memory leaks.
/// - [`Index`]: Allows indexing into a [`Collection`]. However, this this trait could be
/// incompatible with the [`get`] and [`get_mut`] methods.
///
/// Special mention to the [`All`] derive macro, which can be used to derive all traits at once.
///
/// For more information about the derivable traits, see the [`Collection Macros`] module.
///
/// # Safety
///
/// The implementation of the [`Collection`] trait could be unsafe as some of the methods in the
/// trait need the use of unsafe code. However, the different implementations of the [`Collection`]
/// should ensure that the behavior is safe.
///
/// [`new_default`]: Collection::new_default
/// [`with_capacity`]: Collection::with_capacity
/// [`with_approximate_capacity`]: Collection::with_approximate_capacity
/// [`add`]: Collection::add
/// [`remove`]: Collection::remove
/// [`clear`]: Collection::clear
/// [`peek`]: Collection::peek
/// [`peek_mut`]: Collection::peek_mut
/// [`get`]: Collection::get
/// [`get_mut`]: Collection::get_mut
/// [`find`]: Collection::find
/// [`contains`]: Collection::contains
/// [`len`]: Collection::len
/// [`is_empty`]: Collection::is_empty
/// [`ItemRef`]: Iterators::ItemRef
/// [`ItemMut`]: Iterators::ItemMut
/// [`iter`]: Iterators::iter
///
/// [`Collection Macros`]: crate::macros
/// [`FromIterator`]: trait_based_collection_macros::FromIterator
/// [`IntoIterator`]: trait_based_collection_macros::IntoIterator
/// [`Default`]: trait_based_collection_macros::Default
/// [`Extend`]: trait_based_collection_macros::Extend
/// [`Display`]: trait_based_collection_macros::Display
/// [`NewMacro`]: trait_based_collection_macros::NewMacro
/// [`Drop`]: trait_based_collection_macros::Drop
/// [`Index`]: trait_based_collection_macros::Index
/// [`All`]: trait_based_collection_macros::All
pub trait Collection<'a, T>: Iterators<'a, T> {
/// Creates a new [`Collection`] with a default capacity.
///
/// Generally, this means that the collection will be expandable. This method is useful if you
/// don't know the amount of items that will be added to the collection . The default capacity
/// will depend on the implementation of the collection .
///
/// # Examples
///
/// Example using the [`Queue`]:
///
/// ```
/// use trait_based_collection::{prelude::*, Queue};
///
/// let mut queue: Queue<i32> = Queue::new_default();
/// ```
///
/// [`Queue`]: crate::queue::Queue
#[must_use]
fn new_default() -> Self
where
Self: Sized;
/// Creates a new [`Collection`] with a specific capacity.
///
/// This method is useful if you want to avoid the expense of resizing the collection when
/// adding items. The capacity will be the specified capacity.
///
/// This method is especially useful for collections of [`FixedSizeCollection`] types. As
/// linked based data structures don't have an extra cost for adding items, they can be used
/// with an unlimited capacity.
///
/// # Panics
///
/// This method will panic if the specified capacity is equal to zero.
///
/// # Examples
///
/// Example using the [`Deque`]:
///
/// ```
/// use trait_based_collection::{prelude::*, Deque};
///
/// let mut deque: Deque<usize> = Deque::with_capacity(10);
/// ```
///
/// [`Deque`]: crate::queue::Deque
#[must_use]
fn with_capacity(capacity: usize) -> Self
where
Self: Sized,
{
assert!(capacity > 0, "capacity must be greater than zero");
Self::new_default()
}
/// Creates a new [`Collection`] with a capacity that is at least the specified capacity.
///
/// This method is useful if you want to avoid the expense of resizing the collection when
/// adding items. The capacity will be the specified capacity, with the capacity to be increased
/// if needed.
///
/// This method is especially useful for collections of [`FixedSizeCollection`] types. As
/// linked based data structures don't have an extra cost for adding items, they can be used
/// with an unlimited capacity.
///
/// # Panics
///
/// This method will panic if the specified capacity is equal to zero.
///
/// # Examples
///
/// Example using the [`CircularDeque`]:
///
/// ```
/// use trait_based_collection::{prelude::*, CircularDeque};
///
/// let mut circular_deque: CircularDeque<usize> = CircularDeque::with_approximate_capacity(10);
/// ```
///
/// [`CircularDeque`]: crate::queue::CircularDeque
#[must_use]
fn with_approximate_capacity(approx_capacity: usize) -> Self
where
Self: Sized,
{
assert!(
approx_capacity > 0,
"approx_capacity must be greater than zero"
);
Self::with_capacity(approx_capacity)
}
/// Adds an item to the [`Collection`].
///
/// # Examples
///
/// Example using the [`Stack`]:
///
/// ```
/// use trait_based_collection::{prelude::*, Stack};
///
/// let mut stack = Stack::new_default();
/// stack.add(10);
/// assert_eq!(stack.len(), 1);
/// ```
///
/// [`Stack`]: crate::stack::Stack
fn add(&mut self, value: T);
/// Removes an item from the [`Collection`].
///
/// The item that will be removed will depend on the type of data structure. For example,
/// the [`Stack`] type will remove the last item added to the stack. The [`Queue`] type will
/// remove the first item added to the queue.
///
/// # Examples
///
/// Example using the [`ArrayStack`]:
///
/// ```
/// use trait_based_collection::{import, ArrayStack};
/// import!();
///
/// # fn main() {
/// let mut stack = array_stack![0, 1, 2, 3, 4];
///
/// for i in (0..5).rev() {
/// assert_eq!(stack.remove().unwrap(), i);
/// }
///
/// assert_eq!(stack.remove(), None);
/// # }
/// ```
///
/// [`Stack`]: crate::stack::Stack
/// [`Queue`]: crate::queue::Queue
/// [`ArrayStack`]: crate::stack::ArrayStack
fn remove(&mut self) -> Option<T>;
/// Clears all items from the [`Collection`] while keeping the capacity.
///
/// This method is useful if you want to reuse the collection . It will not free any memory.
///
/// # Examples
///
/// Example using the [`Queue`]:
///
/// ```
/// use trait_based_collection::{prelude::*, Queue};
///
/// let mut queue = Queue::new_default();
/// queue.add(0);
///
/// assert!(!queue.is_empty());
/// queue.clear();
/// assert!(queue.is_empty());
/// ```
///
/// [`Queue`]: crate::queue::Queue
fn clear(&mut self) {
while !self.is_empty() {
self.remove()
.expect("The collection is not empty, so remove should never return None");
}
}
/// Returns an immutable reference of the item that will be removed next.
///
/// # Examples
///
/// Example using the [`Deque`]:
///
/// ```
/// use trait_based_collection::{import, Deque};
/// import!();
///
/// # fn main() {
/// let mut queue = deque![0, 1, 2, 3, 4];
///
/// assert_eq!(queue.peek(), Some(&0));
/// # }
/// ```
///
/// [`Deque`]: crate::queue::Deque
fn peek(&'a self) -> Option<Self::ItemRef> {
self.iter().next()
}
/// Returns a mutable reference of the item that will be removed next.
///
/// # Examples
///
/// Example using the [`CircularDeque`]:
///
/// ```
/// use trait_based_collection::{import, CircularDeque};
/// import!();
///
/// # fn main() {
/// let mut queue = circular_deque![0, 1, 2, 3, 4];
///
/// assert_eq!(queue.peek(), Some(&0));
/// # }
/// ```
///
/// [`CircularDeque`]: crate::queue::CircularDeque
fn peek_mut(&'a mut self) -> Option<Self::ItemMut> {
self.iter_mut().next()
}
/// Returns a immutable reference to the n-th item in the [`Collection`].
///
/// This should return the same item as if we removed `n-1` items from the [`Collection`] and
/// returned the last item.
///
/// # Examples
///
/// Example using the [`Stack`]:
///
/// ```
/// use trait_based_collection::{import, Stack};
/// import!();
///
/// # fn main() {
/// let mut stack = stack![0, 1, 2, 3, 4];
///
/// assert_eq!(stack.get(3), Some(&1));
/// # }
/// ```
///
/// [`Stack`]: crate::stack::Stack
fn get(&'a self, index: usize) -> Option<Self::ItemRef> {
self.iter().nth(index)
}
/// Returns a mutable reference to the n-th item in the [`Collection`].
///
/// This should return the same item as if we removed `n-1` items from the [`Collection`] and
/// returned the last item.
///
/// # Examples
///
/// Example using the [`ArrayStack`]:
///
/// ```
/// use trait_based_collection::{import, ArrayStack};
/// import!();
///
/// # fn main() {
/// let mut stack = array_stack![0, 1, 2, 3, 4];
///
/// assert_eq!(stack.get_mut(1), Some(&mut 3));
/// # }
/// ```
///
/// [`ArrayStack`]: crate::stack::ArrayStack
fn get_mut(&'a mut self, index: usize) -> Option<Self::ItemMut> {
self.iter_mut().nth(index)
}
/// Searches for an item in the [`Collection`] and returns its index if found.
/// Returns `None` if the item is not found.
///
/// The default implementation uses [`iter`] to find the item with a linear search.
///
/// # Examples
///
/// Example using the [`Queue`]:
///
/// ```
/// use trait_based_collection::{import, Queue};
/// import!();
///
/// # fn main() {
/// let mut queue = queue![4, 1, 0, 2, 3];
///
/// assert_eq!(queue.find(&2), Some(3));
/// # }
/// ```
///
/// [`Queue`]: crate::queue::Queue
/// [`iter`]: Iterators::iter
fn find(&'a self, value: &T) -> Option<usize>
where
T: PartialEq,
{
self.iter().position(|x| *x == *value)
}
/// Checks if an item is in the [`Collection`].
///
/// The default implementation uses [`find`] to check if the item is in the collection .
///
/// # Examples
///
/// Example using the [`Deque`]:
///
/// ```
/// use trait_based_collection::{import, Deque};
/// import!();
///
/// # fn main() {
/// let mut queue = deque![0, 1, 2, 3, 4];
///
/// assert!(queue.contains(&3));
/// # }
/// ```
///
/// [`Deque`]: crate::queue::Deque
/// [`find`]: Collection::find
fn contains(&'a self, value: &T) -> bool
where
T: PartialEq,
{
self.find(value).is_some()
}
/// Returns the number of items in the [`Collection`].
///
/// # Examples
///
/// Example using the [`CircularDeque`]:
///
/// ```
/// use trait_based_collection::{prelude::*, CircularDeque};
///
/// let mut queue = CircularDeque::new_default();
/// assert_eq!(queue.len(), 0);
///
/// for i in 0..10 {
/// queue.add(i);
/// }
///
/// assert_eq!(queue.len(), 10);
/// ```
///
/// [`CircularDeque`]: crate::queue::CircularDeque
fn len(&self) -> usize;
/// Checks if the [`Collection`] is empty.
///
/// # Examples
///
/// Example using the [`Stack`]:
///
/// ```
/// use trait_based_collection::{prelude::*, Stack};
///
/// let mut stack = Stack::new_default();
///
/// assert!(stack.is_empty());
/// stack.add(0);
/// assert!(!stack.is_empty());
/// ```
///
/// [`Stack`]: crate::stack::Stack
fn is_empty(&self) -> bool {
self.len() == 0
}
}
/// Different modes of action when the [`FixedSizeCollection`] is full. Depending on the mode, the
/// collection will behave differently through the [`check_expansion`] method.
///
/// There are four modes of expansion:
/// 1. [`Panic`] - The collection will panic when the capacity is reached.
/// 2. [`Ignore`] - The collection will ignore the addition of the new item.
/// 3. [`Overwrite`] - The collection will overwrite the an item when the capacity is reached.
/// 4. [`Expand`] - The collection will expand the capacity by a specific factor. The factor must
/// be greater than `1.0`. If the factor is `1.0`, the function will `panic!`. This is the
/// default mode with a factor of `2.0`.
///
/// # Examples
///
/// Example on how the expansion is checked in [`FixedSizeCollection`] through the
/// [`check_expansion`] method:
///
/// ```
/// use trait_based_collection::prelude::*;
///
/// fn check_expansion<'a, T, C: FixedSizeCollection<'a, T>>(mut collection: C) -> bool {
/// if collection.is_full() {
/// match collection.mode() {
/// ExpansionMode::Panic => {
/// panic!("The collection is full");
/// }
/// ExpansionMode::Ignore => {
/// return true;
/// }
/// ExpansionMode::Overwrite => {
/// collection.remove();
/// }
/// ExpansionMode::Expand(factor) => {
/// if *factor < 1.0 {
/// panic!("Expand factor must be greater than 1");
/// }
/// let size = ((*factor - 1.0) * collection.capacity() as f64).floor() as usize;
/// collection.expand(size);
/// }
/// }
/// }
/// false
/// }
/// ```
///
/// [`Panic`]: ExpansionMode::Panic
/// [`Ignore`]: ExpansionMode::Ignore
/// [`Overwrite`]: ExpansionMode::Overwrite
/// [`Expand`]: ExpansionMode::Expand
#[derive(Debug)]
pub enum ExpansionMode {
/// The collection will panic when the capacity is reached.
Panic,
/// The collection will ignore the addition of the new item.
Ignore,
/// The collection will overwrite the an item when the capacity is reached. This means that
/// before the new item is added, an item is removed by calling the [`remove`] method.
///
/// [`remove`]: Collection::remove
Overwrite,
/// The collection will expand the capacity by a specific factor. The factor must be greater
/// than `1.0`. If the factor is `1.0`, the function will `panic!`. This is the default mode
/// with a factor of `2.0`.
Expand(f64),
}
impl PartialEq for ExpansionMode {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Panic, Self::Panic)
| (Self::Ignore, Self::Ignore)
| (Self::Overwrite, Self::Overwrite) => true,
(ExpansionMode::Expand(a), ExpansionMode::Expand(b)) => a == b,
_ => false,
}
}
}
impl Default for ExpansionMode {
fn default() -> Self {
Self::Expand(2.0)
}
}
/// A [`Collection`] that can easily be expanded, as the capacity is fixed. Normally, this are data
/// structures that use a fixed size buffer in memory ([`Array`]-like).
///
/// Similar to [`Collection`], the [`FixedSizeCollection`] is a trait that is generic over any type
/// without the need of any extra traits. This allows the user to create a collection of any type
/// that they want.
///
/// To create a new [`FixedSizeCollection`], the user must add the following extra code into the
/// [`add`] method at the beginning of the method. This code will manage the expansion of the
/// collection depending on the [`ExpansionMode`] of the collection :
///
/// ```text
/// if check_expansion(self) {
/// return;
/// }
/// ```
///
/// An alternative way is to call the [`check_expansion_add`] macro. This macro will add the
/// the previous code into the [`add`] method.
///
/// For a full example, see the Examples section.
///
/// The trait provide extra methods related with the capacity of the collection . The capacity is
/// the amount of items that the collection can hold. The [`FixedSizeCollection`] provides the
/// following methods:
///
/// - [`with_mode`] - Creates a new [`FixedSizeCollection`] with the specified capacity and
/// [`ExpansionMode`].
/// - [`capacity`] - Returns the maximum amount of items that the collection can hold.
/// - [`expand`] - Expands the capacity of the collection by a specific amount. This amount or more
/// will be added to the capacity.
/// - [`is_full`] - Returns `true` if the collection is full. Checks if the length of the collection
/// is equal to the capacity.
/// - [`mode`] - Returns the [`ExpansionMode`] of expansion of the collection .
///
/// For implementation of the [`FixedSizeCollection`] data structure, there also exists the
/// following method:
///
/// - [`check_expansion`] - Checks if the collection is full and if it is, it will behave depending
/// on the [`mode`] of the collection .
///
/// # Examples
///
/// Expands the example shown in [`Collection`] by modifying a bit the struct with a mode and
/// implementing the [`FixedSizeCollection`] trait. The example contains only the modified code.
///
/// ```
/// use trait_based_collection::{prelude::*, macros::{check_expansion_add, All}};
///
/// #[derive(All)]
/// struct MyCollection<T> {
/// data: Vec<T>,
/// mode: ExpansionMode,
/// }
/// #
/// # impl<'a, T: 'a> Iterators<'a, T> for MyCollection<T> {
/// # type ItemRef = &'a T;
/// # type ItemMut = &'a mut T;
/// #
/// # type Iter = std::slice::Iter<'a, T>;
/// # type IterMut = std::slice::IterMut<'a, T>;
/// #
/// # fn iter(&'a self) -> Self::Iter {
/// # self.data.iter()
/// # }
/// #
/// # fn iter_mut(&'a mut self) -> Self::IterMut {
/// # self.data.iter_mut()
/// # }
/// # }
///
/// impl<'a, T: 'a> Collection<'a, T> for MyCollection<T> {
/// fn new_default() -> Self where Self: Sized {
/// MyCollection::with_mode(16, ExpansionMode::default())
/// }
///
/// fn with_capacity(capacity: usize) -> Self {
/// MyCollection::with_mode(capacity, ExpansionMode::Panic)
/// }
///
/// fn with_approximate_capacity(approx_capacity: usize) -> Self{
/// MyCollection::with_mode(approx_capacity, ExpansionMode::default())
/// }
///
/// #[check_expansion_add]
/// fn add(&mut self, value: T) {
/// self.data.push(value);
/// }
/// #
/// # fn remove(&mut self) -> Option<T> {
/// # self.data.pop()
/// # }
/// #
/// # fn len(&self) -> usize {
/// # self.data.len()
/// # }
/// }
///
/// impl<'a, T: 'a> FixedSizeCollection<'a, T> for MyCollection<T> {
/// fn with_mode(capacity: usize, mode: ExpansionMode) -> Self {
/// assert_ne!(capacity, 0, "Capacity must be greater than 0");
/// MyCollection {
/// data: Vec::with_capacity(capacity),
/// mode,
/// }
/// }
///
/// fn capacity(&self) -> usize {
/// self.data.capacity()
/// }
///
/// fn expand(&mut self, extra_size: usize) {
/// self.data.reserve(extra_size);
/// }
///
/// fn mode(&self) -> &ExpansionMode {
/// &self.mode
/// }
/// }
/// ```
///
/// # Safety
///
/// The [`FixedSizeCollection`] trait is currently safe as it is based on the [`Vec`]
/// implementation. However, in the future, [`Vec`] could also be implemented in the project and
/// the [`FixedSizeCollection`] trait would be unsafe. Similarly to [`Collection`], the trait
/// is implemented in unsafe code but the usage of the trait should be safe.
///
/// [`Array`]: https://doc.rust-lang.org/std/primitive.array.html
/// [`add`]: Collection::add
/// [`check_expansion_add`]: macro@trait_based_collection_macros::check_expansion_add
/// [`capacity`]: FixedSizeCollection::capacity
/// [`expand`]: FixedSizeCollection::expand
/// [`is_full`]: FixedSizeCollection::is_full
/// [`mode`]: FixedSizeCollection::mode
/// [`with_mode`]: FixedSizeCollection::with_mode
#[allow(clippy::module_name_repetitions)]
pub trait FixedSizeCollection<'a, T>: Collection<'a, T> {
/// Creates a new fixed size [`Collection`] with the specified capacity and [`ExpansionMode`].
///
/// # Panics
///
/// This method will panic if the specified capacity is equal to zero.
///
/// # Examples
///
/// Example using [`ArrayStack`]:
///
/// ```
/// use trait_based_collection::{prelude::*, ArrayStack};
///
/// let mut stack: ArrayStack<i32> = ArrayStack::with_mode(16, ExpansionMode::Expand(2.0));
/// assert_eq!(stack.capacity(), 16);
/// assert_eq!(stack.mode(), &ExpansionMode::Expand(2.0));
/// ```
///
/// [`ArrayStack`]: crate::stack::ArrayStack
#[must_use]
fn with_mode(capacity: usize, mode: ExpansionMode) -> Self
where
Self: Sized;
/// Returns the maximum amount of items that the collection can hold without expanding.
///
/// The number of items on the [`FixedSizeCollection`] can never be greater than the capacity.
///
/// # Examples
///
/// Example using [`CircularDeque`]:
///
/// ```
/// use trait_based_collection::{prelude::*, CircularDeque};
///
/// let mut queue: CircularDeque<usize> = CircularDeque::with_capacity(10);
/// assert_eq!(queue.capacity(), 10);
/// ```
///
/// [`CircularDeque`]: crate::queue::CircularDeque
fn capacity(&self) -> usize;
/// Expands the capacity of the collection by at least the specified amount. This amount or more
/// will be dded to the capacity.
///
/// This method is called automatically if the [`mode`] of the collection is [`Expand`], it will
/// be called when the collection is full through the [`check_expansion`] method.
///
/// This method ensures that after the expansion, the integrity of the [`FixedSizeCollection`]
/// is not compromised.
///
/// # Examples
///
/// Example using [`ArrayStack`]:
///
/// ```
/// use trait_based_collection::{prelude::*, ArrayStack};
///
/// let mut stack: ArrayStack<usize> = ArrayStack::with_capacity(10);
/// assert_eq!(stack.capacity(), 10);
/// stack.expand(10);
/// assert!(stack.capacity() >= 20);
/// ```
///
/// [`mode`]: FixedSizeCollection::mode
/// [`Expand`]: ExpansionMode::Expand
/// [`ArrayStack`]: crate::stack::ArrayStack
fn expand(&mut self, extra_size: usize);
/// Checks if the number of items in the [`FixedSizeCollection`] is equal to the capacity.
///
/// # Examples
///
/// Example using [`CircularDeque`]:
///
/// ```
/// use trait_based_collection::{prelude::*, CircularDeque};
///
/// let mut queue = CircularDeque::with_capacity(10);
/// assert!(!queue.is_full());
///
/// for i in 0..10 {
/// queue.add(i);
/// }
/// assert!(queue.is_full());
/// ```
///
/// [`CircularDeque`]: crate::queue::CircularDeque
fn is_full(&self) -> bool {
self.len() == self.capacity()
}
/// Returns the [`ExpansionMode`] of the collection . This is used to determine how the
/// collection will behave when it is full.
///
/// The possibility of modifying the [`ExpansionMode`] will be determined by the implementation.
/// The built-in implementations of [`FixedSizeCollection`] will allow modifications through
/// a public attribute.
///
/// # Examples
///
/// Example using [`ArrayStack`] with default constructor:
///
/// ```
/// use trait_based_collection::{prelude::*, ArrayStack};
///
/// let mut stack: ArrayStack<u8> = ArrayStack::default();
/// assert_eq!(stack.mode(), &ExpansionMode::default());
/// ```
///
/// Another example but using the [`with_capacity`] constructor:
///
/// ```
/// use trait_based_collection::{prelude::*, ArrayStack};
///
/// let mut stack: ArrayStack<u8> = ArrayStack::with_capacity(10);
/// assert_eq!(stack.mode(), &ExpansionMode::Panic);
/// ```
///
/// [`ArrayStack`]: crate::stack::ArrayStack
/// [`with_capacity`]: crate::stack::ArrayStack::with_capacity
fn mode(&self) -> &ExpansionMode;
}
/// Checks if the collection is full and if it is, expands the collection depending on the
/// [`ExpansionMode`]. Also, returns `true` if the [`add`] method should finish execution.
///
/// This method should be called before adding an item to the collection . If the collection is
/// full, the method will do:
/// - [`Panic`]: The method will panic.
/// - [`Ignore`]: The item will not be added to the collection and will npt
/// - [`Overwrite`]: An item will be removed from the collection and the new item will be added.
/// In this case, the [`remove`] method will be called.
/// - [`Expand`]: The collection will be expanded and the item will be added. In this case, the
/// [`expand`] method will be called.
///
/// Instead of using this method inside the [`add`] method, it is recommended to use the
/// [`check_expansion_add`] macro.
///
/// # Panics
///
/// This method will panic if [`mode`] is [`Panic`] and the collection is full.
///
/// # Examples
///
/// Example without using [`check_expansion_add`] macro on an [`add`] method:
///
/// ```
/// # use trait_based_collection::{prelude::*, macros::All};
/// use trait_based_collection::macros::check_expansion_add;
/// #
/// # #[derive(All)]
/// # struct MyCollection<T> {
/// # data: Vec<T>,
/// # mode: ExpansionMode,
/// # }
/// #
/// # impl<'a, T: 'a> Iterators<'a, T> for MyCollection<T> {
/// # type ItemRef = &'a T;
/// # type ItemMut = &'a mut T;
/// #
/// # type Iter = std::slice::Iter<'a, T>;
/// # type IterMut = std::slice::IterMut<'a, T>;
/// #
/// # fn iter(&'a self) -> Self::Iter {
/// # self.data.iter()
/// # }
/// #
/// # fn iter_mut(&'a mut self) -> Self::IterMut {
/// # self.data.iter_mut()
/// # }
/// # }
/// #
/// # impl<'a, T: 'a> Collection<'a, T> for MyCollection<T> {
/// # fn new_default() -> Self where Self: Sized {
/// # MyCollection::with_mode(16, ExpansionMode::default())
/// # }
/// #
/// # fn with_capacity(capacity: usize) -> Self {
/// # MyCollection::with_mode(capacity, ExpansionMode::Panic)
/// # }
/// #
/// # fn with_approximate_capacity(approx_capacity: usize) -> Self{
/// # MyCollection::with_mode(approx_capacity, ExpansionMode::default())
/// # }
/// #
/// #[check_expansion_add]
/// fn add(&mut self, value: T) {
/// self.data.push(value);
/// }
/// #
/// # fn remove(&mut self) -> Option<T> {
/// # self.data.pop()
/// # }
/// #
/// # fn len(&self) -> usize {
/// # self.data.len()
/// # }
/// # }
/// #
/// # impl<'a, T: 'a> FixedSizeCollection<'a, T> for MyCollection<T> {
/// # fn with_mode(capacity: usize, mode: ExpansionMode) -> Self {
/// # assert_ne!(capacity, 0, "Capacity must be greater than 0");
/// # MyCollection {
/// # data: Vec::with_capacity(capacity),
/// # mode,
/// # }
/// # }
/// #
/// # fn capacity(&self) -> usize {
/// # self.data.capacity()
/// # }
/// #
/// # fn expand(&mut self, extra_size: usize) {
/// # self.data.reserve(extra_size);
/// # }
/// #
/// # fn mode(&self) -> &ExpansionMode {
/// # &self.mode
/// # }
/// # }
/// ```
///
/// Alternative example without using [`check_expansion_add`] macro on an [`add`] method:
///
/// ```
/// # use trait_based_collection::{prelude::*, macros::All};
/// use trait_based_collection::collection::check_expansion;
/// #
/// # #[derive(All)]
/// # struct MyCollection<T> {
/// # data: Vec<T>,
/// # mode: ExpansionMode,
/// # }
/// #
/// # impl<'a, T: 'a> Iterators<'a, T> for MyCollection<T> {
/// # type ItemRef = &'a T;
/// # type ItemMut = &'a mut T;
/// #
/// # type Iter = std::slice::Iter<'a, T>;
/// # type IterMut = std::slice::IterMut<'a, T>;
/// #
/// # fn iter(&'a self) -> Self::Iter {
/// # self.data.iter()
/// # }
/// #
/// # fn iter_mut(&'a mut self) -> Self::IterMut {
/// # self.data.iter_mut()
/// # }
/// # }
/// #
/// # impl<'a, T: 'a> Collection<'a, T> for MyCollection<T> {
/// # fn new_default() -> Self where Self: Sized {
/// # MyCollection::with_mode(16, ExpansionMode::default())
/// # }
/// #
/// # fn with_capacity(capacity: usize) -> Self {
/// # MyCollection::with_mode(capacity, ExpansionMode::Panic)
/// # }
/// #
/// # fn with_approximate_capacity(approx_capacity: usize) -> Self{
/// # MyCollection::with_mode(approx_capacity, ExpansionMode::default())
/// # }
/// #
/// fn add(&mut self, value: T) {
/// if check_expansion(self) {
/// return;
/// }
/// self.data.push(value);
/// }
/// #
/// # fn remove(&mut self) -> Option<T> {
/// # self.data.pop()
/// # }
/// #
/// # fn len(&self) -> usize {
/// # self.data.len()
/// # }
/// # }
/// #
/// # impl<'a, T: 'a> FixedSizeCollection<'a, T> for MyCollection<T> {
/// # fn with_mode(capacity: usize, mode: ExpansionMode) -> Self {
/// # assert_ne!(capacity, 0, "Capacity must be greater than 0");
/// # MyCollection {
/// # data: Vec::with_capacity(capacity),
/// # mode,
/// # }
/// # }
/// #
/// # fn capacity(&self) -> usize {
/// # self.data.capacity()
/// # }
/// #
/// # fn expand(&mut self, extra_size: usize) {
/// # self.data.reserve(extra_size);
/// # }
/// #
/// # fn mode(&self) -> &ExpansionMode {
/// # &self.mode
/// # }
/// # }
/// ```
///
/// [`add`]: Collection::add
/// [`Panic`]: ExpansionMode::Panic
/// [`Ignore`]: ExpansionMode::Ignore
/// [`Overwrite`]: ExpansionMode::Overwrite
/// [`remove`]: Collection::remove
/// [`Expand`]: ExpansionMode::Expand
/// [`expand`]: FixedSizeCollection::expand
/// [`mode`]: FixedSizeCollection::mode
pub fn check_expansion<'a, T, C: FixedSizeCollection<'a, T>>(collection: &mut C) -> bool {
if collection.is_full() {
match collection.mode() {
ExpansionMode::Panic => {
panic!("The collection is full");
}
ExpansionMode::Ignore => {
return true;
}
ExpansionMode::Overwrite => {
collection.remove(); // Tries to remove an element from the collection
}
ExpansionMode::Expand(factor) => {
assert!(*factor >= 1.0, "Expand factor must be greater than 1");
// We are sure that the cast is safe
#[allow(clippy::cast_precision_loss)]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
let size = ((*factor - 1.0) * collection.capacity() as f64).floor() as usize;
collection.expand(size);
}
}
}
false
}
#[cfg(test)]
mod test_fixed_size_collection {
use super::*;
use crate::{ArrayStack, CircularDeque};
use trait_based_collection_macros::test_collection;
#[test_collection(ArrayStack, CircularDeque)]
fn test_capacity<C: FixedSizeCollection<i32>>(_collection: C) {
let mut collection = C::with_capacity(10);
assert_eq!(collection.capacity(), 10);
for i in 0..10 {
collection.add(i);
}
assert_eq!(collection.capacity(), 10);
let mut collection: C<i32> = C::with_capacity(100);
assert_eq!(collection.capacity(), 100);
for i in 0..100 {
collection.add(i);
}
assert_eq!(collection.capacity(), 100);
let collection: C<i32> = C::new_default();
assert_eq!(collection.capacity(), 16);
}
#[test_collection(ArrayStack, CircularDeque)]
fn test_expand<C: FixedSizeCollection<i32>>(_collection: C) {
let mut collection = C::with_capacity(13);
assert_eq!(collection.capacity(), 13);
// Expand with empty collection
collection.expand(11);
assert!(collection.capacity() >= 24);
// Simulates random usage of collection
for _ in 0..7 {
for i in 0..5 {
collection.add(i);
}
for _ in 0..5 {
collection.remove().unwrap();
}
}
// Fill half-collection
for i in 0..12 {
collection.add(i);
}
assert_eq!(collection.len(), 12);
assert!(collection.capacity() >= 24);
// Expand with half-collection
collection.expand(3);
assert!(collection.capacity() >= 27);
}
#[test_collection(ArrayStack, CircularDeque)]
fn test_is_full<C: FixedSizeCollection<i32>>(_collection: C) {
let mut collection = C::with_mode(10, ExpansionMode::Expand(2.0));
assert!(!collection.is_full());
for i in 0..10 {
collection.add(i);
}
assert!(collection.is_full());
// Expands the collection
collection.add(10);
assert!(!collection.is_full());
}
#[test_collection(ArrayStack, CircularDeque)]
fn test_mode<C: FixedSizeCollection<i32>>(_collection: C) {
let collection: C<i32> = C::with_mode(10, ExpansionMode::Expand(2.0));
assert_eq!(collection.mode(), &ExpansionMode::Expand(2.0));
let collection: C<i32> = C::with_mode(10, ExpansionMode::Overwrite);
assert_eq!(collection.mode(), &ExpansionMode::Overwrite);
let collection: C<i32> = C::with_mode(10, ExpansionMode::Ignore);
assert_eq!(collection.mode(), &ExpansionMode::Ignore);
let collection: C<i32> = C::with_mode(10, ExpansionMode::Panic);
assert_eq!(collection.mode(), &ExpansionMode::Panic);
}
}
#[cfg(test)]
mod test_modes {
use super::*;
use crate::{ArrayStack, CircularDeque};
use trait_based_collection_macros::test_collection;
#[test_collection(ArrayStack, CircularDeque; should_panic)]
fn test_mode_panic<C: FixedSizeCollection<i32>>(_collection: C) {
let mut collection = C::with_mode(5, ExpansionMode::Panic);
for i in 0..5 {
collection.add(i);
}
// Chis should panic
collection.add(5);
}
#[test_collection(ArrayStack, CircularDeque)]
fn test_mode_ignore<C: FixedSizeCollection<i32> + IntoIterator<Item = i32>>(_collection: C) {
let mut collection = C::with_mode(5, ExpansionMode::Ignore);
for i in 0..5 {
collection.add(i);
}
for i in 10..15 {
collection.add(i);
assert_eq!(collection.len(), 5);
assert_eq!(collection.capacity(), 5);
}
// Only numbers of the first loop should be in the collection
let mut collection_vec = collection.into_iter().collect::<Vec<_>>();
collection_vec.sort_unstable();
assert_eq!(collection_vec, vec![0, 1, 2, 3, 4]);
}
#[test_collection(ArrayStack, CircularDeque)]
fn test_mode_overwrite<C: FixedSizeCollection<i32> + IntoIterator<Item = i32>>(_collection: C) {
let mut collection = C::with_mode(5, ExpansionMode::Overwrite);
for i in 0..5 {
collection.add(i);
}
for i in 10..15 {
collection.add(i);
assert_eq!(collection.len(), 5);
assert_eq!(collection.capacity(), 5);
}
// At least one of the numbers of the should be changed in the second loop
let mut collection_vec = collection.into_iter().collect::<Vec<_>>();
collection_vec.sort_unstable();
assert_ne!(collection_vec, vec![0, 1, 2, 3, 4]);
}
#[test_collection(ArrayStack, CircularDeque)]
fn test_mode_expand<C: FixedSizeCollection<i32> + IntoIterator<Item = i32>>(_collection: C) {
let mut collection = C::with_mode(5, ExpansionMode::Expand(2.0));
for i in 0..5 {
collection.add(i);
}
for i in 10..15 {
collection.add(i);
assert_ne!(collection.len(), 5);
assert_ne!(collection.capacity(), 5);
}
assert!(collection.capacity() >= 10);
// All numbers should be in the collection
let mut collection_vec = collection.into_iter().collect::<Vec<_>>();
collection_vec.sort_unstable();
assert_eq!(collection_vec, vec![0, 1, 2, 3, 4, 10, 11, 12, 13, 14]);
}
}