From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id EA8E86AC95 for ; Wed, 17 Feb 2021 08:57:25 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id DDAFA1F035 for ; Wed, 17 Feb 2021 08:56:55 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 4D0AD1F02B for ; Wed, 17 Feb 2021 08:56:55 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 15E4A41ABB for ; Wed, 17 Feb 2021 08:56:55 +0100 (CET) Date: Wed, 17 Feb 2021 08:56:54 +0100 From: Wolfgang Bumiller To: Stefan Reiter Cc: pbs-devel@lists.proxmox.com Message-ID: <20210217075654.yokzlgbb3anix6mi@olga.proxmox.com> References: <20210216170710.31767-1-s.reiter@proxmox.com> <20210216170710.31767-2-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210216170710.31767-2-s.reiter@proxmox.com> User-Agent: NeoMutt/20180716 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.045 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [aio.rs] Subject: Re: [pbs-devel] [PATCH pxar 01/22] decoder/aio: add contents() and content_size() calls X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Feb 2021 07:57:26 -0000 On Tue, Feb 16, 2021 at 06:06:49PM +0100, Stefan Reiter wrote: > Returns a tokio AsyncRead implementation for its "Contents" to keep with > the aio theme. > > Signed-off-by: Stefan Reiter > --- > src/decoder/aio.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 42 insertions(+), 1 deletion(-) > > diff --git a/src/decoder/aio.rs b/src/decoder/aio.rs > index 82030b0..5cc6694 100644 > --- a/src/decoder/aio.rs > +++ b/src/decoder/aio.rs > @@ -56,6 +56,18 @@ impl Decoder { > self.inner.next_do().await.transpose() > } > > + /// Get a reader for the contents of the current entry, if the entry has contents. > + /// Only available for feature "tokio-io", since it returns an AsyncRead reader. > + #[cfg(feature = "tokio-io")] ^ Don't do this. We basically have our own async I/O "entry point" with the SeqRead trait, and if you want to use it with another I/O runtime (say async-std or w/e futures or w/e else there is), you want to be able to use `Contents` there as well and just wrap it an alternative to the provided `TokioReader` manually. So just leave `Contents` as a public just-`SeqRead` type. > + pub fn contents(&mut self) -> Option> { > + self.inner.content_reader().map(|inner| Contents { inner }) > + } > + > + /// Get the size of the current contents, if the entry has contents. > + pub fn content_size(&self) -> Option { > + self.inner.content_size() > + } > + > /// Include goodbye tables in iteration. > pub fn enable_goodbye_entries(&mut self, on: bool) { > self.inner.with_goodbye_tables = on; > @@ -93,7 +105,36 @@ mod tok { > } > } > } > + > + pub struct Contents<'a, T: crate::decoder::SeqRead> { > + pub(crate) inner: crate::decoder::Contents<'a, T>, ^ no need for the `pub(crate)` then when you move it up > + } > + > + impl<'a, T: crate::decoder::SeqRead> tokio::io::AsyncRead for Contents<'a, T> { > + fn poll_read( > + self: Pin<&mut Self>, > + cx: &mut Context<'_>, > + buf: &mut tokio::io::ReadBuf<'_>, > + ) -> Poll> { > + unsafe { > + // Safety: poll_seq_read will only write to the buffer, so we don't need to > + // initialize it first, we can treat is a &[u8] immediately as long as we uphold > + // the ReadBuf invariants in the conditional below ^ This comment is actually wrong. `poll_seq_read` will do whatever the heck the implementer of the trait decides to do ;-) Personally, I really don't mind doing this *anyway* until a definitive "solution" actually lands in the *standard* library. Because if someone f's up a read impl even if it causes wild codegen bugs with tentacles then... sorry not sorry. > + let write_buf = > + &mut *(buf.unfilled_mut() as *mut [std::mem::MaybeUninit] as *mut [u8]); > + let result = self > + .map_unchecked_mut(|this| &mut this.inner as &mut dyn crate::decoder::SeqRead) > + .poll_seq_read(cx, write_buf); > + if let Poll::Ready(Ok(n)) = result { > + // if we've written data, advance both initialized and filled bytes cursor > + buf.assume_init(buf.filled().len() + n); > + buf.advance(n); > + } > + result.map(|_| Ok(())) > + } > + } > + } > } > > #[cfg(feature = "tokio-io")] > -use tok::TokioReader; > +use tok::{Contents, TokioReader}; ^ Needs a `pub` otherwise the type *can* exist as the return type of a function, but users have no way to actually *type out* the type...