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
// The MIT License (MIT) // // Copyright (c) 2015-2016 Nick Stevens <nick@bitcurry.com> // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // // Portions of this work are based on the Rust standard library implementation // of Vec: https://github.com/rust-lang/rust/blob/master/src/libcollections/vec.rs #![crate_type = "lib"] #![crate_name = "fixedvec"] #![no_std] //! Heapless Vec implementation using only libcore //! //! When developing for certain types of systems, especially embedded systems, //! it is desirable to avoid the non-determinism that can be introduced by //! using a heap. A commonly used data structure is a "buffer" - a //! preallocated chunk of memory, either in static memory or on the stack. //! //! Thanks to the extensibility of Rust, it is possible to have a datatype //! that performs _almost_ like the libstd `Vec` type, without requiring a //! heap and while only using libcore. //! //! # Differences from `std::vec::Vec` //! //! For now, `FixedVec` only works for types that implement `Copy`. This //! requirement will be lifted in the future, but for now it is the most //! straightforward way to get to a minimum viable product. //! //! Although every effort has been made to mimic the functionality of `Vec`, //! this is not a perfect clone. Specifically, functions that require memory //! allocation are not included. There are also a few functions where the type //! signatures are different - usually to add a `Result` that indicates whether //! or not adding an element was successful. //! //! Note that the `Vec` functionality of panicking when an invalid index is //! accessed has been preserved. Note that this is true _even if the index is //! valid for the underlying memory_. So, for example, if a `FixedVec` were //! allocated with 10 elements, and 3 new elements were pushed to it, accessing //! index 5 would panic, even though accessing that memory would be safe. //! //! ## Functions with different signatures //! //! The following functions have different signatures than their equivalents in //! `Vec`. //! //! * `new`: Self-explanatory - instantiating a different object //! * `push`, `push_all`, `insert`: Functions that add elements return a Result //! indicating if the result was successful. //! * `map_in_place`: Similar to `Vec` `map_in_place`, except there is no //! coercion of the types. //! //! ## Functions in `FixedVec` not in `Vec` //! //! * `available`: Convenience function for checking remaining space. //! * `iter`: `FixedVec` cannot implement `IntoIterator` because the type //! signature of that trait requires taking ownership of the underlying //! struct. Since `FixedVec` keeps a reference to its backing store, //! ownership is not its to give. It's possible I'm just being dense and this //! is possible - I'd love to be proven wrong. //! //! ## Functions in `Vec` excluded from `FixedVec` //! //! The following `Vec` functions do not exist in `FixedVec` because they deal //! with allocating or reserving memory - a step that is done up-front in //! `FixedVec`. //! //! * `with_capacity` //! * `from_raw_parts` //! * `from_raw_buffer` //! * `reserve` //! * `reserve_exact` //! * `shrink_to_fit` //! * `into_boxed_slice` //! * `truncate` //! * `set_len` //! * `append` //! * `drain` //! * `split_off` //! //! # Example //! //! Typical usage looks like the following: //! //! ``` //! // Pull in fixedvec //! #[macro_use] //! extern crate fixedvec; //! //! use fixedvec::FixedVec; //! //! fn main() { //! let mut preallocated_space = alloc_stack!([u8; 10]); //! let mut vec = FixedVec::new(&mut preallocated_space); //! assert_eq!(vec.len(), 0); //! //! vec.push_all(&[1, 2, 3]).unwrap(); //! assert_eq!(vec.len(), 3); //! assert_eq!(vec[1], 2); //! //! vec[1] = 5; //! assert_eq!(vec[1], 5); //! } //! ``` //! //! If you're building for an embedded system, you will want to refer to the //! Rust book section ["No stdlib"](https://doc.rust-lang.org/book/no-stdlib.html) //! for instructions on building executables using only libcore. use core::hash::{Hash, Hasher}; use core::ops; #[cfg(test)] #[macro_use] extern crate std; /// Convenience macro for use with `FixedVec`. Allocates the specified number /// of elements of specified type on the stack. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// // Allocate space for 16 u8's /// let mut space = alloc_stack!([u8; 16]); /// /// // Give the space to a `FixedVec`, which manages it from here on out /// let vec = FixedVec::new(&mut space); /// # } /// ``` #[macro_export] macro_rules! alloc_stack { ([$item_type:ty; $len:expr]) => {{ let space: [$item_type; $len] = [Default::default(); $len]; space }}; } pub type Result<T> = core::result::Result<T, ErrorKind>; #[derive(Debug)] pub enum ErrorKind { NoSpace, } #[derive(Debug)] pub struct FixedVec<'a, T: 'a + Copy> { memory: &'a mut [T], len: usize, } pub use core::slice::Iter; pub use core::slice::IterMut; impl<'a, T> FixedVec<'a, T> where T: 'a + Copy, { /// Create a new `FixedVec` from the provided slice, in the process taking /// ownership of the slice. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let vec = FixedVec::new(&mut space); /// assert_eq!(vec.capacity(), 16); /// assert_eq!(vec.len(), 0); /// assert_eq!(&[] as &[u8], vec.as_slice()); /// # } /// ``` /// pub fn new(memory: &'a mut [T]) -> Self { FixedVec { memory: memory, len: 0, } } /// Returns the capacity of the vector. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let mut vec = FixedVec::new(&mut space); /// assert_eq!(vec.capacity(), 16); /// vec.push(1).unwrap(); /// assert_eq!(vec.capacity(), 16); /// # } /// ``` #[inline] pub fn capacity(&self) -> usize { self.memory.len() } /// Returns the number of elements in the vector. This will always be /// less than or equal to the `capacity()`. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let mut vec = FixedVec::new(&mut space); /// vec.push(1).unwrap(); /// vec.push(2).unwrap(); /// assert_eq!(vec.len(), 2); /// # } /// ``` #[inline] pub fn len(&self) -> usize { self.len } /// Returns the number of available elements in the vector. Adding more /// than this number of elements (without removing some elements) will /// cause further calls to element-adding functions to fail. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let mut vec = FixedVec::new(&mut space); /// assert_eq!(vec.available(), 16); /// vec.push(1).unwrap(); /// assert_eq!(vec.available(), 15); /// assert_eq!(vec.available(), vec.capacity() - vec.len()); /// # } /// ``` #[inline] pub fn available(&self) -> usize { self.capacity() - self.len() } /// Returns `true` if the vector contains no elements. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let mut vec = FixedVec::new(&mut space); /// assert!(vec.is_empty()); /// vec.push(1); /// assert!(!vec.is_empty()); /// # } #[inline] pub fn is_empty(&self) -> bool { self.len == 0 } /// Extracts a slice containing the entire vector. /// /// Equivalent to `&s[..]`. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push_all(&[1, 2, 3, 4]).unwrap(); /// assert_eq!(vec.as_slice(), &[1, 2, 3, 4]); /// # } #[inline] pub fn as_slice(&self) -> &[T] { &self.memory[..self.len] } /// Extracts a mutable slice of the entire vector. /// /// Equivalent to `&mut s[..]`. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push(1).unwrap(); /// let mut slice = vec.as_mut_slice(); /// slice[0] = 2; /// assert_eq!(slice[0], 2); /// # } #[inline] pub fn as_mut_slice(&mut self) -> &mut [T] { &mut self.memory[..self.len] } /// Inserts an element at position `index` within the vector, shifting all /// elements after position `i` one position to the right. /// /// # Panics /// /// Panics if `index` is greater than the vector's length. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 5]); /// let mut vec = FixedVec::new(&mut space); /// /// // Inserting in the middle moves elements to the right /// vec.push_all(&[1, 2, 3]).unwrap(); /// vec.insert(1, 15).unwrap(); /// assert_eq!(vec.as_slice(), &[1, 15, 2, 3]); /// /// // Can also insert at the end of the vector /// vec.insert(4, 16).unwrap(); /// assert_eq!(vec.as_slice(), &[1, 15, 2, 3, 16]); /// /// // Cannot insert if there is not enough capacity /// assert!(vec.insert(2, 17).is_err()); /// # } pub fn insert(&mut self, index: usize, element: T) -> Result<()> { assert!(index <= self.len); if index == self.len || self.len == 0 { self.push(element) } else if self.available() >= 1 { self.len += 1; let mut i = self.len; loop { if i == index { break; } self.memory[i] = self.memory[i - 1]; i -= 1; } self.memory[index] = element; Ok(()) } else { Err(ErrorKind::NoSpace) } } /// Removes and returns the element at position `index` within the vector, /// shifting all elements after position `index` one position to the left. /// /// # Panics /// /// Panics if `index` is out of bounds. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let mut vec = FixedVec::new(&mut space); /// /// // Remove element from the middle /// vec.push_all(&[1, 2, 3]).unwrap(); /// assert_eq!(vec.remove(1), 2); /// assert_eq!(vec.as_slice(), &[1, 3]); /// /// // Remove element from the end /// assert_eq!(vec.remove(1), 3); /// assert_eq!(vec.as_slice(), &[1]); /// # } pub fn remove(&mut self, index: usize) -> T { assert!(index < self.len); let ret = self.memory[index]; self.len -= 1; for i in index..self.len { self.memory[i] = self.memory[i + 1]; } ret } /// Appends an element to the back of the vector. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 3]); /// let mut vec = FixedVec::new(&mut space); /// /// // Pushing appends to the end of the vector /// vec.push(1).unwrap(); /// vec.push(2).unwrap(); /// vec.push(3).unwrap(); /// assert_eq!(vec.as_slice(), &[1, 2, 3]); /// /// // Attempting to push a full vector results in an error /// assert!(vec.push(4).is_err()); /// # } /// ``` #[inline] pub fn push(&mut self, value: T) -> Result<()> { if self.available() >= 1 { self.memory[self.len] = value; self.len += 1; Ok(()) } else { Err(ErrorKind::NoSpace) } } /// Removes the last element from the vector and returns it, or `None` if /// the vector is empty /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 16]); /// let mut vec = FixedVec::new(&mut space); /// vec.push_all(&[1, 2]).unwrap(); /// assert_eq!(vec.pop(), Some(2)); /// assert_eq!(vec.pop(), Some(1)); /// assert_eq!(vec.pop(), None); /// # } /// ``` #[inline] pub fn pop(&mut self) -> Option<T> { if self.len > 0 { self.len -= 1; Some(self.memory[self.len]) } else { None } } /// Copies all elements from slice `other` to this vector. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 5]); /// let mut vec = FixedVec::new(&mut space); /// /// // All elements are pushed to vector /// vec.push_all(&[1, 2, 3, 4]).unwrap(); /// assert_eq!(vec.as_slice(), &[1, 2, 3, 4]); /// /// // If there is insufficient space, NO values are pushed /// assert!(vec.push_all(&[5, 6, 7]).is_err()); /// assert_eq!(vec.as_slice(), &[1, 2, 3, 4]); /// # } /// ``` #[inline] pub fn push_all(&mut self, other: &[T]) -> Result<()> { if other.len() > self.available() { Err(ErrorKind::NoSpace) } else { for item in other.iter() { self.memory[self.len] = *item; self.len += 1; } Ok(()) } } /// Clears the vector, removing all values. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// vec.push_all(&[1, 2, 3]).unwrap(); /// assert_eq!(vec.len(), 3); /// vec.clear(); /// assert_eq!(vec.len(), 0); /// # } /// ``` pub fn clear(&mut self) { self.len = 0 } /// Applies the function `f` to all elements in the vector, mutating the /// vector in place. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push_all(&[1, 2, 3]).unwrap(); /// vec.map_in_place(|x: &mut u8| { *x *= 2 }); /// assert_eq!(vec.as_slice(), &[2, 4, 6]); /// # } /// ``` pub fn map_in_place<F>(&mut self, f: F) where F: Fn(&mut T), { for i in 0..self.len { f(&mut self.memory[i]); } } /// Provides a forward iterator. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// vec.push_all(&[1, 2, 3]).unwrap(); /// { /// let mut iter = vec.iter(); /// assert_eq!(iter.next(), Some(&1)); /// assert_eq!(iter.next(), Some(&2)); /// assert_eq!(iter.next(), Some(&3)); /// assert_eq!(iter.next(), None); /// } /// # } /// ``` #[inline] pub fn iter(&self) -> Iter<T> { let (slice, _) = self.memory.split_at(self.len); slice.iter() } /// Provides a mutable forward iterator. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// vec.push_all(&[1, 2, 3]).unwrap(); /// { /// let mut iter = vec.iter_mut(); /// let mut x = iter.next().unwrap(); /// *x = 5; /// } /// assert_eq!(vec.as_slice(), &[5, 2, 3]); /// # } /// ``` #[inline] pub fn iter_mut(&mut self) -> IterMut<T> { let (slice, _) = self.memory.split_at_mut(self.len); slice.iter_mut() } /// Removes an element from anywhere in the vector and returns it, /// replacing it with the last element. /// /// This does not preserve ordering, but is O(1) /// /// # Panics /// /// Panics if `index` is out of bounds /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push_all(&[0, 1, 2, 3]).unwrap(); /// assert_eq!(vec.swap_remove(1), 1); /// assert_eq!(vec.as_slice(), &[0, 3, 2]); /// # } /// ``` pub fn swap_remove(&mut self, index: usize) -> T { assert!(index < self.len); if self.len == 1 { self.remove(0) } else { let removed = self.memory[index]; self.memory[index] = self.pop().unwrap(); removed } } /// Resizes the vector in-place so that `len()` is equal to `new_len`. /// /// New elements (if needed) are cloned from `value`. /// /// # Panics /// /// Panics if `new_len` is greater than capacity /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// /// assert_eq!(vec.len(), 0); /// vec.resize(5, 255); /// assert_eq!(vec.as_slice(), &[255, 255, 255, 255, 255]); /// vec.resize(2, 0); /// assert_eq!(vec.as_slice(), &[255, 255]); /// # } /// ``` pub fn resize(&mut self, new_len: usize, value: T) { assert!(new_len <= self.capacity()); if new_len <= self.len { self.len = new_len; } else { for i in self.memory[self.len..new_len].iter_mut() { *i = Clone::clone(&value); } self.len = new_len; } } /// Retains only the elements specified by the predicate. /// /// In other words, remove all elements `e` such that `f(&e)` returns /// false. This method operates in-place, in O(N) time, and preserves the /// order of the retained elements. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push_all(&[1, 2, 3, 4]).unwrap(); /// vec.retain(|&x| x%2 == 0); /// assert_eq!(vec.as_slice(), &[2, 4]); /// # } /// ``` pub fn retain<F>(&mut self, f: F) where F: Fn(&T) -> bool, { let mut head: usize = 0; let mut tail: usize = 0; loop { if head >= self.len { break; } if f(&self.memory[head]) { self.memory[tail] = self.memory[head]; tail += 1; } head += 1; } self.len = tail; } /// Returns a reference to the element at the given index, or `None` if the /// index is out of bounds. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push_all(&[10, 40, 30]).unwrap(); /// assert_eq!(Some(&40), vec.get(1)); /// assert_eq!(None, vec.get(3)); /// # } /// ``` pub fn get(&self, index: usize) -> Option<&T> { self.as_slice().get(index) } /// Returns a mutable reference to the element at the given index, or /// `None` if the index is out of bounds. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push_all(&[10, 40, 30]).unwrap(); /// { /// let x = vec.get_mut(1).unwrap(); /// *x = 50; /// } /// assert_eq!(Some(&50), vec.get(1)); /// assert_eq!(None, vec.get(3)); /// # } /// ``` pub fn get_mut(&mut self, index: usize) -> Option<&mut T> { self.as_mut_slice().get_mut(index) } /// Returns a reference to the element at the given index, without doing /// bounds checking. Note that the result of an invalid index is undefined, /// and may not panic. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push_all(&[10, 40, 30]).unwrap(); /// assert_eq!(&40, unsafe { vec.get_unchecked(1) }); /// /// // Index beyond bounds is undefined /// //assert_eq!(None, vec.get(3)); /// # } /// ``` pub unsafe fn get_unchecked(&self, index: usize) -> &T { self.as_slice().get_unchecked(index) } /// Returns a mutable reference to the element at the given index, without /// doing bounds checking. Note that the result of an invalid index is /// undefined, and may not panic. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// /// vec.push_all(&[10, 40, 30]).unwrap(); /// { /// let mut x = unsafe { vec.get_unchecked_mut(1) }; /// *x = 50; /// } /// assert_eq!(Some(&50), vec.get(1)); /// /// // Index beyond bounds is undefined /// //assert_eq!(None, vec.get(3)); /// # } /// ``` pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T { self.as_mut_slice().get_unchecked_mut(index) } } impl<'a, T> FixedVec<'a, T> where T: 'a + Copy + PartialEq<T>, { /// Removes consecutive repeated elements in the vector in O(N) time. /// /// If the vector is sorted, this removes all duplicates. /// /// # Example /// /// ``` /// # #[macro_use] extern crate fixedvec; /// # use fixedvec::FixedVec; /// # fn main() { /// let mut space = alloc_stack!([u8; 10]); /// let mut vec = FixedVec::new(&mut space); /// vec.push_all(&[1, 2, 2, 3, 2]).unwrap(); /// vec.dedup(); /// assert_eq!(vec.as_slice(), &[1, 2, 3, 2]); /// # } /// ``` pub fn dedup(&mut self) { if self.len <= 1 { return; } let mut head: usize = 1; let mut tail: usize = 0; loop { if head >= self.len { break; } if self.memory[head] != self.memory[tail] { tail += 1; self.memory[tail] = self.memory[head]; } head += 1; } self.len = tail + 1; } } impl<'a, T: Copy> IntoIterator for &'a FixedVec<'a, T> { type Item = &'a T; type IntoIter = Iter<'a, T>; fn into_iter(self) -> Iter<'a, T> { self.iter() } } impl<'a, T: Copy> IntoIterator for &'a mut FixedVec<'a, T> { type Item = &'a mut T; type IntoIter = IterMut<'a, T>; fn into_iter(self) -> IterMut<'a, T> { self.iter_mut() } } impl<'a, T> Hash for FixedVec<'a, T> where T: Copy + Hash, { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { Hash::hash(&*self.memory, state) } } impl<'a, T> Extend<T> for FixedVec<'a, T> where T: Copy, { fn extend<I: IntoIterator<Item = T>>(&mut self, iterable: I) { if self.available() == 0 { return; } for n in iterable { self.memory[self.len] = n; self.len += 1; if self.available() == 0 { break; } } } } impl<'a, T> ops::Index<usize> for FixedVec<'a, T> where T: Copy, { type Output = T; #[inline] fn index(&self, index: usize) -> &T { &(self.memory)[index] } } impl<'a, T> ops::IndexMut<usize> for FixedVec<'a, T> where T: Copy, { #[inline] fn index_mut(&mut self, index: usize) -> &mut T { &mut (self.memory)[index] } } impl<'a, T> PartialEq for FixedVec<'a, T> where T: Copy + PartialEq, { fn eq(&self, other: &FixedVec<'a, T>) -> bool { if self.len() != other.len() { return false; } (0..self.len()).all(|i| self[i] == other[i]) } } impl<'a, T> Eq for FixedVec<'a, T> where T: Copy + Eq {} #[cfg(test)] mod test { use super::FixedVec; use std::collections::hash_map::DefaultHasher; use std::hash::Hash; use std::prelude::v1::*; #[test] fn test_empty_array() { let mut empty = alloc_stack!([u8; 0]); let mut vec = FixedVec::new(&mut empty); assert!(vec.is_empty()); assert_eq!(vec.capacity(), 0); assert_eq!(vec.len(), 0); assert_eq!(vec.available(), 0); assert_eq!(vec.as_slice(), &[] as &[u8]); assert!(vec.push(0).is_err()); assert!(vec.push_all(&[] as &[u8]).is_ok()); assert!(vec.push_all(&[1]).is_err()); vec.clear(); vec.map_in_place(|x: &mut u8| *x = 1); vec.retain(|_: &u8| true); vec.dedup(); { let mut iter = vec.iter(); assert!(iter.next().is_none()); } } #[test] #[should_panic] fn test_insert_bad_index() { let mut space = alloc_stack!([u8; 10]); let mut vec = FixedVec::new(&mut space); vec.insert(3, 0).unwrap(); } #[test] #[should_panic] fn test_remove_bad_index() { let mut space = alloc_stack!([u8; 10]); let mut vec = FixedVec::new(&mut space); vec.push_all(&[1, 2, 3, 4, 5]).unwrap(); vec.remove(8); } #[test] #[should_panic] fn test_resize_bad_len() { let mut space = alloc_stack!([u8; 10]); let mut vec = FixedVec::new(&mut space); vec.resize(15, 0); } #[test] #[should_panic] fn test_swap_remove_bad_index() { let mut space = alloc_stack!([u8; 10]); let mut vec = FixedVec::new(&mut space); vec.push_all(&[1, 2, 3, 4, 5]).unwrap(); vec.swap_remove(8); } #[test] fn test_iterator() { let mut space = alloc_stack!([u8; 10]); let mut vec = FixedVec::new(&mut space); vec.push_all(&[1, 2, 3, 4, 5]).unwrap(); let result: Vec<u8> = vec.iter().map(|&x| x).collect(); assert_eq!(vec.as_slice(), &result[..]); } #[test] fn test_hash() { // Two vectors with the same contents should have the same hash let mut space1 = alloc_stack!([u8; 10]); let mut vec1 = FixedVec::new(&mut space1); let mut hasher1 = DefaultHasher::new(); vec1.push_all(&[1, 2, 3, 4, 5]).unwrap(); let mut space2 = alloc_stack!([u8; 10]); let mut vec2 = FixedVec::new(&mut space2); let mut hasher2 = DefaultHasher::new(); vec2.push_all(&[1, 2, 3, 4, 5]).unwrap(); assert_eq!(vec1.hash(&mut hasher1), vec2.hash(&mut hasher2)); } #[test] fn test_extend() { let mut space = alloc_stack!([u8; 10]); let mut vec = FixedVec::new(&mut space); vec.extend(0..6); assert_eq!(vec.as_slice(), &[0, 1, 2, 3, 4, 5]); } #[test] fn test_equal() { let mut space1 = alloc_stack!([u8; 10]); let mut vec1 = FixedVec::new(&mut space1); vec1.push_all(&[1, 2, 3, 4, 5]).unwrap(); // Should be equal even if alloc'd space isn't the same let mut space2 = alloc_stack!([u8; 5]); let mut vec2 = FixedVec::new(&mut space2); vec2.push_all(&[1, 2, 3, 4, 5]).unwrap(); assert_eq!(vec1, vec2); } }