Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
use crate::{
|
||||
codecs::DecodeV2,
|
||||
core::util::{PartialBuffer, WriteBuffer},
|
||||
generic::bufread::impl_decoder,
|
||||
};
|
||||
use core::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite};
|
||||
use std::io::{IoSlice, Result};
|
||||
|
||||
impl_decoder!();
|
||||
|
||||
impl<R: AsyncBufRead, D: DecodeV2> AsyncRead for Decoder<R, D> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<Result<usize>> {
|
||||
if buf.is_empty() {
|
||||
return Poll::Ready(Ok(0));
|
||||
}
|
||||
|
||||
let mut output = WriteBuffer::new_initialized(buf);
|
||||
self.do_poll_read(cx, &mut output)
|
||||
.map_ok(|()| output.written_len())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncWrite, D> AsyncWrite for Decoder<R, D> {
|
||||
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
|
||||
self.get_pin_mut().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
self.get_pin_mut().poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
self.get_pin_mut().poll_close(cx)
|
||||
}
|
||||
|
||||
fn poll_write_vectored(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
bufs: &[IoSlice<'_>],
|
||||
) -> Poll<Result<usize>> {
|
||||
self.get_pin_mut().poll_write_vectored(cx, bufs)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
use crate::{
|
||||
codecs::EncodeV2,
|
||||
core::util::{PartialBuffer, WriteBuffer},
|
||||
generic::bufread::impl_encoder,
|
||||
};
|
||||
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSlice};
|
||||
use std::{
|
||||
io::Result,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
impl_encoder!();
|
||||
|
||||
impl<R: AsyncBufRead, E: EncodeV2> AsyncRead for Encoder<R, E> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<Result<usize>> {
|
||||
if buf.is_empty() {
|
||||
return Poll::Ready(Ok(0));
|
||||
}
|
||||
|
||||
let mut output = WriteBuffer::new_initialized(buf);
|
||||
self.do_poll_read(cx, &mut output)
|
||||
.map_ok(|()| output.written_len())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncWrite, E> AsyncWrite for Encoder<R, E> {
|
||||
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
|
||||
self.get_pin_mut().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
self.get_pin_mut().poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
self.get_pin_mut().poll_close(cx)
|
||||
}
|
||||
|
||||
fn poll_write_vectored(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
bufs: &[IoSlice<'_>],
|
||||
) -> Poll<Result<usize>> {
|
||||
self.get_pin_mut().poll_write_vectored(cx, bufs)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
mod decoder;
|
||||
mod encoder;
|
||||
|
||||
pub use self::{decoder::Decoder, encoder::Encoder};
|
||||
@@ -0,0 +1,126 @@
|
||||
macro_rules! decoder {
|
||||
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
|
||||
pin_project_lite::pin_project! {
|
||||
$(#[$attr])*
|
||||
///
|
||||
/// This structure implements an [`AsyncRead`](futures_io::AsyncRead) interface and will
|
||||
/// read compressed data from an underlying stream and emit a stream of uncompressed data.
|
||||
#[derive(Debug)]
|
||||
pub struct $name<$inner> {
|
||||
#[pin]
|
||||
inner: crate::futures::bufread::Decoder<$inner, crate::codecs::$name>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncBufRead> $name<$inner> {
|
||||
/// Creates a new decoder which will read compressed data from the given stream and
|
||||
/// emit an uncompressed stream.
|
||||
pub fn new(read: $inner) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::futures::bufread::Decoder::new(read, crate::codecs::$name::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new decoder with the given codec, which will read compressed data from the given stream and
|
||||
/// emit an uncompressed stream.
|
||||
pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::futures::bufread::Decoder::new(read, codec)
|
||||
}
|
||||
}
|
||||
|
||||
$($($inherent_methods)*)*
|
||||
}
|
||||
|
||||
impl<$inner> $name<$inner> {
|
||||
/// Configure multi-member/frame decoding, if enabled this will reset the decoder state
|
||||
/// when reaching the end of a compressed member/frame and expect either EOF or another
|
||||
/// compressed member/frame to follow it in the stream.
|
||||
pub fn multiple_members(&mut self, enabled: bool) {
|
||||
self.inner.multiple_members(enabled);
|
||||
}
|
||||
|
||||
/// Acquires a reference to the underlying reader that this decoder is wrapping.
|
||||
pub fn get_ref(&self) -> &$inner {
|
||||
self.inner.get_ref()
|
||||
}
|
||||
|
||||
/// Acquires a mutable reference to the underlying reader that this decoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this decoder.
|
||||
pub fn get_mut(&mut self) -> &mut $inner {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Acquires a pinned mutable reference to the underlying reader that this decoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this decoder.
|
||||
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
|
||||
self.project().inner.get_pin_mut()
|
||||
}
|
||||
|
||||
/// Consumes this decoder returning the underlying reader.
|
||||
///
|
||||
/// Note that this may discard internal state of this decoder, so care should be taken
|
||||
/// to avoid losing resources when this is called.
|
||||
pub fn into_inner(self) -> $inner {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<$inner> {
|
||||
fn poll_read(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.project().inner.poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncWrite> futures_io::AsyncWrite for $name<$inner> {
|
||||
fn poll_write(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.get_pin_mut().poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_close(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.get_pin_mut().poll_close(cx)
|
||||
}
|
||||
|
||||
fn poll_write_vectored(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
bufs: &[std::io::IoSlice<'_>]
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_write_vectored(cx, bufs)
|
||||
}
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
use crate::core::util::{_assert_send, _assert_sync};
|
||||
use core::pin::Pin;
|
||||
use futures_io::AsyncBufRead;
|
||||
|
||||
_assert_send::<$name<Pin<Box<dyn AsyncBufRead + Send>>>>();
|
||||
_assert_sync::<$name<Pin<Box<dyn AsyncBufRead + Sync>>>>();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
macro_rules! encoder {
|
||||
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
|
||||
pin_project_lite::pin_project! {
|
||||
$(#[$attr])*
|
||||
///
|
||||
/// This structure implements an [`AsyncRead`](futures_io::AsyncRead) interface and will
|
||||
/// read uncompressed data from an underlying stream and emit a stream of compressed data.
|
||||
#[derive(Debug)]
|
||||
pub struct $name<$inner> {
|
||||
#[pin]
|
||||
inner: crate::futures::bufread::Encoder<$inner, crate::codecs::$name>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncBufRead> $name<$inner> {
|
||||
$(
|
||||
/// Creates a new encoder which will read uncompressed data from the given stream
|
||||
/// and emit an compressed stream.
|
||||
///
|
||||
$($inherent_methods)*
|
||||
)*
|
||||
|
||||
/// Creates a new encoder with the given codec, which will read uncompressed data from the given stream
|
||||
/// and emit an compressed stream.
|
||||
pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::futures::bufread::Encoder::new(read, codec)
|
||||
}
|
||||
}
|
||||
|
||||
/// Acquires a reference to the underlying reader that this encoder is wrapping.
|
||||
pub fn get_ref(&self) -> &$inner {
|
||||
self.inner.get_ref()
|
||||
}
|
||||
|
||||
/// Acquires a mutable reference to the underlying reader that this encoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this encoder.
|
||||
pub fn get_mut(&mut self) -> &mut $inner {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Acquires a pinned mutable reference to the underlying reader that this encoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this encoder.
|
||||
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
|
||||
self.project().inner.get_pin_mut()
|
||||
}
|
||||
|
||||
/// Consumes this encoder returning the underlying reader.
|
||||
///
|
||||
/// Note that this may discard internal state of this encoder, so care should be taken
|
||||
/// to avoid losing resources when this is called.
|
||||
pub fn into_inner(self) -> $inner {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<$inner> {
|
||||
fn poll_read(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.project().inner.poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncWrite> futures_io::AsyncWrite for $name<$inner> {
|
||||
fn poll_write(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.project().inner.poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_close(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_close(cx)
|
||||
}
|
||||
|
||||
fn poll_write_vectored(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
bufs: &[std::io::IoSlice<'_>]
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.project().inner.poll_write_vectored(cx, bufs)
|
||||
}
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
use crate::core::util::{_assert_send, _assert_sync};
|
||||
use core::pin::Pin;
|
||||
use futures_io::AsyncBufRead;
|
||||
|
||||
_assert_send::<$name<Pin<Box<dyn AsyncBufRead + Send>>>>();
|
||||
_assert_sync::<$name<Pin<Box<dyn AsyncBufRead + Sync>>>>();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#[macro_use]
|
||||
mod decoder;
|
||||
#[macro_use]
|
||||
mod encoder;
|
||||
@@ -0,0 +1,10 @@
|
||||
//! Types which operate over [`AsyncBufRead`](futures_io::AsyncBufRead) streams, both encoders and
|
||||
//! decoders for various formats.
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
mod generic;
|
||||
|
||||
pub(crate) use generic::{Decoder, Encoder};
|
||||
|
||||
algos!(futures::bufread<R>);
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
//! Implementations for IO traits exported by [`futures-io`](::futures_io).
|
||||
|
||||
pub mod bufread;
|
||||
pub mod write;
|
||||
@@ -0,0 +1,27 @@
|
||||
// Originally sourced from `futures_util::io::buf_writer`, needs to be redefined locally so that
|
||||
// the `AsyncBufWrite` impl can access its internals, and changed a bit to make it more efficient
|
||||
// with those methods.
|
||||
|
||||
use crate::generic::write::impl_buf_writer;
|
||||
use futures_io::{AsyncSeek, AsyncWrite, SeekFrom};
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
impl_buf_writer!(poll_close);
|
||||
|
||||
impl<W: AsyncWrite + AsyncSeek> AsyncSeek for BufWriter<W> {
|
||||
/// Seek to the offset, in bytes, in the underlying writer.
|
||||
///
|
||||
/// Seeking always writes out the internal buffer before seeking.
|
||||
fn poll_seek(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
pos: SeekFrom,
|
||||
) -> Poll<io::Result<u64>> {
|
||||
ready!(self.as_mut().flush_buf(cx))?;
|
||||
self.project().writer.poll_seek(cx, pos)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
use crate::{futures::write::BufWriter, generic::write::impl_decoder};
|
||||
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSliceMut};
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
impl_decoder!(poll_close);
|
||||
|
||||
impl<W: AsyncRead, D> AsyncRead for Decoder<W, D> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.get_pin_mut().poll_read(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_read_vectored(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.get_pin_mut().poll_read_vectored(cx, bufs)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
use crate::{futures::write::BufWriter, generic::write::impl_encoder};
|
||||
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSliceMut};
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
impl_encoder!(poll_close);
|
||||
|
||||
impl<W: AsyncRead, E> AsyncRead for Encoder<W, E> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.get_pin_mut().poll_read(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_read_vectored(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
self.get_pin_mut().poll_read_vectored(cx, bufs)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
mod decoder;
|
||||
mod encoder;
|
||||
|
||||
pub use self::{decoder::Decoder, encoder::Encoder};
|
||||
@@ -0,0 +1,132 @@
|
||||
macro_rules! decoder {
|
||||
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
|
||||
pin_project_lite::pin_project! {
|
||||
$(#[$attr])*
|
||||
///
|
||||
/// This structure implements an [`AsyncWrite`](futures_io::AsyncWrite) interface and will
|
||||
/// take in compressed data and write it, uncompressed, to an underlying stream.
|
||||
#[derive(Debug)]
|
||||
pub struct $name<$inner> {
|
||||
#[pin]
|
||||
inner: crate::futures::write::Decoder<$inner, crate::codecs::$name>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncWrite> $name<$inner> {
|
||||
/// Creates a new decoder which will take in compressed data and write it, uncompressed,
|
||||
/// to the given stream.
|
||||
pub fn new(read: $inner) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::futures::write::Decoder::new(read, crate::codecs::$name::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new decoder with the given codec, which will take in compressed data and write it, uncompressed,
|
||||
/// to the given stream.
|
||||
pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::futures::write::Decoder::new(read, codec)
|
||||
}
|
||||
}
|
||||
|
||||
$($($inherent_methods)*)*
|
||||
}
|
||||
|
||||
impl<$inner> $name<$inner> {
|
||||
/// Acquires a reference to the underlying reader that this decoder is wrapping.
|
||||
pub fn get_ref(&self) -> &$inner {
|
||||
self.inner.get_ref()
|
||||
}
|
||||
|
||||
/// Acquires a mutable reference to the underlying reader that this decoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this decoder.
|
||||
pub fn get_mut(&mut self) -> &mut $inner {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Acquires a pinned mutable reference to the underlying reader that this decoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this decoder.
|
||||
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
|
||||
self.project().inner.get_pin_mut()
|
||||
}
|
||||
|
||||
/// Consumes this decoder returning the underlying reader.
|
||||
///
|
||||
/// Note that this may discard internal state of this decoder, so care should be taken
|
||||
/// to avoid losing resources when this is called.
|
||||
pub fn into_inner(self) -> $inner {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncWrite> futures_io::AsyncWrite for $name<$inner> {
|
||||
fn poll_write(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.project().inner.poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_close(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_close(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncRead> futures_io::AsyncRead for $name<$inner> {
|
||||
fn poll_read(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &mut [u8]
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_read(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_read_vectored(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
bufs: &mut [futures_io::IoSliceMut<'_>]
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_read_vectored(cx, bufs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncBufRead for $name<$inner> {
|
||||
fn poll_fill_buf(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>
|
||||
) -> std::task::Poll<std::io::Result<&[u8]>> {
|
||||
self.get_pin_mut().poll_fill_buf(cx)
|
||||
}
|
||||
|
||||
fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
|
||||
self.get_pin_mut().consume(amt)
|
||||
}
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
use crate::core::util::{_assert_send, _assert_sync};
|
||||
use core::pin::Pin;
|
||||
use futures_io::AsyncWrite;
|
||||
|
||||
_assert_send::<$name<Pin<Box<dyn AsyncWrite + Send>>>>();
|
||||
_assert_sync::<$name<Pin<Box<dyn AsyncWrite + Sync>>>>();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
macro_rules! encoder {
|
||||
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
|
||||
pin_project_lite::pin_project! {
|
||||
$(#[$attr])*
|
||||
///
|
||||
/// This structure implements an [`AsyncWrite`](futures_io::AsyncWrite) interface and will
|
||||
/// take in uncompressed data and write it compressed to an underlying stream.
|
||||
#[derive(Debug)]
|
||||
pub struct $name<$inner> {
|
||||
#[pin]
|
||||
inner: crate::futures::write::Encoder<$inner, crate::codecs::$name>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncWrite> $name<$inner> {
|
||||
$(
|
||||
/// Creates a new encoder which will take in uncompressed data and write it
|
||||
/// compressed to the given stream.
|
||||
///
|
||||
$($inherent_methods)*
|
||||
)*
|
||||
|
||||
/// Creates a new encoder with the given codec, which will take in uncompressed data and write it
|
||||
/// compressed to the given stream.
|
||||
pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::futures::write::Encoder::new(read, codec)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner> $name<$inner> {
|
||||
/// Acquires a reference to the underlying writer that this encoder is wrapping.
|
||||
pub fn get_ref(&self) -> &$inner {
|
||||
self.inner.get_ref()
|
||||
}
|
||||
|
||||
/// Acquires a mutable reference to the underlying writer that this encoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the writer which
|
||||
/// may otherwise confuse this encoder.
|
||||
pub fn get_mut(&mut self) -> &mut $inner {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Acquires a pinned mutable reference to the underlying writer that this encoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the writer which
|
||||
/// may otherwise confuse this encoder.
|
||||
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
|
||||
self.project().inner.get_pin_mut()
|
||||
}
|
||||
|
||||
/// Consumes this encoder returning the underlying writer.
|
||||
///
|
||||
/// Note that this may discard internal state of this encoder, so care should be taken
|
||||
/// to avoid losing resources when this is called.
|
||||
pub fn into_inner(self) -> $inner {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncWrite> futures_io::AsyncWrite for $name<$inner> {
|
||||
fn poll_write(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.project().inner.poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_close(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_close(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncRead> futures_io::AsyncRead for $name<$inner> {
|
||||
fn poll_read(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &mut [u8]
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_read(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_read_vectored(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
bufs: &mut [futures_io::IoSliceMut<'_>]
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_read_vectored(cx, bufs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncBufRead for $name<$inner> {
|
||||
fn poll_fill_buf(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>
|
||||
) -> std::task::Poll<std::io::Result<&[u8]>> {
|
||||
self.get_pin_mut().poll_fill_buf(cx)
|
||||
}
|
||||
|
||||
fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
|
||||
self.get_pin_mut().consume(amt)
|
||||
}
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
use crate::core::util::{_assert_send, _assert_sync};
|
||||
use core::pin::Pin;
|
||||
use futures_io::AsyncWrite;
|
||||
|
||||
_assert_send::<$name<Pin<Box<dyn AsyncWrite + Send>>>>();
|
||||
_assert_sync::<$name<Pin<Box<dyn AsyncWrite + Sync>>>>();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#[macro_use]
|
||||
mod decoder;
|
||||
#[macro_use]
|
||||
mod encoder;
|
||||
@@ -0,0 +1,16 @@
|
||||
//! Types which operate over [`AsyncWrite`](futures_io::AsyncWrite) streams, both encoders and
|
||||
//! decoders for various formats.
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
mod generic;
|
||||
|
||||
mod buf_writer;
|
||||
|
||||
use self::{
|
||||
buf_writer::BufWriter,
|
||||
generic::{Decoder, Encoder},
|
||||
};
|
||||
use crate::generic::write::AsyncBufWrite;
|
||||
|
||||
algos!(futures::write<W>);
|
||||
@@ -0,0 +1,243 @@
|
||||
use crate::{
|
||||
codecs::DecodeV2,
|
||||
core::util::{PartialBuffer, WriteBuffer},
|
||||
};
|
||||
use std::{io::Result, ops::ControlFlow, panic::AssertUnwindSafe};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
Decoding,
|
||||
Flushing,
|
||||
Done,
|
||||
Next,
|
||||
Error(AssertUnwindSafe<std::io::Error>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Decoder {
|
||||
state: State,
|
||||
multiple_members: bool,
|
||||
}
|
||||
|
||||
impl Default for Decoder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
state: State::Decoding,
|
||||
multiple_members: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
pub fn multiple_members(&mut self, enabled: bool) {
|
||||
self.multiple_members = enabled;
|
||||
}
|
||||
|
||||
pub fn do_poll_read(
|
||||
&mut self,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
decoder: &mut dyn DecodeV2,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
mut first: bool,
|
||||
) -> ControlFlow<Result<()>> {
|
||||
loop {
|
||||
self.state = match self.state {
|
||||
State::Decoding => {
|
||||
if input.unwritten().is_empty() && !first {
|
||||
// Avoid attempting to reinitialise the decoder if the
|
||||
// reader has returned EOF.
|
||||
self.multiple_members = false;
|
||||
|
||||
State::Flushing
|
||||
} else {
|
||||
match decoder.decode(input, output) {
|
||||
Ok(true) => State::Flushing,
|
||||
// ignore the first error, occurs when input is empty
|
||||
// but we need to run decode to flush
|
||||
Err(err) if !first => {
|
||||
self.state = State::Error(AssertUnwindSafe(err));
|
||||
if output.written_len() > 0 {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// poll for more data for the next decode
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
State::Flushing => {
|
||||
let before = output.written_len();
|
||||
let result = decoder.finish(output).and_then(|finished| {
|
||||
if !finished
|
||||
&& output.written_len() == before
|
||||
&& !output.has_no_spare_space()
|
||||
{
|
||||
Err(std::io::ErrorKind::UnexpectedEof.into())
|
||||
} else {
|
||||
Ok(finished)
|
||||
}
|
||||
});
|
||||
match result {
|
||||
Ok(true) => {
|
||||
if self.multiple_members {
|
||||
if let Err(err) = decoder.reinit() {
|
||||
self.state = State::Error(AssertUnwindSafe(err));
|
||||
if output.written_len() > 0 {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Poll again only if the decode stage consumed all the input.
|
||||
first = input.unwritten().is_empty();
|
||||
State::Next
|
||||
} else {
|
||||
State::Done
|
||||
}
|
||||
}
|
||||
Ok(false) => State::Flushing,
|
||||
Err(err) => {
|
||||
self.state = State::Error(AssertUnwindSafe(err));
|
||||
if output.written_len() > 0 {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
State::Done => return ControlFlow::Break(Ok(())),
|
||||
|
||||
State::Next => {
|
||||
if input.unwritten().is_empty() {
|
||||
if first {
|
||||
// poll for more data to check if there's another stream
|
||||
break;
|
||||
}
|
||||
State::Done
|
||||
} else {
|
||||
State::Decoding
|
||||
}
|
||||
}
|
||||
|
||||
State::Error(_) => {
|
||||
let State::Error(err) = std::mem::replace(&mut self.state, State::Done) else {
|
||||
unreachable!()
|
||||
};
|
||||
return ControlFlow::Break(Err(err.0));
|
||||
}
|
||||
};
|
||||
|
||||
if output.has_no_spare_space() {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
}
|
||||
}
|
||||
|
||||
if output.has_no_spare_space() {
|
||||
ControlFlow::Break(Ok(()))
|
||||
} else {
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_decoder {
|
||||
() => {
|
||||
use crate::generic::bufread::Decoder as GenericDecoder;
|
||||
|
||||
use std::{ops::ControlFlow, task::ready};
|
||||
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
#[derive(Debug)]
|
||||
pub struct Decoder<R, D> {
|
||||
#[pin]
|
||||
reader: R,
|
||||
decoder: D,
|
||||
inner: GenericDecoder,
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncBufRead, D: DecodeV2> Decoder<R, D> {
|
||||
pub fn new(reader: R, decoder: D) -> Self {
|
||||
Self {
|
||||
reader,
|
||||
decoder,
|
||||
inner: GenericDecoder::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, D> Decoder<R, D> {
|
||||
pub fn get_ref(&self) -> &R {
|
||||
&self.reader
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self) -> &mut R {
|
||||
&mut self.reader
|
||||
}
|
||||
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R> {
|
||||
self.project().reader
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> R {
|
||||
self.reader
|
||||
}
|
||||
|
||||
pub fn multiple_members(&mut self, enabled: bool) {
|
||||
self.inner.multiple_members(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
fn do_poll_read(
|
||||
inner: &mut GenericDecoder,
|
||||
decoder: &mut dyn DecodeV2,
|
||||
mut reader: Pin<&mut dyn AsyncBufRead>,
|
||||
cx: &mut Context<'_>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> Poll<Result<()>> {
|
||||
if let ControlFlow::Break(res) =
|
||||
inner.do_poll_read(output, decoder, &mut PartialBuffer::new(&[][..]), true)
|
||||
{
|
||||
return Poll::Ready(res);
|
||||
}
|
||||
|
||||
loop {
|
||||
let mut input = PartialBuffer::new(match reader.as_mut().poll_fill_buf(cx)? {
|
||||
Poll::Ready(input) => input,
|
||||
Poll::Pending if output.written().is_empty() => return Poll::Pending,
|
||||
_ => return Poll::Ready(Ok(())),
|
||||
});
|
||||
|
||||
let control_flow = inner.do_poll_read(output, decoder, &mut input, false);
|
||||
|
||||
let bytes_read = input.written().len();
|
||||
reader.as_mut().consume(bytes_read);
|
||||
|
||||
if let ControlFlow::Break(res) = control_flow {
|
||||
break Poll::Ready(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncBufRead, D: DecodeV2> Decoder<R, D> {
|
||||
fn do_poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> Poll<Result<()>> {
|
||||
let this = self.project();
|
||||
|
||||
do_poll_read(this.inner, this.decoder, this.reader, cx, output)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use impl_decoder;
|
||||
@@ -0,0 +1,233 @@
|
||||
use crate::{
|
||||
codecs::EncodeV2,
|
||||
core::util::{PartialBuffer, WriteBuffer},
|
||||
};
|
||||
use std::{io::Result, ops::ControlFlow, panic::AssertUnwindSafe};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
Encoding(usize),
|
||||
Flushing,
|
||||
Finishing,
|
||||
Done,
|
||||
Error(AssertUnwindSafe<std::io::Error>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Encoder {
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl Default for Encoder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
state: State::Encoding(0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
/// `input` - should be `None` if `Poll::Pending`.
|
||||
pub fn do_poll_read(
|
||||
&mut self,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
encoder: &mut dyn EncodeV2,
|
||||
mut input: Option<&mut PartialBuffer<&[u8]>>,
|
||||
) -> ControlFlow<Result<()>> {
|
||||
loop {
|
||||
self.state = match &mut self.state {
|
||||
State::Encoding(read) => match input.as_mut() {
|
||||
None => {
|
||||
if *read == 0 {
|
||||
if output.written().is_empty() {
|
||||
// Poll for more data
|
||||
break;
|
||||
} else {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
}
|
||||
} else {
|
||||
State::Flushing
|
||||
}
|
||||
}
|
||||
Some(input) => {
|
||||
if input.unwritten().is_empty() {
|
||||
State::Finishing
|
||||
} else {
|
||||
if let Err(err) = encoder.encode(input, output) {
|
||||
self.state = State::Error(AssertUnwindSafe(err));
|
||||
if output.written_len() > 0 {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
*read += input.written().len();
|
||||
|
||||
// Poll for more data
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
State::Flushing => match encoder.flush(output) {
|
||||
Ok(true) => {
|
||||
self.state = State::Encoding(0);
|
||||
|
||||
// Poll for more data
|
||||
break;
|
||||
}
|
||||
Ok(false) => State::Flushing,
|
||||
Err(err) => {
|
||||
self.state = State::Error(AssertUnwindSafe(err));
|
||||
if output.written_len() > 0 {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
State::Finishing => match encoder.finish(output) {
|
||||
Ok(true) => State::Done,
|
||||
Ok(false) => State::Finishing,
|
||||
Err(err) => {
|
||||
self.state = State::Error(AssertUnwindSafe(err));
|
||||
if output.written_len() > 0 {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
State::Done => return ControlFlow::Break(Ok(())),
|
||||
|
||||
State::Error(_) => {
|
||||
let State::Error(err) = std::mem::replace(&mut self.state, State::Done) else {
|
||||
unreachable!()
|
||||
};
|
||||
return ControlFlow::Break(Err(err.0));
|
||||
}
|
||||
};
|
||||
|
||||
if output.has_no_spare_space() {
|
||||
return ControlFlow::Break(Ok(()));
|
||||
}
|
||||
}
|
||||
|
||||
if output.has_no_spare_space() {
|
||||
ControlFlow::Break(Ok(()))
|
||||
} else {
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_encoder {
|
||||
() => {
|
||||
use crate::generic::bufread::Encoder as GenericEncoder;
|
||||
|
||||
use std::{ops::ControlFlow, task::ready};
|
||||
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
#[derive(Debug)]
|
||||
pub struct Encoder<R, E> {
|
||||
#[pin]
|
||||
reader: R,
|
||||
encoder: E,
|
||||
inner: GenericEncoder,
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncBufRead, E: EncodeV2> Encoder<R, E> {
|
||||
pub fn new(reader: R, encoder: E) -> Self {
|
||||
Self {
|
||||
reader,
|
||||
encoder,
|
||||
inner: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_capacity(reader: R, encoder: E, _cap: usize) -> Self {
|
||||
Self::new(reader, encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, E> Encoder<R, E> {
|
||||
pub fn get_ref(&self) -> &R {
|
||||
&self.reader
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self) -> &mut R {
|
||||
&mut self.reader
|
||||
}
|
||||
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R> {
|
||||
self.project().reader
|
||||
}
|
||||
|
||||
pub(crate) fn get_encoder_ref(&self) -> &E {
|
||||
&self.encoder
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> R {
|
||||
self.reader
|
||||
}
|
||||
}
|
||||
|
||||
fn do_poll_read(
|
||||
inner: &mut GenericEncoder,
|
||||
encoder: &mut dyn EncodeV2,
|
||||
mut reader: Pin<&mut dyn AsyncBufRead>,
|
||||
cx: &mut Context<'_>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> Poll<Result<()>> {
|
||||
if let ControlFlow::Break(res) = inner.do_poll_read(output, encoder, None) {
|
||||
return Poll::Ready(res);
|
||||
}
|
||||
|
||||
loop {
|
||||
let mut input = match reader.as_mut().poll_fill_buf(cx) {
|
||||
Poll::Pending => None,
|
||||
Poll::Ready(res) => Some(PartialBuffer::new(res?)),
|
||||
};
|
||||
|
||||
let control_flow = inner.do_poll_read(output, encoder, input.as_mut());
|
||||
|
||||
let is_pending = input.is_none();
|
||||
if let Some(input) = input {
|
||||
let len = input.written().len();
|
||||
reader.as_mut().consume(len);
|
||||
}
|
||||
|
||||
if let ControlFlow::Break(res) = control_flow {
|
||||
break Poll::Ready(res);
|
||||
}
|
||||
|
||||
if is_pending {
|
||||
if output.written().is_empty() {
|
||||
return Poll::Pending;
|
||||
} else {
|
||||
return Poll::Ready(Ok(()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncBufRead, E: EncodeV2> Encoder<R, E> {
|
||||
fn do_poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> Poll<Result<()>> {
|
||||
let this = self.project();
|
||||
|
||||
do_poll_read(this.inner, this.encoder, this.reader, cx, output)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use impl_encoder;
|
||||
@@ -0,0 +1,5 @@
|
||||
mod decoder;
|
||||
mod encoder;
|
||||
|
||||
pub(crate) use decoder::*;
|
||||
pub(crate) use encoder::*;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
pub(crate) mod bufread;
|
||||
pub(crate) mod write;
|
||||
@@ -0,0 +1,21 @@
|
||||
use super::Buffer;
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
pub(crate) trait AsyncBufWrite {
|
||||
/// Attempt to return an internal buffer to write to, flushing data out to the inner reader if
|
||||
/// it is full.
|
||||
///
|
||||
/// On success, returns `Poll::Ready(Ok(buf))`.
|
||||
///
|
||||
/// If the buffer is full and cannot be flushed, the method returns `Poll::Pending` and
|
||||
/// arranges for the current task context (`cx`) to receive a notification when the object
|
||||
/// becomes readable or is closed.
|
||||
fn poll_partial_flush_buf(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<io::Result<Buffer<'_>>>;
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
// Originally sourced from `futures_util::io::buf_writer`, needs to be redefined locally so that
|
||||
// the `AsyncBufWrite` impl can access its internals, and changed a bit to make it more efficient
|
||||
// with those methods.
|
||||
|
||||
use super::AsyncBufWrite;
|
||||
use compression_core::util::WriteBuffer;
|
||||
use std::{
|
||||
fmt, io,
|
||||
pin::Pin,
|
||||
task::{ready, Context, Poll},
|
||||
};
|
||||
|
||||
const DEFAULT_BUF_SIZE: usize = 8192;
|
||||
|
||||
pub struct BufWriter {
|
||||
buf: Box<[u8]>,
|
||||
written: usize,
|
||||
buffered: usize,
|
||||
}
|
||||
|
||||
impl fmt::Debug for BufWriter {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("GenericBufWriter")
|
||||
.field(
|
||||
"buffer",
|
||||
&format_args!("{}/{}", self.buffered, self.buf.len()),
|
||||
)
|
||||
.field("written", &self.written)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl BufWriter {
|
||||
/// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
|
||||
/// but may change in the future.
|
||||
pub fn new() -> Self {
|
||||
Self::with_capacity(DEFAULT_BUF_SIZE)
|
||||
}
|
||||
|
||||
/// Creates a new `BufWriter` with the specified buffer capacity.
|
||||
pub fn with_capacity(cap: usize) -> Self {
|
||||
Self {
|
||||
buf: vec![0; cap].into(),
|
||||
written: 0,
|
||||
buffered: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove the already written data
|
||||
fn remove_written(&mut self) {
|
||||
self.buf.copy_within(self.written..self.buffered, 0);
|
||||
self.buffered -= self.written;
|
||||
self.written = 0;
|
||||
}
|
||||
|
||||
fn do_flush(
|
||||
&mut self,
|
||||
poll_write: &mut dyn FnMut(&[u8]) -> Poll<io::Result<usize>>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
while self.written < self.buffered {
|
||||
let bytes_written = ready!(poll_write(&self.buf[self.written..self.buffered]))?;
|
||||
if bytes_written == 0 {
|
||||
return Poll::Ready(Err(io::Error::new(
|
||||
io::ErrorKind::WriteZero,
|
||||
"failed to write the buffered data",
|
||||
)));
|
||||
}
|
||||
|
||||
self.written += bytes_written;
|
||||
}
|
||||
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn partial_flush_buf(
|
||||
&mut self,
|
||||
poll_write: &mut dyn FnMut(&[u8]) -> Poll<io::Result<usize>>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
let ret = if let Poll::Ready(res) = self.do_flush(poll_write) {
|
||||
res
|
||||
} else {
|
||||
Ok(())
|
||||
};
|
||||
|
||||
if self.written > 0 || self.buffered < self.buf.len() {
|
||||
Poll::Ready(ret)
|
||||
} else {
|
||||
ret?;
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
|
||||
pub fn flush_buf(
|
||||
&mut self,
|
||||
poll_write: &mut dyn FnMut(&[u8]) -> Poll<io::Result<usize>>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
ready!(self.do_flush(poll_write))?;
|
||||
|
||||
debug_assert_eq!(self.buffered, self.written);
|
||||
self.buffered = 0;
|
||||
self.written = 0;
|
||||
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
pub fn poll_write(
|
||||
&mut self,
|
||||
buf: &[u8],
|
||||
poll_write: &mut dyn FnMut(&[u8]) -> Poll<io::Result<usize>>,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
if buf.len() >= self.buf.len() {
|
||||
ready!(self.flush_buf(poll_write))?;
|
||||
poll_write(buf)
|
||||
} else if (self.buf.len() - self.buffered) >= buf.len() {
|
||||
self.buf[self.buffered..].copy_from_slice(buf);
|
||||
self.buffered += buf.len();
|
||||
|
||||
Poll::Ready(Ok(buf.len()))
|
||||
} else {
|
||||
ready!(self.partial_flush_buf(poll_write))?;
|
||||
if self.written > 0 {
|
||||
self.remove_written();
|
||||
}
|
||||
|
||||
let len = buf.len().min(self.buf.len() - self.buffered);
|
||||
self.buf[self.buffered..self.buffered + len].copy_from_slice(&buf[..len]);
|
||||
self.buffered += len;
|
||||
|
||||
Poll::Ready(Ok(len))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_partial_flush_buf(
|
||||
&mut self,
|
||||
poll_write: &mut dyn FnMut(&[u8]) -> Poll<io::Result<usize>>,
|
||||
) -> Poll<io::Result<Buffer<'_>>> {
|
||||
ready!(self.partial_flush_buf(poll_write))?;
|
||||
|
||||
// when the flushed data is larger than or equal to half of yet-to-be-flushed data,
|
||||
// the copyback could use version of memcpy that do copies from the head of the buffer.
|
||||
// Anything smaller than that, an overlap would happen that forces use of memmove.
|
||||
if self.written >= (self.buffered / 3)
|
||||
|| self.written >= 512
|
||||
|| self.buffered == self.buf.len()
|
||||
{
|
||||
self.remove_written();
|
||||
}
|
||||
|
||||
Poll::Ready(Ok(Buffer {
|
||||
write_buffer: WriteBuffer::new_initialized(&mut self.buf[self.buffered..]),
|
||||
buffered: &mut self.buffered,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Buffer<'a> {
|
||||
buffered: &'a mut usize,
|
||||
pub write_buffer: WriteBuffer<'a>,
|
||||
}
|
||||
|
||||
impl Drop for Buffer<'_> {
|
||||
fn drop(&mut self) {
|
||||
*self.buffered += self.write_buffer.written_len();
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_buf_writer {
|
||||
($poll_close: tt) => {
|
||||
use crate::generic::write::{AsyncBufWrite, BufWriter as GenericBufWriter, Buffer};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::task::ready;
|
||||
|
||||
pin_project! {
|
||||
#[derive(Debug)]
|
||||
pub struct BufWriter<W> {
|
||||
#[pin]
|
||||
writer: W,
|
||||
inner: GenericBufWriter,
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> BufWriter<W> {
|
||||
/// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
|
||||
/// but may change in the future.
|
||||
pub fn new(writer: W) -> Self {
|
||||
Self {
|
||||
writer,
|
||||
inner: GenericBufWriter::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new `BufWriter` with the specified buffer capacity.
|
||||
pub fn with_capacity(cap: usize, writer: W) -> Self {
|
||||
Self {
|
||||
writer,
|
||||
inner: GenericBufWriter::with_capacity(cap),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets a reference to the underlying writer.
|
||||
pub fn get_ref(&self) -> &W {
|
||||
&self.writer
|
||||
}
|
||||
|
||||
/// Gets a mutable reference to the underlying writer.
|
||||
///
|
||||
/// It is inadvisable to directly write to the underlying writer.
|
||||
pub fn get_mut(&mut self) -> &mut W {
|
||||
&mut self.writer
|
||||
}
|
||||
|
||||
/// Gets a pinned mutable reference to the underlying writer.
|
||||
///
|
||||
/// It is inadvisable to directly write to the underlying writer.
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
|
||||
self.project().writer
|
||||
}
|
||||
|
||||
/// Consumes this `BufWriter`, returning the underlying writer.
|
||||
///
|
||||
/// Note that any leftover data in the internal buffer is lost.
|
||||
pub fn into_inner(self) -> W {
|
||||
self.writer
|
||||
}
|
||||
}
|
||||
|
||||
fn get_poll_write<'a, 'b, W: AsyncWrite>(
|
||||
mut writer: Pin<&'a mut W>,
|
||||
cx: &'a mut Context<'b>,
|
||||
) -> impl for<'buf> FnMut(&'buf [u8]) -> Poll<io::Result<usize>> + use<'a, 'b, W> {
|
||||
move |buf| writer.as_mut().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite> BufWriter<W> {
|
||||
fn flush_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
let this = self.project();
|
||||
this.inner.flush_buf(&mut get_poll_write(this.writer, cx))
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite> AsyncWrite for BufWriter<W> {
|
||||
fn poll_write(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
let this = self.project();
|
||||
this.inner
|
||||
.poll_write(buf, &mut get_poll_write(this.writer, cx))
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
ready!(self.as_mut().flush_buf(cx))?;
|
||||
self.project().writer.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn $poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
ready!(self.as_mut().flush_buf(cx))?;
|
||||
self.project().writer.$poll_close(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite> AsyncBufWrite for BufWriter<W> {
|
||||
fn poll_partial_flush_buf(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<io::Result<Buffer<'_>>> {
|
||||
let this = self.project();
|
||||
this.inner
|
||||
.poll_partial_flush_buf(&mut get_poll_write(this.writer, cx))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use impl_buf_writer;
|
||||
@@ -0,0 +1,244 @@
|
||||
use crate::{
|
||||
codecs::DecodeV2,
|
||||
core::util::{PartialBuffer, WriteBuffer},
|
||||
generic::write::AsyncBufWrite,
|
||||
};
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{ready, Context, Poll},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
Decoding,
|
||||
Finishing,
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Decoder {
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl Default for Decoder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
state: State::Decoding,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
fn do_poll_write(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
mut writer: Pin<&mut dyn AsyncBufWrite>,
|
||||
decoder: &mut dyn DecodeV2,
|
||||
) -> Poll<io::Result<()>> {
|
||||
loop {
|
||||
let mut output = ready!(writer.as_mut().poll_partial_flush_buf(cx))?;
|
||||
let output = &mut output.write_buffer;
|
||||
|
||||
self.state = match self.state {
|
||||
State::Decoding => {
|
||||
if decoder.decode(input, output)? {
|
||||
State::Finishing
|
||||
} else {
|
||||
State::Decoding
|
||||
}
|
||||
}
|
||||
|
||||
State::Finishing => {
|
||||
if decoder.finish(output)? {
|
||||
State::Done
|
||||
} else {
|
||||
State::Finishing
|
||||
}
|
||||
}
|
||||
|
||||
State::Done => {
|
||||
return Poll::Ready(Err(io::Error::other("Write after end of stream")));
|
||||
}
|
||||
};
|
||||
|
||||
if let State::Done = self.state {
|
||||
return Poll::Ready(Ok(()));
|
||||
}
|
||||
|
||||
if input.unwritten().is_empty() {
|
||||
return Poll::Ready(Ok(()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_write(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
writer: Pin<&mut dyn AsyncBufWrite>,
|
||||
decoder: &mut dyn DecodeV2,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
if buf.is_empty() {
|
||||
return Poll::Ready(Ok(0));
|
||||
}
|
||||
|
||||
let mut input = PartialBuffer::new(buf);
|
||||
|
||||
match self.do_poll_write(cx, &mut input, writer, decoder)? {
|
||||
Poll::Pending if input.written().is_empty() => Poll::Pending,
|
||||
_ => Poll::Ready(Ok(input.written().len())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn do_poll_flush(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
mut writer: Pin<&mut dyn AsyncBufWrite>,
|
||||
decoder: &mut dyn DecodeV2,
|
||||
) -> Poll<io::Result<()>> {
|
||||
loop {
|
||||
let mut output = ready!(writer.as_mut().poll_partial_flush_buf(cx))?;
|
||||
let output = &mut output.write_buffer;
|
||||
|
||||
let (state, done) = match self.state {
|
||||
State::Decoding => {
|
||||
let done = decoder.flush(output)?;
|
||||
(State::Decoding, done)
|
||||
}
|
||||
|
||||
State::Finishing => {
|
||||
let before = output.written_len();
|
||||
if decoder.finish(output)? {
|
||||
(State::Done, false)
|
||||
} else if output.written_len() == before && !output.has_no_spare_space() {
|
||||
return Poll::Ready(Err(io::ErrorKind::UnexpectedEof.into()));
|
||||
} else {
|
||||
(State::Finishing, false)
|
||||
}
|
||||
}
|
||||
|
||||
State::Done => (State::Done, true),
|
||||
};
|
||||
|
||||
self.state = state;
|
||||
|
||||
if done {
|
||||
break Poll::Ready(Ok(()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn do_close(&mut self) {
|
||||
if let State::Decoding = self.state {
|
||||
self.state = State::Finishing;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_done(&self) -> bool {
|
||||
matches!(self.state, State::Done)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_decoder {
|
||||
($poll_close: tt) => {
|
||||
use crate::{
|
||||
codecs::DecodeV2, core::util::PartialBuffer, generic::write::Decoder as GenericDecoder,
|
||||
};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::task::ready;
|
||||
|
||||
pin_project! {
|
||||
#[derive(Debug)]
|
||||
pub struct Decoder<W, D> {
|
||||
#[pin]
|
||||
writer: BufWriter<W>,
|
||||
decoder: D,
|
||||
inner: GenericDecoder,
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite, D: DecodeV2> Decoder<W, D> {
|
||||
pub fn new(writer: W, decoder: D) -> Self {
|
||||
Self {
|
||||
writer: BufWriter::new(writer),
|
||||
decoder,
|
||||
inner: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W, D> Decoder<W, D> {
|
||||
pub fn get_ref(&self) -> &W {
|
||||
self.writer.get_ref()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self) -> &mut W {
|
||||
self.writer.get_mut()
|
||||
}
|
||||
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
|
||||
self.project().writer.get_pin_mut()
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> W {
|
||||
self.writer.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite, D: DecodeV2> Decoder<W, D> {
|
||||
fn do_poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
let mut this = self.project();
|
||||
|
||||
this.inner.do_poll_flush(cx, this.writer, this.decoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite, D: DecodeV2> AsyncWrite for Decoder<W, D> {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
let mut this = self.project();
|
||||
|
||||
this.inner.poll_write(cx, buf, this.writer, this.decoder)
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
ready!(self.as_mut().do_poll_flush(cx))?;
|
||||
self.project().writer.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn $poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
self.as_mut().project().inner.do_close();
|
||||
|
||||
ready!(self.as_mut().do_poll_flush(cx))?;
|
||||
|
||||
let this = self.project();
|
||||
if this.inner.is_done() {
|
||||
this.writer.$poll_close(cx)
|
||||
} else {
|
||||
Poll::Ready(Err(io::Error::other(
|
||||
"Attempt to close before finishing input",
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncBufRead, D> AsyncBufRead for Decoder<W, D> {
|
||||
fn poll_fill_buf(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<io::Result<&[u8]>> {
|
||||
self.get_pin_mut().poll_fill_buf(cx)
|
||||
}
|
||||
|
||||
fn consume(self: Pin<&mut Self>, amt: usize) {
|
||||
self.get_pin_mut().consume(amt)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use impl_decoder;
|
||||
@@ -0,0 +1,230 @@
|
||||
use crate::{
|
||||
codecs::EncodeV2,
|
||||
core::util::{PartialBuffer, WriteBuffer},
|
||||
generic::write::AsyncBufWrite,
|
||||
};
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{ready, Context, Poll},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
Encoding,
|
||||
Finishing,
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Encoder {
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl Default for Encoder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
state: State::Encoding,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
fn do_poll_write(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
mut writer: Pin<&mut dyn AsyncBufWrite>,
|
||||
encoder: &mut dyn EncodeV2,
|
||||
) -> Poll<io::Result<()>> {
|
||||
loop {
|
||||
let mut output = ready!(writer.as_mut().poll_partial_flush_buf(cx))?;
|
||||
let output = &mut output.write_buffer;
|
||||
|
||||
self.state = match self.state {
|
||||
State::Encoding => {
|
||||
encoder.encode(input, output)?;
|
||||
State::Encoding
|
||||
}
|
||||
|
||||
State::Finishing | State::Done => {
|
||||
break Poll::Ready(Err(io::Error::other("Write after close")));
|
||||
}
|
||||
};
|
||||
|
||||
if input.unwritten().is_empty() {
|
||||
break Poll::Ready(Ok(()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_write(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
writer: Pin<&mut dyn AsyncBufWrite>,
|
||||
encoder: &mut dyn EncodeV2,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
if buf.is_empty() {
|
||||
return Poll::Ready(Ok(0));
|
||||
}
|
||||
|
||||
let mut input = PartialBuffer::new(buf);
|
||||
|
||||
match self.do_poll_write(cx, &mut input, writer, encoder)? {
|
||||
Poll::Pending if input.written().is_empty() => Poll::Pending,
|
||||
_ => Poll::Ready(Ok(input.written().len())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn do_poll_flush(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
mut writer: Pin<&mut dyn AsyncBufWrite>,
|
||||
encoder: &mut dyn EncodeV2,
|
||||
) -> Poll<io::Result<()>> {
|
||||
loop {
|
||||
let mut output = ready!(writer.as_mut().poll_partial_flush_buf(cx))?;
|
||||
let output = &mut output.write_buffer;
|
||||
|
||||
let done = match self.state {
|
||||
State::Encoding => encoder.flush(output)?,
|
||||
|
||||
State::Finishing | State::Done => {
|
||||
break Poll::Ready(Err(io::Error::other("Flush after close")));
|
||||
}
|
||||
};
|
||||
|
||||
if done {
|
||||
break Poll::Ready(Ok(()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn do_poll_close(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
mut writer: Pin<&mut dyn AsyncBufWrite>,
|
||||
encoder: &mut dyn EncodeV2,
|
||||
) -> Poll<io::Result<()>> {
|
||||
loop {
|
||||
let mut output = ready!(writer.as_mut().poll_partial_flush_buf(cx))?;
|
||||
let output = &mut output.write_buffer;
|
||||
|
||||
self.state = match self.state {
|
||||
State::Encoding | State::Finishing => {
|
||||
if encoder.finish(output)? {
|
||||
State::Done
|
||||
} else {
|
||||
State::Finishing
|
||||
}
|
||||
}
|
||||
|
||||
State::Done => State::Done,
|
||||
};
|
||||
|
||||
if let State::Done = self.state {
|
||||
break Poll::Ready(Ok(()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_encoder {
|
||||
($poll_close: tt) => {
|
||||
use crate::{codecs::EncodeV2, generic::write::Encoder as GenericEncoder};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::task::ready;
|
||||
|
||||
pin_project! {
|
||||
#[derive(Debug)]
|
||||
pub struct Encoder<W, E> {
|
||||
#[pin]
|
||||
writer: BufWriter<W>,
|
||||
encoder: E,
|
||||
inner: GenericEncoder,
|
||||
}
|
||||
}
|
||||
|
||||
impl<W, E> Encoder<W, E> {
|
||||
pub fn get_ref(&self) -> &W {
|
||||
self.writer.get_ref()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self) -> &mut W {
|
||||
self.writer.get_mut()
|
||||
}
|
||||
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
|
||||
self.project().writer.get_pin_mut()
|
||||
}
|
||||
|
||||
pub(crate) fn get_encoder_ref(&self) -> &E {
|
||||
&self.encoder
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> W {
|
||||
self.writer.into_inner()
|
||||
}
|
||||
|
||||
pub fn new(writer: W, encoder: E) -> Self {
|
||||
Self {
|
||||
writer: BufWriter::new(writer),
|
||||
encoder,
|
||||
inner: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_capacity(writer: W, encoder: E, cap: usize) -> Self {
|
||||
Self {
|
||||
writer: BufWriter::with_capacity(cap, writer),
|
||||
encoder,
|
||||
inner: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncWrite, E: EncodeV2> AsyncWrite for Encoder<W, E> {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
let this = self.project();
|
||||
this.inner.poll_write(cx, buf, this.writer, this.encoder)
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
let mut this = self.project();
|
||||
|
||||
ready!(this
|
||||
.inner
|
||||
.do_poll_flush(cx, this.writer.as_mut(), this.encoder))?;
|
||||
this.writer.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn $poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
let mut this = self.project();
|
||||
|
||||
ready!(this
|
||||
.inner
|
||||
.do_poll_close(cx, this.writer.as_mut(), this.encoder))?;
|
||||
this.writer.$poll_close(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: AsyncBufRead, E> AsyncBufRead for Encoder<W, E> {
|
||||
fn poll_fill_buf(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<io::Result<&[u8]>> {
|
||||
self.get_pin_mut().poll_fill_buf(cx)
|
||||
}
|
||||
|
||||
fn consume(self: Pin<&mut Self>, amt: usize) {
|
||||
self.get_pin_mut().consume(amt)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use impl_encoder;
|
||||
@@ -0,0 +1,9 @@
|
||||
mod buf_write;
|
||||
mod buf_writer;
|
||||
mod decoder;
|
||||
mod encoder;
|
||||
|
||||
pub(crate) use buf_write::*;
|
||||
pub(crate) use buf_writer::*;
|
||||
pub(crate) use decoder::*;
|
||||
pub(crate) use encoder::*;
|
||||
Vendored
+172
@@ -0,0 +1,172 @@
|
||||
//! Adaptors between compression crates and Rust's modern asynchronous IO types.
|
||||
//!
|
||||
|
||||
//! # Feature Organization
|
||||
//!
|
||||
//! This crate is divided up along two axes, which can each be individually selected via Cargo
|
||||
//! features.
|
||||
//!
|
||||
//! All features are disabled by default, you should enable just the ones you need from the lists
|
||||
//! below.
|
||||
//!
|
||||
//! If you want to pull in everything there are three group features defined:
|
||||
//!
|
||||
|
||||
//! Feature | Does
|
||||
//! ---------|------
|
||||
//! `all` | Activates all implementations and algorithms.
|
||||
//! `all-implementations` | Activates all implementations, needs to be paired with a selection of algorithms
|
||||
//! `all-algorithms` | Activates all algorithms, needs to be paired with a selection of implementations
|
||||
//!
|
||||
|
||||
//! ## IO implementation
|
||||
//!
|
||||
//! The first division is which underlying asynchronous IO trait will be wrapped, these are
|
||||
//! available as separate features that have corresponding top-level modules:
|
||||
//!
|
||||
|
||||
//! Feature | Type
|
||||
//! ---------|------
|
||||
// TODO: Kill rustfmt on this section, `#![rustfmt::skip::attributes(cfg_attr)]` should do it, but
|
||||
// that's unstable
|
||||
#![allow(unexpected_cfgs)]
|
||||
# | [`futures::io::AsyncBufRead`](futures_io::AsyncBufRead), [`futures::io::AsyncWrite`](futures_io::AsyncWrite)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "futures-io"),
|
||||
doc = "`futures-io` (*inactive*) | `futures::io::AsyncBufRead`, `futures::io::AsyncWrite`"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
feature = "tokio",
|
||||
doc = "[`tokio`] | [`tokio::io::AsyncBufRead`](::tokio::io::AsyncBufRead), [`tokio::io::AsyncWrite`](::tokio::io::AsyncWrite)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "tokio"),
|
||||
doc = "`tokio` (*inactive*) | `tokio::io::AsyncBufRead`, `tokio::io::AsyncWrite`"
|
||||
)]
|
||||
//!
|
||||
|
||||
//! ## Compression algorithm
|
||||
//!
|
||||
//! The second division is which compression schemes to support, there are currently a few
|
||||
//! available choices, these determine which types will be available inside the above modules:
|
||||
//!
|
||||
|
||||
//! Feature | Types
|
||||
//! ---------|------
|
||||
#, [`BrotliDecoder`](?search=BrotliDecoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "brotli"),
|
||||
doc = "`brotli` (*inactive*) | `BrotliEncoder`, `BrotliDecoder`"
|
||||
)]
|
||||
#, [`BzDecoder`](?search=BzDecoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "bzip2"),
|
||||
doc = "`bzip2` (*inactive*) | `BzEncoder`, `BzDecoder`"
|
||||
)]
|
||||
#, [`DeflateDecoder`](?search=DeflateDecoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "deflate"),
|
||||
doc = "`deflate` (*inactive*) | `DeflateEncoder`, `DeflateDecoder`"
|
||||
)]
|
||||
#, [`GzipDecoder`](?search=GzipDecoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "gzip"),
|
||||
doc = "`gzip` (*inactive*) | `GzipEncoder`, `GzipDecoder`"
|
||||
)]
|
||||
#, [`Lz4Decoder`](?search=Lz4Decoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "lz4"),
|
||||
doc = "`lz4` (*inactive*) | `Lz4Encoder`, `Lz4Decoder`"
|
||||
)]
|
||||
#, [`LzmaDecoder`](?search=LzmaDecoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "lzma"),
|
||||
doc = "`lzma` (*inactive*) | `LzmaEncoder`, `LzmaDecoder`"
|
||||
)]
|
||||
#, [`XzDecoder`](?search=XzDecoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "xz"),
|
||||
doc = "`xz` (*inactive*) | `XzEncoder`, `XzDecoder`"
|
||||
)]
|
||||
#, [`ZlibDecoder`](?search=ZlibDecoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "zlib"),
|
||||
doc = "`zlib` (*inactive*) | `ZlibEncoder`, `ZlibDecoder`"
|
||||
)]
|
||||
#, [`ZstdDecoder`](?search=ZstdDecoder)"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "zstd"),
|
||||
doc = "`zstd` (*inactive*) | `ZstdEncoder`, `ZstdDecoder`"
|
||||
)]
|
||||
#"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(feature = "deflate64"),
|
||||
doc = "`deflate64` (*inactive*) | (encoder not implemented), `Deflate64Decoder`"
|
||||
)]
|
||||
//!
|
||||
|
||||
//! ## Multi-thread support
|
||||
//! The `xz` compression algorithm supports multi-threaded compression and decompression.
|
||||
//! Enable the `xz-parallel` feature to enable multi-threading support.
|
||||
//!
|
||||
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![cfg_attr(not(all), allow(unused))]
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
/// Generic, async runtime agonistc implementation of en/decoders
|
||||
mod generic;
|
||||
|
||||
#[cfg(feature = "futures-io")]
|
||||
pub mod futures;
|
||||
#[cfg(feature = "tokio")]
|
||||
pub mod tokio;
|
||||
|
||||
pub use compression_codecs as codecs;
|
||||
pub use compression_core as core;
|
||||
|
||||
pub use core::Level;
|
||||
|
||||
#[cfg(feature = "zstd")]
|
||||
pub use codecs::zstd::params as zstd;
|
||||
|
||||
#[cfg(feature = "lz4")]
|
||||
pub use codecs::lz4::params as lz4;
|
||||
|
||||
#[cfg(feature = "brotli")]
|
||||
pub use codecs::brotli::params as brotli;
|
||||
+379
@@ -0,0 +1,379 @@
|
||||
macro_rules! algos {
|
||||
(@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident <$inner:ident>
|
||||
{ @enc $($encoder_methods:tt)* }
|
||||
{ @dec $($decoder_methods:tt)* }
|
||||
) => {
|
||||
#[cfg(feature = $algo_s)]
|
||||
decoder! {
|
||||
#[doc = concat!("A ", $algo_s, " decoder, or decompressor")]
|
||||
#[cfg(feature = $algo_s)]
|
||||
$decoder<$inner>
|
||||
|
||||
{ $($decoder_methods)* }
|
||||
}
|
||||
|
||||
#[cfg(feature = $algo_s)]
|
||||
encoder! {
|
||||
#[doc = concat!("A ", $algo_s, " encoder, or compressor.")]
|
||||
#[cfg(feature = $algo_s)]
|
||||
$encoder<$inner> {
|
||||
pub fn new(inner: $inner) -> Self {
|
||||
Self::with_quality(inner, crate::core::Level::Default)
|
||||
}
|
||||
}
|
||||
|
||||
{ $($encoder_methods)* }
|
||||
}
|
||||
};
|
||||
|
||||
(@algo $algo:ident [$algo_s:expr] $decoder:ident $encoder:ident <$inner:ident>
|
||||
{ @dec $($decoder_methods:tt)* }
|
||||
) => {
|
||||
#[cfg(feature = $algo_s)]
|
||||
decoder! {
|
||||
#[doc = concat!("A ", $algo_s, " decoder, or decompressor")]
|
||||
#[cfg(feature = $algo_s)]
|
||||
$decoder<$inner>
|
||||
|
||||
{ $($decoder_methods)* }
|
||||
}
|
||||
};
|
||||
|
||||
($($mod:ident)::+ <$inner:ident>) => {
|
||||
algos!(@algo brotli ["brotli"] BrotliDecoder BrotliEncoder <$inner>
|
||||
{ @enc
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
// let params = brotli::enc::backward_references::BrotliEncoderParams::default();
|
||||
let params = crate::codecs::brotli::params::EncoderParams::default();
|
||||
let params = params.quality(level);
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::BrotliEncoder::new(params),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new encoder, using the specified compression level and parameters, which
|
||||
/// will read uncompressed data from the given stream and emit a compressed stream.
|
||||
pub fn with_params(
|
||||
inner: $inner,
|
||||
params: crate::codecs::brotli::params::EncoderParams,
|
||||
) -> Self {
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::BrotliEncoder::new(params),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
{ @dec }
|
||||
);
|
||||
|
||||
algos!(@algo bzip2 ["bzip2"] BzDecoder BzEncoder <$inner>
|
||||
{ @enc
|
||||
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
let params = crate::codecs::bzip2::params::Bzip2EncoderParams::from(level);
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::BzEncoder::new(params, 0),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
{ @dec }
|
||||
);
|
||||
|
||||
algos!(@algo deflate ["deflate"] DeflateDecoder DeflateEncoder <$inner>
|
||||
{ @enc
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
let mut params = crate::codecs::flate::params::FlateEncoderParams::from(level);
|
||||
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::DeflateEncoder::new(params),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the total number of input bytes which have been processed by this compression object.
|
||||
pub fn total_in(&self) -> u64 {
|
||||
self.inner.get_encoder_ref().get_ref().get_ref().total_in()
|
||||
}
|
||||
|
||||
/// Returns the total number of output bytes which have been produced by this compression object.
|
||||
pub fn total_out(&self) -> u64 {
|
||||
self.inner.get_encoder_ref().get_ref().get_ref().total_out()
|
||||
}
|
||||
}
|
||||
{ @dec }
|
||||
);
|
||||
|
||||
algos!(@algo deflate ["deflate64"] Deflate64Decoder Deflate64Encoder <$inner>
|
||||
{ @dec }
|
||||
);
|
||||
|
||||
algos!(@algo gzip ["gzip"] GzipDecoder GzipEncoder <$inner>
|
||||
{ @enc
|
||||
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
let params = crate::codecs::flate::params::FlateEncoderParams::from(level);
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::GzipEncoder::new(params),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
{ @dec }
|
||||
);
|
||||
|
||||
algos!(@algo zlib ["zlib"] ZlibDecoder ZlibEncoder <$inner>
|
||||
{ @enc
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
let params = crate::codecs::flate::params::FlateEncoderParams::from(level);
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::ZlibEncoder::new(params),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the total number of input bytes which have been processed by this compression object.
|
||||
pub fn total_in(&self) -> u64 {
|
||||
self.inner.get_encoder_ref().get_ref().get_ref().total_in()
|
||||
}
|
||||
|
||||
/// Returns the total number of output bytes which have been produced by this compression object.
|
||||
pub fn total_out(&self) -> u64 {
|
||||
self.inner.get_encoder_ref().get_ref().get_ref().total_out()
|
||||
}
|
||||
}
|
||||
{ @dec }
|
||||
);
|
||||
|
||||
algos!(@algo zstd ["zstd"] ZstdDecoder ZstdEncoder <$inner>
|
||||
{ @enc
|
||||
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
let params = crate::codecs::zstd::params::CParameter::quality(level);
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::ZstdEncoder::new(params),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new encoder, using the specified compression level and parameters, which
|
||||
/// will read uncompressed data from the given stream and emit a compressed stream.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this function is called with a [`CParameter::nb_workers()`] parameter and
|
||||
/// the `zstdmt` crate feature is _not_ enabled.
|
||||
///
|
||||
/// [`CParameter::nb_workers()`]: crate::codecs::zstd::params::CParameter
|
||||
//
|
||||
// TODO: remove panic note on next breaking release, along with `CParameter::nb_workers`
|
||||
// change
|
||||
pub fn with_quality_and_params(inner: $inner, level: crate::core::Level, params: &[crate::codecs::zstd::params::CParameter]) -> Self {
|
||||
let level = crate::codecs::zstd::params::CParameter::quality(level);
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::ZstdEncoder::new_with_params(level, params),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new encoder, using the specified compression level and pre-trained
|
||||
/// dictionary, which will read uncompressed data from the given stream and emit a
|
||||
/// compressed stream.
|
||||
///
|
||||
/// Dictionaries provide better compression ratios for small files, but are required to
|
||||
/// be present during decompression.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error when `dictionary` is not valid.
|
||||
pub fn with_dict(inner: $inner, level: crate::core::Level, dictionary: &[u8]) -> ::std::io::Result<Self> {
|
||||
let level = crate::codecs::zstd::params::CParameter::quality(level);
|
||||
Ok(Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::ZstdEncoder::new_with_dict(level, dictionary)?,
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
{ @dec
|
||||
/// Creates a new decoder, using the specified parameters, which will read compressed
|
||||
/// data from the given stream and emit a decompressed stream.
|
||||
pub fn with_params(inner: $inner, params: &[crate::codecs::zstd::params::DParameter]) -> Self {
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Decoder::new(
|
||||
inner,
|
||||
crate::codecs::ZstdDecoder::new_with_params(params),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new decoder, using the specified compression level and pre-trained
|
||||
/// dictionary, which will read compressed data from the given stream and emit an
|
||||
/// uncompressed stream.
|
||||
///
|
||||
/// Dictionaries provide better compression ratios for small files, but are required to
|
||||
/// be present during decompression. The dictionary used must be the same as the one
|
||||
/// used for compression.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns error when `dictionary` is not valid.
|
||||
pub fn with_dict(inner: $inner, dictionary: &[u8]) -> ::std::io::Result<Self> {
|
||||
Ok(Self {
|
||||
inner: crate::$($mod::)+generic::Decoder::new(
|
||||
inner,
|
||||
crate::codecs::ZstdDecoder::new_with_dict(dictionary)?,
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
algos!(@algo xz ["xz"] XzDecoder XzEncoder <$inner>
|
||||
{ @enc
|
||||
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::XzEncoder::new(level),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new multi-threaded encoder.
|
||||
///
|
||||
/// Note that flushing will severely impact multi-threaded performance.
|
||||
#[cfg(feature = "xz-parallel")]
|
||||
pub fn parallel(inner: $inner, level: crate::core::Level, threads: std::num::NonZeroU32) -> Self {
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::new(
|
||||
inner,
|
||||
crate::codecs::XzEncoder::parallel(threads, level),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
{ @dec
|
||||
/// Creates a new decoder with the specified limit of memory.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// An IO error may be returned during decoding if the specified limit is too small.
|
||||
pub fn with_mem_limit(read: $inner, memlimit: u64) -> Self {
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Decoder::new(
|
||||
read,
|
||||
crate::codecs::XzDecoder::with_memlimit(memlimit),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new multi-threaded decoder.
|
||||
#[cfg(feature = "xz-parallel")]
|
||||
pub fn parallel(read: $inner, threads: std::num::NonZeroU32) -> Self {
|
||||
use std::convert::TryInto;
|
||||
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Decoder::new(
|
||||
read,
|
||||
crate::codecs::XzDecoder::parallel(threads, usize::MAX.try_into().unwrap()),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new multi-threaded decoder with the specified limit of memory.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// An IO error may be returned during decoding if the specified limit is too small.
|
||||
#[cfg(feature = "xz-parallel")]
|
||||
pub fn parallel_with_mem_limit(read: $inner, threads: std::num::NonZeroU32, memlimit: u64) -> Self {
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Decoder::new(
|
||||
read,
|
||||
crate::codecs::XzDecoder::parallel(threads, memlimit),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
algos!(@algo lzma ["lzma"] LzmaDecoder LzmaEncoder <$inner>
|
||||
{ @enc
|
||||
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
let encoder = crate::codecs::LzmaEncoder::new(level);
|
||||
let inner = crate::$($mod::)+generic::Encoder::new(inner, encoder);
|
||||
Self {
|
||||
inner
|
||||
}
|
||||
}
|
||||
}
|
||||
{ @dec
|
||||
/// Creates a new decoder with the specified limit of memory.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// An IO error may be returned during decoding if the specified limit is too small.
|
||||
pub fn with_mem_limit(read: $inner, memlimit: u64) -> Self {
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Decoder::new(
|
||||
read,
|
||||
crate::codecs::LzmaDecoder::with_memlimit(memlimit),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
algos!(@algo lz4 ["lz4"] Lz4Decoder Lz4Encoder <$inner>
|
||||
{ @enc
|
||||
|
||||
pub fn with_quality(inner: $inner, level: crate::core::Level) -> Self {
|
||||
Self::with_quality_and_params(inner, level, crate::codecs::lz4::params::EncoderParams::default())
|
||||
}
|
||||
|
||||
/// Creates a new encoder, using the specified compression level and parameters, which
|
||||
/// will read uncompressed data from the given stream and emit a compressed stream.
|
||||
pub fn with_quality_and_params(
|
||||
inner: $inner,
|
||||
level: crate::core::Level,
|
||||
mut params: crate::codecs::lz4::params::EncoderParams,
|
||||
) -> Self {
|
||||
let params = params.level(level);
|
||||
let encoder = crate::codecs::Lz4Encoder::new(params);
|
||||
let cap = encoder.buffer_size();
|
||||
Self {
|
||||
inner: crate::$($mod::)+generic::Encoder::with_capacity(
|
||||
inner,
|
||||
encoder,
|
||||
cap,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
{ @dec }
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
use crate::{
|
||||
codecs::DecodeV2,
|
||||
core::util::{PartialBuffer, WriteBuffer},
|
||||
generic::bufread::impl_decoder,
|
||||
};
|
||||
|
||||
use std::{
|
||||
io::{IoSlice, Result},
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
|
||||
|
||||
impl_decoder!();
|
||||
|
||||
impl<R: AsyncBufRead, D: DecodeV2> AsyncRead for Decoder<R, D> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut ReadBuf<'_>,
|
||||
) -> Poll<Result<()>> {
|
||||
super::poll_read(buf, |output| self.do_poll_read(cx, output))
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncWrite, D> AsyncWrite for Decoder<R, D> {
|
||||
fn poll_write(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize>> {
|
||||
self.get_pin_mut().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_write_vectored(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
mut bufs: &[IoSlice<'_>],
|
||||
) -> Poll<Result<usize>> {
|
||||
self.get_pin_mut().poll_write_vectored(cx, bufs)
|
||||
}
|
||||
|
||||
fn is_write_vectored(&self) -> bool {
|
||||
self.get_ref().is_write_vectored()
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
self.get_pin_mut().poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
self.get_pin_mut().poll_shutdown(cx)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
use crate::{
|
||||
codecs::EncodeV2,
|
||||
core::util::{PartialBuffer, WriteBuffer},
|
||||
generic::bufread::impl_encoder,
|
||||
};
|
||||
use std::{
|
||||
io::{IoSlice, Result},
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
|
||||
|
||||
impl_encoder!();
|
||||
|
||||
impl<R: AsyncBufRead, E: EncodeV2> AsyncRead for Encoder<R, E> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut ReadBuf<'_>,
|
||||
) -> Poll<Result<()>> {
|
||||
super::poll_read(buf, |output| self.do_poll_read(cx, output))
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: AsyncWrite, E> AsyncWrite for Encoder<R, E> {
|
||||
fn poll_write(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize>> {
|
||||
self.get_pin_mut().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_write_vectored(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
mut bufs: &[IoSlice<'_>],
|
||||
) -> Poll<Result<usize>> {
|
||||
self.get_pin_mut().poll_write_vectored(cx, bufs)
|
||||
}
|
||||
|
||||
fn is_write_vectored(&self) -> bool {
|
||||
self.get_ref().is_write_vectored()
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
self.get_pin_mut().poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
self.get_pin_mut().poll_shutdown(cx)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
mod decoder;
|
||||
mod encoder;
|
||||
|
||||
pub use self::{decoder::Decoder, encoder::Encoder};
|
||||
|
||||
use crate::core::util::WriteBuffer;
|
||||
use std::{io::Result, task::Poll};
|
||||
use tokio::io::ReadBuf;
|
||||
|
||||
fn poll_read(
|
||||
buf: &mut ReadBuf<'_>,
|
||||
do_poll_read: impl FnOnce(&mut WriteBuffer<'_>) -> Poll<Result<()>>,
|
||||
) -> Poll<Result<()>> {
|
||||
if buf.remaining() == 0 {
|
||||
return Poll::Ready(Ok(()));
|
||||
}
|
||||
|
||||
let initialized = buf.initialized().len() - buf.filled().len();
|
||||
// Safety: `WriteBuffer` has the same safety invariant as `ReadBuf`
|
||||
let mut output = WriteBuffer::new_uninitialized(unsafe { buf.unfilled_mut() });
|
||||
// Safety: `ReadBuf` ensures that it is initialized
|
||||
unsafe { output.assume_init(initialized) };
|
||||
|
||||
let res = do_poll_read(&mut output);
|
||||
|
||||
let initialized = output.initialized_len();
|
||||
let written = output.written_len();
|
||||
|
||||
// Safety: We trust our implementation to have properly initialized it
|
||||
unsafe { buf.assume_init(initialized) };
|
||||
buf.advance(written);
|
||||
|
||||
res
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
macro_rules! decoder {
|
||||
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
|
||||
pin_project_lite::pin_project! {
|
||||
$(#[$attr])*
|
||||
///
|
||||
/// This structure implements an [`AsyncRead`](tokio::io::AsyncRead) interface and will
|
||||
/// read compressed data from an underlying stream and emit a stream of uncompressed data.
|
||||
#[derive(Debug)]
|
||||
pub struct $name<$inner> {
|
||||
#[pin]
|
||||
inner: crate::tokio::bufread::Decoder<$inner, crate::codecs::$name>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncBufRead> $name<$inner> {
|
||||
/// Creates a new decoder which will read compressed data from the given stream and
|
||||
/// emit an uncompressed stream.
|
||||
pub fn new(read: $inner) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::tokio::bufread::Decoder::new(read, crate::codecs::$name::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new decoder with the given codec, which will read compressed data from the given stream and
|
||||
/// emit an uncompressed stream.
|
||||
pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::tokio::bufread::Decoder::new(read, codec)
|
||||
}
|
||||
}
|
||||
|
||||
$($($inherent_methods)*)*
|
||||
}
|
||||
|
||||
impl<$inner> $name<$inner> {
|
||||
/// Configure multi-member/frame decoding, if enabled this will reset the decoder state
|
||||
/// when reaching the end of a compressed member/frame and expect either EOF or another
|
||||
/// compressed member/frame to follow it in the stream.
|
||||
pub fn multiple_members(&mut self, enabled: bool) {
|
||||
self.inner.multiple_members(enabled);
|
||||
}
|
||||
|
||||
/// Acquires a reference to the underlying reader that this decoder is wrapping.
|
||||
pub fn get_ref(&self) -> &$inner {
|
||||
self.inner.get_ref()
|
||||
}
|
||||
|
||||
/// Acquires a mutable reference to the underlying reader that this decoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this decoder.
|
||||
pub fn get_mut(&mut self) -> &mut $inner {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Acquires a pinned mutable reference to the underlying reader that this decoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this decoder.
|
||||
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
|
||||
self.project().inner.get_pin_mut()
|
||||
}
|
||||
|
||||
/// Consumes this decoder returning the underlying reader.
|
||||
///
|
||||
/// Note that this may discard internal state of this decoder, so care should be taken
|
||||
/// to avoid losing resources when this is called.
|
||||
pub fn into_inner(self) -> $inner {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncRead for $name<$inner> {
|
||||
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<()>> {
|
||||
self.project().inner.poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<$inner> {
|
||||
fn poll_write(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.get_pin_mut().poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.get_pin_mut().poll_shutdown(cx)
|
||||
}
|
||||
|
||||
fn poll_write_vectored(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
bufs: &[std::io::IoSlice<'_>],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_write_vectored(cx, bufs)
|
||||
}
|
||||
|
||||
fn is_write_vectored(&self) -> bool {
|
||||
self.get_ref().is_write_vectored()
|
||||
}
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
use crate::core::util::{_assert_send, _assert_sync};
|
||||
use core::pin::Pin;
|
||||
use tokio::io::AsyncBufRead;
|
||||
|
||||
_assert_send::<$name<Pin<Box<dyn AsyncBufRead + Send>>>>();
|
||||
_assert_sync::<$name<Pin<Box<dyn AsyncBufRead + Sync>>>>();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
macro_rules! encoder {
|
||||
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
|
||||
pin_project_lite::pin_project! {
|
||||
$(#[$attr])*
|
||||
///
|
||||
/// This structure implements an [`AsyncRead`](tokio::io::AsyncRead) interface and will
|
||||
/// read uncompressed data from an underlying stream and emit a stream of compressed data.
|
||||
#[derive(Debug)]
|
||||
pub struct $name<$inner> {
|
||||
#[pin]
|
||||
inner: crate::tokio::bufread::Encoder<$inner, crate::codecs::$name>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncBufRead> $name<$inner> {
|
||||
$(
|
||||
/// Creates a new encoder which will read uncompressed data from the given stream
|
||||
/// and emit an compressed stream.
|
||||
///
|
||||
$($inherent_methods)*
|
||||
)*
|
||||
|
||||
/// Creates a new encoder with the given codec, which will read uncompressed data from the given stream
|
||||
/// and emit an compressed stream.
|
||||
pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::tokio::bufread::Encoder::new(read, codec)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner> $name<$inner> {
|
||||
/// Acquires a reference to the underlying reader that this encoder is wrapping.
|
||||
pub fn get_ref(&self) -> &$inner {
|
||||
self.inner.get_ref()
|
||||
}
|
||||
|
||||
/// Acquires a mutable reference to the underlying reader that this encoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this encoder.
|
||||
pub fn get_mut(&mut self) -> &mut $inner {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Acquires a pinned mutable reference to the underlying reader that this encoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this encoder.
|
||||
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
|
||||
self.project().inner.get_pin_mut()
|
||||
}
|
||||
|
||||
/// Consumes this encoder returning the underlying reader.
|
||||
///
|
||||
/// Note that this may discard internal state of this encoder, so care should be taken
|
||||
/// to avoid losing resources when this is called.
|
||||
pub fn into_inner(self) -> $inner {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncRead for $name<$inner> {
|
||||
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<()>> {
|
||||
self.project().inner.poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<$inner> {
|
||||
fn poll_write(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.get_pin_mut().poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.get_pin_mut().poll_shutdown(cx)
|
||||
}
|
||||
|
||||
fn poll_write_vectored(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
bufs: &[std::io::IoSlice<'_>],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.get_pin_mut().poll_write_vectored(cx, bufs)
|
||||
}
|
||||
|
||||
fn is_write_vectored(&self) -> bool {
|
||||
self.get_ref().is_write_vectored()
|
||||
}
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
use crate::core::util::{_assert_send, _assert_sync};
|
||||
use core::pin::Pin;
|
||||
use tokio::io::AsyncBufRead;
|
||||
|
||||
_assert_send::<$name<Pin<Box<dyn AsyncBufRead + Send>>>>();
|
||||
_assert_sync::<$name<Pin<Box<dyn AsyncBufRead + Sync>>>>();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#[macro_use]
|
||||
mod decoder;
|
||||
#[macro_use]
|
||||
mod encoder;
|
||||
@@ -0,0 +1,10 @@
|
||||
//! Types which operate over [`AsyncBufRead`](::tokio::io::AsyncBufRead) streams, both encoders and
|
||||
//! decoders for various formats.
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
mod generic;
|
||||
|
||||
pub(crate) use generic::{Decoder, Encoder};
|
||||
|
||||
algos!(tokio::bufread<R>);
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
//! Implementations for IO traits exported by [`tokio` v1.x](::tokio).
|
||||
|
||||
pub mod bufread;
|
||||
pub mod write;
|
||||
@@ -0,0 +1,13 @@
|
||||
// Originally sourced from `futures_util::io::buf_writer`, needs to be redefined locally so that
|
||||
// the `AsyncBufWrite` impl can access its internals, and changed a bit to make it more efficient
|
||||
// with those methods.
|
||||
|
||||
use crate::generic::write::impl_buf_writer;
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tokio::io::AsyncWrite;
|
||||
|
||||
impl_buf_writer!(poll_shutdown);
|
||||
@@ -0,0 +1,19 @@
|
||||
use crate::{generic::write::impl_decoder, tokio::write::BufWriter};
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
|
||||
|
||||
impl_decoder!(poll_shutdown);
|
||||
|
||||
impl<W: AsyncRead, D> AsyncRead for Decoder<W, D> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut ReadBuf<'_>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
self.get_pin_mut().poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
use crate::{generic::write::impl_encoder, tokio::write::BufWriter};
|
||||
use std::{
|
||||
io,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
|
||||
|
||||
impl_encoder!(poll_shutdown);
|
||||
|
||||
impl<W: AsyncRead, E> AsyncRead for Encoder<W, E> {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut ReadBuf<'_>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
self.get_pin_mut().poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
mod decoder;
|
||||
mod encoder;
|
||||
|
||||
pub use self::{decoder::Decoder, encoder::Encoder};
|
||||
@@ -0,0 +1,124 @@
|
||||
macro_rules! decoder {
|
||||
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
|
||||
pin_project_lite::pin_project! {
|
||||
$(#[$attr])*
|
||||
///
|
||||
/// This structure implements an [`AsyncWrite`](tokio::io::AsyncWrite) interface and will
|
||||
/// take in compressed data and write it uncompressed to an underlying stream.
|
||||
#[derive(Debug)]
|
||||
pub struct $name<$inner> {
|
||||
#[pin]
|
||||
inner: crate::tokio::write::Decoder<$inner, crate::codecs::$name>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncWrite> $name<$inner> {
|
||||
/// Creates a new decoder which will take in compressed data and write it, uncompressed,
|
||||
/// to the given stream.
|
||||
pub fn new(read: $inner) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::tokio::write::Decoder::new(read, crate::codecs::$name::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new decoder which will take in compressed data and write it, uncompressed,
|
||||
/// to the given stream.
|
||||
pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::tokio::write::Decoder::new(read, codec)
|
||||
}
|
||||
}
|
||||
|
||||
$($($inherent_methods)*)*
|
||||
}
|
||||
|
||||
impl<$inner> $name<$inner> {
|
||||
/// Acquires a reference to the underlying reader that this decoder is wrapping.
|
||||
pub fn get_ref(&self) -> &$inner {
|
||||
self.inner.get_ref()
|
||||
}
|
||||
|
||||
/// Acquires a mutable reference to the underlying reader that this decoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this decoder.
|
||||
pub fn get_mut(&mut self) -> &mut $inner {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Acquires a pinned mutable reference to the underlying reader that this decoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the reader which
|
||||
/// may otherwise confuse this decoder.
|
||||
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
|
||||
self.project().inner.get_pin_mut()
|
||||
}
|
||||
|
||||
/// Consumes this decoder returning the underlying reader.
|
||||
///
|
||||
/// Note that this may discard internal state of this decoder, so care should be taken
|
||||
/// to avoid losing resources when this is called.
|
||||
pub fn into_inner(self) -> $inner {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<$inner> {
|
||||
fn poll_write(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.project().inner.poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_shutdown(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncRead> tokio::io::AsyncRead for $name<$inner> {
|
||||
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<()>> {
|
||||
self.get_pin_mut().poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncBufRead for $name<$inner> {
|
||||
fn poll_fill_buf(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>
|
||||
) -> std::task::Poll<std::io::Result<&[u8]>> {
|
||||
self.get_pin_mut().poll_fill_buf(cx)
|
||||
}
|
||||
|
||||
fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
|
||||
self.get_pin_mut().consume(amt)
|
||||
}
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
use crate::core::util::{_assert_send, _assert_sync};
|
||||
use core::pin::Pin;
|
||||
use tokio::io::AsyncWrite;
|
||||
|
||||
_assert_send::<$name<Pin<Box<dyn AsyncWrite + Send>>>>();
|
||||
_assert_sync::<$name<Pin<Box<dyn AsyncWrite + Sync>>>>();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
macro_rules! encoder {
|
||||
($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
|
||||
pin_project_lite::pin_project! {
|
||||
$(#[$attr])*
|
||||
///
|
||||
/// This structure implements an [`AsyncWrite`](tokio::io::AsyncWrite) interface and will
|
||||
/// take in uncompressed data and write it compressed to an underlying stream.
|
||||
#[derive(Debug)]
|
||||
pub struct $name<$inner> {
|
||||
#[pin]
|
||||
inner: crate::tokio::write::Encoder<$inner, crate::codecs::$name>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncWrite> $name<$inner> {
|
||||
$(
|
||||
/// Creates a new encoder which will take in uncompressed data and write it
|
||||
/// compressed to the given stream.
|
||||
///
|
||||
$($inherent_methods)*
|
||||
)*
|
||||
|
||||
/// Creates a new encoder with the given codec, which will take in uncompressed data and write it,
|
||||
/// compressed, to the given stream.
|
||||
pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
|
||||
$name {
|
||||
inner: crate::tokio::write::Encoder::new(read, codec)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner> $name<$inner> {
|
||||
/// Acquires a reference to the underlying writer that this encoder is wrapping.
|
||||
pub fn get_ref(&self) -> &$inner {
|
||||
self.inner.get_ref()
|
||||
}
|
||||
|
||||
/// Acquires a mutable reference to the underlying writer that this encoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the writer which
|
||||
/// may otherwise confuse this encoder.
|
||||
pub fn get_mut(&mut self) -> &mut $inner {
|
||||
self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Acquires a pinned mutable reference to the underlying writer that this encoder is
|
||||
/// wrapping.
|
||||
///
|
||||
/// Note that care must be taken to avoid tampering with the state of the writer which
|
||||
/// may otherwise confuse this encoder.
|
||||
pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
|
||||
self.project().inner.get_pin_mut()
|
||||
}
|
||||
|
||||
/// Consumes this encoder returning the underlying writer.
|
||||
///
|
||||
/// Note that this may discard internal state of this encoder, so care should be taken
|
||||
/// to avoid losing resources when this is called.
|
||||
pub fn into_inner(self) -> $inner {
|
||||
self.inner.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<$inner> {
|
||||
fn poll_write(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> std::task::Poll<std::io::Result<usize>> {
|
||||
self.project().inner.poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
self.project().inner.poll_shutdown(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncRead> tokio::io::AsyncRead for $name<$inner> {
|
||||
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<()>> {
|
||||
self.get_pin_mut().poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncBufRead for $name<$inner> {
|
||||
fn poll_fill_buf(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>
|
||||
) -> std::task::Poll<std::io::Result<&[u8]>> {
|
||||
self.get_pin_mut().poll_fill_buf(cx)
|
||||
}
|
||||
|
||||
fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
|
||||
self.get_pin_mut().consume(amt)
|
||||
}
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
use crate::core::util::{_assert_send, _assert_sync};
|
||||
use core::pin::Pin;
|
||||
use tokio::io::AsyncWrite;
|
||||
|
||||
_assert_send::<$name<Pin<Box<dyn AsyncWrite + Send>>>>();
|
||||
_assert_sync::<$name<Pin<Box<dyn AsyncWrite + Sync>>>>();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#[macro_use]
|
||||
mod decoder;
|
||||
#[macro_use]
|
||||
mod encoder;
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
//! Types which operate over [`AsyncWrite`](tokio::io::AsyncWrite) streams, both encoders and
|
||||
//! decoders for various formats.
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
mod generic;
|
||||
|
||||
mod buf_writer;
|
||||
|
||||
use self::{
|
||||
buf_writer::BufWriter,
|
||||
generic::{Decoder, Encoder},
|
||||
};
|
||||
use crate::generic::write::AsyncBufWrite;
|
||||
|
||||
algos!(tokio::write<W>);
|
||||
Reference in New Issue
Block a user