diff options
| author | Srinivas Reddy Thatiparthy <thatiparthysreenivas@gmail.com> | 2016-12-20 09:54:00 +0530 |
|---|---|---|
| committer | Srinivas Reddy Thatiparthy <thatiparthysreenivas@gmail.com> | 2016-12-20 09:54:00 +0530 |
| commit | 6414e67dbaa27f8638b01a33f49e462e87ecfab3 (patch) | |
| tree | f29af576e5be30f3fea9c08ea2eeb5add82cd135 /src/libcollections/vec_deque.rs | |
| parent | 94ae2a2e6791e0c4ab6fba836b2b09a07f2d3c8a (diff) | |
| download | rust-6414e67dbaa27f8638b01a33f49e462e87ecfab3.tar.gz rust-6414e67dbaa27f8638b01a33f49e462e87ecfab3.zip | |
run rustfmt on libcollections folder
Diffstat (limited to 'src/libcollections/vec_deque.rs')
| -rw-r--r-- | src/libcollections/vec_deque.rs | 83 |
1 files changed, 35 insertions, 48 deletions
diff --git a/src/libcollections/vec_deque.rs b/src/libcollections/vec_deque.rs index dbe3fec205c..67621b860bf 100644 --- a/src/libcollections/vec_deque.rs +++ b/src/libcollections/vec_deque.rs @@ -206,11 +206,7 @@ impl<T> VecDeque<T> { unsafe fn wrap_copy(&self, dst: usize, src: usize, len: usize) { #[allow(dead_code)] fn diff(a: usize, b: usize) -> usize { - if a <= b { - b - a - } else { - a - b - } + if a <= b { b - a } else { a - b } } debug_assert!(cmp::min(diff(dst, src), self.cap() - diff(dst, src)) + len <= self.cap(), "wrc dst={} src={} len={} cap={}", @@ -552,8 +548,8 @@ impl<T> VecDeque<T> { let old_cap = self.cap(); let used_cap = self.len() + 1; let new_cap = used_cap.checked_add(additional) - .and_then(|needed_cap| needed_cap.checked_next_power_of_two()) - .expect("capacity overflow"); + .and_then(|needed_cap| needed_cap.checked_next_power_of_two()) + .expect("capacity overflow"); if new_cap > self.capacity() { self.buf.reserve_exact(used_cap, new_cap - used_cap); @@ -1293,9 +1289,7 @@ impl<T> VecDeque<T> { let contiguous = self.is_contiguous(); - match (contiguous, - distance_to_tail <= distance_to_head, - idx >= self.tail) { + match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { (true, true, _) if index == 0 => { // push_front // @@ -1513,9 +1507,7 @@ impl<T> VecDeque<T> { let contiguous = self.is_contiguous(); - match (contiguous, - distance_to_tail <= distance_to_head, - idx >= self.tail) { + match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { (true, true, _) => { unsafe { // contiguous, remove closer to tail: @@ -1812,7 +1804,7 @@ fn wrap_index(index: usize, size: usize) -> usize { } /// Returns the two slices that cover the VecDeque's valid range -trait RingSlices : Sized { +trait RingSlices: Sized { fn slice(self, from: usize, to: usize) -> Self; fn split_at(self, i: usize) -> (Self, Self); @@ -1895,7 +1887,7 @@ impl<'a, T> Iterator for Iter<'a, T> { } fn fold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc - where F: FnMut(Acc, Self::Item) -> Acc, + where F: FnMut(Acc, Self::Item) -> Acc { let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); accum = front.iter().fold(accum, &mut f); @@ -1959,7 +1951,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { } fn fold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc - where F: FnMut(Acc, Self::Item) -> Acc, + where F: FnMut(Acc, Self::Item) -> Acc { let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); accum = front.iter_mut().fold(accum, &mut f); @@ -2082,17 +2074,15 @@ impl<'a, T: 'a> Drop for Drain<'a, T> { (_, 0) => { source_deque.head = drain_tail; } - _ => { - unsafe { - if tail_len <= head_len { - source_deque.tail = source_deque.wrap_sub(drain_head, tail_len); - source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len); - } else { - source_deque.head = source_deque.wrap_add(drain_tail, head_len); - source_deque.wrap_copy(drain_tail, drain_head, head_len); - } + _ => unsafe { + if tail_len <= head_len { + source_deque.tail = source_deque.wrap_sub(drain_head, tail_len); + source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len); + } else { + source_deque.head = source_deque.wrap_add(drain_tail, head_len); + source_deque.wrap_copy(drain_tail, drain_head, head_len); } - } + }, } } } @@ -2288,10 +2278,8 @@ impl<T> From<Vec<T>> for VecDeque<T> { // We need to extend the buf if it's not a power of two, too small // or doesn't have at least one free space - if !buf.cap().is_power_of_two() - || (buf.cap() < (MINIMUM_CAPACITY + 1)) - || (buf.cap() == len) - { + if !buf.cap().is_power_of_two() || (buf.cap() < (MINIMUM_CAPACITY + 1)) || + (buf.cap() == len) { let cap = cmp::max(buf.cap() + 1, MINIMUM_CAPACITY + 1).next_power_of_two(); buf.reserve_exact(len, cap - len); } @@ -2299,7 +2287,7 @@ impl<T> From<Vec<T>> for VecDeque<T> { VecDeque { tail: 0, head: len, - buf: buf + buf: buf, } } } @@ -2324,18 +2312,17 @@ impl<T> From<VecDeque<T>> for Vec<T> { // do this in at most three copy moves. if (cap - tail) > head { // right hand block is the long one; move that enough for the left - ptr::copy( - buf.offset(tail as isize), - buf.offset((tail - head) as isize), - cap - tail); + ptr::copy(buf.offset(tail as isize), + buf.offset((tail - head) as isize), + cap - tail); // copy left in the end ptr::copy(buf, buf.offset((cap - head) as isize), head); // shift the new thing to the start - ptr::copy(buf.offset((tail-head) as isize), buf, len); + ptr::copy(buf.offset((tail - head) as isize), buf, len); } else { // left hand block is the long one, we can do it in two! - ptr::copy(buf, buf.offset((cap-tail) as isize), head); - ptr::copy(buf.offset(tail as isize), buf, cap-tail); + ptr::copy(buf, buf.offset((cap - tail) as isize), head); + ptr::copy(buf.offset(tail as isize), buf, cap - tail); } } else { // Need to use N swaps to move the ring @@ -2576,8 +2563,8 @@ mod tests { // We should see the correct values in the VecDeque let expected: VecDeque<_> = (0..drain_start) - .chain(drain_end..len) - .collect(); + .chain(drain_end..len) + .collect(); assert_eq!(expected, tester); } } @@ -2693,19 +2680,19 @@ mod tests { let cap = (2i32.pow(cap_pwr) - 1) as usize; // In these cases there is enough free space to solve it with copies - for len in 0..((cap+1)/2) { + for len in 0..((cap + 1) / 2) { // Test contiguous cases - for offset in 0..(cap-len) { + for offset in 0..(cap - len) { create_vec_and_test_convert(cap, offset, len) } // Test cases where block at end of buffer is bigger than block at start - for offset in (cap-len)..(cap-(len/2)) { + for offset in (cap - len)..(cap - (len / 2)) { create_vec_and_test_convert(cap, offset, len) } // Test cases where block at start of buffer is bigger than block at end - for offset in (cap-(len/2))..cap { + for offset in (cap - (len / 2))..cap { create_vec_and_test_convert(cap, offset, len) } } @@ -2714,19 +2701,19 @@ mod tests { // the ring will use swapping when: // (cap + 1 - offset) > (cap + 1 - len) && (len - (cap + 1 - offset)) > (cap + 1 - len)) // right block size > free space && left block size > free space - for len in ((cap+1)/2)..cap { + for len in ((cap + 1) / 2)..cap { // Test contiguous cases - for offset in 0..(cap-len) { + for offset in 0..(cap - len) { create_vec_and_test_convert(cap, offset, len) } // Test cases where block at end of buffer is bigger than block at start - for offset in (cap-len)..(cap-(len/2)) { + for offset in (cap - len)..(cap - (len / 2)) { create_vec_and_test_convert(cap, offset, len) } // Test cases where block at start of buffer is bigger than block at end - for offset in (cap-(len/2))..cap { + for offset in (cap - (len / 2))..cap { create_vec_and_test_convert(cap, offset, len) } } |
