use std::fmt::Debug; use std::pin::Pin; use std::task::{Context, Poll}; use futures_util::{Sink, Stream}; use crate::collab_sync::SyncError; use tokio::sync::mpsc::UnboundedSender; use tokio_stream::wrappers::UnboundedReceiverStream; pub trait CollabConnect: Sink + Stream {} pub struct TokioUnboundedSink(pub UnboundedSender); impl TokioUnboundedSink { pub fn new(tx: UnboundedSender) -> Self { Self(tx) } } impl Sink for TokioUnboundedSink where T: Send + Sync + 'static + Debug, { type Error = SyncError; fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { // An unbounded channel can always accept messages without blocking, so we always return Ready. Poll::Ready(Ok(())) } fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> { let _ = self.0.send(item); Ok(()) } fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { // There is no buffering in an unbounded channel, so we always return Ready. Poll::Ready(Ok(())) } fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { // An unbounded channel is closed by dropping the sender, so we don't need to do anything here. Poll::Ready(Ok(())) } } pub type TokioUnboundedStream = UnboundedReceiverStream;