Skip to content

Commit

Permalink
Loosen trait bounds on AfiSafiNlri / Route<N> / RouteWorkshop
Browse files Browse the repository at this point in the history
  • Loading branch information
DRiKE committed Apr 4, 2024
1 parent ac2008d commit fea1290
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 64 deletions.
5 changes: 2 additions & 3 deletions src/bgp/message/update_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ where
) -> UpdateBuilder<Vec<u8>, A> {

let mut res = Self::from_attributes_builder(ws.attributes().clone());
let nlri = ws.nlri();
let _ = res.add_announcement(nlri.clone());
let _ = res.add_announcement(ws.into_nlri());

res
}
Expand Down Expand Up @@ -679,7 +678,7 @@ pub struct PduIterator<'a, Target, A> {

impl<'a, Target, A> Iterator for PduIterator<'a, Target, A>
where
A: AfiSafiNlri + NlriCompose,
A: AfiSafiNlri + NlriCompose + Clone,
Target: Clone + OctetsBuilder + FreezeBuilder + AsMut<[u8]> + octseq::Truncate,
<Target as FreezeBuilder>::Octets: Octets
{
Expand Down
105 changes: 52 additions & 53 deletions src/bgp/nlri/afisafi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ paste! {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Hash)]
pub struct [<$nlri AddpathNlri>]$(<$gen>)?(PathId, [<$nlri Nlri>]$(<$gen>)?);
impl$(<$gen: Clone + Debug + Hash>)? AfiSafiNlri for [<$nlri AddpathNlri>]$(<$gen>)? {
impl$(<$gen>)? AfiSafiNlri for [<$nlri AddpathNlri>]$(<$gen>)? {
type Nlri = <[<$nlri Nlri>]$(<$gen>)? as AfiSafiNlri>::Nlri;
fn nlri(&self) -> Self::Nlri {
self.1.nlri()
fn nlri(&self) -> &Self::Nlri {
&self.1.nlri()
}
}

Expand All @@ -69,7 +69,7 @@ paste! {

impl$(<$gen>)? NlriCompose for [<$nlri AddpathNlri>]$(<$gen>)?
$(where
$gen: AsRef<[u8]> + Clone + Debug + Hash
$gen: AsRef<[u8]>
)?
{
fn compose<Target: OctetsBuilder>(&self, target: &mut Target)
Expand All @@ -90,7 +90,7 @@ paste! {
}
}

impl$(<$gen: Clone + Debug + Hash>)? Addpath for [<$nlri AddpathNlri>]$(<$gen>)? {
impl$(<$gen>)? Addpath for [<$nlri AddpathNlri>]$(<$gen>)? {
fn path_id(&self) -> PathId {
self.0
}
Expand Down Expand Up @@ -248,7 +248,7 @@ paste! {

impl<Octs> Nlri<Octs>
where
Octs: AsRef<[u8]> + Clone + Debug + Hash
Octs: AsRef<[u8]>
{

pub fn compose<Target: OctetsBuilder>(&self, target: &mut Target)
Expand Down Expand Up @@ -523,9 +523,9 @@ pub trait IsNlri {
}

/// A type representing an NLRI for a certain AFI+SAFI.
pub trait AfiSafiNlri: AfiSafi + IsNlri + Clone + Hash + Debug {
pub trait AfiSafiNlri: AfiSafi + IsNlri { // + Clone + Hash + Debug {
type Nlri;
fn nlri(&self) -> Self::Nlri;
fn nlri(&self) -> &Self::Nlri;

// TODO
//fn nexthop_compatible(&self, nh: &super::nexthop::NextHop) -> bool;
Expand Down Expand Up @@ -568,12 +568,12 @@ macro_rules! is_prefix {
($nlri:ident) => { paste! {
impl IsPrefix for [<$nlri Nlri>] {
fn prefix(&self) -> Prefix {
self.nlri().into()
(*self.nlri()).into()
}
}
impl IsPrefix for [<$nlri AddpathNlri>] {
fn prefix(&self) -> Prefix {
self.nlri().into()
(*self.nlri()).into()
}
fn path_id(&self) -> Option<PathId> {
Some(<Self as Addpath>::path_id(&self))
Expand Down Expand Up @@ -635,8 +635,8 @@ pub struct Ipv4UnicastNlri(Prefix);

impl AfiSafiNlri for Ipv4UnicastNlri {
type Nlri = Prefix;
fn nlri(&self) -> Self::Nlri {
self.0
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand Down Expand Up @@ -713,8 +713,8 @@ pub struct Ipv4MulticastNlri(Prefix);

impl AfiSafiNlri for Ipv4MulticastNlri {
type Nlri = Prefix;
fn nlri(&self) -> Self::Nlri {
self.0
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand Down Expand Up @@ -789,10 +789,10 @@ impl fmt::Display for Ipv4MulticastNlri {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Ipv4MplsUnicastNlri<Octs>(MplsNlri<Octs>);

impl<Octs: Clone + Debug + Hash> AfiSafiNlri for Ipv4MplsUnicastNlri<Octs> {
impl<Octs> AfiSafiNlri for Ipv4MplsUnicastNlri<Octs> {
type Nlri = MplsNlri<Octs>;
fn nlri(&self) -> Self::Nlri {
self.0.clone()
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand All @@ -814,7 +814,7 @@ where
}
}

impl<Octs: AsRef<[u8]> + Clone + Debug + Hash> NlriCompose for Ipv4MplsUnicastNlri<Octs> {
impl<Octs: AsRef<[u8]>> NlriCompose for Ipv4MplsUnicastNlri<Octs> {
fn compose_len(&self) -> usize {
// 1 byte for the length itself
1 + self.nlri().compose_len()
Expand Down Expand Up @@ -856,10 +856,10 @@ impl<T> fmt::Display for Ipv4MplsUnicastNlri<T> {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Ipv4MplsVpnUnicastNlri<Octs>(MplsVpnNlri<Octs>);

impl<Octs: Clone + Debug + Hash> AfiSafiNlri for Ipv4MplsVpnUnicastNlri<Octs> {
impl<Octs> AfiSafiNlri for Ipv4MplsVpnUnicastNlri<Octs> {
type Nlri = MplsVpnNlri<Octs>;
fn nlri(&self) -> Self::Nlri {
self.0.clone()
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand All @@ -880,7 +880,7 @@ where
}
}

impl<Octs: AsRef<[u8]> + Clone + Debug + Hash> NlriCompose for Ipv4MplsVpnUnicastNlri<Octs> {
impl<Octs: AsRef<[u8]>> NlriCompose for Ipv4MplsVpnUnicastNlri<Octs> {
fn compose_len(&self) -> usize {
1 + self.nlri().compose_len()
}
Expand Down Expand Up @@ -917,15 +917,14 @@ impl<T> fmt::Display for Ipv4MplsVpnUnicastNlri<T> {

//--- Ipv4RouteTarget


#[derive(Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Ipv4RouteTargetNlri<Octs>(RouteTargetNlri<Octs>);

impl<Octs: Clone + Debug + Hash> AfiSafiNlri for Ipv4RouteTargetNlri<Octs> {
impl<Octs> AfiSafiNlri for Ipv4RouteTargetNlri<Octs> {
type Nlri = RouteTargetNlri<Octs>;
fn nlri(&self) -> Self::Nlri {
self.0.clone()
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand All @@ -944,7 +943,7 @@ where
}
}

impl<Octs: AsRef<[u8]> + Clone + Debug + Hash> NlriCompose for Ipv4RouteTargetNlri<Octs> {
impl<Octs: AsRef<[u8]>> NlriCompose for Ipv4RouteTargetNlri<Octs> {
fn compose_len(&self) -> usize {
self.nlri().compose_len()
}
Expand Down Expand Up @@ -985,10 +984,10 @@ impl<T> fmt::Display for Ipv4RouteTargetNlri<T> {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Ipv4FlowSpecNlri<Octs>(FlowSpecNlri<Octs>);

impl<Octs: Clone + Debug + Hash> AfiSafiNlri for Ipv4FlowSpecNlri<Octs> {
impl<Octs> AfiSafiNlri for Ipv4FlowSpecNlri<Octs> {
type Nlri = FlowSpecNlri<Octs>;
fn nlri(&self) -> Self::Nlri {
self.0.clone()
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand All @@ -1007,7 +1006,7 @@ where
}
}

impl<Octs: AsRef<[u8]> + Clone + Debug + Hash> NlriCompose for Ipv4FlowSpecNlri<Octs> {
impl<Octs: AsRef<[u8]>> NlriCompose for Ipv4FlowSpecNlri<Octs> {
fn compose_len(&self) -> usize {
// for lenghts of >= 240, the lenght is described in two bytes
let len = self.nlri().compose_len();
Expand Down Expand Up @@ -1064,8 +1063,8 @@ impl<T> fmt::Display for Ipv4FlowSpecNlri<T> {
pub struct Ipv6UnicastNlri(Prefix);
impl AfiSafiNlri for Ipv6UnicastNlri {
type Nlri = Prefix;
fn nlri(&self) -> Self::Nlri {
self.0
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand Down Expand Up @@ -1169,8 +1168,8 @@ impl TryFrom<(Prefix, PathId)> for Ipv6MulticastAddpathNlri {

impl AfiSafiNlri for Ipv6MulticastNlri {
type Nlri = Prefix;
fn nlri(&self) -> Self::Nlri {
self.0
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand Down Expand Up @@ -1219,10 +1218,10 @@ impl fmt::Display for Ipv6MulticastNlri {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Ipv6MplsUnicastNlri<Octs>(MplsNlri<Octs>);

impl<Octs: Clone + Debug + Hash> AfiSafiNlri for Ipv6MplsUnicastNlri<Octs> {
impl<Octs> AfiSafiNlri for Ipv6MplsUnicastNlri<Octs> {
type Nlri = MplsNlri<Octs>;
fn nlri(&self) -> Self::Nlri {
self.0.clone()
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand All @@ -1244,7 +1243,7 @@ where
}
}

impl<Octs: AsRef<[u8]> + Clone + Debug + Hash> NlriCompose for Ipv6MplsUnicastNlri<Octs> {
impl<Octs: AsRef<[u8]>> NlriCompose for Ipv6MplsUnicastNlri<Octs> {
fn compose_len(&self) -> usize {
// 1 byte for the length itself
1 + self.nlri().labels().len() +
Expand Down Expand Up @@ -1287,10 +1286,10 @@ impl<T> fmt::Display for Ipv6MplsUnicastNlri<T> {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Ipv6MplsVpnUnicastNlri<Octs>(MplsVpnNlri<Octs>);

impl<Octs: Clone + Debug + Hash> AfiSafiNlri for Ipv6MplsVpnUnicastNlri<Octs> {
impl<Octs> AfiSafiNlri for Ipv6MplsVpnUnicastNlri<Octs> {
type Nlri = MplsVpnNlri<Octs>;
fn nlri(&self) -> Self::Nlri {
self.0.clone()
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand All @@ -1311,7 +1310,7 @@ where
}
}

impl<Octs: AsRef<[u8]> + Clone + Debug + Hash> NlriCompose for Ipv6MplsVpnUnicastNlri<Octs> {
impl<Octs: AsRef<[u8]>> NlriCompose for Ipv6MplsVpnUnicastNlri<Octs> {
fn compose_len(&self) -> usize {
1 + self.nlri().compose_len()
}
Expand Down Expand Up @@ -1354,10 +1353,10 @@ impl<T> fmt::Display for Ipv6MplsVpnUnicastNlri<T> {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Ipv6FlowSpecNlri<Octs>(FlowSpecNlri<Octs>);

impl<Octs: Clone + Debug + Hash> AfiSafiNlri for Ipv6FlowSpecNlri<Octs> {
impl<Octs> AfiSafiNlri for Ipv6FlowSpecNlri<Octs> {
type Nlri = FlowSpecNlri<Octs>;
fn nlri(&self) -> Self::Nlri {
self.0.clone()
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand All @@ -1376,7 +1375,7 @@ where
}
}

impl<Octs: AsRef<[u8]> + Clone + Debug + Hash> NlriCompose for Ipv6FlowSpecNlri<Octs> {
impl<Octs: AsRef<[u8]>> NlriCompose for Ipv6FlowSpecNlri<Octs> {
fn compose_len(&self) -> usize {
// for lenghts of >= 240, the lenght is described in two bytes
let len = self.nlri().compose_len();
Expand Down Expand Up @@ -1456,8 +1455,8 @@ pub struct L2VpnVplsNlri(VplsNlri);

impl AfiSafiNlri for L2VpnVplsNlri {
type Nlri = VplsNlri;
fn nlri(&self) -> Self::Nlri {
self.0
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand Down Expand Up @@ -1505,10 +1504,10 @@ impl fmt::Display for L2VpnVplsNlri {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct L2VpnEvpnNlri<Octs>(EvpnNlri<Octs>);

impl<Octs: Clone + Debug + Hash> AfiSafiNlri for L2VpnEvpnNlri<Octs> {
impl<Octs> AfiSafiNlri for L2VpnEvpnNlri<Octs> {
type Nlri = EvpnNlri<Octs>;
fn nlri(&self) -> Self::Nlri {
self.0.clone()
fn nlri(&self) -> &Self::Nlri {
&self.0
}
}

Expand All @@ -1527,7 +1526,7 @@ where
}
}

impl<Octs: AsRef<[u8]> + Clone + Debug + Hash> NlriCompose for L2VpnEvpnNlri<Octs> {
impl<Octs: AsRef<[u8]>> NlriCompose for L2VpnEvpnNlri<Octs> {
fn compose_len(&self) -> usize {
// EPVN uses typed NLRI with embedded the length field
self.nlri().compose_len()
Expand Down
23 changes: 16 additions & 7 deletions src/bgp/workshop/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub enum TypedRoute<N: Clone + Debug + Hash> {

#[derive(Debug, Eq, PartialEq, Clone, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Route<N: Clone + Debug + Hash>(N, PaMap);
pub struct Route<N>(N, PaMap);

impl<N: Clone + Debug + Hash> Route<N> {
impl<N> Route<N> {
pub fn new(nlri: N, attrs: PaMap) -> Self {
Self(nlri, attrs)
}
Expand Down Expand Up @@ -87,7 +87,7 @@ impl From<crate::bgp::aspath::AsPath<Vec<u8>>> for PathAttribute {
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct RouteWorkshop<N>(N, Option<NextHop>, PaMap);

impl<N: AfiSafiNlri + Clone + Debug + Hash> RouteWorkshop<N> {
impl<N: AfiSafiNlri> RouteWorkshop<N> {

/// Creates an empty RouteWorkshop.
///
Expand Down Expand Up @@ -165,6 +165,10 @@ impl<N: AfiSafiNlri + Clone + Debug + Hash> RouteWorkshop<N> {
&self.0
}

pub fn into_nlri(self) -> N {
self.0
}

pub fn nexthop(&self) -> &Option<NextHop> {
&self.1
}
Expand All @@ -186,10 +190,6 @@ impl<N: AfiSafiNlri + Clone + Debug + Hash> RouteWorkshop<N> {
self.2.get::<A>().or_else(|| A::retrieve(&self.2))
}

pub fn clone_into_route(&self) -> Route<N> {
Route::<N>(self.0.clone(), self.2.clone())
}

pub fn into_route(self) -> Route<N> {
Route::<N>(self.0, self.2)
}
Expand All @@ -207,6 +207,15 @@ impl<N: AfiSafiNlri + Clone + Debug + Hash> RouteWorkshop<N> {
}
}

impl<N: AfiSafiNlri + Clone > RouteWorkshop<N> {
pub fn clone_into_route(&self) -> Route<N> {
Route::<N>(self.0.clone(), self.2.clone())
}
}




macro_rules! impl_workshop {
(
$( $attr:ty )+
Expand Down
2 changes: 1 addition & 1 deletion src/bmp/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,7 @@ mod tests {
let mut nlris = bgp_update.announcements().unwrap();
if let Some(Ok(Nlri::Ipv4Unicast(n1))) = nlris.next() {
assert_eq!(
n1.nlri(),
*n1.nlri(),
Prefix::from_str("10.10.10.2/32").unwrap()
);
} else {
Expand Down

0 comments on commit fea1290

Please sign in to comment.