Next Generation WASM Microkernel Operating System

fix(arrayvec): implement traits and fix IntoIterator impl

+154 -19
+154 -19
libs/arrayvec/src/lib.rs
··· 1 1 #![cfg_attr(not(test), no_std)] 2 2 // #![no_std] 3 3 4 + use core::borrow::{Borrow, BorrowMut}; 4 5 use core::error::Error; 6 + use core::hash::{Hash, Hasher}; 5 7 use core::mem::MaybeUninit; 6 8 use core::ops::{Bound, Deref, DerefMut, RangeBounds}; 7 9 use core::ptr::NonNull; ··· 31 33 /// 32 34 /// The maximum capacity of the vector is determined by the `CAP` generic parameter, attempting to 33 35 /// insert more elements than `CAP` will always fail. 34 - #[derive(Debug)] 35 36 pub struct ArrayVec<T, const CAP: usize> { 36 37 len: usize, 37 38 data: [MaybeUninit<T>; CAP], ··· 381 382 } 382 383 } 383 384 385 + impl<T, const CAP: usize> fmt::Debug for ArrayVec<T, CAP> 386 + where 387 + T: fmt::Debug, 388 + { 389 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 390 + (**self).fmt(f) 391 + } 392 + } 393 + 384 394 impl<T, const CAP: usize> Clone for ArrayVec<T, CAP> 385 395 where 386 396 T: Clone, ··· 404 414 } 405 415 } 406 416 417 + impl<T, const CAP: usize> Deref for ArrayVec<T, CAP> { 418 + type Target = [T]; 419 + #[inline] 420 + fn deref(&self) -> &Self::Target { 421 + self.as_slice() 422 + } 423 + } 424 + 425 + impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP> { 426 + #[inline] 427 + fn deref_mut(&mut self) -> &mut Self::Target { 428 + self.as_mut_slice() 429 + } 430 + } 431 + 432 + impl<T, const CAP: usize> Hash for ArrayVec<T, CAP> 433 + where 434 + T: Hash, 435 + { 436 + fn hash<H: Hasher>(&self, state: &mut H) { 437 + Hash::hash(&**self, state); 438 + } 439 + } 440 + 441 + impl<T, const CAP: usize> PartialEq for ArrayVec<T, CAP> 442 + where 443 + T: PartialEq, 444 + { 445 + fn eq(&self, other: &Self) -> bool { 446 + **self == **other 447 + } 448 + } 449 + 450 + impl<T, const CAP: usize> PartialEq<[T]> for ArrayVec<T, CAP> 451 + where 452 + T: PartialEq, 453 + { 454 + fn eq(&self, other: &[T]) -> bool { 455 + **self == *other 456 + } 457 + } 458 + 459 + impl<T, const CAP: usize> Eq for ArrayVec<T, CAP> where T: Eq {} 460 + 461 + impl<T, const CAP: usize> Borrow<[T]> for ArrayVec<T, CAP> { 462 + fn borrow(&self) -> &[T] { 463 + self 464 + } 465 + } 466 + 467 + impl<T, const CAP: usize> BorrowMut<[T]> for ArrayVec<T, CAP> { 468 + fn borrow_mut(&mut self) -> &mut [T] { 469 + self 470 + } 471 + } 472 + 473 + impl<T, const CAP: usize> AsRef<[T]> for ArrayVec<T, CAP> { 474 + fn as_ref(&self) -> &[T] { 475 + self 476 + } 477 + } 478 + 479 + impl<T, const CAP: usize> AsMut<[T]> for ArrayVec<T, CAP> { 480 + fn as_mut(&mut self) -> &mut [T] { 481 + self 482 + } 483 + } 484 + 485 + impl<T, const CAP: usize> PartialOrd for ArrayVec<T, CAP> 486 + where 487 + T: PartialOrd, 488 + { 489 + fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { 490 + (**self).partial_cmp(other) 491 + } 492 + 493 + fn lt(&self, other: &Self) -> bool { 494 + (**self).lt(other) 495 + } 496 + 497 + fn le(&self, other: &Self) -> bool { 498 + (**self).le(other) 499 + } 500 + 501 + fn ge(&self, other: &Self) -> bool { 502 + (**self).ge(other) 503 + } 504 + 505 + fn gt(&self, other: &Self) -> bool { 506 + (**self).gt(other) 507 + } 508 + } 509 + 510 + impl<T, const CAP: usize> Ord for ArrayVec<T, CAP> 511 + where 512 + T: Ord, 513 + { 514 + fn cmp(&self, other: &Self) -> cmp::Ordering { 515 + (**self).cmp(other) 516 + } 517 + } 518 + 407 519 /// Create an `ArrayVec` from an iterator. 408 520 /// 409 521 /// ***Panics*** if the number of elements in the iterator exceeds the arrayvec's capacity. ··· 420 532 } 421 533 } 422 534 423 - impl<T, const CAP: usize> Deref for ArrayVec<T, CAP> { 424 - type Target = [T]; 425 - #[inline] 426 - fn deref(&self) -> &Self::Target { 427 - self.as_slice() 428 - } 429 - } 430 - 431 - impl<T, const CAP: usize> DerefMut for ArrayVec<T, CAP> { 432 - #[inline] 433 - fn deref_mut(&mut self) -> &mut Self::Target { 434 - self.as_mut_slice() 435 - } 436 - } 437 - 438 535 impl<'a, T: 'a, const CAP: usize> IntoIterator for &'a ArrayVec<T, CAP> { 439 536 type Item = &'a T; 440 537 type IntoIter = slice::Iter<'a, T>; ··· 478 575 } 479 576 } 480 577 578 + impl<T, const CAP: usize> fmt::Debug for IntoIter<T, CAP> 579 + where 580 + T: fmt::Debug, 581 + { 582 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 583 + f.debug_list().entries(&self.vec[self.index..]).finish() 584 + } 585 + } 586 + 481 587 impl<T, const CAP: usize> Iterator for IntoIter<T, CAP> { 482 588 type Item = T; 483 589 484 590 fn next(&mut self) -> Option<Self::Item> { 485 - if self.vec.is_empty() { 591 + if self.vec.is_empty() || self.index >= self.vec.len { 486 592 None 487 593 } else { 488 594 let elt = mem::replace(&mut self.vec.data[self.index], MaybeUninit::uninit()); ··· 499 605 500 606 impl<T, const CAP: usize> DoubleEndedIterator for IntoIter<T, CAP> { 501 607 fn next_back(&mut self) -> Option<Self::Item> { 502 - if self.vec.is_empty() { 608 + if self.vec.is_empty() || self.index >= self.vec.len { 503 609 None 504 610 } else { 505 611 let elt = mem::replace(&mut self.vec.data[self.vec.len - 1], MaybeUninit::uninit()); 506 - self.index -= 1; 612 + self.vec.len -= 1; 507 613 // Safety: ArrayVec API guarantees properly-initialized items within 0..len 508 614 Some(unsafe { MaybeUninit::assume_init(elt) }) 509 615 } ··· 835 941 assert_eq!(drain.next(), None); 836 942 drop(drain); 837 943 assert_eq!(vec.as_slice(), &[1, 5]); 944 + } 945 + 946 + #[test] 947 + fn into_iter_consumes_all_elements() { 948 + let mut vec: ArrayVec<i32, 10> = ArrayVec::new(); 949 + vec.extend_from_slice(&[1, 2, 3, 4, 5]); 950 + let collected: Vec<_> = vec.into_iter().collect(); 951 + assert_eq!(collected, &[1, 2, 3, 4, 5]); 952 + } 953 + 954 + #[test] 955 + fn into_iter_empty_vec() { 956 + let vec: ArrayVec<i32, 10> = ArrayVec::new(); 957 + let collected: Vec<_> = vec.into_iter().collect(); 958 + assert_eq!(collected.len(), 0); 959 + } 960 + 961 + #[test] 962 + fn into_iter_double_ended() { 963 + let mut vec: ArrayVec<i32, 10> = ArrayVec::new(); 964 + vec.extend_from_slice(&[1, 2, 3, 4, 5]); 965 + let mut iter = vec.into_iter(); 966 + assert_eq!(iter.next(), Some(1)); 967 + assert_eq!(iter.next_back(), Some(5)); 968 + assert_eq!(iter.next(), Some(2)); 969 + assert_eq!(iter.next_back(), Some(4)); 970 + assert_eq!(iter.next(), Some(3)); 971 + assert_eq!(iter.next(), None); 972 + assert_eq!(iter.next_back(), None); 838 973 } 839 974 840 975 #[test]