Perseus commented on code in PR #3283:
URL: https://github.com/apache/iceberg-rust/pull/3283#discussion_r4652309635


##########
crates/iceberg/src/encryption/io.rs:
##########
@@ -74,8 +74,22 @@ impl EncryptedInputFile {
 
     /// Creates a reader that transparently decrypts on each read.
     pub async fn reader(&self) -> Result<Box<dyn FileRead>> {
+        // Identity just returns the file reader it was given.
+        self.reader_with_wrapper(std::convert::identity).await
+    }
+
+    /// Creates a decrypting reader, letting the caller wrap the file reader 
first.
+    ///
+    /// Calls `wrap` once with the file reader and uses the result for 
decryption.
+    /// This lets callers add behavior such as logging reads or counting bytes
+    /// without changing the decryption code.
+    pub(crate) async fn reader_with_wrapper<F, R>(&self, wrap: F) -> 
Result<Box<dyn FileRead>>
+    where
+        F: FnOnce(Box<dyn FileRead>) -> R,
+        R: FileRead,
+    {
         let encrypted_length = self.encrypted_length()?;
-        let raw_reader = self.inner.reader().await?;
+        let raw_reader = Box::new(wrap(self.inner.reader().await?));

Review Comment:
   Okay, I went down a bit of a Rust rabbit hole trying to understand these 
signatures involving traits and callbacks; please correct me if I’m missing 
something.
   
   > I was thinking F has following signature: F: FnOnce(impl FileRead) -> R
   
   Rust rejects 
   ```rust 
   F: FnOnce(impl FileRead) -> R
   ```
   with `E0562` because `impl Trait` isn’t supported in the parameter list of a 
callback like `FnOnce`. Relevant issues/threads - 
[[1]](https://rust-lang.github.io/rfcs/1951-expand-impl-trait.html#expansion-to-arguments)
 [[2]](https://internals.rust-lang.org/t/help-test-impl-trait/6516/2) 
[[3]](https://github.com/rust-lang/rust/issues/45994) [ 
[4]](https://users.rust-lang.org/t/how-to-declare-a-function-accepting-a-closure-as-argument-whitch-itself-take-another-closure-as-argument/40088/3)
   
   
   > We only need to add impl FileRead for Box<dyn FileRead>
   
   The generic implementation for `imp FileRead for T` here - 
https://github.com/apache/iceberg-rust/blob/a7160a0a2318028df1a8ae88abbb3b5d611fa3d8/crates/iceberg/src/io/file_io.rs#L314-L319
   already covers `Box<dyn FileRead>` as it satisfies those bounds. `.read()` 
calls are forwarded to the Box's inner reader and it works.
   
   The main issue, I think, is that we have an extra `Box::new()` wrapper when 
`reader_with_wrapper` receives the `identity` function as a callback, which is 
what the original `.reader()` function now passes.
   
   To avoid the extra allocation because of the `Box`, we can make the callback 
return `Box<dyn FileRead>` as well; then the new function signature becomes 
something like
   ```rust
   async fn reader_with_wrapper(
     &self,
     wrap: impl FnOnce(Box<dyn FileRead>) -> Box<dyn FileRead>
   ) -> Result<Box<dyn FileRead>> {
     ...
     let raw_reader = wrap(self.inner.reader().await?);
     ...
   }
   ```
   
   Does that sound okay to you? 
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to