mirror of
https://github.com/telemt/telemt.git
synced 2026-04-16 18:14:10 +03:00
1.0.0
Tschuss Status Quo - Hallo, Zukunft!
This commit is contained in:
113
src/stream/traits.rs
Normal file
113
src/stream/traits.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
//! Stream traits and common types
|
||||
|
||||
use bytes::Bytes;
|
||||
use std::io::Result;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
||||
|
||||
/// Extra metadata for frames
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct FrameMeta {
|
||||
/// Quick ACK requested
|
||||
pub quickack: bool,
|
||||
/// This is a simple ACK message
|
||||
pub simple_ack: bool,
|
||||
/// Skip sending this frame
|
||||
pub skip_send: bool,
|
||||
}
|
||||
|
||||
impl FrameMeta {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn with_quickack(mut self) -> Self {
|
||||
self.quickack = true;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_simple_ack(mut self) -> Self {
|
||||
self.simple_ack = true;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of reading a frame
|
||||
#[derive(Debug)]
|
||||
pub enum ReadFrameResult {
|
||||
/// Frame data with metadata
|
||||
Frame(Bytes, FrameMeta),
|
||||
/// Connection closed
|
||||
Closed,
|
||||
}
|
||||
|
||||
/// Trait for streams that wrap another stream
|
||||
pub trait LayeredStream<U> {
|
||||
/// Get reference to upstream
|
||||
fn upstream(&self) -> &U;
|
||||
|
||||
/// Get mutable reference to upstream
|
||||
fn upstream_mut(&mut self) -> &mut U;
|
||||
|
||||
/// Consume self and return upstream
|
||||
fn into_upstream(self) -> U;
|
||||
}
|
||||
|
||||
/// A split read half of a stream
|
||||
pub struct ReadHalf<R> {
|
||||
inner: R,
|
||||
}
|
||||
|
||||
impl<R> ReadHalf<R> {
|
||||
pub fn new(inner: R) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> R {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncRead + Unpin> AsyncRead for ReadHalf<R> {
|
||||
fn poll_read(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut ReadBuf<'_>,
|
||||
) -> Poll<Result<()>> {
|
||||
Pin::new(&mut self.inner).poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
/// A split write half of a stream
|
||||
pub struct WriteHalf<W> {
|
||||
inner: W,
|
||||
}
|
||||
|
||||
impl<W> WriteHalf<W> {
|
||||
pub fn new(inner: W) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> W {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite + Unpin> AsyncWrite for WriteHalf<W> {
|
||||
fn poll_write(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize>> {
|
||||
Pin::new(&mut self.inner).poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
Pin::new(&mut self.inner).poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
Pin::new(&mut self.inner).poll_shutdown(cx)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user