Skip to content

Commit

Permalink
Rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
pczarn committed May 31, 2024
1 parent e897ebd commit 08bad63
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,13 @@ impl<B: BitBlock> BitVec<B> {
/// ```
#[inline]
pub fn get_mut(&mut self, index: usize) -> Option<MutBorrowedBit<B>> {
self.get(index).map(move |value|
MutBorrowedBit {
vec: Rc::new(RefCell::new(self)),
index,
#[cfg(debug_assertions)]
old_value: value,
new_value: value
})
self.get(index).map(move |value| MutBorrowedBit {
vec: Rc::new(RefCell::new(self)),
index,
#[cfg(debug_assertions)]
old_value: value,
new_value: value,
})
}

/// Retrieves a smart pointer to the value at index `i`, without doing bounds checking.
Expand Down Expand Up @@ -1131,7 +1130,7 @@ impl<B: BitBlock> BitVec<B> {
let nbits = self.nbits;
IterMut {
vec: Rc::new(RefCell::new(self)),
range: 0..nbits
range: 0..nbits,
}
}

Expand Down Expand Up @@ -1739,7 +1738,7 @@ pub struct MutBorrowedBit<'a, B: 'a + BitBlock> {
index: usize,
#[cfg(debug_assertions)]
old_value: bool,
new_value: bool
new_value: bool,
}

/// An iterator for mutable references to the bits in a `BitVec`.
Expand All @@ -1748,7 +1747,7 @@ pub struct IterMut<'a, B: 'a + BitBlock = u32> {
range: Range<usize>,
}

impl <'a, B: 'a + BitBlock> IterMut<'a, B> {
impl<'a, B: 'a + BitBlock> IterMut<'a, B> {
fn get(&mut self, index: Option<usize>) -> Option<MutBorrowedBit<'a, B>> {
let index = index?;
let value = (*self.vec).borrow().get(index)?;
Expand All @@ -1757,30 +1756,34 @@ impl <'a, B: 'a + BitBlock> IterMut<'a, B> {
index,
#[cfg(debug_assertions)]
old_value: value,
new_value: value
new_value: value,
})
}
}

impl <'a, B: BitBlock> Deref for MutBorrowedBit<'a, B> {
impl<'a, B: BitBlock> Deref for MutBorrowedBit<'a, B> {
type Target = bool;

fn deref(&self) -> &Self::Target {
&self.new_value
}
}

impl <'a, B: BitBlock> DerefMut for MutBorrowedBit<'a, B> {
impl<'a, B: BitBlock> DerefMut for MutBorrowedBit<'a, B> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.new_value
}
}

impl <'a, B: BitBlock> Drop for MutBorrowedBit<'a, B> {
impl<'a, B: BitBlock> Drop for MutBorrowedBit<'a, B> {
fn drop(&mut self) {
let mut vec = (*self.vec).borrow_mut();
#[cfg(debug_assertions)]
debug_assert_eq!(Some(self.old_value), vec.get(self.index), "Mutably-borrowed bit was modified externally!");
debug_assert_eq!(
Some(self.old_value),
vec.get(self.index),
"Mutably-borrowed bit was modified externally!"
);
vec.set(self.index, self.new_value);
}
}
Expand Down Expand Up @@ -2988,18 +2991,14 @@ mod tests {
assert_eq!(false, *a_bit_1);
*a_bit_1 = true;
drop(a_bit_1);
assert!(a.eq_vec(&[
false, true, false
]));
assert!(a.eq_vec(&[false, true, false]));
}
#[test]
fn test_iter_mut() {
let mut a = BitVec::from_elem(8, false);
a.iter_mut().enumerate().for_each(|(index, mut bit)| {
*bit = if index % 2 == 1 { true } else { false };
});
assert!(a.eq_vec(&[
false, true, false, true, false, true, false, true
]));
assert!(a.eq_vec(&[false, true, false, true, false, true, false, true]));
}
}

0 comments on commit 08bad63

Please sign in to comment.