Skip to content

Add first and first_mut methods #507

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

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
}
}

/// Returns a reference to the first element of the array, or `None` if it
/// is empty.
pub fn first(&self) -> Option<&A> {
if self.is_empty() {
None
} else {
Some(unsafe { &*self.as_ptr() })
}
}

/// Returns a mutable reference to the first element of the array, or
/// `None` if it is empty.
pub fn first_mut(&mut self) -> Option<&mut A>
where
S: DataMut,
{
if self.is_empty() {
None
} else {
Some(unsafe { &mut *self.as_mut_ptr() })
}
}

/// Return an iterator of references to the elements of the array.
///
/// Elements are visited in the *logical order* of the array, which
Expand Down