@@ -43,7 +43,7 @@ impl Buffer {
4343 self . mergers . push ( alias) ;
4444 }
4545
46- pub fn update ( & mut self , new_chunk : Chunk ) {
46+ pub fn update ( & mut self , new_chunk : & Chunk ) {
4747 self . pending_delta . clear ( ) ;
4848
4949 let mut empty_lanes: Vec < usize > = self
@@ -56,17 +56,15 @@ impl Buffer {
5656 // sort descending so we can pop the lowest index first
5757 empty_lanes. sort_unstable_by ( |a, b| b. cmp ( a) ) ;
5858
59- let mut found_idx = None ;
60- if new_chunk. alias . is_some ( ) {
61- for ( i, c) in self . current . iter ( ) . enumerate ( ) {
62- if let Some ( c) = c {
63- if c. parent_a == new_chunk. alias {
64- found_idx = Some ( i) ;
65- break ;
66- }
67- }
68- }
69- }
59+ let found_idx = if new_chunk. alias . is_some ( ) {
60+ self . current . iter ( ) . enumerate ( ) . find_map ( |( i, c) | {
61+ c. as_ref ( ) . and_then ( |c| {
62+ ( c. parent_a == new_chunk. alias ) . then_some ( i)
63+ } )
64+ } )
65+ } else {
66+ None
67+ } ;
7068
7169 if let Some ( idx) = found_idx {
7270 self . record_replace ( idx, Some ( new_chunk. clone ( ) ) ) ;
@@ -89,20 +87,18 @@ impl Buffer {
8987 }
9088
9189 if let Some ( mut c) = self . current [ index] . clone ( ) {
92- let mut changed = false ;
93-
94- if new_chunk. alias . is_some ( )
95- && c. parent_a == new_chunk. alias
96- {
97- c. parent_a = None ;
98- changed = true ;
99- }
100- if new_chunk. alias . is_some ( )
101- && c. parent_b == new_chunk. alias
102- {
103- c. parent_b = None ;
104- changed = true ;
105- }
90+ let changed = new_chunk. alias . is_some_and ( |alias| {
91+ let mut changed = false ;
92+ if c. parent_a == Some ( alias) {
93+ c. parent_a = None ;
94+ changed = true ;
95+ }
96+ if c. parent_b == Some ( alias) {
97+ c. parent_b = None ;
98+ changed = true ;
99+ }
100+ changed
101+ } ) ;
106102
107103 if changed {
108104 if c. parent_a . is_none ( ) && c. parent_b . is_none ( ) {
@@ -117,7 +113,7 @@ impl Buffer {
117113 while let Some ( alias) = self . mergers . pop ( ) {
118114 if let Some ( index) = self . current . iter ( ) . position ( |c| {
119115 c. as_ref ( )
120- . map_or ( false , |chunk| chunk. alias == Some ( alias) )
116+ . is_some_and ( |chunk| chunk. alias == Some ( alias) )
121117 } ) {
122118 if let Some ( mut c) = self . current [ index] . clone ( ) {
123119 let parent_b = c. parent_b ;
@@ -146,14 +142,8 @@ impl Buffer {
146142 }
147143 }
148144
149- loop {
150- if let Some ( last) = self . current . last ( ) {
151- if last. is_none ( ) {
152- self . record_remove ( self . current . len ( ) - 1 ) ;
153- continue ;
154- }
155- }
156- break ;
145+ while self . current . last ( ) . is_some_and ( Option :: is_none) {
146+ self . record_remove ( self . current . len ( ) - 1 ) ;
157147 }
158148
159149 let delta = Delta ( self . pending_delta . clone ( ) ) ;
@@ -193,12 +183,11 @@ impl Buffer {
193183 start : usize ,
194184 end : usize ,
195185 ) -> Vec < Vector < Option < Chunk > > > {
196- let ( current_index, mut state) = self
197- . checkpoints
198- . range ( ..=start)
199- . next_back ( )
200- . map ( |( & i, s) | ( Some ( i) , s. clone ( ) ) )
201- . unwrap_or ( ( None , Vector :: new ( ) ) ) ;
186+ let ( current_index, mut state) =
187+ self . checkpoints . range ( ..=start) . next_back ( ) . map_or_else (
188+ || ( None , Vector :: new ( ) ) ,
189+ |( & i, s) | ( Some ( i) , s. clone ( ) ) ,
190+ ) ;
202191
203192 let mut history =
204193 Vec :: with_capacity ( end. saturating_sub ( start) + 1 ) ;
@@ -209,11 +198,11 @@ impl Buffer {
209198 }
210199 }
211200
212- let loop_start = current_index. map ( |i| i + 1 ) . unwrap_or ( 0 ) ;
201+ let loop_start = current_index. map_or ( 0 , |i| i + 1 ) ;
213202
214203 for delta_index in loop_start..=end {
215204 if let Some ( delta) = self . deltas . get ( delta_index) {
216- self . apply_delta_to_state ( & mut state, delta) ;
205+ Self :: apply_delta_to_state ( & mut state, delta) ;
217206
218207 if delta_index >= start {
219208 history. push ( state. clone ( ) ) ;
@@ -227,14 +216,13 @@ impl Buffer {
227216 }
228217
229218 fn apply_delta_to_state (
230- & self ,
231219 state : & mut Vector < Option < Chunk > > ,
232220 delta : & Delta ,
233221 ) {
234222 for op in & delta. 0 {
235223 match op {
236224 DeltaOp :: Insert { index, item } => {
237- state. insert ( * index, item. clone ( ) )
225+ state. insert ( * index, item. clone ( ) ) ;
238226 }
239227 DeltaOp :: Remove { index } => {
240228 state. remove ( * index) ;
0 commit comments