This is an automated email from the ASF dual-hosted git repository.
alamb 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 950f4d067c Add a strong_count method to Buffer (#7569)
950f4d067c is described below
commit 950f4d067c146edeb8b75bd35ac09fdfbfea32a9
Author: Weston Pace <[email protected]>
AuthorDate: Tue Jun 3 09:05:42 2025 -0700
Add a strong_count method to Buffer (#7569)
# Which issue does this PR close?
- Closes #7568.
# Rationale for this change
See issue
# What changes are included in this PR?
Adds a `strong_count` method to `Buffer`
# Are there any user-facing changes?
There is a new public method. There is no change to any existing
behavior.
---------
Co-authored-by: Matthijs Brobbel <[email protected]>
---
arrow-buffer/src/buffer/immutable.rs | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/arrow-buffer/src/buffer/immutable.rs
b/arrow-buffer/src/buffer/immutable.rs
index 5d229a4967..946299d006 100644
--- a/arrow-buffer/src/buffer/immutable.rs
+++ b/arrow-buffer/src/buffer/immutable.rs
@@ -125,6 +125,15 @@ impl Buffer {
self.data.ptr()
}
+ /// Returns the number of strong references to the buffer.
+ ///
+ /// This method is safe but if the buffer is shared across multiple threads
+ /// the underlying value could change between calling this method and using
+ /// the result.
+ pub fn strong_count(&self) -> usize {
+ Arc::strong_count(&self.data)
+ }
+
/// Create a [`Buffer`] from the provided [`Vec`] without copying
#[inline]
pub fn from_vec<T: ArrowNativeType>(vec: Vec<T>) -> Self {
@@ -1009,4 +1018,31 @@ mod tests {
}
}
}
+
+ #[test]
+ fn test_strong_count() {
+ let buffer = Buffer::from_iter(std::iter::repeat(0_u8).take(100));
+ assert_eq!(buffer.strong_count(), 1);
+
+ let buffer2 = buffer.clone();
+ assert_eq!(buffer.strong_count(), 2);
+
+ let buffer3 = buffer2.clone();
+ assert_eq!(buffer.strong_count(), 3);
+
+ drop(buffer);
+ assert_eq!(buffer2.strong_count(), 2);
+ assert_eq!(buffer3.strong_count(), 2);
+
+ // Strong count does not increase on move
+ let capture = move || {
+ assert_eq!(buffer3.strong_count(), 2);
+ };
+
+ capture();
+ assert_eq!(buffer2.strong_count(), 2);
+
+ drop(capture);
+ assert_eq!(buffer2.strong_count(), 1);
+ }
}