feat: use FnOnce for node hooks (#7975)

Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com>
This commit is contained in:
Matthias Seitz
2024-04-30 12:23:02 +02:00
committed by GitHub
parent 053b14abdc
commit 1fe00a7c35
4 changed files with 32 additions and 27 deletions

View File

@ -157,7 +157,7 @@ impl<T: FullNodeTypes, CB: NodeComponentsBuilder<T>> NodeBuilderWithComponents<T
/// Sets the hook that is run once the node's components are initialized.
pub fn on_component_initialized<F>(mut self, hook: F) -> Self
where
F: Fn(NodeAdapter<T, CB::Components>) -> eyre::Result<()> + Send + 'static,
F: FnOnce(NodeAdapter<T, CB::Components>) -> eyre::Result<()> + Send + 'static,
{
self.add_ons.hooks.set_on_component_initialized(hook);
self
@ -166,7 +166,7 @@ impl<T: FullNodeTypes, CB: NodeComponentsBuilder<T>> NodeBuilderWithComponents<T
/// Sets the hook that is run once the node has started.
pub fn on_node_started<F>(mut self, hook: F) -> Self
where
F: Fn(FullNode<NodeAdapter<T, CB::Components>>) -> eyre::Result<()> + Send + 'static,
F: FnOnce(FullNode<NodeAdapter<T, CB::Components>>) -> eyre::Result<()> + Send + 'static,
{
self.add_ons.hooks.set_on_node_started(hook);
self
@ -175,7 +175,7 @@ impl<T: FullNodeTypes, CB: NodeComponentsBuilder<T>> NodeBuilderWithComponents<T
/// Sets the hook that is run once the rpc server is started.
pub fn on_rpc_started<F>(mut self, hook: F) -> Self
where
F: Fn(
F: FnOnce(
RpcContext<'_, NodeAdapter<T, CB::Components>>,
RethRpcServerHandles,
) -> eyre::Result<()>
@ -189,7 +189,9 @@ impl<T: FullNodeTypes, CB: NodeComponentsBuilder<T>> NodeBuilderWithComponents<T
/// Sets the hook that is run to configure the rpc modules.
pub fn extend_rpc_modules<F>(mut self, hook: F) -> Self
where
F: Fn(RpcContext<'_, NodeAdapter<T, CB::Components>>) -> eyre::Result<()> + Send + 'static,
F: FnOnce(RpcContext<'_, NodeAdapter<T, CB::Components>>) -> eyre::Result<()>
+ Send
+ 'static,
{
self.add_ons.rpc.set_extend_rpc_modules(hook);
self
@ -202,7 +204,7 @@ impl<T: FullNodeTypes, CB: NodeComponentsBuilder<T>> NodeBuilderWithComponents<T
/// The ExEx ID must be unique.
pub fn install_exex<F, R, E>(mut self, exex_id: impl Into<String>, exex: F) -> Self
where
F: Fn(ExExContext<NodeAdapter<T, CB::Components>>) -> R + Send + 'static,
F: FnOnce(ExExContext<NodeAdapter<T, CB::Components>>) -> R + Send + 'static,
R: Future<Output = eyre::Result<E>> + Send,
E: Future<Output = eyre::Result<()>> + Send,
{

View File

@ -77,15 +77,15 @@ pub trait OnComponentInitializedHook<Node>: Send {
/// Consumes the event hook and runs it.
///
/// If this returns an error, the node launch will be aborted.
fn on_event(&self, node: Node) -> eyre::Result<()>;
fn on_event(self: Box<Self>, node: Node) -> eyre::Result<()>;
}
impl<Node, F> OnComponentInitializedHook<Node> for F
where
F: Fn(Node) -> eyre::Result<()> + Send,
F: FnOnce(Node) -> eyre::Result<()> + Send,
{
fn on_event(&self, node: Node) -> eyre::Result<()> {
self(node)
fn on_event(self: Box<Self>, node: Node) -> eyre::Result<()> {
(*self)(node)
}
}
@ -94,27 +94,27 @@ pub trait OnNodeStartedHook<Node: FullNodeComponents>: Send {
/// Consumes the event hook and runs it.
///
/// If this returns an error, the node launch will be aborted.
fn on_event(&self, node: FullNode<Node>) -> eyre::Result<()>;
fn on_event(self: Box<Self>, node: FullNode<Node>) -> eyre::Result<()>;
}
impl<Node, F> OnNodeStartedHook<Node> for F
where
Node: FullNodeComponents,
F: Fn(FullNode<Node>) -> eyre::Result<()> + Send,
F: FnOnce(FullNode<Node>) -> eyre::Result<()> + Send,
{
fn on_event(&self, node: FullNode<Node>) -> eyre::Result<()> {
self(node)
fn on_event(self: Box<Self>, node: FullNode<Node>) -> eyre::Result<()> {
(*self)(node)
}
}
impl<Node> OnComponentInitializedHook<Node> for () {
fn on_event(&self, _node: Node) -> eyre::Result<()> {
fn on_event(self: Box<Self>, _node: Node) -> eyre::Result<()> {
Ok(())
}
}
impl<Node: FullNodeComponents> OnNodeStartedHook<Node> for () {
fn on_event(&self, _node: FullNode<Node>) -> eyre::Result<()> {
fn on_event(self: Box<Self>, _node: FullNode<Node>) -> eyre::Result<()> {
Ok(())
}
}

View File

@ -232,8 +232,7 @@ where
async move {
while let Ok(notification) = canon_state_notifications.recv().await {
handle.send_async(notification.into()).await.expect(
"blockchain tree notification could not be sent to exex
manager",
"blockchain tree notification could not be sent to exex manager",
);
}
},

View File

@ -98,7 +98,7 @@ impl<Node: FullNodeComponents> fmt::Debug for RpcHooks<Node> {
pub trait OnRpcStarted<Node: FullNodeComponents>: Send {
/// The hook that is called once the rpc server is started.
fn on_rpc_started(
&self,
self: Box<Self>,
ctx: RpcContext<'_, Node>,
handles: RethRpcServerHandles,
) -> eyre::Result<()>;
@ -106,20 +106,24 @@ pub trait OnRpcStarted<Node: FullNodeComponents>: Send {
impl<Node, F> OnRpcStarted<Node> for F
where
F: Fn(RpcContext<'_, Node>, RethRpcServerHandles) -> eyre::Result<()> + Send,
F: FnOnce(RpcContext<'_, Node>, RethRpcServerHandles) -> eyre::Result<()> + Send,
Node: FullNodeComponents,
{
fn on_rpc_started(
&self,
self: Box<Self>,
ctx: RpcContext<'_, Node>,
handles: RethRpcServerHandles,
) -> eyre::Result<()> {
self(ctx, handles)
(*self)(ctx, handles)
}
}
impl<Node: FullNodeComponents> OnRpcStarted<Node> for () {
fn on_rpc_started(&self, _: RpcContext<'_, Node>, _: RethRpcServerHandles) -> eyre::Result<()> {
fn on_rpc_started(
self: Box<Self>,
_: RpcContext<'_, Node>,
_: RethRpcServerHandles,
) -> eyre::Result<()> {
Ok(())
}
}
@ -127,21 +131,21 @@ impl<Node: FullNodeComponents> OnRpcStarted<Node> for () {
/// Event hook that is called when the rpc server is started.
pub trait ExtendRpcModules<Node: FullNodeComponents>: Send {
/// The hook that is called once the rpc server is started.
fn extend_rpc_modules(&self, ctx: RpcContext<'_, Node>) -> eyre::Result<()>;
fn extend_rpc_modules(self: Box<Self>, ctx: RpcContext<'_, Node>) -> eyre::Result<()>;
}
impl<Node, F> ExtendRpcModules<Node> for F
where
F: Fn(RpcContext<'_, Node>) -> eyre::Result<()> + Send,
F: FnOnce(RpcContext<'_, Node>) -> eyre::Result<()> + Send,
Node: FullNodeComponents,
{
fn extend_rpc_modules(&self, ctx: RpcContext<'_, Node>) -> eyre::Result<()> {
self(ctx)
fn extend_rpc_modules(self: Box<Self>, ctx: RpcContext<'_, Node>) -> eyre::Result<()> {
(*self)(ctx)
}
}
impl<Node: FullNodeComponents> ExtendRpcModules<Node> for () {
fn extend_rpc_modules(&self, _: RpcContext<'_, Node>) -> eyre::Result<()> {
fn extend_rpc_modules(self: Box<Self>, _: RpcContext<'_, Node>) -> eyre::Result<()> {
Ok(())
}
}