Adds an element first in the list.
(&mut self, val: L::Handle)
| 152 | impl<L: Link> LinkedList<L, L::Target> { |
| 153 | /// Adds an element first in the list. |
| 154 | pub fn push_front(&mut self, val: L::Handle) { |
| 155 | // The value should not be dropped, it is being inserted into the list |
| 156 | let val = ManuallyDrop::new(val); |
| 157 | let ptr = L::as_raw(&val); |
| 158 | assert_ne!(self.head, Some(ptr)); |
| 159 | unsafe { |
| 160 | // Verify the node is not already in a list (pointers must be clean) |
| 161 | debug_assert!( |
| 162 | L::pointers(ptr).as_ref().get_prev().is_none(), |
| 163 | "push_front: node already has prev pointer (double-insert?)" |
| 164 | ); |
| 165 | debug_assert!( |
| 166 | L::pointers(ptr).as_ref().get_next().is_none(), |
| 167 | "push_front: node already has next pointer (double-insert?)" |
| 168 | ); |
| 169 | L::pointers(ptr).as_mut().set_next(self.head); |
| 170 | L::pointers(ptr).as_mut().set_prev(None); |
| 171 | |
| 172 | if let Some(head) = self.head { |
| 173 | L::pointers(head).as_mut().set_prev(Some(ptr)); |
| 174 | } |
| 175 | |
| 176 | self.head = Some(ptr); |
| 177 | |
| 178 | // if self.tail.is_none() { |
| 179 | // self.tail = Some(ptr); |
| 180 | // } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // /// Removes the last element from a list and returns it, or None if it is |
| 185 | // /// empty. |
no test coverage detected