refactor: split into more logical files

This commit is contained in:
Noa Aarts 2024-10-18 01:56:23 +02:00
parent 19eb943865
commit 9dc681086d
Signed by: noa
GPG key ID: 1850932741EFF672
11 changed files with 728 additions and 276 deletions

62
src/utils.rs Normal file
View file

@ -0,0 +1,62 @@
use std::task::Poll;
use tokio::io::{AsyncRead, AsyncWrite};
pub struct RepeatSome {
bytes: &'static [u8],
len: usize,
}
impl RepeatSome {
pub fn new(bytes: &'static [u8]) -> Self {
RepeatSome {
bytes,
len: bytes.len(),
}
}
}
impl AsyncRead for RepeatSome {
fn poll_read(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<std::io::Result<()>> {
while buf.remaining() > self.len {
buf.put_slice(self.bytes)
}
Poll::Ready(Ok(()))
}
}
pub struct Drain {}
impl Drain {
pub fn new() -> Self {
Drain {}
}
}
impl AsyncWrite for Drain {
fn poll_write(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, std::io::Error>> {
Poll::Ready(Ok(buf.len()))
}
fn poll_flush(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), std::io::Error>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), std::io::Error>> {
Poll::Ready(Ok(()))
}
}