Skip to content

Commit 9820166

Browse files
authored
Merge pull request #507 from jturner314/first-elem
Add first and first_mut methods
2 parents 7df47d2 + faf7bd0 commit 9820166

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/impl_methods.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,29 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
191191
}
192192
}
193193

194+
/// Returns a reference to the first element of the array, or `None` if it
195+
/// is empty.
196+
pub fn first(&self) -> Option<&A> {
197+
if self.is_empty() {
198+
None
199+
} else {
200+
Some(unsafe { &*self.as_ptr() })
201+
}
202+
}
203+
204+
/// Returns a mutable reference to the first element of the array, or
205+
/// `None` if it is empty.
206+
pub fn first_mut(&mut self) -> Option<&mut A>
207+
where
208+
S: DataMut,
209+
{
210+
if self.is_empty() {
211+
None
212+
} else {
213+
Some(unsafe { &mut *self.as_mut_ptr() })
214+
}
215+
}
216+
194217
/// Return an iterator of references to the elements of the array.
195218
///
196219
/// Elements are visited in the *logical order* of the array, which

0 commit comments

Comments
 (0)