1
This commit is contained in:
parent
e0ccc2ecc4
commit
271b788e1f
@ -6,6 +6,7 @@ pub struct ListHead<T> {
|
|||||||
prev: Weak::<RefCell::<ListHead<T>>>,
|
prev: Weak::<RefCell::<ListHead<T>>>,
|
||||||
next: Weak::<RefCell::<ListHead<T>>>,
|
next: Weak::<RefCell::<ListHead<T>>>,
|
||||||
data: Weak::<RefCell::<T>>,
|
data: Weak::<RefCell::<T>>,
|
||||||
|
_self_wp: Weak::<RefCell::<ListHead<T>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ListHead<T> {
|
impl<T> ListHead<T> {
|
||||||
@ -14,8 +15,10 @@ impl<T> ListHead<T> {
|
|||||||
let this = Rc::new(RefCell::new(ListHead{
|
let this = Rc::new(RefCell::new(ListHead{
|
||||||
prev: Weak::<RefCell::<ListHead<T>>>::new(),
|
prev: Weak::<RefCell::<ListHead<T>>>::new(),
|
||||||
next: Weak::<RefCell::<ListHead<T>>>::new(),
|
next: Weak::<RefCell::<ListHead<T>>>::new(),
|
||||||
data: Weak::<RefCell::<T>>::new()
|
data: Weak::<RefCell::<T>>::new(),
|
||||||
|
_self_wp: Weak::<RefCell::<ListHead<T>>>::new()
|
||||||
}));
|
}));
|
||||||
|
this.borrow_mut()._self_wp = Rc::downgrade(&this);
|
||||||
this.borrow_mut().init();
|
this.borrow_mut().init();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -24,26 +27,20 @@ impl<T> ListHead<T> {
|
|||||||
let this = Rc::new(RefCell::new(ListHead{
|
let this = Rc::new(RefCell::new(ListHead{
|
||||||
prev: Weak::<RefCell::<ListHead<T>>>::new(),
|
prev: Weak::<RefCell::<ListHead<T>>>::new(),
|
||||||
next: Weak::<RefCell::<ListHead<T>>>::new(),
|
next: Weak::<RefCell::<ListHead<T>>>::new(),
|
||||||
data: data
|
data: data,
|
||||||
|
_self_wp: Weak::<RefCell::<ListHead<T>>>::new()
|
||||||
}));
|
}));
|
||||||
this.borrow_mut().init();
|
this.borrow_mut().init();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_rc_refcell(&self) -> Rc::<RefCell::<ListHead<T>>> {
|
fn shared_from_self(&self) -> Rc::<RefCell::<ListHead<T>>> {
|
||||||
let cell_rc: &Rc<RefCell<ListHead<T>>> = &self.next.upgrade().unwrap();
|
return self._self_wp.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>>);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(&mut self) {
|
fn init(&mut self) {
|
||||||
self.prev = Rc::downgrade(&self.get_rc_refcell());
|
self.prev = Rc::downgrade(&self.shared_from_self());
|
||||||
self.next = Rc::downgrade(&self.get_rc_refcell());
|
self.next = Rc::downgrade(&self.shared_from_self());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn data(&self) -> Weak::<RefCell::<T>> {
|
pub fn data(&self) -> Weak::<RefCell::<T>> {
|
||||||
@ -87,8 +84,8 @@ impl<T> ListHead<T> {
|
|||||||
|
|
||||||
pub fn for_each(&self, cb: fn (&Weak::<RefCell::<T>>) -> bool) {
|
pub fn for_each(&self, cb: fn (&Weak::<RefCell::<T>>) -> bool) {
|
||||||
let mut pos = self.next.clone();
|
let mut pos = self.next.clone();
|
||||||
let self_weak = &Rc::downgrade(&self.get_rc_refcell());
|
let self_weak = Rc::downgrade(&self.shared_from_self());
|
||||||
while !Weak::ptr_eq(&pos, self_weak) {
|
while !Weak::ptr_eq(&pos, &self_weak) {
|
||||||
if !cb(&pos.upgrade().unwrap().borrow().data()) {
|
if !cb(&pos.upgrade().unwrap().borrow().data()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user