From 53493f4522d294d54038550e5335dd7b344f4ead Mon Sep 17 00:00:00 2001 From: Gris Ge Date: Mon, 20 Jan 2025 17:00:34 +0800 Subject: [PATCH] Fix cargo clippy Signed-off-by: Gris Ge --- src/address/attribute.rs | 2 +- src/lib.rs | 44 ++++++++++++++++++------------------ src/link/attribute.rs | 2 +- src/link/prop_list.rs | 2 +- src/link/sriov/broadcast.rs | 2 +- src/link/sriov/guid.rs | 4 +--- src/link/sriov/link_state.rs | 2 +- src/link/sriov/mac.rs | 4 +--- src/link/sriov/rate.rs | 4 +--- src/link/sriov/rss_query.rs | 2 +- src/link/sriov/spoofchk.rs | 2 +- src/link/sriov/trust.rs | 4 +--- src/link/sriov/tx_rate.rs | 2 +- src/link/sriov/vf_vlan.rs | 4 +--- src/link/sriov/vlan.rs | 4 +--- src/route/mpls.rs | 8 +++---- src/route/next_hops.rs | 4 ++-- src/route/realm.rs | 2 +- src/rule/attribute.rs | 2 +- src/tc/actions/action.rs | 7 +++--- src/tc/actions/mirror.rs | 4 +--- src/tc/actions/nat.rs | 2 +- src/tc/actions/tunnel_key.rs | 4 +--- src/tc/attribute.rs | 7 +++--- 24 files changed, 53 insertions(+), 71 deletions(-) diff --git a/src/address/attribute.rs b/src/address/attribute.rs index 3c72062d..e7b90dc1 100644 --- a/src/address/attribute.rs +++ b/src/address/attribute.rs @@ -60,7 +60,7 @@ impl Nla for AddressAttribute { IPV4_ADDR_LEN } } - Self::Label(ref string) => string.as_bytes().len() + 1, + Self::Label(ref string) => string.len() + 1, Self::Flags(_) => size_of::(), diff --git a/src/lib.rs b/src/lib.rs index 16a808bf..314dee9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,27 @@ // SPDX-License-Identifier: MIT +//! The `netlink-packet-route` crate is designed to abstract Netlink route +//! protocol(`rtnetlink`) packet into Rust data types. The goal of this crate is +//! saving netlink user from reading Kernel Netlink codes. +//! +//! This crate grouped Netlink route protocol into these modules: +//! * `link`: NIC interface, similar to to `ip link` command. +//! * `address`: IP address, similar to `ip address` command. +//! * `route`: Route, similar to `ip route` command. +//! * `rule`: Route rule, similar to `ip rule` command. +//! * `tc`: Traffic control, similar to `tc` command. +//! * `neighbour`: Neighbour, similar to `ip neighbour` command. +//! * `neighbour_table`: Neighbour table, similar to `ip ntable` command. +//! * `nsid`: Namespace, similar to `ip netns` command. +//! +//! At the top level of this crate, we also provide: +//! * [AddressFamily] +//! +//! Normally, you should use [`rtnetlink`][rtnetlink_url] instead of using this +//! crate directly. +//! +//! [rtnetlink_url]: https://docs.rs/rtnetlink + pub mod address; pub mod link; pub mod neighbour; @@ -42,28 +64,6 @@ pub use self::address_family_fallback::AddressFamily; pub use self::ip::IpProtocol; pub use self::message::{RouteNetlinkMessage, RouteNetlinkMessageBuffer}; -/// The `netlink-packet-route` crate is designed to abstract Netlink route -/// protocol(`rtnetlink`) packet into Rust data types. The goal of this crate is -/// saving netlink user from reading Kernel Netlink codes. -/// -/// This crate grouped Netlink route protocol into these modules: -/// * `link`: NIC interface, similar to to `ip link` command. -/// * `address`: IP address, similar to `ip address` command. -/// * `route`: Route, similar to `ip route` command. -/// * `rule`: Route rule, similar to `ip rule` command. -/// * `tc`: Traffic control, similar to `tc` command. -/// * `neighbour`: Neighbour, similar to `ip neighbour` command. -/// * `neighbour_table`: Neighbour table, similar to `ip ntable` command. -/// * `nsid`: Namespace, similar to `ip netns` command. -/// -/// At the top level of this crate, we also provide: -/// * [AddressFamily] -/// -/// Normally, you should use [`rtnetlink`][rtnetlink_url] instead of using this -/// crate directly. -/// -/// [rtnetlink_url]: https://docs.rs/rtnetlink - #[macro_use] extern crate netlink_packet_utils; diff --git a/src/link/attribute.rs b/src/link/attribute.rs index 0a2392ef..756742ff 100644 --- a/src/link/attribute.rs +++ b/src/link/attribute.rs @@ -188,7 +188,7 @@ impl Nla for LinkAttribute { Self::IfName(string) | Self::Qdisc(string) | Self::IfAlias(string) - | Self::PhysPortName(string) => string.as_bytes().len() + 1, + | Self::PhysPortName(string) => string.len() + 1, Self::Mode(_) | Self::Carrier(_) | Self::ProtoDown(_) => 1, diff --git a/src/link/prop_list.rs b/src/link/prop_list.rs index 7ba4061b..45fd74e0 100644 --- a/src/link/prop_list.rs +++ b/src/link/prop_list.rs @@ -22,7 +22,7 @@ impl Nla for Prop { fn value_len(&self) -> usize { use self::Prop::*; match self { - AltIfName(ref string) => string.as_bytes().len() + 1, + AltIfName(ref string) => string.len() + 1, Other(nla) => nla.value_len() } } diff --git a/src/link/sriov/broadcast.rs b/src/link/sriov/broadcast.rs index e23a1234..5cbc8d7d 100644 --- a/src/link/sriov/broadcast.rs +++ b/src/link/sriov/broadcast.rs @@ -26,7 +26,7 @@ buffer!(VfInfoBroadcastBuffer(VF_INFO_BROADCAST_LEN) { addr: (slice, 0..VF_INFO_BROADCAST_LEN), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> +impl + ?Sized> Parseable> for VfInfoBroadcast { fn parse(buf: &VfInfoBroadcastBuffer<&T>) -> Result { diff --git a/src/link/sriov/guid.rs b/src/link/sriov/guid.rs index a57af829..af54cd5d 100644 --- a/src/link/sriov/guid.rs +++ b/src/link/sriov/guid.rs @@ -22,9 +22,7 @@ buffer!(VfInfoGuidBuffer(VF_INFO_GUID_LEN) { guid: (u64, 4..12), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> - for VfInfoGuid -{ +impl + ?Sized> Parseable> for VfInfoGuid { fn parse(buf: &VfInfoGuidBuffer<&T>) -> Result { Ok(Self::new(buf.vf_id(), buf.guid())) } diff --git a/src/link/sriov/link_state.rs b/src/link/sriov/link_state.rs index baf3144d..f1b7418f 100644 --- a/src/link/sriov/link_state.rs +++ b/src/link/sriov/link_state.rs @@ -22,7 +22,7 @@ buffer!(VfInfoLinkStateBuffer(VF_INFO_LINK_STATE_LEN) { state: (u32, 4..8), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> +impl + ?Sized> Parseable> for VfInfoLinkState { fn parse(buf: &VfInfoLinkStateBuffer<&T>) -> Result { diff --git a/src/link/sriov/mac.rs b/src/link/sriov/mac.rs index 5be6dbce..a7ff77dc 100644 --- a/src/link/sriov/mac.rs +++ b/src/link/sriov/mac.rs @@ -33,9 +33,7 @@ buffer!(VfInfoMacBuffer(VF_INFO_MAC_LEN) { mac: (slice, 4..VF_INFO_MAC_LEN), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> - for VfInfoMac -{ +impl + ?Sized> Parseable> for VfInfoMac { fn parse(buf: &VfInfoMacBuffer<&T>) -> Result { Ok(Self::new(buf.vf_id(), buf.mac())) } diff --git a/src/link/sriov/rate.rs b/src/link/sriov/rate.rs index 111e482a..8618531d 100644 --- a/src/link/sriov/rate.rs +++ b/src/link/sriov/rate.rs @@ -28,9 +28,7 @@ buffer!(VfInfoRateBuffer(VF_INFO_RATE_LEN) { max_tx_rate: (u32, 8..12) }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> - for VfInfoRate -{ +impl + ?Sized> Parseable> for VfInfoRate { fn parse(buf: &VfInfoRateBuffer<&T>) -> Result { Ok(Self { vf_id: buf.vf_id(), diff --git a/src/link/sriov/rss_query.rs b/src/link/sriov/rss_query.rs index d595487c..e728ae69 100644 --- a/src/link/sriov/rss_query.rs +++ b/src/link/sriov/rss_query.rs @@ -22,7 +22,7 @@ buffer!(VfInfoRssQueryEnBuffer(VF_INFO_RSS_QUERY_EN_LEN) { setting: (u32, 4..8), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> +impl + ?Sized> Parseable> for VfInfoRssQueryEn { fn parse(buf: &VfInfoRssQueryEnBuffer<&T>) -> Result { diff --git a/src/link/sriov/spoofchk.rs b/src/link/sriov/spoofchk.rs index d45ae38f..a568c650 100644 --- a/src/link/sriov/spoofchk.rs +++ b/src/link/sriov/spoofchk.rs @@ -22,7 +22,7 @@ buffer!(VfInfoSpoofCheckBuffer(VF_INFO_SPOOFCHK_LEN) { setting: (u32, 4..8), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> +impl + ?Sized> Parseable> for VfInfoSpoofCheck { fn parse(buf: &VfInfoSpoofCheckBuffer<&T>) -> Result { diff --git a/src/link/sriov/trust.rs b/src/link/sriov/trust.rs index 57478d44..72a1f834 100644 --- a/src/link/sriov/trust.rs +++ b/src/link/sriov/trust.rs @@ -22,9 +22,7 @@ buffer!(VfInfoTrustBuffer(VF_INFO_TRUST_LEN) { setting: (u32, 4..8), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> - for VfInfoTrust -{ +impl + ?Sized> Parseable> for VfInfoTrust { fn parse(buf: &VfInfoTrustBuffer<&T>) -> Result { Ok(Self::new( buf.vf_id(), diff --git a/src/link/sriov/tx_rate.rs b/src/link/sriov/tx_rate.rs index 302f4851..09678e1a 100644 --- a/src/link/sriov/tx_rate.rs +++ b/src/link/sriov/tx_rate.rs @@ -22,7 +22,7 @@ buffer!(VfInfoTxRateBuffer(VF_INFO_TX_RATE_LEN) { rate: (u32, 4..8), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> +impl + ?Sized> Parseable> for VfInfoTxRate { fn parse(buf: &VfInfoTxRateBuffer<&T>) -> Result { diff --git a/src/link/sriov/vf_vlan.rs b/src/link/sriov/vf_vlan.rs index 62b3954b..5449c0ed 100644 --- a/src/link/sriov/vf_vlan.rs +++ b/src/link/sriov/vf_vlan.rs @@ -90,9 +90,7 @@ buffer!(VfVlanInfoBuffer(VF_VLAN_INFO_LEN) { protocol: (u16, 12..14), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> - for VfVlanInfo -{ +impl + ?Sized> Parseable> for VfVlanInfo { fn parse(buf: &VfVlanInfoBuffer<&T>) -> Result { Ok(Self { vf_id: buf.vf_id(), diff --git a/src/link/sriov/vlan.rs b/src/link/sriov/vlan.rs index e2f87d5a..316d7426 100644 --- a/src/link/sriov/vlan.rs +++ b/src/link/sriov/vlan.rs @@ -28,9 +28,7 @@ buffer!(VfInfoVlanBuffer(VF_INFO_VLAN_LEN) { qos: (u32, 8..12) }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> - for VfInfoVlan -{ +impl + ?Sized> Parseable> for VfInfoVlan { fn parse(buf: &VfInfoVlanBuffer<&T>) -> Result { Ok(Self { vf_id: buf.vf_id(), diff --git a/src/route/mpls.rs b/src/route/mpls.rs index 5c4b4ece..96f75153 100644 --- a/src/route/mpls.rs +++ b/src/route/mpls.rs @@ -138,10 +138,10 @@ impl From for MplsLabel { impl From for u32 { fn from(v: MplsLabel) -> u32 { - v.label << MPLS_LS_LABEL_SHIFT - | (v.traffic_class as u32) << MPLS_LS_TC_SHIFT - | (v.bottom_of_stack as u32) << MPLS_LS_S_SHIFT - | (v.ttl as u32) << MPLS_LS_TTL_SHIFT + (v.label << MPLS_LS_LABEL_SHIFT) + | ((v.traffic_class as u32) << MPLS_LS_TC_SHIFT) + | ((v.bottom_of_stack as u32) << MPLS_LS_S_SHIFT) + | ((v.ttl as u32) << MPLS_LS_TTL_SHIFT) } } diff --git a/src/route/next_hops.rs b/src/route/next_hops.rs index af23a8dd..1586586c 100644 --- a/src/route/next_hops.rs +++ b/src/route/next_hops.rs @@ -94,9 +94,9 @@ pub struct RouteNextHop { pub attributes: Vec, } -impl<'a, T: AsRef<[u8]>> +impl> ParseableParametrized< - RouteNextHopBuffer<&'a T>, + RouteNextHopBuffer<&T>, (AddressFamily, RouteType, RouteLwEnCapType), > for RouteNextHop { diff --git a/src/route/realm.rs b/src/route/realm.rs index 8b395ff5..ed167279 100644 --- a/src/route/realm.rs +++ b/src/route/realm.rs @@ -34,7 +34,7 @@ impl Emitable for RouteRealm { } fn emit(&self, buffer: &mut [u8]) { - let all = (self.source as u32) << 16 | self.destination as u32; + let all = ((self.source as u32) << 16) | self.destination as u32; buffer.copy_from_slice(&all.to_ne_bytes()); } } diff --git a/src/rule/attribute.rs b/src/rule/attribute.rs index e56e554d..2c38972c 100644 --- a/src/rule/attribute.rs +++ b/src/rule/attribute.rs @@ -80,7 +80,7 @@ impl Nla for RuleAttribute { Self::SourcePortRange(v) | Self::DestinationPortRange(v) => { v.buffer_len() } - Self::Iifname(s) | Self::Oifname(s) => s.as_bytes().len() + 1, + Self::Iifname(s) | Self::Oifname(s) => s.len() + 1, Self::Priority(_) | Self::FwMark(_) | Self::FwMask(_) diff --git a/src/tc/actions/action.rs b/src/tc/actions/action.rs index 2e061c82..c19e6229 100644 --- a/src/tc/actions/action.rs +++ b/src/tc/actions/action.rs @@ -192,9 +192,8 @@ impl Nla for TcActionAttribute { match self { Self::Cookie(bytes) => buffer.copy_from_slice(bytes.as_slice()), Self::Kind(string) => { - buffer[..string.as_bytes().len()] - .copy_from_slice(string.as_bytes()); - buffer[string.as_bytes().len()] = 0; + buffer[..string.len()].copy_from_slice(string.as_bytes()); + buffer[string.len()] = 0; } Self::Options(opt) => opt.as_slice().emit(buffer), Self::Index(value) | Self::InHwCount(value) => { @@ -580,7 +579,7 @@ buffer!(TcfBuffer(TC_TCF_BUF_LEN) { firstuse: (u64, 24..32), }); -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for Tcf { +impl + ?Sized> Parseable> for Tcf { fn parse(buf: &TcfBuffer<&T>) -> Result { Ok(Self { install: buf.install(), diff --git a/src/tc/actions/mirror.rs b/src/tc/actions/mirror.rs index cec6fde2..a43ce687 100644 --- a/src/tc/actions/mirror.rs +++ b/src/tc/actions/mirror.rs @@ -116,9 +116,7 @@ impl Emitable for TcMirror { } } -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> - for TcMirror -{ +impl + ?Sized> Parseable> for TcMirror { fn parse(buf: &TcMirrorBuffer<&T>) -> Result { Ok(Self { generic: TcActionGeneric::parse(&TcActionGenericBuffer::new( diff --git a/src/tc/actions/nat.rs b/src/tc/actions/nat.rs index 2f9a8b14..4c0d14e2 100644 --- a/src/tc/actions/nat.rs +++ b/src/tc/actions/nat.rs @@ -139,7 +139,7 @@ impl Emitable for TcNat { } } -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for TcNat { +impl + ?Sized> Parseable> for TcNat { fn parse(buf: &TcNatBuffer<&T>) -> Result { Ok(Self { generic: TcActionGeneric::parse(&TcActionGenericBuffer::new( diff --git a/src/tc/actions/tunnel_key.rs b/src/tc/actions/tunnel_key.rs index 1a2a2569..706ace64 100755 --- a/src/tc/actions/tunnel_key.rs +++ b/src/tc/actions/tunnel_key.rs @@ -189,9 +189,7 @@ impl Emitable for TcTunnelKey { } } -impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> - for TcTunnelKey -{ +impl + ?Sized> Parseable> for TcTunnelKey { fn parse(buf: &TcTunnelKeyBuffer<&T>) -> Result { Ok(Self { generic: TcActionGeneric::parse(&TcActionGenericBuffer::new( diff --git a/src/tc/attribute.rs b/src/tc/attribute.rs index 992ec880..01a0b577 100644 --- a/src/tc/attribute.rs +++ b/src/tc/attribute.rs @@ -60,7 +60,7 @@ impl Nla for TcAttribute { Self::HwOffload(_) => 1, Self::Stats2(ref v) => v.as_slice().buffer_len(), Self::Stats(ref v) => v.buffer_len(), - Self::Kind(ref string) => string.as_bytes().len() + 1, + Self::Kind(ref string) => string.len() + 1, Self::Options(ref opt) => opt.as_slice().buffer_len(), Self::DumpInvisible(_) => 0, // The existence of NLA means true Self::Other(ref attr) => attr.value_len(), @@ -78,9 +78,8 @@ impl Nla for TcAttribute { Self::Stats2(ref stats) => stats.as_slice().emit(buffer), Self::Stats(ref stats) => stats.emit(buffer), Self::Kind(ref string) => { - buffer[..string.as_bytes().len()] - .copy_from_slice(string.as_bytes()); - buffer[string.as_bytes().len()] = 0; + buffer[..string.len()].copy_from_slice(string.as_bytes()); + buffer[string.len()] = 0; } Self::Options(ref opt) => opt.as_slice().emit(buffer), Self::DumpInvisible(_) => (),