From 3b9ac7648c42f6c31e78adfa61107ddcfef04bc6 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 2 Feb 2017 17:02:46 +0100 Subject: [PATCH] with_context on LightSync --- sync/src/api.rs | 10 ++++++++++ util/network/src/host.rs | 4 ++-- util/network/src/service.rs | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/sync/src/api.rs b/sync/src/api.rs index 458b61daa..5b97bc566 100644 --- a/sync/src/api.rs +++ b/sync/src/api.rs @@ -674,6 +674,16 @@ impl LightSync { subprotocol_name: params.subprotocol_name, }) } + + /// Execute a closure with a protocol context. + pub fn with_context(&self, f: F) -> Option + where F: FnOnce(&::light::net::BasicContext) -> T + { + self.network.with_context_eval( + self.subprotocol_name, + move |ctx| self.proto.with_context(ctx, f), + ) + } } impl ManageNetwork for LightSync { diff --git a/util/network/src/host.rs b/util/network/src/host.rs index 98efbd6a0..c3f5ab8cf 100644 --- a/util/network/src/host.rs +++ b/util/network/src/host.rs @@ -983,14 +983,14 @@ impl Host { self.nodes.write().update(node_changes, &*self.reserved_nodes.read()); } - pub fn with_context(&self, protocol: ProtocolId, io: &IoContext, action: F) where F: Fn(&NetworkContext) { + pub fn with_context(&self, protocol: ProtocolId, io: &IoContext, action: F) where F: FnOnce(&NetworkContext) { let reserved = { self.reserved_nodes.read() }; let context = NetworkContext::new(io, protocol, None, self.sessions.clone(), &reserved); action(&context); } - pub fn with_context_eval(&self, protocol: ProtocolId, io: &IoContext, action: F) -> T where F: Fn(&NetworkContext) -> T { + pub fn with_context_eval(&self, protocol: ProtocolId, io: &IoContext, action: F) -> T where F: FnOnce(&NetworkContext) -> T { let reserved = { self.reserved_nodes.read() }; let context = NetworkContext::new(io, protocol, None, self.sessions.clone(), &reserved); diff --git a/util/network/src/service.rs b/util/network/src/service.rs index 73b5dafb5..76e21c7f8 100644 --- a/util/network/src/service.rs +++ b/util/network/src/service.rs @@ -177,7 +177,7 @@ impl NetworkService { } /// Executes action in the network context - pub fn with_context(&self, protocol: ProtocolId, action: F) where F: Fn(&NetworkContext) { + pub fn with_context(&self, protocol: ProtocolId, action: F) where F: FnOnce(&NetworkContext) { let io = IoContext::new(self.io_service.channel(), 0); let host = self.host.read(); if let Some(ref host) = host.as_ref() { @@ -186,7 +186,7 @@ impl NetworkService { } /// Evaluates function in the network context - pub fn with_context_eval(&self, protocol: ProtocolId, action: F) -> Option where F: Fn(&NetworkContext) -> T { + pub fn with_context_eval(&self, protocol: ProtocolId, action: F) -> Option where F: FnOnce(&NetworkContext) -> T { let io = IoContext::new(self.io_service.channel(), 0); let host = self.host.read(); host.as_ref().map(|ref host| host.with_context_eval(protocol, &io, action))