use std::future::Future;
use vox_types::{Link, MaybeSend};
pub struct Attachment<L> {
link: L,
}
impl<L> Attachment<L> {
pub fn initiator(link: L) -> Self {
Self { link }
}
pub fn into_link(self) -> L {
self.link
}
}
pub trait LinkSource: MaybeSend + 'static {
type Link: Link + MaybeSend;
fn next_link(
&mut self,
) -> impl Future<Output = std::io::Result<Attachment<Self::Link>>> + MaybeSend + '_;
}
pub struct SingleAttachmentSource<L> {
attachment: Option<Attachment<L>>,
}
pub fn single_attachment_source<L: Link + MaybeSend + 'static>(
attachment: Attachment<L>,
) -> SingleAttachmentSource<L> {
SingleAttachmentSource {
attachment: Some(attachment),
}
}
pub fn single_link_source<L: Link + MaybeSend + 'static>(link: L) -> SingleAttachmentSource<L> {
single_attachment_source(Attachment::initiator(link))
}
impl<L: Link + MaybeSend + 'static> LinkSource for SingleAttachmentSource<L> {
type Link = L;
async fn next_link(&mut self) -> std::io::Result<Attachment<Self::Link>> {
self.attachment.take().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::ConnectionRefused,
"single-use LinkSource exhausted",
)
})
}
}