use crate::Result;
use crate::channel::Channel;
use crate::circuit::UniqId;
use async_trait::async_trait;
use futures::channel::mpsc;
use std::sync::Arc;
use tor_linkspec::HasRelayIds;
pub type ChannelResult = Result<Arc<Channel>>;
pub struct OutboundChanSender(pub(crate) mpsc::UnboundedSender<ChannelResult>);
impl OutboundChanSender {
#[allow(dead_code)] pub(crate) fn new(tx: mpsc::UnboundedSender<ChannelResult>) -> Self {
Self(tx)
}
pub fn send(self, result: ChannelResult) {
let _ = self.0.unbounded_send(result);
}
}
#[async_trait]
pub trait ChannelProvider {
type BuildSpec: HasRelayIds;
fn get_or_launch(
self: Arc<Self>,
reactor_id: UniqId,
target: Self::BuildSpec,
tx: OutboundChanSender,
) -> Result<()>;
}
#[cfg(test)]
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct NoOpChannelProvider;
#[cfg(test)]
impl ChannelProvider for NoOpChannelProvider {
type BuildSpec = tor_linkspec::OwnedChanTarget;
fn get_or_launch(
self: Arc<Self>,
_reactor_id: UniqId,
_target: Self::BuildSpec,
_tx: OutboundChanSender,
) -> Result<()> {
Err(tor_error::internal!("NoOpChannelProvider cannot launch channels").into())
}
}