Skip to content

Commit 3deb05f

Browse files
committed
clippy
1 parent 4ef9e51 commit 3deb05f

File tree

5 files changed

+115
-77
lines changed

5 files changed

+115
-77
lines changed

src/packets.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,8 @@ impl HandshakeResponse {
10761076
UTF8_GENERAL_CI
10771077
};
10781078

1079-
if let Some(_) = db_name {
1080-
client_flags = client_flags | CapabilityFlags::CLIENT_CONNECT_WITH_DB;
1079+
if db_name.is_some() {
1080+
client_flags |= CapabilityFlags::CLIENT_CONNECT_WITH_DB;
10811081
}
10821082

10831083
let mut data = Vec::with_capacity(1024);
@@ -1323,7 +1323,7 @@ impl ComStmtExecuteRequestBuilder {
13231323
}
13241324

13251325
pub fn build(mut self, params: &[Value]) -> (Vec<u8>, bool) {
1326-
if params.len() > 0 {
1326+
if !params.is_empty() {
13271327
self.bitmap_len = NullBitmap::<ClientSide>::bitmap_len(params.len());
13281328
let meta_len = params.len() * 2;
13291329
let data_len: usize = params.iter().map(Value::bin_len).sum();

src/proto/codec/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub mod error;
2828

2929
/// Helper that transmutes `&mut [MaybeUninit<u8>]` to `&mut [u8]`.
3030
pub(crate) unsafe fn transmute_buf(buf: &mut [mem::MaybeUninit<u8>]) -> &mut [u8] {
31-
mem::transmute(buf)
31+
&mut *(buf as *mut [mem::MaybeUninit<u8>] as *mut [u8])
3232
}
3333

3434
/// Will split given `packet` to MySql packet chunks and write into `dst`.
@@ -165,7 +165,7 @@ impl ChunkDecoder {
165165
match NonZeroUsize::new(raw_chunk_len) {
166166
Some(chunk_len) => {
167167
if dst.len() + chunk_len.get() > max_allowed_packet {
168-
Err(PacketCodecError::PacketTooLarge)?
168+
return Err(PacketCodecError::PacketTooLarge);
169169
}
170170

171171
dst.reserve(chunk_len.get());
@@ -233,7 +233,7 @@ impl CompData {
233233
) -> Result<Option<Self>, PacketCodecError> {
234234
// max_allowed_packet will be an upper boundary
235235
if max(compressed_len, uncompressed_len) > max_allowed_packet {
236-
Err(PacketCodecError::PacketTooLarge)?
236+
return Err(PacketCodecError::PacketTooLarge);
237237
}
238238

239239
let compressed_len = NonZeroUsize::new(compressed_len);
@@ -427,7 +427,7 @@ impl PacketCodecInner {
427427
in_buf: BytesMut::with_capacity(DEFAULT_MAX_ALLOWED_PACKET),
428428
out_buf: BytesMut::with_capacity(DEFAULT_MAX_ALLOWED_PACKET),
429429
comp_decoder: CompDecoder::Idle,
430-
plain_codec: mem::replace(c, PlainPacketCodec::default()),
430+
plain_codec: mem::take(c),
431431
})
432432
}
433433
PacketCodecInner::Comp(c) => c.level = level,
@@ -496,7 +496,7 @@ impl PlainPacketCodec {
496496
match self.chunk_decoder.decode(src, dst, max_allowed_packet)? {
497497
Some(chunk_info) => {
498498
if self.seq_id != chunk_info.seq_id() {
499-
Err(PacketCodecError::PacketsOutOfSync)?
499+
return Err(PacketCodecError::PacketsOutOfSync);
500500
}
501501

502502
self.seq_id = self.seq_id.wrapping_add(1);
@@ -524,7 +524,7 @@ impl PlainPacketCodec {
524524
max_allowed_packet: usize,
525525
) -> Result<(), PacketCodecError> {
526526
if packet.len() > max_allowed_packet {
527-
Err(PacketCodecError::PacketTooLarge)?;
527+
return Err(PacketCodecError::PacketTooLarge);
528528
}
529529

530530
self.seq_id = packet_to_chunks(self.seq_id, &*packet, dst);
@@ -588,7 +588,7 @@ impl CompPacketCodec {
588588
{
589589
Some(chunk_info) => {
590590
if self.comp_seq_id != chunk_info.seq_id() {
591-
Err(PacketCodecError::PacketsOutOfSync)?
591+
return Err(PacketCodecError::PacketsOutOfSync);
592592
}
593593

594594
self.comp_seq_id = self.comp_seq_id.wrapping_add(1);

src/row/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Row {
105105
.idx(&*self.columns)
106106
.and_then(|idx| self.values.get(idx))
107107
.and_then(|x| x.as_ref())
108-
.and_then(|x| Some(from_value_opt::<T>(x.clone())))
108+
.map(|x| from_value_opt::<T>(x.clone()))
109109
}
110110

111111
/// Will take value of a column with index `index` if it exists and wasn't taken earlier then
@@ -135,7 +135,7 @@ impl Row {
135135
.idx(&*self.columns)
136136
.and_then(|idx| self.values.get_mut(idx))
137137
.and_then(|x| x.take())
138-
.and_then(|x| Some(from_value_opt::<T>(x)))
138+
.map(from_value_opt::<T>)
139139
}
140140

141141
/// Unwraps values of a row.

src/value/convert/mod.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,11 @@ impl ConvIr<Vec<u8>> for Vec<u8> {
468468
impl ConvIr<PrimitiveDateTime> for ParseIr<PrimitiveDateTime> {
469469
fn new(value: Value) -> Result<ParseIr<PrimitiveDateTime>, FromValueError> {
470470
match value {
471-
Value::Date(y, m, d, h, i, s, u) => Ok(ParseIr {
472-
value: Value::Date(y, m, d, h, i, s, u),
473-
output: match create_primitive_date_time(y, m, d, h, i, s, u) {
471+
Value::Date(year, month, day, hour, minute, second, micros) => Ok(ParseIr {
472+
value: Value::Date(year, month, day, hour, minute, second, micros),
473+
output: match create_primitive_date_time(
474+
year, month, day, hour, minute, second, micros,
475+
) {
474476
Some(datetime) => datetime,
475477
None => return Err(FromValueError(value)),
476478
},
@@ -497,9 +499,9 @@ impl ConvIr<PrimitiveDateTime> for ParseIr<PrimitiveDateTime> {
497499
impl ConvIr<Date> for ParseIr<Date> {
498500
fn new(value: Value) -> Result<ParseIr<Date>, FromValueError> {
499501
match value {
500-
Value::Date(y, m, d, h, i, s, u) => Ok(ParseIr {
501-
value: Value::Date(y, m, d, h, i, s, u),
502-
output: match Date::try_from_ymd(y as i32, m, d) {
502+
Value::Date(year, month, day, hour, minute, second, micros) => Ok(ParseIr {
503+
value: Value::Date(year, month, day, hour, minute, second, micros),
504+
output: match Date::try_from_ymd(year as i32, month, day) {
503505
Ok(date) => date,
504506
Err(_) => return Err(FromValueError(value)),
505507
},
@@ -561,16 +563,16 @@ impl ConvIr<Time> for ParseIr<Time> {
561563

562564
#[inline]
563565
fn create_primitive_date_time(
564-
y: u16,
565-
m: u8,
566-
d: u8,
567-
h: u8,
568-
i: u8,
569-
s: u8,
570-
u: u32,
566+
year: u16,
567+
month: u8,
568+
day: u8,
569+
hour: u8,
570+
minute: u8,
571+
second: u8,
572+
micros: u32,
571573
) -> Option<PrimitiveDateTime> {
572-
if let Ok(date) = Date::try_from_ymd(y as i32, m, d) {
573-
if let Ok(time) = Time::try_from_hms_micro(h, i, s, u) {
574+
if let Ok(date) = Date::try_from_ymd(year as i32, month, day) {
575+
if let Ok(time) = Time::try_from_hms_micro(hour, minute, second, micros) {
574576
return Some(PrimitiveDateTime::new(date, time));
575577
}
576578
}
@@ -595,15 +597,26 @@ fn parse_mysql_time_string_with_time(bytes: &[u8]) -> Result<Time, ParseError> {
595597
impl ConvIr<NaiveDateTime> for ParseIr<NaiveDateTime> {
596598
fn new(value: Value) -> Result<ParseIr<NaiveDateTime>, FromValueError> {
597599
let result = match value {
598-
Value::Date(y, m, d, h, i, s, u) => {
599-
let date = NaiveDate::from_ymd_opt(y.into(), m.into(), d.into());
600-
let time = NaiveTime::from_hms_micro_opt(h.into(), i.into(), s.into(), u);
601-
Ok((date, time, Value::Date(y, m, d, h, i, s, u)))
600+
Value::Date(year, month, day, hour, minute, second, micros) => {
601+
let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into());
602+
let time = NaiveTime::from_hms_micro_opt(
603+
hour.into(),
604+
minute.into(),
605+
second.into(),
606+
micros,
607+
);
608+
Ok((
609+
date,
610+
time,
611+
Value::Date(year, month, day, hour, minute, second, micros),
612+
))
602613
}
603614
Value::Bytes(bytes) => {
604-
if let Some((y, m, d, h, i, s, u)) = parse_mysql_datetime_string(&*bytes) {
605-
let date = NaiveDate::from_ymd_opt(y as i32, m, d);
606-
let time = NaiveTime::from_hms_micro_opt(h, i, s, u);
615+
if let Some((year, month, day, hour, minute, second, micros)) =
616+
parse_mysql_datetime_string(&*bytes)
617+
{
618+
let date = NaiveDate::from_ymd_opt(year as i32, month, day);
619+
let time = NaiveTime::from_hms_micro_opt(hour, minute, second, micros);
607620
Ok((date, time, Value::Bytes(bytes)))
608621
} else {
609622
Err(FromValueError(Value::Bytes(bytes)))
@@ -614,10 +627,10 @@ impl ConvIr<NaiveDateTime> for ParseIr<NaiveDateTime> {
614627

615628
let (date, time, value) = result?;
616629

617-
if date.is_some() && time.is_some() {
630+
if let (Some(date), Some(time)) = (date, time) {
618631
Ok(ParseIr {
619632
value,
620-
output: NaiveDateTime::new(date.unwrap(), time.unwrap()),
633+
output: NaiveDateTime::new(date, time),
621634
})
622635
} else {
623636
Err(FromValueError(value))
@@ -634,9 +647,12 @@ impl ConvIr<NaiveDateTime> for ParseIr<NaiveDateTime> {
634647
impl ConvIr<NaiveDate> for ParseIr<NaiveDate> {
635648
fn new(value: Value) -> Result<ParseIr<NaiveDate>, FromValueError> {
636649
let result = match value {
637-
Value::Date(y, m, d, h, i, s, u) => {
638-
let date = NaiveDate::from_ymd_opt(y.into(), m.into(), d.into());
639-
Ok((date, Value::Date(y, m, d, h, i, s, u)))
650+
Value::Date(year, month, day, hour, minute, second, micros) => {
651+
let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into());
652+
Ok((
653+
date,
654+
Value::Date(year, month, day, hour, minute, second, micros),
655+
))
640656
}
641657
Value::Bytes(bytes) => {
642658
if let Some((y, m, d, _, _, _, _)) = parse_mysql_datetime_string(&*bytes) {
@@ -651,11 +667,8 @@ impl ConvIr<NaiveDate> for ParseIr<NaiveDate> {
651667

652668
let (date, value) = result?;
653669

654-
if date.is_some() {
655-
Ok(ParseIr {
656-
value,
657-
output: date.unwrap(),
658-
})
670+
if let Some(output) = date {
671+
Ok(ParseIr { value, output })
659672
} else {
660673
Err(FromValueError(value))
661674
}
@@ -812,11 +825,8 @@ impl ConvIr<NaiveTime> for ParseIr<NaiveTime> {
812825

813826
let (time, value) = result?;
814827

815-
if time.is_some() {
816-
Ok(ParseIr {
817-
value,
818-
output: time.unwrap(),
819-
})
828+
if let Some(output) = time {
829+
Ok(ParseIr { value, output })
820830
} else {
821831
Err(FromValueError(value))
822832
}
@@ -1070,7 +1080,7 @@ impl From<u128> for Value {
10701080

10711081
impl From<f32> for Value {
10721082
fn from(x: f32) -> Value {
1073-
Value::Float(x.into())
1083+
Value::Float(x)
10741084
}
10751085
}
10761086

src/value/mod.rs

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,13 @@ impl Value {
154154
Value::Float(x) => format!("{}", x),
155155
Value::Double(x) => format!("{}", x),
156156
Value::Date(y, m, d, 0, 0, 0, 0) => format!("'{:04}-{:02}-{:02}'", y, m, d),
157-
Value::Date(y, m, d, h, i, s, 0) => {
158-
format!("'{:04}-{:02}-{:02} {:02}:{:02}:{:02}'", y, m, d, h, i, s)
159-
}
160-
Value::Date(y, m, d, h, i, s, u) => format!(
157+
Value::Date(year, month, day, hour, minute, second, 0) => format!(
158+
"'{:04}-{:02}-{:02} {:02}:{:02}:{:02}'",
159+
year, month, day, hour, minute, second
160+
),
161+
Value::Date(year, month, day, hour, minute, second, micros) => format!(
161162
"'{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:06}'",
162-
y, m, d, h, i, s, u
163+
year, month, day, hour, minute, second, micros
163164
),
164165
Value::Time(neg, d, h, i, s, 0) => {
165166
if neg {
@@ -168,11 +169,23 @@ impl Value {
168169
format!("'{:03}:{:02}:{:02}'", d * 24 + u32::from(h), i, s)
169170
}
170171
}
171-
Value::Time(neg, d, h, i, s, u) => {
172+
Value::Time(neg, days, hours, minutes, seconds, micros) => {
172173
if neg {
173-
format!("'-{:03}:{:02}:{:02}.{:06}'", d * 24 + u32::from(h), i, s, u)
174+
format!(
175+
"'-{:03}:{:02}:{:02}.{:06}'",
176+
days * 24 + u32::from(hours),
177+
minutes,
178+
seconds,
179+
micros
180+
)
174181
} else {
175-
format!("'{:03}:{:02}:{:02}.{:06}'", d * 24 + u32::from(h), i, s, u)
182+
format!(
183+
"'{:03}:{:02}:{:02}.{:06}'",
184+
days * 24 + u32::from(hours),
185+
minutes,
186+
seconds,
187+
micros
188+
)
176189
}
177190
}
178191
Value::Bytes(ref bytes) => match from_utf8(&*bytes) {
@@ -351,11 +364,11 @@ impl Value {
351364
}
352365

353366
impl fmt::Debug for Value {
354-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
367+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
355368
match *self {
356-
Value::NULL => f.debug_tuple("Null").finish(),
369+
Value::NULL => formatter.debug_tuple("Null").finish(),
357370
Value::Bytes(ref bytes) => {
358-
let mut debug = f.debug_tuple("Bytes");
371+
let mut debug = formatter.debug_tuple("Bytes");
359372
if bytes.len() <= 8 {
360373
debug
361374
.field(&String::from_utf8_lossy(&*bytes).replace("\n", "\\n"))
@@ -365,40 +378,55 @@ impl fmt::Debug for Value {
365378
debug.field(&format!("{}..", bytes)).finish()
366379
}
367380
}
368-
Value::Int(ref val) => f.debug_tuple("Int").field(val).finish(),
369-
Value::UInt(ref val) => f.debug_tuple("UInt").field(val).finish(),
370-
Value::Float(ref val) => f.debug_tuple("Float").field(val).finish(),
371-
Value::Double(ref val) => f.debug_tuple("Double").field(val).finish(),
381+
Value::Int(ref val) => formatter.debug_tuple("Int").field(val).finish(),
382+
Value::UInt(ref val) => formatter.debug_tuple("UInt").field(val).finish(),
383+
Value::Float(ref val) => formatter.debug_tuple("Float").field(val).finish(),
384+
Value::Double(ref val) => formatter.debug_tuple("Double").field(val).finish(),
372385
Value::Date(y, m, d, 0, 0, 0, 0) => {
373386
let format = format!("'{:04}-{:02}-{:02}'", y, m, d);
374-
f.debug_tuple("Date").field(&format).finish()
387+
formatter.debug_tuple("Date").field(&format).finish()
375388
}
376-
Value::Date(y, m, d, h, i, s, 0) => {
377-
let format = format!("'{:04}-{:02}-{:02} {:02}:{:02}:{:02}'", y, m, d, h, i, s);
378-
f.debug_tuple("Date").field(&format).finish()
389+
Value::Date(year, month, day, hour, minute, second, 0) => {
390+
let format = format!(
391+
"'{:04}-{:02}-{:02} {:02}:{:02}:{:02}'",
392+
year, month, day, hour, minute, second
393+
);
394+
formatter.debug_tuple("Date").field(&format).finish()
379395
}
380-
Value::Date(y, m, d, h, i, s, u) => {
396+
Value::Date(year, month, day, hour, minute, second, micros) => {
381397
let format = format!(
382398
"'{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:06}'",
383-
y, m, d, h, i, s, u
399+
year, month, day, hour, minute, second, micros
384400
);
385-
f.debug_tuple("Date").field(&format).finish()
401+
formatter.debug_tuple("Date").field(&format).finish()
386402
}
387403
Value::Time(neg, d, h, i, s, 0) => {
388404
let format = if neg {
389405
format!("'-{:03}:{:02}:{:02}'", d * 24 + u32::from(h), i, s)
390406
} else {
391407
format!("'{:03}:{:02}:{:02}'", d * 24 + u32::from(h), i, s)
392408
};
393-
f.debug_tuple("Time").field(&format).finish()
409+
formatter.debug_tuple("Time").field(&format).finish()
394410
}
395-
Value::Time(neg, d, h, i, s, u) => {
411+
Value::Time(neg, days, hours, minutes, seconds, micros) => {
396412
let format = if neg {
397-
format!("'-{:03}:{:02}:{:02}.{:06}'", d * 24 + u32::from(h), i, s, u)
413+
format!(
414+
"'-{:03}:{:02}:{:02}.{:06}'",
415+
days * 24 + u32::from(hours),
416+
minutes,
417+
seconds,
418+
micros
419+
)
398420
} else {
399-
format!("'{:03}:{:02}:{:02}.{:06}'", d * 24 + u32::from(h), i, s, u)
421+
format!(
422+
"'{:03}:{:02}:{:02}.{:06}'",
423+
days * 24 + u32::from(hours),
424+
minutes,
425+
seconds,
426+
micros
427+
)
400428
};
401-
f.debug_tuple("Time").field(&format).finish()
429+
formatter.debug_tuple("Time").field(&format).finish()
402430
}
403431
}
404432
}

0 commit comments

Comments
 (0)