mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-29 12:14:48 +00:00
runtime-rs/resource: use macro to reduce duplicated code
Some device types have the same definition, they can be implemented by macro to reduce code. And this commit also deleted the `peer_name` field of the structs that is never been used. Fixes: #5170 Signed-off-by: Bin Liu <bin@hyper.sh>
This commit is contained in:
parent
be22e8408d
commit
a8a8a28a34
@ -181,119 +181,40 @@ fn parse_bridge(mut ibs: Vec<InfoBridge>) -> Bridge {
|
|||||||
}
|
}
|
||||||
bridge
|
bridge
|
||||||
}
|
}
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
|
||||||
pub struct Device {
|
macro_rules! impl_network_dev {
|
||||||
attrs: Option<LinkAttrs>,
|
($r_type: literal , $r_struct: ty) => {
|
||||||
|
impl Link for $r_struct {
|
||||||
|
fn attrs(&self) -> &LinkAttrs {
|
||||||
|
self.attrs.as_ref().unwrap()
|
||||||
|
}
|
||||||
|
fn set_attrs(&mut self, attr: LinkAttrs) {
|
||||||
|
self.attrs = Some(attr);
|
||||||
|
}
|
||||||
|
fn r#type(&self) -> &'static str {
|
||||||
|
$r_type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Link for Device {
|
macro_rules! define_and_impl_network_dev {
|
||||||
fn attrs(&self) -> &LinkAttrs {
|
($r_type: literal , $r_struct: tt) => {
|
||||||
self.attrs.as_ref().unwrap()
|
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
||||||
}
|
pub struct $r_struct {
|
||||||
fn set_attrs(&mut self, attr: LinkAttrs) {
|
attrs: Option<LinkAttrs>,
|
||||||
self.attrs = Some(attr);
|
}
|
||||||
}
|
|
||||||
fn r#type(&self) -> &'static str {
|
impl_network_dev!($r_type, $r_struct);
|
||||||
"device"
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
define_and_impl_network_dev!("device", Device);
|
||||||
pub struct Tuntap {
|
define_and_impl_network_dev!("tuntap", Tuntap);
|
||||||
pub attrs: Option<LinkAttrs>,
|
define_and_impl_network_dev!("veth", Veth);
|
||||||
}
|
define_and_impl_network_dev!("ipvlan", IpVlan);
|
||||||
|
define_and_impl_network_dev!("macvlan", MacVlan);
|
||||||
impl Link for Tuntap {
|
define_and_impl_network_dev!("vlan", Vlan);
|
||||||
fn attrs(&self) -> &LinkAttrs {
|
|
||||||
self.attrs.as_ref().unwrap()
|
|
||||||
}
|
|
||||||
fn set_attrs(&mut self, attr: LinkAttrs) {
|
|
||||||
self.attrs = Some(attr);
|
|
||||||
}
|
|
||||||
fn r#type(&self) -> &'static str {
|
|
||||||
"tuntap"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
|
||||||
pub struct Veth {
|
|
||||||
attrs: Option<LinkAttrs>,
|
|
||||||
|
|
||||||
/// on create only
|
|
||||||
pub peer_name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Link for Veth {
|
|
||||||
fn attrs(&self) -> &LinkAttrs {
|
|
||||||
self.attrs.as_ref().unwrap()
|
|
||||||
}
|
|
||||||
fn set_attrs(&mut self, attr: LinkAttrs) {
|
|
||||||
self.attrs = Some(attr);
|
|
||||||
}
|
|
||||||
fn r#type(&self) -> &'static str {
|
|
||||||
"veth"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
|
||||||
pub struct IpVlan {
|
|
||||||
attrs: Option<LinkAttrs>,
|
|
||||||
|
|
||||||
/// on create only
|
|
||||||
pub peer_name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Link for IpVlan {
|
|
||||||
fn attrs(&self) -> &LinkAttrs {
|
|
||||||
self.attrs.as_ref().unwrap()
|
|
||||||
}
|
|
||||||
fn set_attrs(&mut self, attr: LinkAttrs) {
|
|
||||||
self.attrs = Some(attr);
|
|
||||||
}
|
|
||||||
fn r#type(&self) -> &'static str {
|
|
||||||
"ipvlan"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
|
||||||
pub struct MacVlan {
|
|
||||||
attrs: Option<LinkAttrs>,
|
|
||||||
|
|
||||||
/// on create only
|
|
||||||
pub peer_name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Link for MacVlan {
|
|
||||||
fn attrs(&self) -> &LinkAttrs {
|
|
||||||
self.attrs.as_ref().unwrap()
|
|
||||||
}
|
|
||||||
fn set_attrs(&mut self, attr: LinkAttrs) {
|
|
||||||
self.attrs = Some(attr)
|
|
||||||
}
|
|
||||||
fn r#type(&self) -> &'static str {
|
|
||||||
"macvlan"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
|
||||||
pub struct Vlan {
|
|
||||||
attrs: Option<LinkAttrs>,
|
|
||||||
|
|
||||||
/// on create only
|
|
||||||
pub peer_name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Link for Vlan {
|
|
||||||
fn attrs(&self) -> &LinkAttrs {
|
|
||||||
self.attrs.as_ref().unwrap()
|
|
||||||
}
|
|
||||||
fn set_attrs(&mut self, attr: LinkAttrs) {
|
|
||||||
self.attrs = Some(attr);
|
|
||||||
}
|
|
||||||
fn r#type(&self) -> &'static str {
|
|
||||||
"vlan"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
||||||
pub struct Bridge {
|
pub struct Bridge {
|
||||||
@ -303,14 +224,4 @@ pub struct Bridge {
|
|||||||
pub vlan_filtering: bool,
|
pub vlan_filtering: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Link for Bridge {
|
impl_network_dev!("bridge", Bridge);
|
||||||
fn attrs(&self) -> &LinkAttrs {
|
|
||||||
self.attrs.as_ref().unwrap()
|
|
||||||
}
|
|
||||||
fn set_attrs(&mut self, attr: LinkAttrs) {
|
|
||||||
self.attrs = Some(attr);
|
|
||||||
}
|
|
||||||
fn r#type(&self) -> &'static str {
|
|
||||||
"bridge"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user