Skip to content

Commit

Permalink
♻️ zb: replace some Node::at() usage with Node::add_interface()
Browse files Browse the repository at this point in the history
Node::at() isn't a very friendly API. The closure isn't necessary in
normal situation (only in error case will it actually save a few cycles,
eventually).

Provide a simpler Node::add_interface() method and use it for standard
interfaces registration.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
  • Loading branch information
elmarco committed Apr 28, 2024
1 parent aafc9e1 commit 7fc3ab7
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions zbus/src/object_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ impl Node {
path,
..Default::default()
};
node.at(Peer::name(), || ArcInterface::new(Peer));
node.at(Introspectable::name(), || ArcInterface::new(Introspectable));
node.at(Properties::name(), || ArcInterface::new(Properties));
debug_assert!(node.add_interface(Peer));
debug_assert!(node.add_interface(Introspectable));
debug_assert!(node.add_interface(Properties));

node
}
Expand Down Expand Up @@ -277,6 +277,23 @@ impl Node {
self.children.remove(node).is_some()
}

fn add_arc_interface(&mut self, name: InterfaceName<'static>, arc_iface: ArcInterface) -> bool {
match self.interfaces.entry(name) {
Entry::Vacant(e) => {
e.insert(arc_iface);
true
}
Entry::Occupied(_) => false,
}
}

fn add_interface<I>(&mut self, iface: I) -> bool
where
I: Interface,
{
self.add_arc_interface(I::name(), ArcInterface::new(iface))
}

// Takes a closure so caller can avoid having to create an Arc & RwLock in case interface was
// already added.
fn at<F>(&mut self, name: InterfaceName<'static>, iface_creator: F) -> bool
Expand Down

0 comments on commit 7fc3ab7

Please sign in to comment.