From 4d2504947336744a575e560ded0889ace3483154 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 11 Oct 2022 11:35:44 -0400 Subject: [PATCH] Implement ChannelFactory for (a wrapper of) TransportRegistry. This will let us just have ChanMgr take a `dyn ChannelFactory`. --- crates/tor-chanmgr/src/factory.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/tor-chanmgr/src/factory.rs b/crates/tor-chanmgr/src/factory.rs index 317e69dd9..5f19e6383 100644 --- a/crates/tor-chanmgr/src/factory.rs +++ b/crates/tor-chanmgr/src/factory.rs @@ -3,7 +3,7 @@ use async_trait::async_trait; use futures::{AsyncRead, AsyncWrite}; -use tor_linkspec::{OwnedChanTarget, TransportId}; +use tor_linkspec::{HasChanMethod, OwnedChanTarget, TransportId}; use tor_proto::channel::Channel; use tor_rtcompat::Runtime; @@ -114,3 +114,25 @@ pub trait TransportRegistry { // TODO pt-client: implement a DefaultTransportRegistry that returns a // DefaultChannelFactory for TransportId::Builtin, and nothing otherwise. + +/// Helper type: Wrap a `TransportRegistry` so that it can be used as a +/// `ChannelFactory`. +/// +/// (This has to be a new type, or else the blanket implementation of +/// `ChannelFactory` for `TransportHelper` would conflict.) +#[derive(Clone, Debug)] +pub(crate) struct RegistryAsFactory(R); + +#[async_trait] +impl ChannelFactory for RegistryAsFactory { + async fn connect_via_transport(&self, target: &OwnedChanTarget) -> crate::Result { + let method = target.chan_method(); + let id = method.transport_id(); + let factory = self + .0 + .get_factory(&id) + .ok_or(crate::Error::NoSuchTransport(id))?; + + factory.connect_via_transport(target).await + } +}