@@ -141,17 +141,12 @@ impl CircularBuffer {
141141
142142 pub fn apply_soft_limit ( & mut self , limit : usize ) {
143143 let limit = std:: cmp:: min ( limit, self . max_capacity ) ;
144- if self . remaining ( ) > limit || self . current_capacity ( ) <= limit {
145- return ;
146- }
147-
148- if self . remaining ( ) == 0 {
144+ if self . remaining ( ) == 0 && self . current_capacity ( ) > limit {
149145 self . buffer = Vec :: new ( ) . into_boxed_slice ( ) ;
150146 self . position = 0 ;
151- return ;
147+ } else if self . remaining ( ) <= limit / 2 && self . current_capacity ( ) >= 2 * limit {
148+ self . resize_buffer ( limit) ;
152149 }
153-
154- self . resize_buffer ( limit) ;
155150 }
156151}
157152
@@ -337,34 +332,71 @@ mod test {
337332 }
338333
339334 #[ test]
340- fn apply_soft_limit ( ) {
335+ fn resize_buffer ( ) {
341336 let mut b = CircularBuffer :: new ( 0 , 16 ) ;
342337 b. write_all ( b"0123456789ABCDEF" ) . unwrap ( ) ;
343338 assert_eq ! ( b. current_capacity( ) , 16 ) ;
344339
345- b. apply_soft_limit ( 16 ) ;
340+ b. resize_buffer ( 16 ) ;
346341 assert_eq ! ( b. current_capacity( ) , 16 ) ;
347342 assert_eq ! ( b. bytes( ) , b"0123456789ABCDEF" ) ;
348343
349344 b. advance ( 1 ) ;
350- b. apply_soft_limit ( 16 ) ;
345+ b. resize_buffer ( 16 ) ;
351346 assert_eq ! ( b. current_capacity( ) , 16 ) ;
352347 assert_eq ! ( b. bytes( ) , b"123456789ABCDEF" ) ;
353348
354- b. apply_soft_limit ( 15 ) ;
355- assert_eq ! ( b. current_capacity( ) , 15 ) ;
356- assert_eq ! ( b. bytes( ) , b"123456789ABCDEF" ) ;
357-
358- b. apply_soft_limit ( 8 ) ;
349+ b. resize_buffer ( 15 ) ;
359350 assert_eq ! ( b. current_capacity( ) , 15 ) ;
360351 assert_eq ! ( b. bytes( ) , b"123456789ABCDEF" ) ;
361352
362353 b. advance ( 15 ) ;
363- b. apply_soft_limit ( 15 ) ;
354+ b. resize_buffer ( 15 ) ;
364355 assert_eq ! ( b. current_capacity( ) , 15 ) ;
365356 assert_eq ! ( b. bytes( ) , b"" ) ;
366357
367- b. apply_soft_limit ( 14 ) ;
358+ b. resize_buffer ( 0 ) ;
359+ assert_eq ! ( b. current_capacity( ) , 0 ) ;
360+ assert_eq ! ( b. bytes( ) , b"" ) ;
361+ }
362+
363+ #[ test]
364+ fn apply_soft_limit ( ) {
365+ let mut b = CircularBuffer :: new ( 0 , 16 ) ;
366+ b. write_all ( b"0123456789ABCDEF" ) . unwrap ( ) ;
367+ assert_eq ! ( b. current_capacity( ) , 16 ) ;
368+
369+ b. apply_soft_limit ( 16 ) ;
370+ assert_eq ! ( b. current_capacity( ) , 16 ) ;
371+ assert_eq ! ( b. bytes( ) , b"0123456789ABCDEF" ) ;
372+
373+ b. apply_soft_limit ( 0 ) ;
374+ assert_eq ! ( b. current_capacity( ) , 16 ) ;
375+ assert_eq ! ( b. bytes( ) , b"0123456789ABCDEF" ) ;
376+
377+ b. advance ( 8 ) ;
378+ b. apply_soft_limit ( 8 ) ;
379+ assert_eq ! ( b. current_capacity( ) , 16 ) ;
380+ assert_eq ! ( b. bytes( ) , b"89ABCDEF" ) ;
381+
382+ b. advance ( 3 ) ;
383+ b. apply_soft_limit ( 8 ) ;
384+ assert_eq ! ( b. current_capacity( ) , 16 ) ;
385+ assert_eq ! ( b. bytes( ) , b"BCDEF" ) ;
386+
387+ b. advance ( 1 ) ;
388+ b. apply_soft_limit ( 8 ) ;
389+ assert_eq ! ( b. current_capacity( ) , 8 ) ;
390+ assert_eq ! ( b. bytes( ) , b"CDEF" ) ;
391+
392+ b. advance ( 4 ) ;
393+ b. apply_soft_limit ( 8 ) ;
394+ assert_eq ! ( b. current_capacity( ) , 8 ) ;
395+ assert_eq ! ( b. bytes( ) , b"" ) ;
396+
397+ assert ! ( b. write_all( b"0123456789ABCDEF" ) . is_ok( ) ) ;
398+ b. advance ( 16 ) ;
399+ b. apply_soft_limit ( 8 ) ;
368400 assert_eq ! ( b. current_capacity( ) , 0 ) ;
369401 assert_eq ! ( b. bytes( ) , b"" ) ;
370402 }
0 commit comments