Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add into_scalar for ArrayView0 and ArrayViewMut0 #700

Merged
merged 7 commits into from Sep 8, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/impl_views/conversions.rs
Expand Up @@ -13,6 +13,7 @@ use crate::imp_prelude::*;
use crate::{Baseiter, ElementsBase, ElementsBaseMut, Iter, IterMut};

use crate::iter::{self, AxisIter, AxisIterMut};
use crate::IndexLonger;

/// Methods for read-only array views.
impl<'a, A, D> ArrayView<'a, A, D>
Expand Down Expand Up @@ -56,6 +57,36 @@ where
}
}


/// Methods specific to `ArrayView0`.
///
/// ***See also all methods for [`ArrayView`] and [`ArrayBase`]***
///
/// [`ArrayBase`]: struct.ArrayBase.html
/// [`ArrayView`]: struct.ArrayView.html
impl<'a, A> ArrayView<'a, A, Ix0> {
/// Consume the view and returns a reference to the single element in the array.
///
/// The lifetime of the returned reference matches the lifetime of the data
/// the array view was pointing to.
///
/// ```
/// use ndarray::{arr0, Array0};
///
/// // `Foo` doesn't implement `Clone`.
/// #[derive(Debug, Eq, PartialEq)]
/// struct Foo;
///
/// let array: Array0<Foo> = arr0(Foo);
/// let view = array.view();
/// let scalar: &Foo = view.into_scalar();
/// assert_eq!(scalar, &Foo);
/// ```
pub fn into_scalar(self) -> &'a A {
Copy link
Member

@bluss bluss Sep 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ArrayView doesn't need to be consumed to allow this (infuriating difference between shared and mut for IndexLonger). I guess one could just as well make this a &self method, it makes a difference for non-copy ArrayViews?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but then I opted to keep the signature consistent with the other two (Array/ArrayViewMut). Given that it's named into_*, it sounded preferable to consume the input.
On the other hand, taking &self makes it more convenient for ArrayView because you can keep using self afterwards.

I don't know, I would lean on the side of symmetry in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, that works.

self.index(Ix0())
}
}

/// Methods for read-write array views.
impl<'a, A, D> ArrayViewMut<'a, A, D>
where
Expand Down