Skip to content

Commit

Permalink
Replace bool_to_option code with if/else
Browse files Browse the repository at this point in the history
Signed-off-by: Esteve Fernandez <[email protected]>
  • Loading branch information
esteve committed Jun 1, 2021
1 parent 9ee11c4 commit c917ca6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
9 changes: 7 additions & 2 deletions zenoh/src/net/routing/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,10 +753,15 @@ pub(super) fn shared_nodes(net1: &Network, net2: &Network) -> Vec<PeerId> {
net1.graph
.node_references()
.filter_map(|(_, node1)| {
net2.graph
if net2
.graph
.node_references()
.any(|(_, node2)| node1.pid == node2.pid)
.then(|| node1.pid.clone())
{
Some(node1.pid.clone())
} else {
None
}
})
.collect()
}
36 changes: 28 additions & 8 deletions zenoh/src/net/routing/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,27 @@ impl Resource {
#[inline(always)]
pub fn routers_data_route(&self, context: usize) -> Option<Arc<Route>> {
match &self.context {
Some(ctx) => (ctx.routers_data_routes.len() > context)
.then(|| ctx.routers_data_routes[context].clone()),
Some(ctx) => {
if (ctx.routers_data_routes.len() > context) {
Some(ctx.routers_data_routes[context].clone())
} else {
None
}
}
None => None,
}
}

#[inline(always)]
pub fn peers_data_route(&self, context: usize) -> Option<Arc<Route>> {
match &self.context {
Some(ctx) => (ctx.peers_data_routes.len() > context)
.then(|| ctx.peers_data_routes[context].clone()),
Some(ctx) => {
if (ctx.peers_data_routes.len() > context) {
Some(ctx.peers_data_routes[context].clone())
} else {
None
}
}
None => None,
}
}
Expand All @@ -174,17 +184,27 @@ impl Resource {
#[inline(always)]
pub fn routers_query_route(&self, context: usize) -> Option<Arc<Route>> {
match &self.context {
Some(ctx) => (ctx.routers_query_routes.len() > context)
.then(|| ctx.routers_query_routes[context].clone()),
Some(ctx) => {
if (ctx.routers_query_routes.len() > context) {
Some(ctx.routers_query_routes[context].clone())
} else {
None
}
}
None => None,
}
}

#[inline(always)]
pub fn peers_query_route(&self, context: usize) -> Option<Arc<Route>> {
match &self.context {
Some(ctx) => (ctx.peers_query_routes.len() > context)
.then(|| ctx.peers_query_routes[context].clone()),
Some(ctx) => {
if (ctx.peers_query_routes.len() > context) {
Some(ctx.peers_query_routes[context].clone())
} else {
None
}
}
None => None,
}
}
Expand Down
24 changes: 20 additions & 4 deletions zenoh/src/net/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,22 @@ impl Session {
let joined_pub = state.publishers.values().any(|p| {
rname::include(join_pub, &state.localkey_to_resname(&p.reskey).unwrap())
});
(!joined_pub).then(|| join_pub.clone().into())
if !joined_pub {
Some(join_pub.clone().into())
} else {
None
}
}
None => {
let twin_pub = state.publishers.values().any(|p| {
state.localkey_to_resname(&p.reskey).unwrap()
== state.localkey_to_resname(&pub_state.reskey).unwrap()
});
(!twin_pub).then(|| resource.clone())
if !twin_pub {
Some(resource.clone())
} else {
None
}
}
};

Expand Down Expand Up @@ -553,14 +561,22 @@ impl Session {
let joined_sub = state.subscribers.values().any(|s| {
rname::include(join_sub, &state.localkey_to_resname(&s.reskey).unwrap())
});
(!joined_sub).then(|| join_sub.clone().into())
if !joined_sub {
Some(join_sub.clone().into())
} else {
None
}
}
None => {
let twin_sub = state.subscribers.values().any(|s| {
state.localkey_to_resname(&s.reskey).unwrap()
== state.localkey_to_resname(&sub_state.reskey).unwrap()
});
(!twin_sub).then(|| sub_state.reskey.clone())
if !twin_sub {
Some(sub_state.reskey.clone())
} else {
None
}
}
};

Expand Down

0 comments on commit c917ca6

Please sign in to comment.