Skip to content

Commit 6273494

Browse files
committed
Add into_scalar method for Array0
1 parent 0a02be3 commit 6273494

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/impl_owned_array.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
use imp_prelude::*;
22

3+
/// Methods specific to `Array0`.
4+
///
5+
/// ***See also all methods for [`ArrayBase`]***
6+
///
7+
/// [`ArrayBase`]: struct.ArrayBase.html
8+
impl<A> Array<A, Ix0> {
9+
/// Returns the single element in the array without cloning it.
10+
///
11+
/// ```
12+
/// use ndarray::{arr0, Array0};
13+
///
14+
/// // `Foo` doesn't implement `Clone`.
15+
/// #[derive(Debug, Eq, PartialEq)]
16+
/// struct Foo;
17+
///
18+
/// let array: Array0<Foo> = arr0(Foo);
19+
/// let scalar: Foo = array.into_scalar();
20+
/// assert_eq!(scalar, Foo);
21+
/// ```
22+
pub fn into_scalar(mut self) -> A {
23+
self.data.0.remove(0)
24+
}
25+
}
26+
327
/// Methods specific to `Array`.
428
///
529
/// ***See also all methods for [`ArrayBase`]***

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,11 @@ pub type Ixs = isize;
855855
/// </tr>
856856
/// </table>
857857
///
858-
/// ### Conversions Between Arrays and `Vec`s/Slices
858+
/// ### Conversions Between Arrays and `Vec`s/Slices/Scalars
859859
///
860-
/// This is a table of the safe conversions between arrays and `Vec`s/slices.
861-
/// Note that some of the return values are actually `Result`/`Option` wrappers
862-
/// around the indicated output types.
860+
/// This is a table of the safe conversions between arrays and
861+
/// `Vec`s/slices/scalars. Note that some of the return values are actually
862+
/// `Result`/`Option` wrappers around the indicated output types.
863863
///
864864
/// Input | Output | Methods
865865
/// ------|--------|--------
@@ -872,6 +872,7 @@ pub type Ixs = isize;
872872
/// `&mut ArrayBase<S: DataMut, D>` | `&mut [A]` | [`.as_slice_mut()`](#method.as_slice_mut)<sup>[1](#req_contig_std)</sup>, [`.as_slice_memory_order_mut()`](#method.as_slice_memory_order_mut)<sup>[2](#req_contig)</sup>
873873
/// `ArrayView<A, D>` | `&[A]` | [`.into_slice()`](type.ArrayView.html#method.into_slice)
874874
/// `ArrayViewMut<A, D>` | `&mut [A]` | [`.into_slice()`](type.ArrayViewMut.html#method.into_slice)
875+
/// `Array0<A>` | `A` | [`.into_scalar()`](type.Array.html#method.into_scalar)
875876
///
876877
/// <sup><a name="req_contig_std">1</a></sup>Works only if the array is
877878
/// contiguous and in standard order.

0 commit comments

Comments
 (0)