1
This commit is contained in:
parent
805594a5fb
commit
98a3cd3fe7
49
src/timer.rs
49
src/timer.rs
@ -9,11 +9,14 @@ const TVR_SIZE: usize = 1 << TVR_BITS;
|
||||
const TVN_MASK: usize = TVN_SIZE - 1;
|
||||
const TVR_MASK: usize = TVR_SIZE - 1;
|
||||
|
||||
pub type TimerAttacherRp = Rc::<RefCell::<TimerAttacher>>;
|
||||
pub type TimerWp = Weak::<Rc::<RefCell::<TimerList>>>;
|
||||
pub type TimerCb = Box::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>;
|
||||
type TimerListListHeadRp = Rc::<RefCell::<crate::ListHead<TimerList>>>;
|
||||
type TimerListRp = Rc::<RefCell::<TimerList>>;
|
||||
|
||||
pub struct TimerAttacher {
|
||||
timers: Rc::<RefCell::<crate::ListHead<TimerList>>>,
|
||||
timers: TimerListListHeadRp,
|
||||
}
|
||||
|
||||
enum TimerType {
|
||||
@ -32,10 +35,10 @@ impl Default for TimerType {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TimerList {
|
||||
holder: Rc::<RefCell::<TimerList>>,
|
||||
wp: Rc::<Rc::<RefCell::<TimerList>>>,
|
||||
timer_entry: Rc::<RefCell::<crate::ListHead<TimerList>>>,
|
||||
attach_entry: Rc::<RefCell::<crate::ListHead<TimerList>>>,
|
||||
holder: TimerListRp,
|
||||
wp: Rc::<TimerListRp>,
|
||||
timer_entry: TimerListListHeadRp,
|
||||
attach_entry: TimerListListHeadRp,
|
||||
timer_type: TimerType,
|
||||
expire_time: i32,
|
||||
expires: i64,
|
||||
@ -84,7 +87,7 @@ impl Timer {
|
||||
self.tv5[i] = crate::ListHead::<TimerList>::new_head();
|
||||
}
|
||||
self.timer_tick = (self.get_tick_count)();
|
||||
let self_wp = self.get_rc_refcell();
|
||||
let self_wp = self.shared_from_self();
|
||||
let cb = Box::new(
|
||||
move |_event: i32, _args: Option<Vec<Rc::<dyn Any>>>| {
|
||||
self_wp.borrow_mut().gc_timer(_event, _args);
|
||||
@ -97,7 +100,7 @@ impl Timer {
|
||||
);
|
||||
}
|
||||
|
||||
fn get_rc_refcell(&self) -> Rc::<RefCell::<Timer>> {
|
||||
fn shared_from_self(&self) -> Rc::<RefCell::<Timer>> {
|
||||
//let cell_rc: &Rc<RefCell<Timer> = &self ;
|
||||
//let head: &ListHead<T> = &*cell_rc.borrow();
|
||||
let cell_start = 0 as *const u8;
|
||||
@ -176,7 +179,7 @@ impl Timer {
|
||||
timer_type: TimerType,
|
||||
expire_time: i32,
|
||||
cb: TimerCb,
|
||||
attacher: Option<Rc::<RefCell::<TimerAttacher>>>) -> TimerWp {
|
||||
attacher: Option<TimerAttacherRp>) -> TimerWp {
|
||||
let t = self.new_timer_list();
|
||||
t.borrow_mut().cb = Some(cb);
|
||||
t.borrow_mut().timer_type = timer_type;
|
||||
@ -220,32 +223,22 @@ impl Timer {
|
||||
return Rc::downgrade(&t.borrow().wp);
|
||||
}
|
||||
|
||||
pub fn set_timeout(&mut self,
|
||||
expire_time: i32,
|
||||
cb: TimerCb
|
||||
) -> TimerWp {
|
||||
return self.internal_add(TimerType::Timeout, expire_time, cb, None);
|
||||
pub fn set_timeout(&mut self, time: i32, cb: TimerCb) -> TimerWp {
|
||||
return self.internal_add(TimerType::Timeout, time, cb, None);
|
||||
}
|
||||
|
||||
pub fn set_timeout_ex(&mut self,
|
||||
expire_time: i32,
|
||||
cb: TimerCb,
|
||||
attacher: Rc::<RefCell::<TimerAttacher>>) -> TimerWp {
|
||||
return self.internal_add(TimerType::Timeout, expire_time, cb, Some(attacher));
|
||||
pub fn set_timeout_ex
|
||||
(&mut self, time: i32, cb: TimerCb, attacher: TimerAttacherRp) -> TimerWp {
|
||||
return self.internal_add(TimerType::Timeout, time, cb, Some(attacher));
|
||||
}
|
||||
|
||||
pub fn set_interval(&mut self,
|
||||
expire_time: i32,
|
||||
cb: TimerCb
|
||||
) -> TimerWp {
|
||||
return self.internal_add(TimerType::Interval, expire_time, cb, None);
|
||||
pub fn set_interval(&mut self, time: i32, cb: TimerCb) -> TimerWp {
|
||||
return self.internal_add(TimerType::Interval, time, cb, None);
|
||||
}
|
||||
|
||||
pub fn set_interval_ex(&mut self,
|
||||
expire_time: i32,
|
||||
cb: TimerCb,
|
||||
attacher: Rc::<RefCell::<TimerAttacher>>) -> TimerWp {
|
||||
return self.internal_add(TimerType::Interval, expire_time, cb, Some(attacher));
|
||||
pub fn set_interval_ex
|
||||
(&mut self, time: i32, cb: TimerCb, attacher: TimerAttacherRp) -> TimerWp {
|
||||
return self.internal_add(TimerType::Interval, time, cb, Some(attacher));
|
||||
}
|
||||
|
||||
pub fn fire_event(&mut self,
|
||||
|
Loading…
x
Reference in New Issue
Block a user