From e589fcffcc8da46b5949d15284a466d9ed27a003 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:54:34 -0400 Subject: [PATCH] Implemented list::len() based on Container trait --- src/libcollections/list.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index ddac516d8bbd..9223669c7876 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -10,6 +10,8 @@ //! A standard, garbage-collected linked list. +use std::container::Container; + #[deriving(Clone, Eq)] #[allow(missing_doc)] pub enum List { @@ -53,6 +55,11 @@ impl List { } } +impl Container for List { + /// Returns the length of a list + fn len(&self) -> uint { self.iter().len() } +} + /// Returns true if a list contains an element with the given value pub fn has(list: @List, element: T) -> bool { let mut found = false; @@ -70,13 +77,6 @@ pub fn is_empty(list: @List) -> bool { } } -/// Returns the length of a list -pub fn len(list: @List) -> uint { - let mut count = 0u; - iter(list, |_e| count += 1u); - count -} - /// Returns all but the first element of a list pub fn tail(list: @List) -> @List { match *list { @@ -252,10 +252,11 @@ mod tests { #[test] fn test_len() { - let list = @List::from_vec([0, 1, 2]); - let empty = @list::Nil::; - assert_eq!(list::len(list), 3u); - assert_eq!(list::len(empty), 0u); + let empty = Nil::; + assert_eq!(empty.len(), 0u); + + let list = List::from_vec([0, 1, 2]); + assert_eq!(list.len(), 3u); } #[test]