Files
harmony/fleet/bounded_writer.rs
2026-09-15 22:08:08 -04:00

41 lines
1001 B
Rust

use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::AsyncWrite;
pub struct BoundedWriter {
pub bytes: Vec<u8>,
limit: u64,
}
impl BoundedWriter {
pub fn new(limit: u64) -> Self {
Self {
bytes: Vec::new(),
limit,
}
}
}
impl AsyncWrite for BoundedWriter {
fn poll_write(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buffer: &[u8],
) -> Poll<std::io::Result<usize>> {
if self.bytes.len() as u64 + buffer.len() as u64 > self.limit {
return Poll::Ready(Err(std::io::Error::other("artifact exceeds byte limit")));
}
self.bytes.extend_from_slice(buffer);
Poll::Ready(Ok(buffer.len()))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Poll::Ready(Ok(()))
}
}