Repository: bluss/rust-ndarray
Pull request #211 (2016-08-04 19:18:54)

0.6.1 with from_shape_ptr


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 008a4d45..1592bb9f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
 [package]
 
 name = "ndarray"
-version = "0.6.0"
+version = "0.6.1"
 authors = ["bluss"]
 license = "MIT/Apache-2.0"
 
diff --git a/README.rst b/README.rst
index 3d77d1e0..ac6e47b6 100644
--- a/README.rst
+++ b/README.rst
@@ -88,6 +88,12 @@ How to use with cargo::
 Recent Changes (ndarray)
 ------------------------
 
+- 0.6.1
+
+  - Add ``unsafe`` array view constructors ``ArrayView::from_shape_ptr``
+    for read-only and read-write array views. These make it easier to
+    create views from raw pointers.
+
 - 0.6.0
 
   - Rename ``OwnedArray`` to ``Array``. The old name is deprecated.
diff --git a/src/impl_views.rs b/src/impl_views.rs
index 6cfbc72e..f8d6bc65 100644
--- a/src/impl_views.rs
+++ b/src/impl_views.rs
@@ -59,20 +59,11 @@ impl<'a, A, D> ArrayBase<ViewRepr<&'a A>, D>
         })
     }
 
-    /// Create an ArrayView from a raw pointer, dim and stride information.
+    /// Create an `ArrayView<A, D>` from shape information and a
+    /// raw pointer to the elements.
     ///
-    /// The caller is responsible for ensuring that the pointer is valid
-    /// and coherent with the dim and stride information.
-    ///
-    /// This is the recommended way for interfacing with FFI arrays.
-    pub unsafe fn from_raw_parts(ptr: *const A, dim: D, strides: D) -> Self {
-        ArrayView::new_(ptr, dim, strides)
-    }
-
-    /// Create an ArrayView from a raw pointer and shape information
-    ///
-    /// The caller is responsible for ensuring that the pointer is valid
-    /// and coherent with the dim and stride information.
+    /// Unsafe because caller is responsible for ensuring that the pointer is
+    /// valid, not mutably aliased and coherent with the dimension and stride information.
     pub unsafe fn from_shape_ptr<Sh>(shape: Sh, ptr: *const A) -> Self
         where Sh: Into<StrideShape<D>>
     {
@@ -171,20 +162,11 @@ impl<'a, A, D> ArrayBase<ViewRepr<&'a mut A>, D>
         })
     }
 
-    /// Create an ArrayViewMut from a raw pointer, shape and stride information.
-    ///
-    /// The caller is responsible for ensuring that the pointer is valid
-    /// and coherent with the dim and stride information.
-    ///
-    /// This is the recommended way for interfacing with FFI arrays.
-    pub unsafe fn from_raw_parts(ptr: *mut A, dim: D, strides: D) -> Self {
-        ArrayViewMut::new_(ptr, dim, strides)
-    }
-
-    /// Create an ArrayViewMut from a raw pointer and shape information
+    /// Create an `ArrayViewMut<A, D>` from shape information and a
+    /// raw pointer to the elements.
     ///
-    /// The caller is responsible for ensuring that the pointer is valid
-    /// and coherent with the dim and stride information.
+    /// Unsafe because caller is responsible for ensuring that the pointer is
+    /// valid, not aliased and coherent with the dimension and stride information.
     pub unsafe fn from_shape_ptr<Sh>(shape: Sh, ptr: *mut A) -> Self
         where Sh: Into<StrideShape<D>>
     {


Test patch:
diff --git a/tests/array.rs b/tests/array.rs
index 561e5c98..fa496953 100644
--- a/tests/array.rs
+++ b/tests/array.rs
@@ -1038,20 +1038,15 @@ fn test_shape() {
 }
 
 #[test]
-fn test_view_from_raw_parts() {
+fn test_view_from_shape_ptr() {
     let data = [0, 1, 2, 3, 4, 5];
-    let ptr = &data[0] as *const i32;
-    let view = unsafe { ArrayView::from_raw_parts(ptr, (2, 3), (3, 1)) };
-    assert_eq!(view[[1, 2]], 5);
-    let view = unsafe { ArrayView::from_shape_ptr((2, 3), ptr) };
-    assert_eq!(view[[0, 1]], 1);
+    let view = unsafe { ArrayView::from_shape_ptr((2, 3), data.as_ptr()) };
+    assert_eq!(view, aview2(&[[0, 1, 2], [3, 4, 5]]));
 
     let mut data = data;
-    let ptr = &mut data[0] as *mut i32;
-    let mut view = unsafe { ArrayViewMut::from_raw_parts(ptr, (2, 3), (3, 1)) };
+    let mut view = unsafe { ArrayViewMut::from_shape_ptr((2, 3), data.as_mut_ptr()) };
     view[[1, 2]] = 6;
-    assert_eq!(view[[1, 2]], 6);
-    let mut view = unsafe { ArrayViewMut::from_shape_ptr((2, 3), ptr) };
+    assert_eq!(view, aview2(&[[0, 1, 2], [3, 4, 6]]));
     view[[0, 1]] = 0;
-    assert_eq!(view[[0, 1]], 0);
+    assert_eq!(view, aview2(&[[0, 0, 2], [3, 4, 6]]));
 }