1414 W : io:: Write ,
1515{
1616 Uncompressed ( W ) ,
17- ZStd ( zstd:: stream:: AutoFinishEncoder < ' a , W > ) ,
17+ Zstd ( zstd:: stream:: AutoFinishEncoder < ' a , W > ) ,
1818}
1919
2020impl < ' a , W > DynWriter < ' a , W >
@@ -28,15 +28,15 @@ where
2828 pub fn new ( writer : W , compression : Compression ) -> Result < Self > {
2929 match compression {
3030 Compression :: None => Ok ( Self ( DynWriterImpl :: Uncompressed ( writer) ) ) ,
31- Compression :: ZStd => zstd_encoder ( writer) . map ( |enc| Self ( DynWriterImpl :: ZStd ( enc) ) ) ,
31+ Compression :: ZStd => zstd_encoder ( writer) . map ( |enc| Self ( DynWriterImpl :: Zstd ( enc) ) ) ,
3232 }
3333 }
3434
3535 /// Returns a mutable reference to the underlying writer.
3636 pub fn get_mut ( & mut self ) -> & mut W {
3737 match & mut self . 0 {
3838 DynWriterImpl :: Uncompressed ( w) => w,
39- DynWriterImpl :: ZStd ( enc) => enc. get_mut ( ) ,
39+ DynWriterImpl :: Zstd ( enc) => enc. get_mut ( ) ,
4040 }
4141 }
4242}
@@ -48,39 +48,41 @@ where
4848 fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
4949 match & mut self . 0 {
5050 DynWriterImpl :: Uncompressed ( writer) => writer. write ( buf) ,
51- DynWriterImpl :: ZStd ( writer) => writer. write ( buf) ,
51+ DynWriterImpl :: Zstd ( writer) => writer. write ( buf) ,
5252 }
5353 }
5454
5555 fn flush ( & mut self ) -> io:: Result < ( ) > {
5656 match & mut self . 0 {
5757 DynWriterImpl :: Uncompressed ( writer) => writer. flush ( ) ,
58- DynWriterImpl :: ZStd ( writer) => writer. flush ( ) ,
58+ DynWriterImpl :: Zstd ( writer) => writer. flush ( ) ,
5959 }
6060 }
6161
6262 fn write_vectored ( & mut self , bufs : & [ io:: IoSlice < ' _ > ] ) -> io:: Result < usize > {
6363 match & mut self . 0 {
6464 DynWriterImpl :: Uncompressed ( writer) => writer. write_vectored ( bufs) ,
65- DynWriterImpl :: ZStd ( writer) => writer. write_vectored ( bufs) ,
65+ DynWriterImpl :: Zstd ( writer) => writer. write_vectored ( bufs) ,
6666 }
6767 }
6868
6969 fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
7070 match & mut self . 0 {
7171 DynWriterImpl :: Uncompressed ( writer) => writer. write_all ( buf) ,
72- DynWriterImpl :: ZStd ( writer) => writer. write_all ( buf) ,
72+ DynWriterImpl :: Zstd ( writer) => writer. write_all ( buf) ,
7373 }
7474 }
7575
7676 fn write_fmt ( & mut self , fmt : std:: fmt:: Arguments < ' _ > ) -> io:: Result < ( ) > {
7777 match & mut self . 0 {
7878 DynWriterImpl :: Uncompressed ( writer) => writer. write_fmt ( fmt) ,
79- DynWriterImpl :: ZStd ( writer) => writer. write_fmt ( fmt) ,
79+ DynWriterImpl :: Zstd ( writer) => writer. write_fmt ( fmt) ,
8080 }
8181 }
8282}
8383
84+ #[ cfg( feature = "async" ) ]
85+ pub use r#async:: DynBufWriter as DynAsyncBufWriter ;
8486#[ cfg( feature = "async" ) ]
8587pub use r#async:: DynWriter as DynAsyncWriter ;
8688
@@ -92,11 +94,91 @@ mod r#async {
9294 } ;
9395
9496 use async_compression:: tokio:: write:: ZstdEncoder ;
95- use tokio:: io;
97+ use tokio:: io:: { self , BufWriter } ;
9698
9799 use crate :: { encode:: async_zstd_encoder, enums:: Compression } ;
98100
101+ /// An object that allows for abstracting over compressed and uncompressed output
102+ /// with buffering.
103+ pub struct DynBufWriter < W , B = W > ( DynBufWriterImpl < W , B > )
104+ where
105+ W : io:: AsyncWriteExt + Unpin ,
106+ B : io:: AsyncWriteExt + Unpin ;
107+
108+ enum DynBufWriterImpl < W , B >
109+ where
110+ W : io:: AsyncWriteExt + Unpin ,
111+ B : io:: AsyncWriteExt + Unpin ,
112+ {
113+ Uncompressed ( B ) ,
114+ Zstd ( ZstdEncoder < W > ) ,
115+ }
116+
117+ impl < W > DynBufWriter < W >
118+ where
119+ W : io:: AsyncWriteExt + Unpin ,
120+ {
121+ /// Creates a new instance of [`DynWriter`] which will wrap `writer` with
122+ /// `compression`.
123+ pub fn new ( writer : W , compression : Compression ) -> Self {
124+ Self ( match compression {
125+ Compression :: None => DynBufWriterImpl :: Uncompressed ( writer) ,
126+ Compression :: ZStd => DynBufWriterImpl :: Zstd ( async_zstd_encoder ( writer) ) ,
127+ } )
128+ }
129+ }
130+
131+ impl < W > DynBufWriter < W , BufWriter < W > >
132+ where
133+ W : io:: AsyncWriteExt + Unpin ,
134+ {
135+ /// Creates a new instance of [`DynWriter`], wrapping `writer` in a `BufWriter`.
136+ pub fn new_buffered ( writer : W , compression : Compression ) -> Self {
137+ Self ( match compression {
138+ Compression :: None => DynBufWriterImpl :: Uncompressed ( BufWriter :: new ( writer) ) ,
139+ // `ZstdEncoder` already wraps `W` in a `BufWriter`, cf.
140+ // https://github.com/Nullus157/async-compression/blob/main/src/tokio/write/generic/encoder.rs
141+ Compression :: ZStd => DynBufWriterImpl :: Zstd ( async_zstd_encoder ( writer) ) ,
142+ } )
143+ }
144+ }
145+
146+ impl < W > io:: AsyncWrite for DynBufWriter < W >
147+ where
148+ W : io:: AsyncWrite + io:: AsyncWriteExt + Unpin ,
149+ {
150+ fn poll_write (
151+ mut self : Pin < & mut Self > ,
152+ cx : & mut Context < ' _ > ,
153+ buf : & [ u8 ] ,
154+ ) -> Poll < io:: Result < usize > > {
155+ match & mut self . 0 {
156+ DynBufWriterImpl :: Uncompressed ( w) => {
157+ io:: AsyncWrite :: poll_write ( Pin :: new ( w) , cx, buf)
158+ }
159+ DynBufWriterImpl :: Zstd ( enc) => io:: AsyncWrite :: poll_write ( Pin :: new ( enc) , cx, buf) ,
160+ }
161+ }
162+
163+ fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
164+ match & mut self . 0 {
165+ DynBufWriterImpl :: Uncompressed ( w) => io:: AsyncWrite :: poll_flush ( Pin :: new ( w) , cx) ,
166+ DynBufWriterImpl :: Zstd ( enc) => io:: AsyncWrite :: poll_flush ( Pin :: new ( enc) , cx) ,
167+ }
168+ }
169+
170+ fn poll_shutdown ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
171+ match & mut self . 0 {
172+ DynBufWriterImpl :: Uncompressed ( w) => io:: AsyncWrite :: poll_shutdown ( Pin :: new ( w) , cx) ,
173+ DynBufWriterImpl :: Zstd ( enc) => io:: AsyncWrite :: poll_shutdown ( Pin :: new ( enc) , cx) ,
174+ }
175+ }
176+ }
177+
99178 /// An object that allows for abstracting over compressed and uncompressed output.
179+ ///
180+ /// Compared with [`DynBufWriter`], only the compressed output is buffered, as it is
181+ /// required by the async Zstd implementation.
100182 pub struct DynWriter < W > ( DynWriterImpl < W > )
101183 where
102184 W : io:: AsyncWriteExt + Unpin ;
@@ -106,7 +188,7 @@ mod r#async {
106188 W : io:: AsyncWriteExt + Unpin ,
107189 {
108190 Uncompressed ( W ) ,
109- ZStd ( ZstdEncoder < W > ) ,
191+ Zstd ( ZstdEncoder < W > ) ,
110192 }
111193
112194 impl < W > DynWriter < W >
@@ -118,15 +200,15 @@ mod r#async {
118200 pub fn new ( writer : W , compression : Compression ) -> Self {
119201 Self ( match compression {
120202 Compression :: None => DynWriterImpl :: Uncompressed ( writer) ,
121- Compression :: ZStd => DynWriterImpl :: ZStd ( async_zstd_encoder ( writer) ) ,
203+ Compression :: ZStd => DynWriterImpl :: Zstd ( async_zstd_encoder ( writer) ) ,
122204 } )
123205 }
124206
125207 /// Returns a mutable reference to the underlying writer.
126208 pub fn get_mut ( & mut self ) -> & mut W {
127209 match & mut self . 0 {
128210 DynWriterImpl :: Uncompressed ( w) => w,
129- DynWriterImpl :: ZStd ( enc) => enc. get_mut ( ) ,
211+ DynWriterImpl :: Zstd ( enc) => enc. get_mut ( ) ,
130212 }
131213 }
132214 }
@@ -142,21 +224,21 @@ mod r#async {
142224 ) -> Poll < io:: Result < usize > > {
143225 match & mut self . 0 {
144226 DynWriterImpl :: Uncompressed ( w) => io:: AsyncWrite :: poll_write ( Pin :: new ( w) , cx, buf) ,
145- DynWriterImpl :: ZStd ( enc) => io:: AsyncWrite :: poll_write ( Pin :: new ( enc) , cx, buf) ,
227+ DynWriterImpl :: Zstd ( enc) => io:: AsyncWrite :: poll_write ( Pin :: new ( enc) , cx, buf) ,
146228 }
147229 }
148230
149231 fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
150232 match & mut self . 0 {
151233 DynWriterImpl :: Uncompressed ( w) => io:: AsyncWrite :: poll_flush ( Pin :: new ( w) , cx) ,
152- DynWriterImpl :: ZStd ( enc) => io:: AsyncWrite :: poll_flush ( Pin :: new ( enc) , cx) ,
234+ DynWriterImpl :: Zstd ( enc) => io:: AsyncWrite :: poll_flush ( Pin :: new ( enc) , cx) ,
153235 }
154236 }
155237
156238 fn poll_shutdown ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
157239 match & mut self . 0 {
158240 DynWriterImpl :: Uncompressed ( w) => io:: AsyncWrite :: poll_shutdown ( Pin :: new ( w) , cx) ,
159- DynWriterImpl :: ZStd ( enc) => io:: AsyncWrite :: poll_shutdown ( Pin :: new ( enc) , cx) ,
241+ DynWriterImpl :: Zstd ( enc) => io:: AsyncWrite :: poll_shutdown ( Pin :: new ( enc) , cx) ,
160242 }
161243 }
162244 }
0 commit comments