This is an automated email from the ASF dual-hosted git repository.

Jefffrey pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 5156d9cc18 Fix AsRef impl for Buffer and generically wrap 
Bytes::from_owner (#10571)
5156d9cc18 is described below

commit 5156d9cc188479a4d90e0dd219f33694252eaa89
Author: Adam Reichold <[email protected]>
AuthorDate: Fri Aug 7 14:02:00 2026 +0200

    Fix AsRef impl for Buffer and generically wrap Bytes::from_owner (#10571)
    
    The AsRef impl was limited to references to Buffer instead of Buffer
    itself which is unnecessary since the signature of as_ref already
    provides the necessary indirection and it also is not the implementation
    required for wider ecosystem integration.
    
    # Which issue does this PR close?
    
    - Closes #10570.
    
    # Rationale for this change
    
    I am trying to use the newly available `StreamEncoder` from the
    `arrow_ipc` crate together with `Body::from_stream` from `axum`, but
    calling `bytes::Bytes::from_owner` fails due to an unsatisfied trait
    bound which I have to work around by local newtype wrapping.
    
    # What changes are included in this PR?
    
    A fix for the overly restrictive `AsRef` impl and also writing
    `Bytes::from_owner` through as generic and canonical way to wrap a given
    `Buffer` as a `bytes::Bytes` object (so it can be used with
    `Body::from_stream` directly.
    
    # Are these changes tested?
    
    The more general impl is covered by the same tests as the existing one
    and the new `From` impl is exercised by the doctest.
    
    # Are there any user-facing changes?
    
    ~~This is strictly speaking a breaking change via downstream trait
    coherence, but I would argue that the existing `AsRef` is erroneously
    restricted and would therefore classify this as a typing bug fix.~~
    
    This is not breaking as [the standard library's blanket
    
impl](https://doc.rust-lang.org/std/convert/trait.AsRef.html#impl-AsRef%3CU%3E-for-%26T)
    will continue to provide the previous impl so this appears strictly
    additive with `cargo semver-checks` v0.50.0 agreeing.
    
    Co-authored-by: Jeffrey Vo <[email protected]>
---
 arrow-buffer/src/buffer/immutable.rs | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/arrow-buffer/src/buffer/immutable.rs 
b/arrow-buffer/src/buffer/immutable.rs
index c3821164e4..3147b683b5 100644
--- a/arrow-buffer/src/buffer/immutable.rs
+++ b/arrow-buffer/src/buffer/immutable.rs
@@ -67,6 +67,18 @@ use super::{MutableBuffer, ScalarBuffer};
 /// let bytes = bytes::Bytes::from("hello");
 /// let buffer = Buffer::from(bytes);
 ///```
+///
+/// # Example: Create a [`bytes::Bytes`] from a `Buffer` (without copying)
+///
+/// [`bytes::Bytes::from_owner`] can also wrap a `Buffer` again without 
copying.
+/// This made generically available via a `From` implementation.
+///
+/// ```
+/// # use arrow_buffer::Buffer;
+/// # let bytes = bytes::Bytes::from("hello");
+/// # let buffer = Buffer::from(bytes);
+/// let bytes = bytes::Bytes::from(buffer);
+///```
 #[derive(Clone, Debug)]
 pub struct Buffer {
     /// the internal byte buffer.
@@ -534,6 +546,13 @@ impl From<bytes::Bytes> for Buffer {
     }
 }
 
+/// Convert a `Buffer` into a [`bytes::Bytes`]
+impl From<Buffer> for bytes::Bytes {
+    fn from(buffer: Buffer) -> Self {
+        Self::from_owner(buffer)
+    }
+}
+
 /// Create a `Buffer` instance by storing the boolean values into the buffer
 impl FromIterator<bool> for Buffer {
     fn from_iter<I>(iter: I) -> Self
@@ -552,7 +571,7 @@ impl std::ops::Deref for Buffer {
     }
 }
 
-impl AsRef<[u8]> for &Buffer {
+impl AsRef<[u8]> for Buffer {
     fn as_ref(&self) -> &[u8] {
         self.as_slice()
     }

Reply via email to