Line data Source code
1 : use crate::{context::RequestContext, virtual_file::owned_buffers_io::write::OwnedAsyncWriter};
2 : use tokio_epoll_uring::{BoundedBuf, IoBuf};
3 :
4 : pub struct Writer<W> {
5 : dst: W,
6 : bytes_amount: u64,
7 : }
8 :
9 : impl<W> Writer<W> {
10 1245 : pub fn new(dst: W) -> Self {
11 1245 : Self {
12 1245 : dst,
13 1245 : bytes_amount: 0,
14 1245 : }
15 1245 : }
16 :
17 15158236 : pub fn bytes_written(&self) -> u64 {
18 15158236 : self.bytes_amount
19 15158236 : }
20 :
21 4303730 : pub fn as_inner(&self) -> &W {
22 4303730 : &self.dst
23 4303730 : }
24 :
25 : /// Returns the wrapped `VirtualFile` object as well as the number
26 : /// of bytes that were written to it through this object.
27 : #[cfg_attr(target_os = "macos", allow(dead_code))]
28 3 : pub fn into_inner(self) -> (u64, W) {
29 3 : (self.bytes_amount, self.dst)
30 3 : }
31 : }
32 :
33 : impl<W> OwnedAsyncWriter for Writer<W>
34 : where
35 : W: OwnedAsyncWriter,
36 : {
37 : #[inline(always)]
38 6613 : async fn write_all<B: BoundedBuf<Buf = Buf>, Buf: IoBuf + Send>(
39 6613 : &mut self,
40 6613 : buf: B,
41 6613 : ctx: &RequestContext,
42 6613 : ) -> std::io::Result<(usize, B::Buf)> {
43 6613 : let (nwritten, buf) = self.dst.write_all(buf, ctx).await?;
44 6613 : self.bytes_amount += u64::try_from(nwritten).unwrap();
45 6613 : Ok((nwritten, buf))
46 6613 : }
47 : }
|