Skip to content

Commit

Permalink
Remove main function from doctests (#752)
Browse files Browse the repository at this point in the history
* Remove main function from doctests

* Add a explicit Ok return in doctest
In order to use the ? operator, we need to make it clear the function
returns a result instead of unit:
https://doc.rust-lang.org/rustdoc/documentation-tests.html#using--in-doc-tests

* Return unit error in order to extract the correct error type

* Use ndarray::ShapeError error

* Unwrap some Options

* Remove empty main function from the prelude docs

* Undo changes at the impl_constructors submodule

* Remove needless main
  • Loading branch information
viniciusd authored and LukeMathWalker committed Nov 20, 2019
1 parent 4d6188a commit 7f67fdf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 57 deletions.
60 changes: 24 additions & 36 deletions src/free_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@ use crate::{dimension, ArcArray1, ArcArray2};
/// three dimensions.
///
/// ```
/// extern crate ndarray;
///
/// use ndarray::array;
/// let a1 = array![1, 2, 3, 4];
///
/// fn main() {
/// let a1 = array![1, 2, 3, 4];
///
/// let a2 = array![[1, 2],
/// [3, 4]];
/// let a2 = array![[1, 2],
/// [3, 4]];
///
/// let a3 = array![[[1, 2], [3, 4]],
/// [[5, 6], [7, 8]]];
/// let a3 = array![[[1, 2], [3, 4]],
/// [[5, 6], [7, 8]]];
///
/// assert_eq!(a1.shape(), &[4]);
/// assert_eq!(a2.shape(), &[2, 2]);
/// assert_eq!(a3.shape(), &[2, 2, 2]);
/// }
/// assert_eq!(a1.shape(), &[4]);
/// assert_eq!(a2.shape(), &[2, 2]);
/// assert_eq!(a3.shape(), &[2, 2, 2]);
/// ```
///
/// This macro uses `vec![]`, and has the same ownership semantics;
Expand Down Expand Up @@ -115,19 +110,14 @@ pub fn aview2<A, V: FixedInitializer<Elem = A>>(xs: &[V]) -> ArrayView2<'_, A> {
/// Create a one-dimensional read-write array view with elements borrowing `xs`.
///
/// ```
/// extern crate ndarray;
///
/// use ndarray::{aview_mut1, s};
///
/// // Create an array view over some data, then slice it and modify it.
/// fn main() {
/// let mut data = [0; 1024];
/// {
/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap();
/// a.slice_mut(s![.., ..;3]).fill(5);
/// }
/// assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]);
/// let mut data = [0; 1024];
/// {
/// let mut a = aview_mut1(&mut data).into_shape((32, 32)).unwrap();
/// a.slice_mut(s![.., ..;3]).fill(5);
/// }
/// assert_eq!(&data[..10], [5, 0, 0, 5, 0, 0, 5, 0, 0, 5]);
/// ```
pub fn aview_mut1<A>(xs: &mut [A]) -> ArrayViewMut1<'_, A> {
ArrayViewMut::from(xs)
Expand All @@ -143,20 +133,18 @@ pub fn aview_mut1<A>(xs: &mut [A]) -> ArrayViewMut1<'_, A> {
/// ```
/// use ndarray::aview_mut2;
///
/// fn main() {
/// // The inner (nested) array must be of length 1 to 16, but the outer
/// // can be of any length.
/// let mut data = [[0.; 2]; 128];
/// {
/// // Make a 128 x 2 mut array view then turn it into 2 x 128
/// let mut a = aview_mut2(&mut data).reversed_axes();
/// // Make the first row ones and second row minus ones.
/// a.row_mut(0).fill(1.);
/// a.row_mut(1).fill(-1.);
/// }
/// // look at the start of the result
/// assert_eq!(&data[..3], [[1., -1.], [1., -1.], [1., -1.]]);
/// // The inner (nested) array must be of length 1 to 16, but the outer
/// // can be of any length.
/// let mut data = [[0.; 2]; 128];
/// {
/// // Make a 128 x 2 mut array view then turn it into 2 x 128
/// let mut a = aview_mut2(&mut data).reversed_axes();
/// // Make the first row ones and second row minus ones.
/// a.row_mut(0).fill(1.);
/// a.row_mut(1).fill(-1.);
/// }
/// // look at the start of the result
/// assert_eq!(&data[..3], [[1., -1.], [1., -1.], [1., -1.]]);
/// ```
pub fn aview_mut2<A, V: FixedInitializer<Elem = A>>(xs: &mut [V]) -> ArrayViewMut2<'_, A> {
let cols = V::len();
Expand Down
8 changes: 2 additions & 6 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ where
/// # Some(())
/// # }
/// #
/// # fn main() { example().unwrap() }
/// # example().unwrap();
/// ```
pub fn geomspace(start: A, end: A, n: usize) -> Option<Self>
where
Expand Down Expand Up @@ -485,8 +485,6 @@ where
/// ### Examples
///
/// ```
/// extern crate ndarray;
///
/// use ndarray::{s, Array2};
///
/// // Example Task: Let's create a column shifted copy of a in b
Expand All @@ -503,9 +501,7 @@ where
/// b
/// }
///
/// # fn main() {
/// # shift_by_two(&Array2::zeros((8, 8)));
/// # }
/// # shift_by_two(&Array2::zeros((8, 8)));
/// ```
pub unsafe fn uninitialized<Sh>(shape: Sh) -> Self
where
Expand Down
15 changes: 2 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,9 @@ pub type Ixs = isize;
/// [`.multi_slice_move()`]: type.ArrayViewMut.html#method.multi_slice_move
///
/// ```
/// extern crate ndarray;
///
/// use ndarray::{arr2, arr3, s};
///
/// fn main() {
///
/// // 2 submatrices of 2 rows with 3 elements per row, means a shape of `[2, 2, 3]`.
///
/// let a = arr3(&[[[ 1, 2, 3], // -- 2 rows \_
Expand Down Expand Up @@ -546,7 +543,6 @@ pub type Ixs = isize;
/// [5, 7]]);
/// assert_eq!(s0, i);
/// assert_eq!(s1, j);
/// }
/// ```
///
/// ## Subviews
Expand Down Expand Up @@ -579,11 +575,9 @@ pub type Ixs = isize;
/// [`.outer_iter_mut()`]: #method.outer_iter_mut
///
/// ```
/// extern crate ndarray;
///
/// use ndarray::{arr3, aview1, aview2, s, Axis};
///
/// # fn main() {
///
/// // 2 submatrices of 2 rows with 3 elements per row, means a shape of `[2, 2, 3]`.
///
Expand Down Expand Up @@ -617,7 +611,6 @@ pub type Ixs = isize;
/// // You can take multiple subviews at once (and slice at the same time)
/// let double_sub = a.slice(s![1, .., 0]);
/// assert_eq!(double_sub, aview1(&[7, 10]));
/// # }
/// ```
///
/// ## Arithmetic Operations
Expand Down Expand Up @@ -1063,7 +1056,6 @@ pub type Ixs = isize;
/// ```rust
/// use ndarray::{array, Array2};
///
/// # fn main() -> Result<(), Box<std::error::Error>> {
/// let ncols = 3;
/// let mut data = Vec::new();
/// let mut nrows = 0;
Expand All @@ -1075,8 +1067,7 @@ pub type Ixs = isize;
/// }
/// let arr = Array2::from_shape_vec((nrows, ncols), data)?;
/// assert_eq!(arr, array![[0, 0, 0], [1, 1, 1]]);
/// # Ok(())
/// # }
/// # Ok::<(), ndarray::ShapeError>(())
/// ```
///
/// If neither of these options works for you, and you really need to convert
Expand All @@ -1089,7 +1080,6 @@ pub type Ixs = isize;
/// ```rust
/// use ndarray::{array, Array2, Array3};
///
/// # fn main() -> Result<(), Box<std::error::Error>> {
/// let nested: Vec<Array2<i32>> = vec![
/// array![[1, 2, 3], [4, 5, 6]],
/// array![[7, 8, 9], [10, 11, 12]],
Expand All @@ -1102,8 +1092,7 @@ pub type Ixs = isize;
/// [[1, 2, 3], [4, 5, 6]],
/// [[7, 8, 9], [10, 11, 12]],
/// ]);
/// # Ok(())
/// # }
/// # Ok::<(), ndarray::ShapeError>(())
/// ```
///
/// Note that this implementation assumes that the nested `Vec`s are all the
Expand Down
2 changes: 0 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
//! and macros that you can import easily as a group.
//!
//! ```
//! extern crate ndarray;
//!
//! use ndarray::prelude::*;
//! # fn main() { }
//! ```
#[doc(no_inline)]
Expand Down

0 comments on commit 7f67fdf

Please sign in to comment.