3 use std::marker::PhantomData;
5 fn box_into_raw<T>(b: Box<T>) -> *mut T {
6 unsafe { mem::transmute(b) }
8 unsafe fn raw_into_box<T>(r: *mut T) -> Box<T> {
17 type NodePtr<T> = *mut Node<T>;
19 pub struct LinkedList<T> {
22 _marker: PhantomData<T>,
25 impl<T> LinkedList<T> {
26 pub fn new() -> Self {
27 LinkedList { first: ptr::null_mut(), last: ptr::null_mut(), _marker: PhantomData }
30 pub fn push_back(&mut self, t: T) {
31 // Create the new node.
32 let new = Box::new( Node { data: t, next: ptr::null_mut(), prev: self.last } );
33 let new = box_into_raw(new);
34 // Update other points to this node.
35 if self.last.is_null() {
36 debug_assert!(self.first.is_null());
39 debug_assert!(!self.first.is_null());
40 unsafe { (*self.last).next = new; }
42 // Make this the last node.
46 pub fn pop_back(&mut self) -> Option<T> {
47 if self.last.is_null() {
51 let new_last = unsafe { (*self.last).prev };
53 if new_last.is_null() {
54 // The list is now empty.
55 self.first = new_last;
57 let last = unsafe { raw_into_box(last) } ;
62 pub fn push_front(&mut self, t: T) {
63 // Create the new node.
64 let new = Box::new( Node { data: t, next: self.first, prev: ptr::null_mut() } );
65 let new = box_into_raw(new);
66 // Update other points to this node.
67 if self.first.is_null() {
68 debug_assert!(self.last.is_null());
72 debug_assert!(!self.last.is_null());
73 unsafe { (*self.first).prev = new; }
75 // Make this the first node.
79 pub fn pop_front(&mut self) -> Option<T> {
80 if self.first.is_null() {
83 let first = self.first;
84 let new_first = unsafe { (*self.first).next };
85 self.first = new_first;
86 if new_first.is_null() {
87 // The list is now empty.
88 self.last = new_first;
90 let first = unsafe { raw_into_box(first) } ;
95 pub fn for_each<F: FnMut(&mut T)>(&mut self, mut f: F) {
96 let mut cur_ptr = self.first;
97 while !cur_ptr.is_null() {
98 // Iterate over every node, and call `f`.
99 f(unsafe{ &mut (*cur_ptr).data });
100 cur_ptr = unsafe{ (*cur_ptr).next };
104 pub fn iter_mut(&mut self) -> IterMut<T> {
105 IterMut { next: self.first, _marker: PhantomData }
109 pub struct IterMut<'a, T> where T: 'a {
111 _marker: PhantomData<&'a T>,
114 impl<'a, T> Iterator for IterMut<'a, T> {
115 type Item = &'a mut T;
117 fn next(&mut self) -> Option<Self::Item> {
118 if self.next.is_null() {
121 let ret = unsafe{ &mut (*self.next).data };
122 self.next = unsafe { (*self.next).next };
128 impl<T> Drop for LinkedList<T> {
130 let mut cur_ptr = self.first;
131 while !cur_ptr.is_null() {
132 let cur = unsafe { raw_into_box(cur_ptr) };
143 use super::LinkedList;
147 let mut l: LinkedList<i32> = LinkedList::new();
153 assert_eq!(l.pop_back(), Some(2));
154 assert_eq!(l.pop_back(), Some(1));
155 assert_eq!(l.pop_back(), Some(0));
156 assert_eq!(l.pop_back(), Some(-0));
157 assert_eq!(l.pop_back(), Some(-1));
158 assert_eq!(l.pop_back(), Some(-2));
159 assert_eq!(l.pop_back(), None);
160 assert_eq!(l.pop_back(), None);
164 fn test_pop_front() {
165 let mut l: LinkedList<i32> = LinkedList::new();
171 assert_eq!(l.pop_front(), Some(-2));
172 assert_eq!(l.pop_front(), Some(-1));
173 assert_eq!(l.pop_front(), Some(-0));
174 assert_eq!(l.pop_front(), Some(0));
175 assert_eq!(l.pop_front(), Some(1));
176 assert_eq!(l.pop_front(), Some(2));
177 assert_eq!(l.pop_front(), None);
178 assert_eq!(l.pop_front(), None);
183 count: Rc<Cell<usize>>,
185 impl Drop for DropChecker {
187 self.count.set(self.count.get() + 1);
193 let count = DropChecker { count: Rc::new(Cell::new(0)) };
195 let mut l = LinkedList::new();
197 l.push_back(count.clone());
198 l.push_front(count.clone());
201 assert_eq!(count.count.get(), 20);