dlist: Simplify by using Option::{map, map_mut}
These methods were fixed or just added so they can now be used.
This commit is contained in:
parent
9ccf443088
commit
7681cf62e3
1 changed files with 4 additions and 13 deletions
|
@ -122,31 +122,22 @@ impl<T> Mutable for DList<T> {
|
||||||
impl<T> Deque<T> for DList<T> {
|
impl<T> Deque<T> for DList<T> {
|
||||||
/// Provide a reference to the front element, or None if the list is empty
|
/// Provide a reference to the front element, or None if the list is empty
|
||||||
fn front<'a>(&'a self) -> Option<&'a T> {
|
fn front<'a>(&'a self) -> Option<&'a T> {
|
||||||
self.list_head.chain_ref(|x| Some(&x.value))
|
self.list_head.map(|head| &head.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provide a mutable reference to the front element, or None if the list is empty
|
/// Provide a mutable reference to the front element, or None if the list is empty
|
||||||
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
||||||
match self.list_head {
|
self.list_head.map_mut(|head| &mut head.value)
|
||||||
None => None,
|
|
||||||
Some(ref mut head) => Some(&mut head.value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provide a reference to the back element, or None if the list is empty
|
/// Provide a reference to the back element, or None if the list is empty
|
||||||
fn back<'a>(&'a self) -> Option<&'a T> {
|
fn back<'a>(&'a self) -> Option<&'a T> {
|
||||||
match self.list_tail.resolve_immut() {
|
self.list_tail.resolve_immut().map(|tail| &tail.value)
|
||||||
None => None,
|
|
||||||
Some(tail) => Some(&tail.value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provide a mutable reference to the back element, or None if the list is empty
|
/// Provide a mutable reference to the back element, or None if the list is empty
|
||||||
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
||||||
match self.list_tail.resolve() {
|
self.list_tail.resolve().map_mut(|tail| &mut tail.value)
|
||||||
None => None,
|
|
||||||
Some(tail) => Some(&mut tail.value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add an element last in the list
|
/// Add an element last in the list
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue