This commit is contained in:
aozhiwei 2023-10-07 17:17:18 +08:00
parent 5df2e5243d
commit 1b89d4c147

View File

@ -15,20 +15,39 @@ impl<T> ListHead<T> {
next: Weak::<RefCell::<ListHead<T>>>::new(), next: Weak::<RefCell::<ListHead<T>>>::new(),
data: Weak::<RefCell::<T>>::new(), data: Weak::<RefCell::<T>>::new(),
})); }));
this.borrow_mut().prev = Rc::downgrade(&this); this.borrow_mut().init();
this.borrow_mut().next = Rc::downgrade(&this);
return this; return this;
} }
pub fn data(&self) -> &Weak::<RefCell::<T>> { fn get_rc_refcell(&self) -> Rc::<RefCell::<ListHead<T>>> {
return &self.data; let cell_rc: &Rc<RefCell<ListHead<T>>> = &self.next.upgrade().unwrap();
let head: &ListHead<T> = &*cell_rc.borrow();
let cell_start = cell_rc.as_ptr() as *const u8;
let data_start = head as *const ListHead<T> as *const u8;
let cell_offset = unsafe { data_start.offset_from(cell_start) };
unsafe {
return Rc::from_raw(cell_start as *const RefCell::<ListHead<T>>);
};
} }
pub fn del(&mut self) { fn init(&mut self) {
self.prev = Rc::downgrade(&self.get_rc_refcell());
self.next = Rc::downgrade(&self.get_rc_refcell());
}
pub fn data(&self) -> Weak::<RefCell::<T>> {
return self.data.clone();
}
pub fn del_init(&mut self) {
self.next.upgrade().unwrap().borrow_mut().prev = self.prev.clone(); self.next.upgrade().unwrap().borrow_mut().prev = self.prev.clone();
self.prev.upgrade().unwrap().borrow_mut().next = self.next.clone(); self.prev.upgrade().unwrap().borrow_mut().next = self.next.clone();
} }
pub fn empty(&self) -> bool {
return self as *const ListHead<T> == self.next.upgrade().unwrap().as_ptr();
}
pub fn add_tail(head: &mut Rc::<RefCell::<ListHead<T>>>, pub fn add_tail(head: &mut Rc::<RefCell::<ListHead<T>>>,
pnew: &mut Rc::<RefCell::<ListHead<T>>>) { pnew: &mut Rc::<RefCell::<ListHead<T>>>) {
let prev = &mut head.borrow_mut().prev; let prev = &mut head.borrow_mut().prev;
@ -40,47 +59,17 @@ impl<T> ListHead<T> {
prev.upgrade().unwrap().borrow_mut().next = Rc::downgrade(pnew); prev.upgrade().unwrap().borrow_mut().next = Rc::downgrade(pnew);
} }
} pub fn first_entry(&self) -> Weak::<RefCell::<T>> {
/* if self.empty() {
pub struct ListHead<T> {
prev: Weak::<ListHead<T>>,
next: Weak::<ListHead<T>>,
data: Weak::<RefCell::<T>>,
}
impl<T> ListHead<T> {
pub fn new() -> RefCell::<Rc::<ListHead<T>>> {
let mut this = Rc::new(ListHead{
prev: Weak::<ListHead<T>>::new(),
next: Weak::<ListHead<T>>::new(),
data: Weak::<RefCell::<T>>::new(),
});
let p = RefCell::new(this);
//this.prev = Rc::downgrade(&this).clone();
//this.prev = Weak::<ListHead<T>>::new();
//this.next = Rc::downgrade(&this);
//p.borrow_mut().prev = Rc::downgrade(&p.borrow());
p.borrow_mut().prev = Weak::<ListHead<T>>::new();
return p;
}
/*
pub fn data(&self) -> &Weak::<RefCell::<T>> {
return &self.data;
}
pub fn del(&mut self) {
self.next.upgrade().unwrap().borrow_mut().prev = self.prev.clone();
self.prev.upgrade().unwrap().borrow_mut().next = self.next.clone();
}
pub fn add_tail(&mut self, pnew: &mut ListHead<T>) {
let prev = &mut self.prev;
unsafe {
let next = Weak::from_raw(&self);
//next.upgrade().unwrap().borrow_mut().prev = Weak::from_raw(pnew);
} }
return self.next.upgrade().unwrap().borrow().data();
} }
*/
pub fn replace_init(head: &mut Rc::<RefCell::<ListHead<T>>>,
pnew: &mut Rc::<RefCell::<ListHead<T>>>) {
pnew.borrow_mut().next = head.borrow_mut().next.clone();
pnew.borrow_mut().next.upgrade().unwrap().borrow_mut().prev = Rc::downgrade(pnew);
pnew.borrow_mut().prev = head.borrow_mut().prev.clone();
}
} }
*/