Restructure Canvas::draw_data(), since change in rust-lang/rust#17403 no
longer allows returning a mutable ref directly.
This commit is contained in:
kennytm 2014-10-24 01:01:34 +08:00
parent e6b80daf11
commit 4d2032e595
2 changed files with 26 additions and 25 deletions

View file

@ -1163,24 +1163,32 @@ mod data_iter_tests {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
//{{{ Data placement //{{{ Data placement
fn draw_codewords<'a, I>(codewords: &[u8], is_half_codeword_at_end: bool, modules: &mut I) impl Canvas {
where I: Iterator<&'a mut Module> fn draw_codewords<'a, I>(&mut self,
{ codewords: &[u8],
let length = codewords.len(); is_half_codeword_at_end: bool,
let last_word = if is_half_codeword_at_end { length-1 } else { length }; coords: &mut I)
for (i, b) in codewords.iter().enumerate() { where I: Iterator<(i16, i16)>
let bits_end = if i == last_word { 4 } else { 0 }; {
for j in range_inclusive(bits_end, 7u).rev() { let length = codewords.len();
let color = if (*b & (1 << j)) != 0 { DarkUnmasked } else { LightUnmasked }; let last_word = if is_half_codeword_at_end { length-1 } else { length };
match modules.next() { for (i, b) in codewords.iter().enumerate() {
Some(module) => { *module = color; } let bits_end = if i == last_word { 4 } else { 0 };
None => { return; } 'outside:
for j in range_inclusive(bits_end, 7u).rev() {
let color = if (*b & (1 << j)) != 0 { DarkUnmasked } else { LightUnmasked };
while let Some((x, y)) = coords.next() {
let r = self.get_mut(x, y);
if *r == Empty {
*r = color;
continue 'outside;
}
}
return;
} }
} }
} }
}
impl Canvas {
/// Draws the encoded data and error correction codes to the empty modules. /// Draws the encoded data and error correction codes to the empty modules.
pub fn draw_data(&mut self, data: &[u8], ec: &[u8]) { pub fn draw_data(&mut self, data: &[u8], ec: &[u8]) {
let is_half_codeword_at_end = match (self.version, self.ec_level) { let is_half_codeword_at_end = match (self.version, self.ec_level) {
@ -1188,13 +1196,9 @@ impl Canvas {
_ => false, _ => false,
}; };
let mut coords = DataModuleIter::new(self.version) let mut coords = DataModuleIter::new(self.version);
.filter_map(|(x, y)| { self.draw_codewords(data, is_half_codeword_at_end, &mut coords);
let r = self.get_mut(x, y); self.draw_codewords(ec, false, &mut coords);
if *r != Empty { None } else { Some(r) }
});
draw_codewords(data, is_half_codeword_at_end, &mut coords);
draw_codewords(ec, false, &mut coords);
} }
} }

View file

@ -20,6 +20,7 @@
#![unstable] #![unstable]
#![feature(slicing_syntax)] #![feature(slicing_syntax)]
#![feature(while_let)]
extern crate test; extern crate test;
@ -176,10 +177,6 @@ impl CloneableVector<bool> for QrCode {
fn to_vec(&self) -> Vec<bool> { fn to_vec(&self) -> Vec<bool> {
self.content.clone() self.content.clone()
} }
fn into_vec(self) -> Vec<bool> {
self.content
}
} }
#[cfg(test)] #[cfg(test)]