fix: change u8::MAX to std::u8::MAX

This commit is contained in:
xiaopengli89 2020-04-06 18:51:34 +08:00 committed by Ryan Leckey
parent 401ffd19e6
commit 771d423c6f

View File

@ -2,12 +2,12 @@ use bigdecimal::{BigDecimal, Signed};
use num_bigint::{BigInt, Sign}; use num_bigint::{BigInt, Sign};
use crate::decode::Decode; use crate::decode::Decode;
use crate::encode::{Encode}; use crate::encode::Encode;
use crate::types::Type;
use crate::mysql::protocol::TypeId;
use crate::mysql::{MySql, MySqlValue, MySqlTypeInfo, MySqlData};
use crate::Error;
use crate::io::Buf; use crate::io::Buf;
use crate::mysql::protocol::TypeId;
use crate::mysql::{MySql, MySqlData, MySqlTypeInfo, MySqlValue};
use crate::types::Type;
use crate::Error;
const SIGN_NEG: u8 = 0x2D; const SIGN_NEG: u8 = 0x2D;
const SCALE_START: u8 = 0x2E; const SCALE_START: u8 = 0x2E;
@ -22,7 +22,7 @@ impl Encode<MySql> for BigDecimal {
fn encode(&self, buf: &mut Vec<u8>) { fn encode(&self, buf: &mut Vec<u8>) {
let size = Encode::<MySql>::size_hint(self) - 1; let size = Encode::<MySql>::size_hint(self) - 1;
assert!(size <= u8::MAX as usize, "Too large size"); assert!(size <= std::u8::MAX as usize, "Too large size");
buf.push(size as u8); buf.push(size as u8);
@ -97,7 +97,7 @@ impl Decode<'_, MySql> for BigDecimal {
loop { loop {
if binary.len() < 1 { if binary.len() < 1 {
break break;
} }
let data = binary.get_u8()?; let data = binary.get_u8()?;
match data { match data {
@ -105,21 +105,29 @@ impl Decode<'_, MySql> for BigDecimal {
if !negative { if !negative {
negative = true; negative = true;
} else { } else {
return Err(Error::Decode(format!("Unexpected byte: {:X?}", data).into())); return Err(Error::Decode(
format!("Unexpected byte: {:X?}", data).into(),
));
}
} }
},
SCALE_START => { SCALE_START => {
if scale.is_none() { if scale.is_none() {
scale = Some(0); scale = Some(0);
} else { } else {
return Err(Error::Decode(format!("Unexpected byte: {:X?}", data).into())); return Err(Error::Decode(
format!("Unexpected byte: {:X?}", data).into(),
));
}
} }
},
0x30..=0x39 => { 0x30..=0x39 => {
scale = scale.map(|s| s + 1); scale = scale.map(|s| s + 1);
v.push(data - 0x30); v.push(data - 0x30);
}, }
_ => return Err(Error::Decode(format!("Unexpected byte: {:X?}", data).into())), _ => {
return Err(Error::Decode(
format!("Unexpected byte: {:X?}", data).into(),
))
}
} }
} }
@ -127,15 +135,14 @@ impl Decode<'_, MySql> for BigDecimal {
if negative { Sign::Minus } else { Sign::Plus }, if negative { Sign::Minus } else { Sign::Plus },
v.as_slice(), v.as_slice(),
10, 10,
).ok_or(Error::Decode("Can't convert to BigInt".into()))?; )
.ok_or(Error::Decode("Can't convert to BigInt".into()))?;
Ok(BigDecimal::new(r, scale.unwrap_or(0))) Ok(BigDecimal::new(r, scale.unwrap_or(0)))
}, }
MySqlData::Text(_) => { MySqlData::Text(_) => Err(Error::Decode(
Err(Error::Decode(
"`BigDecimal` can only be decoded from the binary protocol".into(), "`BigDecimal` can only be decoded from the binary protocol".into(),
)) )),
},
} }
} }
} }
@ -162,19 +169,25 @@ fn test_encode_decimal() {
fn test_decode_decimal() { fn test_decode_decimal() {
let buf: Vec<u8> = vec![0x05, 0x2D, 0x31, 0x2E, 0x30, 0x35]; let buf: Vec<u8> = vec![0x05, 0x2D, 0x31, 0x2E, 0x30, 0x35];
let v = BigDecimal::decode(MySqlValue::binary( let v = BigDecimal::decode(MySqlValue::binary(
MySqlTypeInfo::new(TypeId::NEWDECIMAL), buf.as_slice(), MySqlTypeInfo::new(TypeId::NEWDECIMAL),
)).unwrap(); buf.as_slice(),
))
.unwrap();
assert_eq!(v.to_string(), "-1.05"); assert_eq!(v.to_string(), "-1.05");
let buf: Vec<u8> = vec![0x04, 0x30, 0x2E, 0x30, 0x35]; let buf: Vec<u8> = vec![0x04, 0x30, 0x2E, 0x30, 0x35];
let v = BigDecimal::decode(MySqlValue::binary( let v = BigDecimal::decode(MySqlValue::binary(
MySqlTypeInfo::new(TypeId::NEWDECIMAL), buf.as_slice(), MySqlTypeInfo::new(TypeId::NEWDECIMAL),
)).unwrap(); buf.as_slice(),
))
.unwrap();
assert_eq!(v.to_string(), "0.05"); assert_eq!(v.to_string(), "0.05");
let buf: Vec<u8> = vec![0x06, 0x2D, 0x39, 0x30, 0x30, 0x30, 0x30]; let buf: Vec<u8> = vec![0x06, 0x2D, 0x39, 0x30, 0x30, 0x30, 0x30];
let v = BigDecimal::decode(MySqlValue::binary( let v = BigDecimal::decode(MySqlValue::binary(
MySqlTypeInfo::new(TypeId::NEWDECIMAL), buf.as_slice(), MySqlTypeInfo::new(TypeId::NEWDECIMAL),
)).unwrap(); buf.as_slice(),
))
.unwrap();
assert_eq!(v.to_string(), "-90000"); assert_eq!(v.to_string(), "-90000");
} }