Skip to content

Commit 32f3a1e

Browse files
committed
fix: clippy
Signed-off-by: Woshiluo Luo <[email protected]>
1 parent 2b09d12 commit 32f3a1e

File tree

9 files changed

+55
-64
lines changed

9 files changed

+55
-64
lines changed

examples/serialize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() {
2020
{
2121
let new_base = Base1 { hello: "added" };
2222
let patch = serde_device_tree::ser::patch::Patch::new("/base3", &new_base as _);
23-
let mut list = [patch];
23+
let list = [patch];
2424
let base = Base {
2525
hello: 0xdeedbeef,
2626
base1: Base1 {
@@ -29,7 +29,7 @@ fn main() {
2929
hello2: 0x11223344,
3030
base2: Base1 { hello: "Roger" },
3131
};
32-
serde_device_tree::ser::to_dtb(&base, &mut list, &mut buf1).unwrap();
32+
serde_device_tree::ser::to_dtb(&base, &list, &mut buf1).unwrap();
3333
}
3434
let mut file = std::fs::File::create("gen.dtb").unwrap();
3535
file.write_all(&buf1).unwrap();

src/de.rs

+19-17
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,25 @@ pub unsafe fn from_raw<'de, T>(ptr: *const u8) -> Result<T>
6868
where
6969
T: de::Deserialize<'de>,
7070
{
71-
// read header
72-
if (ptr as usize) & (ALIGN - 1) != 0 {
73-
return Err(Error::unaligned(ptr as usize));
74-
}
75-
let header = &*(ptr as *const Header);
76-
header.verify()?;
77-
78-
let total_size = u32::from_be(header.total_size);
79-
let raw_data_len = (total_size - HEADER_LEN) as usize;
80-
let ans_ptr = core::ptr::from_raw_parts(ptr, raw_data_len);
81-
let device_tree: &DeviceTree = &*ans_ptr;
82-
let tags = device_tree.tags();
83-
let mut d = Deserializer {
84-
tags: tags.peekable(),
85-
};
86-
let ret = T::deserialize(&mut d)?;
87-
Ok(ret)
71+
unsafe {
72+
// read header
73+
if (ptr as usize) & (ALIGN - 1) != 0 {
74+
return Err(Error::unaligned(ptr as usize));
75+
}
76+
let header = &*(ptr as *const Header);
77+
header.verify()?;
78+
79+
let total_size = u32::from_be(header.total_size);
80+
let raw_data_len = (total_size - HEADER_LEN) as usize;
81+
let ans_ptr = core::ptr::from_raw_parts(ptr, raw_data_len);
82+
let device_tree: &DeviceTree = &*ans_ptr;
83+
let tags = device_tree.tags();
84+
let mut d = Deserializer {
85+
tags: tags.peekable(),
86+
};
87+
let ret = T::deserialize(&mut d)?;
88+
Ok(ret)
89+
}
8890
}
8991

9092
#[derive(Debug)]

src/de_mut/node_seq.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'de> NodeSeqItem<'de> {
177177
#[cfg(test)]
178178
mod tests {
179179
use crate::buildin::{NodeSeq, Reg};
180-
use crate::{from_raw_mut, Dtb, DtbPtr};
180+
use crate::{Dtb, DtbPtr, from_raw_mut};
181181
use serde_derive::Deserialize;
182182

183183
const RAW_DEVICE_TREE: &[u8] = include_bytes!("../../examples/bl808.dtb");

src/ser/patch.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ impl<'se> Patch<'se> {
3131
if x == 0 {
3232
return "";
3333
}
34-
match self.name.split('/').nth(x) {
35-
Some(result) => result,
36-
None => "",
37-
}
34+
self.name.split('/').nth(x).unwrap_or_default()
3835
}
3936

4037
// I hope to impl serde::ser::Serializer, but erase_serialize's return value is different from
@@ -62,7 +59,7 @@ impl<'se> PatchList<'se> {
6259
if patch.matched_depth.get() == depth - 1 && patch.get_depth_path(depth) == name {
6360
patch.matched_depth.set(patch.matched_depth.get() + 1);
6461
if patch.get_depth() == depth {
65-
if let Some(_) = matched_patch {
62+
if matched_patch.is_some() {
6663
panic!("More than one replace data on a same path");
6764
}
6865
matched_patch = Some(patch);
@@ -82,7 +79,7 @@ impl<'se> PatchList<'se> {
8279

8380
pub fn add_list(&self, depth: usize) -> impl Iterator<Item = &'se Patch<'se>> + use<'se> {
8481
self.list.iter().filter(move |x| {
85-
x.matched_depth.get() == depth && x.get_depth() == depth + 1 && x.parsed.get() == false
82+
x.matched_depth.get() == depth && x.get_depth() == depth + 1 && !x.parsed.get()
8683
})
8784
}
8885
}

src/ser/pointer.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ impl<'se> Pointer<'se> {
2626

2727
#[inline(always)]
2828
pub fn write_to_offset_u32(&mut self, offset: usize, value: u32) {
29-
match self.data {
30-
Some(ref mut data) => {
31-
data[offset..offset + 4].copy_from_slice(&u32::to_be_bytes(value))
32-
}
33-
None => {}
29+
if let Some(ref mut data) = self.data {
30+
data[offset..offset + 4].copy_from_slice(&u32::to_be_bytes(value))
3431
}
3532
}
3633

@@ -50,30 +47,25 @@ impl<'se> Pointer<'se> {
5047

5148
#[inline(always)]
5249
pub fn step_by_u32(&mut self, value: u32) {
53-
match self.data {
54-
Some(ref mut data) => {
55-
data[self.offset..self.offset + 4].copy_from_slice(&u32::to_be_bytes(value))
56-
}
57-
None => {}
50+
if let Some(ref mut data) = self.data {
51+
data[self.offset..self.offset + 4].copy_from_slice(&u32::to_be_bytes(value))
5852
}
5953
self.step_by_len(4);
6054
}
6155

6256
#[inline(always)]
6357
pub fn step_by_u8(&mut self, value: u8) {
64-
match self.data {
65-
Some(ref mut data) => data[self.offset] = value,
66-
None => {}
58+
if let Some(ref mut data) = self.data {
59+
data[self.offset] = value
6760
}
6861
self.step_by_len(1);
6962
}
7063

7164
#[inline(always)]
7265
pub fn step_align(&mut self) {
7366
while self.offset % 4 != 0 {
74-
match self.data {
75-
Some(ref mut data) => data[self.offset] = 0,
76-
None => {}
67+
if let Some(ref mut data) = self.data {
68+
data[self.offset] = 0
7769
}
7870
self.offset += 1;
7971
}

src/ser/serializer.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ trait SerializeDynamicField<'se> {
4242
T: serde::ser::Serialize + ?Sized;
4343
}
4444

45-
impl<'a, 'se> SerializeDynamicField<'se> for &'a mut Serializer<'se> {
45+
impl<'se> SerializeDynamicField<'se> for &mut Serializer<'se> {
4646
fn serialize_dynamic_field<T>(&mut self, key: &'se str, value: &T) -> Result<(), Error>
4747
where
4848
T: serde::ser::Serialize + ?Sized,
@@ -56,7 +56,7 @@ impl<'a, 'se> SerializeDynamicField<'se> for &'a mut Serializer<'se> {
5656

5757
match matched_patch {
5858
Some(data) => {
59-
data.serialize(&mut **self);
59+
data.serialize(self);
6060
}
6161
None => {
6262
value.serialize(&mut **self)?;
@@ -89,7 +89,7 @@ impl<'a, 'se> SerializeDynamicField<'se> for &'a mut Serializer<'se> {
8989
}
9090
}
9191

92-
impl<'a, 'se> serde::ser::SerializeMap for &'a mut Serializer<'se> {
92+
impl serde::ser::SerializeMap for &mut Serializer<'_> {
9393
type Ok = ();
9494
type Error = Error;
9595

@@ -112,7 +112,7 @@ impl<'a, 'se> serde::ser::SerializeMap for &'a mut Serializer<'se> {
112112
}
113113
}
114114

115-
impl<'a, 'se> serde::ser::SerializeStruct for &'a mut Serializer<'se> {
115+
impl serde::ser::SerializeStruct for &mut Serializer<'_> {
116116
type Ok = ();
117117
type Error = Error;
118118

@@ -135,7 +135,7 @@ impl<'a, 'se> serde::ser::SerializeStruct for &'a mut Serializer<'se> {
135135
}
136136
}
137137

138-
impl<'a, 'se> serde::ser::SerializeStructVariant for &'a mut Serializer<'se> {
138+
impl serde::ser::SerializeStructVariant for &mut Serializer<'_> {
139139
type Ok = ();
140140
type Error = Error;
141141

@@ -151,7 +151,7 @@ impl<'a, 'se> serde::ser::SerializeStructVariant for &'a mut Serializer<'se> {
151151
}
152152
}
153153

154-
impl<'a, 'se> serde::ser::SerializeSeq for &'a mut Serializer<'se> {
154+
impl serde::ser::SerializeSeq for &mut Serializer<'_> {
155155
type Ok = ();
156156
type Error = Error;
157157
// TODO: make sure there are no node seq serialize over this function.
@@ -168,7 +168,7 @@ impl<'a, 'se> serde::ser::SerializeSeq for &'a mut Serializer<'se> {
168168
}
169169
}
170170

171-
impl<'a, 'se> serde::ser::SerializeTuple for &'a mut Serializer<'se> {
171+
impl serde::ser::SerializeTuple for &mut Serializer<'_> {
172172
type Ok = ();
173173
type Error = Error;
174174

@@ -185,7 +185,7 @@ impl<'a, 'se> serde::ser::SerializeTuple for &'a mut Serializer<'se> {
185185
}
186186
}
187187

188-
impl<'a, 'se> serde::ser::SerializeTupleVariant for &'a mut Serializer<'se> {
188+
impl serde::ser::SerializeTupleVariant for &mut Serializer<'_> {
189189
type Ok = ();
190190
type Error = Error;
191191

@@ -201,7 +201,7 @@ impl<'a, 'se> serde::ser::SerializeTupleVariant for &'a mut Serializer<'se> {
201201
}
202202
}
203203

204-
impl<'a, 'se> serde::ser::SerializeTupleStruct for &'a mut Serializer<'se> {
204+
impl serde::ser::SerializeTupleStruct for &mut Serializer<'_> {
205205
type Ok = ();
206206
type Error = Error;
207207

@@ -217,7 +217,7 @@ impl<'a, 'se> serde::ser::SerializeTupleStruct for &'a mut Serializer<'se> {
217217
}
218218
}
219219

220-
impl<'a, 'se> serde::ser::Serializer for &'a mut Serializer<'se> {
220+
impl serde::ser::Serializer for &mut Serializer<'_> {
221221
type Ok = ();
222222
type Error = Error;
223223
type SerializeSeq = Self;
@@ -417,7 +417,7 @@ mod tests {
417417

418418
{
419419
let base = Base { hello: 0xdeedbeef };
420-
crate::ser::to_dtb(&base, &mut [], &mut buf1).unwrap();
420+
crate::ser::to_dtb(&base, &[], &mut buf1).unwrap();
421421
}
422422
// TODO: check buf1 buf2
423423
}
@@ -439,7 +439,7 @@ mod tests {
439439
hello: 0xdeedbeef,
440440
base1: Base1 { hello: 0x10000001 },
441441
};
442-
crate::ser::to_dtb(&base, &mut [], &mut buf1).unwrap();
442+
crate::ser::to_dtb(&base, &[], &mut buf1).unwrap();
443443
}
444444
// TODO: check buf1 buf2
445445
// println!("{:x?} {:x?}", buf1, buf2);
@@ -465,7 +465,7 @@ mod tests {
465465
hello: "Hello, World!",
466466
},
467467
};
468-
crate::ser::to_dtb(&base, &mut [], &mut buf1).unwrap();
468+
crate::ser::to_dtb(&base, &[], &mut buf1).unwrap();
469469
}
470470
// TODO: check buf1 buf2
471471
// println!("{:x?} {:x?}", buf1, buf2);
@@ -539,7 +539,7 @@ mod tests {
539539
{
540540
let number = 0x55667788u32;
541541
let patch = crate::ser::patch::Patch::new("/hello", &number as _);
542-
let mut list = [patch];
542+
let list = [patch];
543543
let base = Base {
544544
hello: 0xdeedbeef,
545545
base1: Base1 {
@@ -548,7 +548,7 @@ mod tests {
548548
hello2: 0x11223344,
549549
base2: Base1 { hello: "Roger" },
550550
};
551-
crate::ser::to_dtb(&base, &mut list, &mut buf1).unwrap();
551+
crate::ser::to_dtb(&base, &list, &mut buf1).unwrap();
552552
}
553553
// TODO: check buf1 buf2
554554
// println!("{:x?} {:x?}", buf1, buf2);
@@ -574,7 +574,7 @@ mod tests {
574574
hello: "replacement",
575575
};
576576
let patch = crate::ser::patch::Patch::new("/hello", &new_base as _);
577-
let mut list = [patch];
577+
let list = [patch];
578578
let base = Base {
579579
hello: 0xdeedbeef,
580580
base1: Base1 {
@@ -583,7 +583,7 @@ mod tests {
583583
hello2: 0x11223344,
584584
base2: Base1 { hello: "Roger" },
585585
};
586-
crate::ser::to_dtb(&base, &mut list, &mut buf1).unwrap();
586+
crate::ser::to_dtb(&base, &list, &mut buf1).unwrap();
587587
}
588588
// TODO: check buf1 buf2
589589
// println!("{:x?} {:x?}", buf1, buf2);
@@ -607,7 +607,7 @@ mod tests {
607607
{
608608
let new_base = Base1 { hello: "added" };
609609
let patch = crate::ser::patch::Patch::new("/base3", &new_base as _);
610-
let mut list = [patch];
610+
let list = [patch];
611611
let base = Base {
612612
hello: 0xdeedbeef,
613613
base1: Base1 {
@@ -616,7 +616,7 @@ mod tests {
616616
hello2: 0x11223344,
617617
base2: Base1 { hello: "Roger" },
618618
};
619-
crate::ser::to_dtb(&base, &mut list, &mut buf1).unwrap();
619+
crate::ser::to_dtb(&base, &list, &mut buf1).unwrap();
620620
}
621621
// TODO: check buf1 buf2
622622
// println!("{:x?}", buf1);

src/ser/string_block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl<'se> StringBlock<'se> {
1111
/// Will panic when len > end
1212
/// TODO: show as error
1313
/// Return (Result String, End Offset)
14-
pub fn get_str_by_offset<'a>(&'a self, offset: usize) -> (&'a str, usize) {
14+
pub fn get_str_by_offset(&self, offset: usize) -> (&str, usize) {
1515
if offset > *self.end {
1616
panic!("invalid read");
1717
}

src/utils/chosen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'de> Node<'de> {
3838

3939
#[cfg(test)]
4040
mod tests {
41-
use crate::{buildin::Node, from_raw_mut, Dtb, DtbPtr};
41+
use crate::{Dtb, DtbPtr, buildin::Node, from_raw_mut};
4242

4343
const RAW_DEVICE_TREE: &[u8] = include_bytes!("../../examples/bl808.dtb");
4444
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();

tests/bl808.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// 在实际使用中,将这里的 `serde_derive::Deserialize` 改为 `serde::Deserialize`。
22
use serde_derive::Deserialize;
33

4-
use serde_device_tree::{buildin::NodeSeq, error::Error, from_raw_mut, Dtb, DtbPtr};
4+
use serde_device_tree::{Dtb, DtbPtr, buildin::NodeSeq, error::Error, from_raw_mut};
55

66
const RAW_DEVICE_TREE: &[u8] = include_bytes!("../examples/bl808.dtb");
77
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();

0 commit comments

Comments
 (0)