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


##########
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:
   🤔 The problem with that is that `self.inner.reader().await?` returns 
`Box<dyn FileRead>` (so I can't make the function accept `<T: FileRead>` here, 
because the type is derived from the struct's own data, not something the 
caller passes in), and then the constructor in the next few lines 
`AesGcmFileRead::new(raw_reader, ...)` also requires `raw_reader` to be 
`Box<dyn FileRead>`. 
   
   If I have a constraint for `where R: FileRead`,  the result of 
`wrap(self.inner.reader().await?)` gives me `R`; `R: FileRead` doesn't 
guarantee that R is `Box<dyn FileRead>` and I can't pass it into 
`AesGcmFileRead::new()` without adding a `Box` wrapper around it.
   
    To avoid that `Box` completely, we'd need to make the decrypting reader 
generic over `R: FileRead` (either via a private `AesGcmReader<R>` so we don't 
change `AesGcmFileRead`'s public API, or `AesGcmFileRead<R = Box<dyn 
FileRead>>`):
   
   ```rust
       struct AesGcmReader<R> {
           inner: R,
           ...
       }
   ```
     which would then allow reader_with_wrapper to do:
   
   ```rust
       async fn reader_with_wrapper<R: FileRead>(
           &self,
           wrap: impl FnOnce(Box<dyn FileRead>) -> R,
       ) -> Result<impl FileRead> {
           ...
           let raw_reader = wrap(self.inner.reader().await?);
           ...
           AesGcmReader::new(raw_reader, ...)
       }
   ```
   The other (simpler) option is to leave `AesGcmFileRead` as-is and just pull 
out a private `decrypt(&self, raw_reader: Box<dyn FileRead>, ...)` helper in 
io.rs:
   
     • `reader()` calls decrypt directly, so no more double-Box on the identity 
path
     • `reader_with_wrapper` still takes `impl FnOnce(Box<dyn FileRead>) -> R` 
(so the caller doesn't write `Box::new`), and only boxes `R` once per file open 
inside `reader_with_wrapper`
   
   I'm leaning a bit toward the second one since making `AesGcmReader` generic 
feels like it might be overdoing it just to satisfy this **one** flow, but 
happy to go with whichever you prefer



-- 
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