From 0193a1fb63c0cefcb352d5be50bf160cb3418cd8 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 19 Nov 2025 13:42:32 -0800 Subject: Applied clippy suggestions. Learned some cool new things, otherwise it was pretty minor. --- rust/src/lib.rs | 74 +++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 42 deletions(-) diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 5a5f96a..88d3410 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -71,11 +71,11 @@ fn write_packed_int( x: i64 ) -> Vec 0x00_u8 } | (v&0x3F) as u8 | if v > (v & 0x3F) { 0x80_u8 } else { 0x00_u8 }; w.push(bb); - v = v >> 6; + v >>= 6; while v > 0 { bb = (v & 0x7F) as u8 | if v > (v & 0x7F) { 0x80_u8 } else { 0x00_u8 }; w.push(bb); - v = v >> 7; + v >>= 7; } w @@ -84,12 +84,12 @@ fn write_packed_int( x: i64 ) -> Vec fn read_packed_int( r: &mut R ) -> Result { let mut out : i64; let mut bb = [0u8]; - r.read( &mut bb )?; + r.read_exact( &mut bb )?; let negative = (bb[0]&0x40_u8) == 0x40_u8; out = (bb[0]&0x3F_u8) as i64; let mut c = 0; while (bb[0]&0x80_u8) != 0 { - r.read( &mut bb )?; + r.read_exact( &mut bb )?; out |= ((bb[0]&0x7F_u8) as i64) << (6+7*c); c += 1; } @@ -105,65 +105,65 @@ impl Value { pub fn write(&self, w: &mut W) -> Result<(), Error> { match self { Self::Integer(x) => { - let bb = ['i' as u8]; + let bb = [b'i']; w.write_all(&bb)?; w.write_all(&write_packed_int( *x ))?; }, Self::ByteString(s) => { - let bb = ['s' as u8]; + let bb = [b's']; w.write_all(&bb)?; w.write_all(&write_packed_int( s.len() as i64 ))?; - w.write_all( &s )?; + w.write_all( s )?; }, Self::Boolean(b) => { let mut bb = [0u8]; - bb[0] = if *b { '1' } else { '0' } as u8; + bb[0] = if *b { b'1' } else { b'0' }; w.write_all( &bb )?; }, Self::List(l) => { - let mut bb = ['l' as u8]; + let mut bb = [b'l']; w.write_all(&bb)?; for v in l { v.write( w )?; } - bb[0] = 'e' as u8; + bb[0] = b'e'; w.write_all(&bb)?; }, Self::Dictionary(d) => { - let mut bb = ['d' as u8]; + let mut bb = [b'd']; w.write_all(&bb)?; for (k, v) in d { (Value::ByteString( k.clone().into_bytes() )).write( w )?; v.write( w )?; } - bb[0] = 'e' as u8; + bb[0] = b'e'; w.write_all(&bb)?; }, Self::Float(f) => { match f.classify() { FpCategory::Nan => { - let mut bb = ['F' as u8, 0u8]; + let mut bb = [b'F', 0u8]; bb[1] = if f.is_sign_negative() { 'N' } else { 'n' } as u8; w.write_all( &bb )?; }, FpCategory::Infinite => { - let mut bb = ['F' as u8, 0u8]; + let mut bb = [b'F', 0u8]; bb[1] = if f.is_sign_negative() { 'I' } else { 'i' } as u8; w.write_all( &bb )?; }, FpCategory::Zero => { - let mut bb = ['F' as u8, 0u8]; + let mut bb = [b'F', 0u8]; bb[1] = if f.is_sign_negative() { 'Z' } else { 'z' } as u8; w.write_all( &bb )?; }, FpCategory::Subnormal => { - let mut bb = ['F' as u8, 0u8]; + let mut bb = [b'F', 0u8]; // The format doesn't account for these...uh...make them zero? bb[1] = if f.is_sign_negative() { 'Z' } else { 'z' } as u8; w.write_all( &bb )?; }, FpCategory::Normal => { - let bb = ['f' as u8]; + let bb = [b'f']; w.write_all( &bb )?; let mut bin = Vec::::with_capacity(10); let (negative, d) = if f.is_sign_negative() { (true, -*f) } else { (false, *f) }; @@ -173,7 +173,7 @@ impl Value { bin.push( d as u8 ); d = d.fract(); for _ in 0..15 { - d = d*256.0; + d *= 256.0; bin.push( d as u8 ); d = d.fract(); if d == 0.0 { @@ -191,7 +191,7 @@ impl Value { } }, Self::Nothing => { - let bb = ['n' as u8]; + let bb = [b'n']; w.write_all( &bb )?; }, } @@ -218,11 +218,8 @@ impl Value { } 'l' => { let mut body = Vec::::new(); - loop { - match Value::read_parse(r)? { - ValueParse::Value(v) => body.push(v), - ValueParse::EndMarker => break, - } + while let ValueParse::Value(v) = Value::read_parse(r)? { + body.push(v); } Ok(ValueParse::Value(Value::List(body))) } @@ -263,27 +260,20 @@ impl Value { 'i' => Ok(ValueParse::Value(Value::Float(f64::INFINITY))), 'Z' => Ok(ValueParse::Value(Value::Float(-0.0f64))), 'z' => Ok(ValueParse::Value(Value::Float(0.0f64))), - _ => { - return Err(Error::new(ErrorKind::InvalidData, "Unknown exceptional float subtype found.")); - } + _ =>Err(Error::new(ErrorKind::InvalidData, "Unknown exceptional float subtype found.")), } } 'd' => { let mut body = HashMap::new(); - loop { - let k = match Value::read_parse(r)? { - ValueParse::Value(v) => { - if let Value::ByteString(s) = v { - if let Ok(s) = String::from_utf8(s) { - s - } else { - return Err(Error::new(ErrorKind::InvalidData, "Keys must be utf8 compatbile strings.")); - } - } else { - return Err(Error::new(ErrorKind::InvalidData, "Non-string cannot be dictionary key.")); - } - }, - ValueParse::EndMarker => break, + while let ValueParse::Value(k) = Value::read_parse(r)? { + let k = if let Value::ByteString(s) = k { + if let Ok(s) = String::from_utf8(s) { + s + } else { + return Err(Error::new(ErrorKind::InvalidData, "Keys must be utf8 compatbile strings.")); + } + } else { + return Err(Error::new(ErrorKind::InvalidData, "Non-string cannot be dictionary key.")); }; let v = match Value::read_parse(r)? { ValueParse::Value(v) => v, @@ -358,7 +348,7 @@ impl Value { if r.len() != size as usize { return Err(Error::new(ErrorKind::InvalidData, "Packet buffer is the wrong length.")); } - Ok(Value::read( &mut Cursor::new( &r[5..] ) )?) + Value::read( &mut Cursor::new( &r[5..] ) ) } pub fn read_packet( r: &mut R ) -> Result { -- cgit v1.2.3