diff --git a/src/kernel/synch.rs b/src/kernel/synch.rs index 6ce265b..2ba4e40 100644 --- a/src/kernel/synch.rs +++ b/src/kernel/synch.rs @@ -26,7 +26,7 @@ impl Semaphore { /// - *counter* initial value of counter /// - *thread_manager* Thread manager which managing threads pub fn new(counter: i32) -> Semaphore{ - Semaphore { counter, waiting_queue: List::new()} + Semaphore { counter, waiting_queue: List::default() } } /// Decrement the value, and wait if it becomes < 0. Checking the @@ -98,7 +98,7 @@ impl Lock { /// ### Parameters /// - **thread_manager** Thread manager which managing threads pub fn new() -> Lock { - Lock { owner: None, waiting_queue: List::new(), free: true } + Lock { owner: None, waiting_queue: List::default(), free: true } } /// Wait until the lock become free. Checking the @@ -196,7 +196,7 @@ impl Condition { /// ### Parameters /// - *thread_manager* Thread manager which managing threads pub fn new() -> Condition { - Condition{ waiting_queue: List::new()} + Condition{ waiting_queue: List::default()} } /// Block the calling thread (put it in the wait queue). diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 0216663..298d6cf 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -28,8 +28,8 @@ impl ThreadManager { Self { g_current_thread: Option::None, g_thread_to_be_destroyed: Option::None, - g_alive: List::new(), - ready_list: List::new(), + g_alive: List::default(), + ready_list: List::default(), } } diff --git a/src/utility/list.rs b/src/utility/list.rs index 756112a..9fb41bd 100644 --- a/src/utility/list.rs +++ b/src/utility/list.rs @@ -31,16 +31,11 @@ pub struct IterMut<'a, T> { impl List { - /// Create an empty list - pub fn new() -> Self { - List { head: ptr::null_mut(), tail: ptr::null_mut() } - } - /// Push an item at the end of the list pub fn push(&mut self, elem: T) { unsafe { let new_tail = Box::into_raw(Box::new(Node { - elem: elem, + elem, next: ptr::null_mut(), })); @@ -97,10 +92,12 @@ impl List { /// Worst case complexity of this function is O(n) pub fn contains(&self, elem: &T) -> bool { let mut iter = self.iter(); - let element = iter.next(); + let mut element = iter.next(); while element.is_some() { if element.unwrap() == elem { return true; + } else { + element = iter.next(); } } false @@ -159,9 +156,16 @@ impl List { } } +impl Default for List { + /// Create an empty list + fn default() -> Self { + Self { head: ptr::null_mut(), tail: ptr::null_mut() } + } +} + impl Drop for List { fn drop(&mut self) { - while let Some(_) = self.pop() {} // removing every item from list (necessary as we using unsafe function) + while self.pop().is_some() {} // removing every item from list (necessary as we using unsafe function) } } @@ -207,7 +211,7 @@ mod test { #[test] fn basics() { - let mut list = List::new(); + let mut list = List::default(); // Check empty list behaves right assert_eq!(list.pop(), None); @@ -236,7 +240,7 @@ mod test { #[test] fn peek() { - let mut list = List::new(); + let mut list = List::default(); assert_eq!(list.peek(), None); assert_eq!(list.peek_mut(), None); list.push(1); @@ -249,7 +253,7 @@ mod test { #[test] fn into_iter() { - let mut list = List::new(); + let mut list = List::default(); list.push(1); list.push(2); list.push(3); @@ -263,7 +267,7 @@ mod test { #[test] fn iter() { - let mut list = List::new(); + let mut list = List::default(); list.push(1); list.push(2); list.push(3); @@ -276,8 +280,10 @@ mod test { #[test] fn iter_mut() { - let mut list = List::new(); - list.push(1); list.push(2); list.push(3); + let mut list = List::default(); + list.push(1); + list.push(2); + list.push(3); let mut iter = list.iter_mut(); assert_eq!(iter.next(), Some(&mut 1)); @@ -285,9 +291,37 @@ mod test { assert_eq!(iter.next(), Some(&mut 3)); } + #[test] + fn contains_test() { + let mut list = List::default(); + assert_eq!(list.peek(), None); + list.push(1); + list.push(2); + list.push(3); + + assert_eq!(list.contains(&1), true); + assert_eq!(list.contains(&4), false); + } + + #[test] + fn remove_test() { + let mut list = List::default(); + assert_eq!(list.peek(), None); + list.push(1); + list.push(2); + list.push(3); + + assert_eq!(list.contains(&2), true); + list.remove(&2); + assert_eq!(list.contains(&2), false); + assert_eq!(list.pop(), Option::Some(1)); + assert_eq!(list.pop(), Option::Some(3)); + assert_eq!(list.pop(), Option::None); + } + #[test] fn miri_test() { - let mut list = List::new(); + let mut list = List::default(); list.push(1); list.push(2);