Skip to content

Commit 2790c8a

Browse files
committed
Run cargo clippy and cargo fmt
Signed-off-by: Gris Ge <[email protected]>
1 parent dbfce74 commit 2790c8a

9 files changed

+137
-60
lines changed

.rustfmt.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
max_width = 80
2+
wrap_comments = true
3+
reorder_imports = true
4+
edition = "2021"

examples/audit_netlink_events.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
use futures::stream::StreamExt;
2525
use netlink_packet_audit::{AuditMessage, StatusMessage};
26-
use netlink_packet_core::{NetlinkMessage, NetlinkPayload, NLM_F_ACK, NLM_F_REQUEST};
26+
use netlink_packet_core::{
27+
NetlinkMessage, NetlinkPayload, NLM_F_ACK, NLM_F_REQUEST,
28+
};
2729
use std::process;
2830

2931
use netlink_proto::{
@@ -38,17 +40,19 @@ const AUDIT_STATUS_PID: u32 = 4;
3840
async fn main() -> Result<(), String> {
3941
// Create a netlink socket. Here:
4042
//
41-
// - `conn` is a `Connection` that has the netlink socket. It's a `Future` that
42-
// keeps polling the socket and must be spawned an the event loop.
43+
// - `conn` is a `Connection` that has the netlink socket. It's a `Future`
44+
// that keeps polling the socket and must be spawned an the event loop.
4345
//
4446
// - `handle` is a `Handle` to the `Connection`. We use it to send netlink
4547
// messages and receive responses to these messages.
4648
//
47-
// - `messages` is a channel receiver through which we receive messages that we
48-
// have not sollicated, ie that are not response to a request we made. In this
49-
// example, we'll receive the audit event through that channel.
49+
// - `messages` is a channel receiver through which we receive messages that
50+
// we have not sollicated, ie that are not response to a request we made.
51+
// In this example, we'll receive the audit event through that channel.
5052
let (conn, mut handle, mut messages) = new_connection(NETLINK_AUDIT)
51-
.map_err(|e| format!("Failed to create a new netlink connection: {}", e))?;
53+
.map_err(|e| {
54+
format!("Failed to create a new netlink connection: {e}")
55+
})?;
5256

5357
// Spawn the `Connection` so that it starts polling the netlink
5458
// socket in the background.
@@ -71,14 +75,14 @@ async fn main() -> Result<(), String> {
7175
let mut response = match handle.request(nl_msg, kernel_unicast) {
7276
Ok(response) => response,
7377
Err(e) => {
74-
eprintln!("{}", e);
78+
eprintln!("{e}");
7579
return;
7680
}
7781
};
7882

7983
while let Some(message) = response.next().await {
8084
if let NetlinkPayload::Error(err_message) = message.payload {
81-
eprintln!("Received an error message: {:?}", err_message);
85+
eprintln!("Received an error message: {err_message:?}");
8286
return;
8387
}
8488
}
@@ -88,9 +92,9 @@ async fn main() -> Result<(), String> {
8892
println!("Starting to print audit events... press ^C to interrupt");
8993
while let Some((message, _addr)) = messages.next().await {
9094
if let NetlinkPayload::Error(err_message) = message.payload {
91-
eprintln!("received an error message: {:?}", err_message);
95+
eprintln!("received an error message: {err_message:?}");
9296
} else {
93-
println!("{:?}", message);
97+
println!("{message:?}");
9498
}
9599
}
96100

examples/dump_links.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
use futures::StreamExt;
44
use netlink_packet_route::{
5-
LinkMessage, NetlinkHeader, NetlinkMessage, RtnlMessage, NLM_F_DUMP, NLM_F_REQUEST,
5+
LinkMessage, NetlinkHeader, NetlinkMessage, RtnlMessage, NLM_F_DUMP,
6+
NLM_F_REQUEST,
67
};
78
use netlink_proto::{
89
new_connection,
@@ -13,25 +14,29 @@ use netlink_proto::{
1314
async fn main() -> Result<(), String> {
1415
// Create the netlink socket. Here, we won't use the channel that
1516
// receives unsolicited messages.
16-
let (conn, mut handle, _) = new_connection(NETLINK_ROUTE)
17-
.map_err(|e| format!("Failed to create a new netlink connection: {}", e))?;
17+
let (conn, mut handle, _) = new_connection(NETLINK_ROUTE).map_err(|e| {
18+
format!("Failed to create a new netlink connection: {e}")
19+
})?;
1820

1921
// Spawn the `Connection` in the background
2022
tokio::spawn(conn);
2123

2224
// Create the netlink message that requests the links to be dumped
2325
let mut nl_hdr = NetlinkHeader::default();
2426
nl_hdr.flags = NLM_F_DUMP | NLM_F_REQUEST;
25-
let request = NetlinkMessage::new(nl_hdr, RtnlMessage::GetLink(LinkMessage::default()).into());
27+
let request = NetlinkMessage::new(
28+
nl_hdr,
29+
RtnlMessage::GetLink(LinkMessage::default()).into(),
30+
);
2631

2732
// Send the request
2833
let mut response = handle
2934
.request(request, SocketAddr::new(0, 0))
30-
.map_err(|e| format!("Failed to send request: {}", e))?;
35+
.map_err(|e| format!("Failed to send request: {e}"))?;
3136

3237
// Print all the messages received in response
3338
while let Some(packet) = response.next().await {
34-
println!("<<< {:?}", packet);
39+
println!("<<< {packet:?}");
3540
}
3641

3742
Ok(())

examples/dump_links_async.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
use futures::StreamExt;
44
use netlink_packet_route::{
5-
LinkMessage, NetlinkHeader, NetlinkMessage, RtnlMessage, NLM_F_DUMP, NLM_F_REQUEST,
5+
LinkMessage, NetlinkHeader, NetlinkMessage, RtnlMessage, NLM_F_DUMP,
6+
NLM_F_REQUEST,
67
};
78
use netlink_proto::{
89
new_connection,
@@ -13,8 +14,9 @@ use netlink_proto::{
1314
async fn main() -> Result<(), String> {
1415
// Create the netlink socket. Here, we won't use the channel that
1516
// receives unsolicited messages.
16-
let (conn, mut handle, _) = new_connection(NETLINK_ROUTE)
17-
.map_err(|e| format!("Failed to create a new netlink connection: {}", e))?;
17+
let (conn, mut handle, _) = new_connection(NETLINK_ROUTE).map_err(|e| {
18+
format!("Failed to create a new netlink connection: {}", e)
19+
})?;
1820

1921
// Spawn the `Connection` so that it starts polling the netlink
2022
// socket in the background.
@@ -23,7 +25,10 @@ async fn main() -> Result<(), String> {
2325
// Create the netlink message that requests the links to be dumped
2426
let mut nl_hdr = NetlinkHeader::default();
2527
nl_hdr.flags = NLM_F_DUMP | NLM_F_REQUEST;
26-
let request = NetlinkMessage::new(nl_hdr, RtnlMessage::GetLink(LinkMessage::default()).into());
28+
let request = NetlinkMessage::new(
29+
nl_hdr,
30+
RtnlMessage::GetLink(LinkMessage::default()).into(),
31+
);
2732

2833
// Send the request
2934
let mut response = handle

src/connection.rs

+30-11
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ where
4646

4747
/// Channel used to transmit to the ConnectionHandle the unsolicited
4848
/// messages received from the socket (multicast messages for instance).
49-
unsolicited_messages_tx: Option<UnboundedSender<(NetlinkMessage<T>, SocketAddr)>>,
49+
unsolicited_messages_tx:
50+
Option<UnboundedSender<(NetlinkMessage<T>, SocketAddr)>>,
5051

5152
socket_closed: bool,
5253
}
@@ -59,7 +60,10 @@ where
5960
{
6061
pub(crate) fn new(
6162
requests_rx: UnboundedReceiver<Request<T>>,
62-
unsolicited_messages_tx: UnboundedSender<(NetlinkMessage<T>, SocketAddr)>,
63+
unsolicited_messages_tx: UnboundedSender<(
64+
NetlinkMessage<T>,
65+
SocketAddr,
66+
)>,
6367
protocol: isize,
6468
) -> io::Result<Self> {
6569
let socket = S::new(protocol)?;
@@ -86,20 +90,25 @@ where
8690
let mut socket = Pin::new(socket);
8791

8892
while !protocol.outgoing_messages.is_empty() {
89-
trace!("found outgoing message to send checking if socket is ready");
90-
if let Poll::Ready(Err(e)) = Pin::as_mut(&mut socket).poll_ready(cx) {
93+
trace!(
94+
"found outgoing message to send checking if socket is ready"
95+
);
96+
if let Poll::Ready(Err(e)) = Pin::as_mut(&mut socket).poll_ready(cx)
97+
{
9198
// Sink errors are usually not recoverable. The socket
9299
// probably shut down.
93100
warn!("netlink socket shut down: {:?}", e);
94101
self.socket_closed = true;
95102
return;
96103
}
97104

98-
let (mut message, addr) = protocol.outgoing_messages.pop_front().unwrap();
105+
let (mut message, addr) =
106+
protocol.outgoing_messages.pop_front().unwrap();
99107
message.finalize();
100108

101109
trace!("sending outgoing message");
102-
if let Err(e) = Pin::as_mut(&mut socket).start_send((message, addr)) {
110+
if let Err(e) = Pin::as_mut(&mut socket).start_send((message, addr))
111+
{
103112
error!("failed to send message: {:?}", e);
104113
self.socket_closed = true;
105114
return;
@@ -147,7 +156,9 @@ where
147156
if let Some(mut stream) = self.requests_rx.as_mut() {
148157
loop {
149158
match Pin::new(&mut stream).poll_next(cx) {
150-
Poll::Ready(Some(request)) => self.protocol.request(request),
159+
Poll::Ready(Some(request)) => {
160+
self.protocol.request(request)
161+
}
151162
Poll::Ready(None) => break,
152163
Poll::Pending => return,
153164
}
@@ -159,7 +170,9 @@ where
159170

160171
pub fn forward_unsolicited_messages(&mut self) {
161172
if self.unsolicited_messages_tx.is_none() {
162-
while let Some((message, source)) = self.protocol.incoming_requests.pop_front() {
173+
while let Some((message, source)) =
174+
self.protocol.incoming_requests.pop_front()
175+
{
163176
warn!(
164177
"ignoring unsolicited message {:?} from {:?}",
165178
message, source
@@ -177,7 +190,9 @@ where
177190
..
178191
} = self;
179192

180-
while let Some((message, source)) = protocol.incoming_requests.pop_front() {
193+
while let Some((message, source)) =
194+
protocol.incoming_requests.pop_front()
195+
{
181196
if unsolicited_messages_tx
182197
.as_mut()
183198
.unwrap()
@@ -250,7 +265,9 @@ where
250265
}
251266

252267
pub fn should_shut_down(&self) -> bool {
253-
self.socket_closed || (self.unsolicited_messages_tx.is_none() && self.requests_rx.is_none())
268+
self.socket_closed
269+
|| (self.unsolicited_messages_tx.is_none()
270+
&& self.requests_rx.is_none())
254271
}
255272
}
256273

@@ -272,7 +289,9 @@ where
272289
debug!("forwarding unsolicited messages to the connection handle");
273290
pinned.forward_unsolicited_messages();
274291

275-
debug!("forwaring responses to previous requests to the connection handle");
292+
debug!(
293+
"forwaring responses to previous requests to the connection handle"
294+
);
276295
pinned.forward_responses();
277296

278297
debug!("handling requests");

src/framed.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use crate::{
1616
codecs::NetlinkMessageCodec,
1717
sys::{AsyncSocket, SocketAddr},
1818
};
19-
use netlink_packet_core::{NetlinkDeserializable, NetlinkMessage, NetlinkSerializable};
19+
use netlink_packet_core::{
20+
NetlinkDeserializable, NetlinkMessage, NetlinkSerializable,
21+
};
2022

2123
pub struct NetlinkFramed<T, S, C> {
2224
socket: S,
@@ -40,7 +42,10 @@ where
4042
{
4143
type Item = (NetlinkMessage<T>, SocketAddr);
4244

43-
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
45+
fn poll_next(
46+
self: Pin<&mut Self>,
47+
cx: &mut Context<'_>,
48+
) -> Poll<Option<Self::Item>> {
4449
let Self {
4550
ref mut socket,
4651
ref mut in_addr,
@@ -80,7 +85,10 @@ where
8085
{
8186
type Error = io::Error;
8287

83-
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
88+
fn poll_ready(
89+
self: Pin<&mut Self>,
90+
cx: &mut Context<'_>,
91+
) -> Poll<Result<(), Self::Error>> {
8492
if !self.flushed {
8593
match self.poll_flush(cx)? {
8694
Poll::Ready(()) => {}
@@ -105,7 +113,10 @@ where
105113
Ok(())
106114
}
107115

108-
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
116+
fn poll_flush(
117+
mut self: Pin<&mut Self>,
118+
cx: &mut Context<'_>,
119+
) -> Poll<Result<(), Self::Error>> {
109120
if self.flushed {
110121
return Poll::Ready(Ok(()));
111122
}
@@ -137,7 +148,10 @@ where
137148
Poll::Ready(res)
138149
}
139150

140-
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
151+
fn poll_close(
152+
self: Pin<&mut Self>,
153+
cx: &mut Context<'_>,
154+
) -> Poll<Result<(), Self::Error>> {
141155
ready!(self.poll_flush(cx))?;
142156
Poll::Ready(Ok(()))
143157
}

src/handle.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ where
4242
let (tx, rx) = unbounded::<NetlinkMessage<T>>();
4343
let request = Request::from((message, destination, tx));
4444
debug!("handle: forwarding new request to connection");
45-
UnboundedSender::unbounded_send(&self.requests_tx, request).map_err(|e| {
46-
// the channel is unbounded, so it can't be full. If this
47-
// failed, it means the Connection shut down.
48-
if e.is_full() {
49-
panic!("internal error: unbounded channel full?!");
50-
} else if e.is_disconnected() {
51-
Error::ConnectionClosed
52-
} else {
53-
panic!("unknown error: {:?}", e);
54-
}
55-
})?;
45+
UnboundedSender::unbounded_send(&self.requests_tx, request).map_err(
46+
|e| {
47+
// the channel is unbounded, so it can't be full. If this
48+
// failed, it means the Connection shut down.
49+
if e.is_full() {
50+
panic!("internal error: unbounded channel full?!");
51+
} else if e.is_disconnected() {
52+
Error::ConnectionClosed
53+
} else {
54+
panic!("unknown error: {:?}", e);
55+
}
56+
},
57+
)?;
5658
Ok(rx)
5759
}
5860

0 commit comments

Comments
 (0)