@@ -50,44 +50,22 @@ impl<T: PartialEq> TinyList<T> {
5050
5151 #[ inline]
5252 pub fn insert ( & mut self , data : T ) {
53- let current_head = mem:: replace ( & mut self . head , None ) ;
54-
55- if let Some ( current_head) = current_head {
56- let current_head = Box :: new ( current_head) ;
57- self . head = Some ( Element {
58- data,
59- next : Some ( current_head)
60- } ) ;
61- } else {
62- self . head = Some ( Element {
63- data,
64- next : None ,
65- } )
66- }
53+ self . head = Some ( Element {
54+ data,
55+ next : mem:: replace ( & mut self . head , None ) . map ( Box :: new) ,
56+ } ) ;
6757 }
6858
6959 #[ inline]
7060 pub fn remove ( & mut self , data : & T ) -> bool {
71- let remove_head = if let Some ( ref mut head) = self . head {
72- if head. data == * data {
73- Some ( mem:: replace ( & mut head. next , None ) )
74- } else {
75- None
61+ self . head = match self . head {
62+ Some ( ref mut head) if head. data == * data => {
63+ mem:: replace ( & mut head. next , None ) . map ( |x| * x)
7664 }
77- } else {
78- return false
65+ Some ( ref mut head ) => return head . remove_next ( data ) ,
66+ None => return false ,
7967 } ;
80-
81- if let Some ( remove_head) = remove_head {
82- if let Some ( next) = remove_head {
83- self . head = Some ( * next) ;
84- } else {
85- self . head = None ;
86- }
87- return true
88- }
89-
90- self . head . as_mut ( ) . unwrap ( ) . remove_next ( data)
68+ true
9169 }
9270
9371 #[ inline]
@@ -156,6 +134,8 @@ impl<T: PartialEq> Element<T> {
156134#[ cfg( test) ]
157135mod test {
158136 use super :: * ;
137+ extern crate test;
138+ use self :: test:: Bencher ;
159139
160140 #[ test]
161141 fn test_contains_and_insert ( ) {
@@ -248,4 +228,41 @@ mod test {
248228
249229 assert_eq ! ( list. len( ) , 0 ) ;
250230 }
231+
232+ #[ bench]
233+ fn bench_insert_empty ( b : & mut Bencher ) {
234+ b. iter ( || {
235+ let mut list = TinyList :: new ( ) ;
236+ list. insert ( 1 ) ;
237+ } )
238+ }
239+
240+ #[ bench]
241+ fn bench_insert_one ( b : & mut Bencher ) {
242+ b. iter ( || {
243+ let mut list = TinyList :: new_single ( 0 ) ;
244+ list. insert ( 1 ) ;
245+ } )
246+ }
247+
248+ #[ bench]
249+ fn bench_remove_empty ( b : & mut Bencher ) {
250+ b. iter ( || {
251+ TinyList :: new ( ) . remove ( & 1 )
252+ } ) ;
253+ }
254+
255+ #[ bench]
256+ fn bench_remove_unknown ( b : & mut Bencher ) {
257+ b. iter ( || {
258+ TinyList :: new_single ( 0 ) . remove ( & 1 )
259+ } ) ;
260+ }
261+
262+ #[ bench]
263+ fn bench_remove_one ( b : & mut Bencher ) {
264+ b. iter ( || {
265+ TinyList :: new_single ( 1 ) . remove ( & 1 )
266+ } ) ;
267+ }
251268}
0 commit comments