diff --git a/src/client/result/ResultParser.rs b/src/client/result/ResultParser.rs index e9e262c1..ef4a4609 100644 --- a/src/client/result/ResultParser.rs +++ b/src/client/result/ResultParser.rs @@ -464,7 +464,7 @@ mod tests { ); let p_res = parse_result_with_parser(&result, |v| { Some(ParsedClientResult::Other(OtherParsedResult::new(Box::new( - v.getRawBytes().clone(), + v.getRawBytes().to_vec(), )))) }) .unwrap(); diff --git a/src/common/bit_array.rs b/src/common/bit_array.rs index 30d12b26..b3136c7a 100644 --- a/src/common/bit_array.rs +++ b/src/common/bit_array.rs @@ -240,6 +240,7 @@ impl BitArray { /** * Clears all bits (sets to false). */ + #[inline] pub fn clear(&mut self) { self.bits.fill(0); } @@ -522,3 +523,14 @@ impl std::io::Write for BitArray { Ok(()) } } + +impl std::io::Seek for BitArray { + fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { + self.read_offset = match pos { + std::io::SeekFrom::Start(s) => s as usize, + std::io::SeekFrom::End(e) => self.size - e as usize, + std::io::SeekFrom::Current(c) => self.read_offset + c as usize, + }; + Ok(self.read_offset as u64) + } +} diff --git a/src/multi/by_quadrant_reader.rs b/src/multi/by_quadrant_reader.rs index d6ba8d4d..984e97db 100644 --- a/src/multi/by_quadrant_reader.rs +++ b/src/multi/by_quadrant_reader.rs @@ -62,7 +62,7 @@ impl Reader for ByQuadrantReader { match result { Ok(res) => { let points = Self::makeAbsolute(res.getPoints(), halfWidth as f32, 0.0); - return Ok(RXingResult::new_from_existing_result(res, points)); + return Ok(res.with_point(points)); } Err(Exceptions::NotFoundException(_)) => {} _ => return result, @@ -75,7 +75,7 @@ impl Reader for ByQuadrantReader { match result { Ok(res) => { let points = Self::makeAbsolute(res.getPoints(), 0.0, halfHeight as f32); - return Ok(RXingResult::new_from_existing_result(res, points)); + return Ok(res.with_point(points)); } Err(Exceptions::NotFoundException(_)) => {} _ => return result, @@ -90,7 +90,7 @@ impl Reader for ByQuadrantReader { Ok(res) => { let points = Self::makeAbsolute(res.getPoints(), halfWidth as f32, halfHeight as f32); - return Ok(RXingResult::new_from_existing_result(res, points)); + return Ok(res.with_point(points)); } Err(Exceptions::NotFoundException(_)) => {} _ => return result, @@ -106,7 +106,7 @@ impl Reader for ByQuadrantReader { quarterWidth as f32, quarterHeight as f32, ); - Ok(RXingResult::new_from_existing_result(result, points)) + Ok(result.with_point(points)) } fn reset(&mut self) { diff --git a/src/multi/generic_multiple_barcode_reader.rs b/src/multi/generic_multiple_barcode_reader.rs index 99bf6c57..456b425d 100644 --- a/src/multi/generic_multiple_barcode_reader.rs +++ b/src/multi/generic_multiple_barcode_reader.rs @@ -135,7 +135,7 @@ impl GenericMultipleBarcodeReader { return; }; - let resultPoints = result.getPoints().clone(); + let resultPoints = result.getPoints().to_vec(); let possible_new_result = Self::translatePoints(result, xOffset, yOffset); @@ -151,7 +151,7 @@ impl GenericMultipleBarcodeReader { let mut minY: f32 = height as f32; let mut maxX: f32 = 0.0; let mut maxY: f32 = 0.0; - for point in &resultPoints { + for point in resultPoints.into_iter() { let x = point.x; let y = point.y; @@ -220,7 +220,7 @@ impl GenericMultipleBarcodeReader { let mut newRXingResult = RXingResult::new_complex( result.getText(), - result.getRawBytes().clone(), + result.getRawBytes().to_vec(), result.getNumBits(), newPoints, *result.getBarcodeFormat(), diff --git a/src/oned/multi_format_upc_ean_reader.rs b/src/oned/multi_format_upc_ean_reader.rs index e7f1332e..cc36b989 100644 --- a/src/oned/multi_format_upc_ean_reader.rs +++ b/src/oned/multi_format_upc_ean_reader.rs @@ -167,8 +167,8 @@ impl MultiFormatUPCEANReader { // Transfer the metadata across let mut resultUPCA = RXingResult::new( &result.getText()[1..], - result.getRawBytes().clone(), - result.getPoints().clone(), + result.getRawBytes().to_vec(), + result.getPoints().to_vec(), BarcodeFormat::UPC_A, ); resultUPCA.putAllMetadata(result.getRXingResultMetadata().clone()); diff --git a/src/oned/telepen_reader.rs b/src/oned/telepen_reader.rs index 2b316663..51b6a6ec 100644 --- a/src/oned/telepen_reader.rs +++ b/src/oned/telepen_reader.rs @@ -14,6 +14,8 @@ * limitations under the License. */ +use std::io::Read; + use rxing_one_d_proc_derive::OneDReader; use crate::common::{BitArray, Result}; @@ -33,14 +35,14 @@ use super::OneDReader; #[derive(OneDReader)] pub struct TelepenReader { // Keep some instance variables to avoid reallocations - counters: Vec, + counters: Box<[u32]>, counterLength: usize, } impl Default for TelepenReader { fn default() -> Self { Self { - counters: vec![0; 80], + counters: Box::new([0; 80]), counterLength: 0, } } @@ -171,7 +173,9 @@ impl OneDReader for TelepenReader { } let mut bytes: Vec = vec![0; byteLength]; - bits.toBytes(0, bytes.as_mut_slice(), 0, byteLength); + // bits.toBytes(0, bytes.as_mut_slice(), 0, byteLength); + bits.read_exact(&mut bytes) + .map_err(|_| Exceptions::ILLEGAL_STATE)?; j = 0; @@ -258,7 +262,7 @@ impl OneDReader for TelepenReader { impl TelepenReader { pub fn new() -> Self { Self { - counters: vec![0; 80], //Vec::with_capacity(80), + counters: Box::new([0; 80]), //Vec::with_capacity(80), counterLength: 0, } } @@ -318,7 +322,7 @@ impl TelepenReader { if self.counterLength >= self.counters.len() { let mut temp = vec![0; self.counterLength * 2]; temp[0..self.counterLength].clone_from_slice(&self.counters[..]); - self.counters = temp; + self.counters = temp.into_boxed_slice(); } } diff --git a/src/oned/upc_ean_reader.rs b/src/oned/upc_ean_reader.rs index aa19ef72..0bc05a70 100644 --- a/src/oned/upc_ean_reader.rs +++ b/src/oned/upc_ean_reader.rs @@ -223,7 +223,7 @@ pub trait UPCEANReader: OneDReader { ), ); decodeRXingResult.putAllMetadata(extensionRXingResult.getRXingResultMetadata().clone()); - decodeRXingResult.addPoints(&mut extensionRXingResult.getPoints().clone()); + decodeRXingResult.addPoints(&mut extensionRXingResult.getPoints().to_vec()); extensionLength = extensionRXingResult.getText().chars().count(); Ok(()) }; diff --git a/src/qrcode/encoder/qrcode_encoder.rs b/src/qrcode/encoder/qrcode_encoder.rs index f6884d53..968505ea 100644 --- a/src/qrcode/encoder/qrcode_encoder.rs +++ b/src/qrcode/encoder/qrcode_encoder.rs @@ -17,7 +17,7 @@ * @author satorux@google.com (Satoru Takabayashi) - creator * @author dswitkin@google.com (Daniel Switkin) - ported from C++ */ -use std::collections::HashMap; +use std::{collections::HashMap, io::Seek}; use unicode_segmentation::UnicodeSegmentation; diff --git a/src/result_point_utils.rs b/src/result_point_utils.rs index 17c9ccdd..c8d955c8 100644 --- a/src/result_point_utils.rs +++ b/src/result_point_utils.rs @@ -30,7 +30,5 @@ pub fn orderBestPatterns + Copy>(patterns: &mut [T; 3]) { std::mem::swap(&mut pointA, &mut pointC); } - patterns[0] = pointA; - patterns[1] = pointB; - patterns[2] = pointC; + *patterns = [pointA, pointB, pointC]; } diff --git a/src/rxing_result.rs b/src/rxing_result.rs index b25d70ae..0527d18e 100644 --- a/src/rxing_result.rs +++ b/src/rxing_result.rs @@ -21,6 +21,8 @@ use crate::{ RXingResultMetadataType, RXingResultMetadataValue, }; +pub type RXingResultMetaDataDictionary = HashMap; + #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -37,7 +39,7 @@ pub struct RXingResult { numBits: usize, resultPoints: Vec, format: BarcodeFormat, - resultMetadata: HashMap, + resultMetadata: RXingResultMetaDataDictionary, timestamp: u128, line_count: usize, } @@ -88,16 +90,16 @@ impl RXingResult { } } - pub fn new_from_existing_result(prev: Self, points: Vec) -> Self { + pub fn with_point(self, points: Vec) -> Self { Self { - text: prev.text, - rawBytes: prev.rawBytes, - numBits: prev.numBits, + text: self.text, + rawBytes: self.rawBytes, + numBits: self.numBits, resultPoints: points, - format: prev.format, - resultMetadata: prev.resultMetadata, - timestamp: prev.timestamp, - line_count: prev.line_count, + format: self.format, + resultMetadata: self.resultMetadata, + timestamp: self.timestamp, + line_count: self.line_count, } } @@ -149,7 +151,7 @@ impl RXingResult { /** * @return raw bytes encoded by the barcode, if applicable, otherwise {@code null} */ - pub fn getRawBytes(&self) -> &Vec { + pub fn getRawBytes(&self) -> &[u8] { &self.rawBytes } @@ -166,21 +168,21 @@ impl RXingResult { * identifying finder patterns or the corners of the barcode. The exact meaning is * specific to the type of barcode that was decoded. */ - pub fn getPoints(&self) -> &Vec { + pub fn getPoints(&self) -> &[Point] { &self.resultPoints } - pub fn getPointsMut(&mut self) -> &mut Vec { + pub fn getPointsMut(&mut self) -> &mut [Point] { &mut self.resultPoints } /** Currently necessary because the external OneDReader proc macro uses it. */ - pub fn getRXingResultPoints(&self) -> &Vec { + pub fn getRXingResultPoints(&self) -> &[Point] { &self.resultPoints } /** Currently necessary because the external OneDReader proc macro uses it. */ - pub fn getRXingResultPointsMut(&mut self) -> &mut Vec { + pub fn getRXingResultPointsMut(&mut self) -> &mut [Point] { &mut self.resultPoints } @@ -196,9 +198,7 @@ impl RXingResult { * {@code null}. This contains optional metadata about what was detected about the barcode, * like orientation. */ - pub fn getRXingResultMetadata( - &self, - ) -> &HashMap { + pub fn getRXingResultMetadata(&self) -> &RXingResultMetaDataDictionary { &self.resultMetadata } @@ -210,12 +210,9 @@ impl RXingResult { self.resultMetadata.insert(md_type, value); } - pub fn putAllMetadata( - &mut self, - metadata: HashMap, - ) { + pub fn putAllMetadata(&mut self, metadata: RXingResultMetaDataDictionary) { if self.resultMetadata.is_empty() { - self.resultMetadata = metadata; + let _ = std::mem::replace(&mut self.resultMetadata, metadata); } else { for (key, value) in metadata.into_iter() { self.resultMetadata.insert(key, value); diff --git a/src/rxing_result_point.rs b/src/rxing_result_point.rs index 21e35f24..082af6ad 100644 --- a/src/rxing_result_point.rs +++ b/src/rxing_result_point.rs @@ -92,13 +92,6 @@ impl Hash for Point { } } -// impl PartialEq for Point { -// fn eq(&self, other: &Self) -> bool { -// self.x == other.x && self.y == other.y -// } -// } -// impl Eq for Point {} - impl PartialEq for PointT where T: PartialEq,