[ 
https://issues.apache.org/jira/browse/ARROW-2381?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16423464#comment-16423464
 ] 

ASF GitHub Bot commented on ARROW-2381:
---------------------------------------

xhochy closed pull request #1823: ARROW-2381: [Rust] Adds iterator support to 
Buffer<T>
URL: https://github.com/apache/arrow/pull/1823
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/rust/src/buffer.rs b/rust/src/buffer.rs
index f70e0e2cd..44fc0076e 100644
--- a/rust/src/buffer.rs
+++ b/rust/src/buffer.rs
@@ -54,6 +54,40 @@ impl<T> Buffer<T> {
             *p.offset(i as isize) = v;
         }
     }
+
+    pub fn iter(&self) -> BufferIterator<T> {
+        BufferIterator {
+            data: self.data,
+            len: self.len,
+            index: 0
+        }
+    }
+
+}
+
+impl<T> Drop for Buffer<T> {
+    fn drop(&mut self) {
+        mem::drop(self.data)
+    }
+}
+
+pub struct BufferIterator<T> {
+    data: *const T,
+    len: i32,
+    index: isize
+}
+
+impl<T> Iterator for BufferIterator<T> where T: Copy {
+    type Item = T;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        if self.index < self.len as isize {
+            self.index += 1;
+            Some(unsafe { *self.data.offset(self.index-1) })
+        } else {
+            None
+        }
+    }
 }
 
 macro_rules! array_from_primitive {
@@ -93,9 +127,18 @@ array_from_primitive!(i64);
 #[cfg(test)]
 mod tests {
     use super::*;
+
     #[test]
     fn test_buffer_i32() {
         let b: Buffer<i32> = Buffer::from(vec![1, 2, 3, 4, 5]);
         assert_eq!(5, b.len);
     }
+
+    #[test]
+    fn test_iterator_i32() {
+        let b: Buffer<i32> = Buffer::from(vec![1, 2, 3, 4, 5]);
+        let it = b.iter();
+        let v : Vec<i32> = it.map(|n| n+1).collect();
+        assert_eq!(vec![2,3,4,5,6], v);
+    }
 }
diff --git a/rust/src/memory.rs b/rust/src/memory.rs
index 5cecaa13d..db527ab34 100644
--- a/rust/src/memory.rs
+++ b/rust/src/memory.rs
@@ -39,7 +39,12 @@ mod tests {
 
     #[test]
     fn test_allocate() {
-        let _ = allocate_aligned(32 * 1024).unwrap();
+        for _ in 0 .. 10 {
+            let p = allocate_aligned(1024).unwrap();
+            // make sure this is 64-byte aligned
+            assert_eq!(0, (p as usize) % 64);
+        }
+
     }
 
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [Rust] Buffer<T> should have an Iterator
> ----------------------------------------
>
>                 Key: ARROW-2381
>                 URL: https://issues.apache.org/jira/browse/ARROW-2381
>             Project: Apache Arrow
>          Issue Type: New Feature
>          Components: Rust
>            Reporter: Andy Grove
>            Assignee: Andy Grove
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.10.0
>
>
> It should be possible to obtain an Iterator from a Buffer<T> so that it is 
> easy to access the values.
>  
> PR: https://github.com/apache/arrow/pull/1823



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to