From e4319de3d526285381f5cc53e14f9a17d123a81a Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 3 Apr 2021 20:30:34 +0300 Subject: const generics --- src/linked_list.rs | 57 ++++++++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) (limited to 'src/linked_list.rs') diff --git a/src/linked_list.rs b/src/linked_list.rs index 9ea4d19..bbb935f 100644 --- a/src/linked_list.rs +++ b/src/linked_list.rs @@ -3,8 +3,6 @@ use core::marker::PhantomData; use core::mem::MaybeUninit; use core::ops::{Deref, DerefMut}; use core::ptr; -pub use generic_array::ArrayLength; -use generic_array::GenericArray; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] struct LinkedIndex(u16); @@ -37,21 +35,19 @@ pub struct Node { } /// Iterator for the linked list. -pub struct Iter<'a, T, Kind, N> +pub struct Iter<'a, T, Kind, const N: usize> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { list: &'a LinkedList, index: LinkedIndex, } -impl<'a, T, Kind, N> Iterator for Iter<'a, T, Kind, N> +impl<'a, T, Kind, const N: usize> Iterator for Iter<'a, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { type Item = &'a T; @@ -66,11 +62,10 @@ where } /// Comes from [`LinkedList::find_mut`]. -pub struct FindMut<'a, T, Kind, N> +pub struct FindMut<'a, T, Kind, const N: usize> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { list: &'a mut LinkedList, is_head: bool, @@ -79,11 +74,10 @@ where maybe_changed: bool, } -impl<'a, T, Kind, N> FindMut<'a, T, Kind, N> +impl<'a, T, Kind, const N: usize> FindMut<'a, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { fn pop_internal(&mut self) -> T { if self.is_head { @@ -122,11 +116,10 @@ where } } -impl Drop for FindMut<'_, T, Kind, N> +impl Drop for FindMut<'_, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { fn drop(&mut self) { // Only resort the list if the element has changed @@ -137,11 +130,10 @@ where } } -impl Deref for FindMut<'_, T, Kind, N> +impl Deref for FindMut<'_, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { type Target = T; @@ -150,11 +142,10 @@ where } } -impl DerefMut for FindMut<'_, T, Kind, N> +impl DerefMut for FindMut<'_, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { fn deref_mut(&mut self) -> &mut Self::Target { self.maybe_changed = true; @@ -162,11 +153,10 @@ where } } -impl fmt::Debug for FindMut<'_, T, Kind, N> +impl fmt::Debug for FindMut<'_, T, Kind, N> where T: PartialEq + PartialOrd + core::fmt::Debug, Kind: kind::Kind, - N: ArrayLength>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("FindMut") @@ -189,23 +179,21 @@ where } /// The linked list. -pub struct LinkedList +pub struct LinkedList where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { - list: MaybeUninit, N>>, + list: MaybeUninit<[Node; N]>, head: LinkedIndex, free: LinkedIndex, _kind: PhantomData, } -impl LinkedList +impl LinkedList where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { /// Internal helper to not do pointer arithmetic all over the place. #[inline] @@ -266,7 +254,7 @@ where _kind: PhantomData, }; - let len = N::U16; + let len = N as u16; let mut free = 0; if len == 0 { @@ -451,11 +439,10 @@ where } } -impl Drop for LinkedList +impl Drop for LinkedList where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { fn drop(&mut self) { let mut index = self.head; @@ -471,11 +458,10 @@ where } } -impl fmt::Debug for LinkedList +impl fmt::Debug for LinkedList where T: PartialEq + PartialOrd + core::fmt::Debug, Kind: kind::Kind, - N: ArrayLength>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() @@ -518,11 +504,10 @@ pub mod kind { mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; - use generic_array::typenum::consts::*; #[test] fn test_peek() { - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(1).unwrap(); assert_eq!(ll.peek().unwrap(), &1); @@ -533,7 +518,7 @@ mod tests { ll.push(3).unwrap(); assert_eq!(ll.peek().unwrap(), &3); - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(2).unwrap(); assert_eq!(ll.peek().unwrap(), &2); @@ -547,7 +532,7 @@ mod tests { #[test] fn test_full() { - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); @@ -557,14 +542,14 @@ mod tests { #[test] fn test_empty() { - let ll: LinkedList = LinkedList::new(); + let ll: LinkedList = LinkedList::new(); assert!(ll.is_empty()) } #[test] fn test_zero_size() { - let ll: LinkedList = LinkedList::new(); + let ll: LinkedList = LinkedList::new(); assert!(ll.is_empty()); assert!(ll.is_full()); @@ -572,7 +557,7 @@ mod tests { #[test] fn test_rejected_push() { - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); @@ -585,7 +570,7 @@ mod tests { #[test] fn test_updating() { - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); -- cgit v1.2.3