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:
Bin Liu 2022-09-15 15:41:57 +08:00
parent be22e8408d
commit a8a8a28a34

View File

@ -181,12 +181,10 @@ fn parse_bridge(mut ibs: Vec<InfoBridge>) -> Bridge {
} }
bridge bridge
} }
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Device {
attrs: Option<LinkAttrs>,
}
impl Link for Device { macro_rules! impl_network_dev {
($r_type: literal , $r_struct: ty) => {
impl Link for $r_struct {
fn attrs(&self) -> &LinkAttrs { fn attrs(&self) -> &LinkAttrs {
self.attrs.as_ref().unwrap() self.attrs.as_ref().unwrap()
} }
@ -194,106 +192,29 @@ impl Link for Device {
self.attrs = Some(attr); self.attrs = Some(attr);
} }
fn r#type(&self) -> &'static str { fn r#type(&self) -> &'static str {
"device" $r_type
} }
}
};
} }
#[derive(Debug, PartialEq, Eq, Clone, Default)] macro_rules! define_and_impl_network_dev {
pub struct Tuntap { ($r_type: literal , $r_struct: tt) => {
pub attrs: Option<LinkAttrs>, #[derive(Debug, PartialEq, Eq, Clone, Default)]
} pub struct $r_struct {
impl Link for Tuntap {
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>, attrs: Option<LinkAttrs>,
}
/// on create only impl_network_dev!($r_type, $r_struct);
pub peer_name: String, };
} }
impl Link for Veth { define_and_impl_network_dev!("device", Device);
fn attrs(&self) -> &LinkAttrs { define_and_impl_network_dev!("tuntap", Tuntap);
self.attrs.as_ref().unwrap() define_and_impl_network_dev!("veth", Veth);
} define_and_impl_network_dev!("ipvlan", IpVlan);
fn set_attrs(&mut self, attr: LinkAttrs) { define_and_impl_network_dev!("macvlan", MacVlan);
self.attrs = Some(attr); define_and_impl_network_dev!("vlan", Vlan);
}
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"
}
}