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 unsafe { (*new_last).next = ptr::null_mut() };
59 let last = unsafe { raw_into_box(last) } ;
64 pub fn push_front(&mut self, t: T) {
65 // Create the new node.
66 let new = Box::new( Node { data: t, next: self.first, prev: ptr::null_mut() } );
67 let new = box_into_raw(new);
68 // Update other points to this node.
69 if self.first.is_null() {
70 debug_assert!(self.last.is_null());
74 debug_assert!(!self.last.is_null());
75 unsafe { (*self.first).prev = new; }
77 // Make this the first node.
81 pub fn pop_front(&mut self) -> Option<T> {
82 if self.first.is_null() {
85 let first = self.first;
86 let new_first = unsafe { (*self.first).next };
87 self.first = new_first;
88 if new_first.is_null() {
89 // The list is now empty.
90 self.last = new_first;
92 unsafe { (*new_first).prev = ptr::null_mut() };
94 let first = unsafe { raw_into_box(first) } ;
99 pub fn for_each<F: FnMut(&mut T)>(&mut self, mut f: F) {
100 let mut cur_ptr = self.first;
101 while !cur_ptr.is_null() {
102 // Iterate over every node, and call `f`.
103 f(unsafe{ &mut (*cur_ptr).data });
104 cur_ptr = unsafe{ (*cur_ptr).next };
108 pub fn iter_mut(&mut self) -> IterMut<T> {
109 IterMut { next: self.first, _marker: PhantomData }
113 pub struct IterMut<'a, T> where T: 'a {
115 _marker: PhantomData<&'a T>,
118 impl<'a, T> Iterator for IterMut<'a, T> {
119 type Item = &'a mut T;
121 fn next(&mut self) -> Option<Self::Item> {
122 if self.next.is_null() {
125 let ret = unsafe{ &mut (*self.next).data };
126 self.next = unsafe { (*self.next).next };
132 impl<T> Drop for LinkedList<T> {
134 let mut cur_ptr = self.first;
135 while !cur_ptr.is_null() {
136 let cur = unsafe { raw_into_box(cur_ptr) };
147 use super::LinkedList;
151 let mut l: LinkedList<i32> = LinkedList::new();
157 assert_eq!(l.pop_back(), Some(2));
158 assert_eq!(l.pop_back(), Some(1));
159 assert_eq!(l.pop_back(), Some(0));
160 assert_eq!(l.pop_back(), Some(-0));
161 assert_eq!(l.pop_back(), Some(-1));
162 assert_eq!(l.pop_back(), Some(-2));
163 assert_eq!(l.pop_back(), None);
164 assert_eq!(l.pop_back(), None);
168 fn test_pop_front() {
169 let mut l: LinkedList<i32> = LinkedList::new();
175 assert_eq!(l.pop_front(), Some(-2));
176 assert_eq!(l.pop_front(), Some(-1));
177 assert_eq!(l.pop_front(), Some(-0));
178 assert_eq!(l.pop_front(), Some(0));
179 assert_eq!(l.pop_front(), Some(1));
180 assert_eq!(l.pop_front(), Some(2));
181 assert_eq!(l.pop_front(), None);
182 assert_eq!(l.pop_front(), None);
187 count: Rc<Cell<usize>>,
189 impl Drop for DropChecker {
191 self.count.set(self.count.get() + 1);
197 let count = DropChecker { count: Rc::new(Cell::new(0)) };
199 let mut l = LinkedList::new();
201 l.push_back(count.clone());
202 l.push_front(count.clone());
205 assert_eq!(count.count.get(), 20);