use bytes::Bytes;
use futures::sink::Sink;
use futures::stream::Stream;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use crate::Socket;
pub struct SocketStream<S> {
socket: S,
}
impl<S> SocketStream<S> {
pub const fn new(socket: S) -> Self {
Self { socket }
}
pub const fn get_ref(&self) -> &S {
&self.socket
}
pub fn get_mut(&mut self) -> &mut S {
&mut self.socket
}
pub fn into_inner(self) -> S {
self.socket
}
}
impl<S: Socket + Unpin> Stream for SocketStream<S> {
type Item = io::Result<Vec<Bytes>>;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(Some(Err(io::Error::new(
io::ErrorKind::Unsupported,
"Use socket.recv().await directly; Stream adapter requires storing an async future",
))))
}
}
pub struct SocketSink<S> {
socket: S,
}
impl<S> SocketSink<S> {
pub const fn new(socket: S) -> Self {
Self { socket }
}
pub const fn get_ref(&self) -> &S {
&self.socket
}
pub fn get_mut(&mut self) -> &mut S {
&mut self.socket
}
pub fn into_inner(self) -> S {
self.socket
}
}
impl<S: Socket + Unpin> Sink<Vec<Bytes>> for SocketSink<S> {
type Error = io::Error;
fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, _item: Vec<Bytes>) -> Result<(), Self::Error> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"Use socket.send(msg).await directly; Sink adapter requires storing an async future",
))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.poll_flush(cx)
}
}
pub struct SocketStreamSink<S> {
socket: S,
pending_send: Option<Vec<Bytes>>,
}
impl<S> SocketStreamSink<S> {
pub const fn new(socket: S) -> Self {
Self {
socket,
pending_send: None,
}
}
pub const fn get_ref(&self) -> &S {
&self.socket
}
pub fn get_mut(&mut self) -> &mut S {
&mut self.socket
}
pub fn into_inner(self) -> S {
self.socket
}
}
impl<S: Socket + Unpin> Stream for SocketStreamSink<S> {
type Item = io::Result<Vec<Bytes>>;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(Some(Err(io::Error::new(
io::ErrorKind::Unsupported,
"Use socket.recv().await directly; Stream adapter requires storing an async future",
))))
}
}
impl<S: Socket + Unpin> Sink<Vec<Bytes>> for SocketStreamSink<S> {
type Error = io::Error;
fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(mut self: Pin<&mut Self>, item: Vec<Bytes>) -> Result<(), Self::Error> {
self.pending_send = Some(item);
Ok(())
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
if self.pending_send.is_some() {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::Unsupported,
"Use socket.send(msg).await directly; Sink adapter requires storing an async future",
)));
}
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.poll_flush(cx)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_stream_adapter_creation() {
}
}