3个版本

使用旧的Rust 2015

0.0.3 2018年2月18日
0.0.2 2018年2月18日
0.0.1 2018年2月17日

#617 in 模板引擎

MIT/Apache

15KB
138

runtime-loop

runtime-loop crate提供了一个通用的运行时循环,它持续尝试找到系统中的变化,并相应地采取行动。

安装

将其添加到cargo.toml

[dependencies]
runtime-loop = "0.0.3"

将其添加到您的crate

extern crate runtime_loop;

示例

extern crate runtime_loop;

use runtime_loop::{RuntimeLoop, Pollable, Signal};
use std::thread;
use std::sync::{Arc, Mutex};
use std::sync::atomic::Ordering;
use std::time;

// Create memory for the runtime loop. Grab the cancel flag to exit the loop based on time.
let mut rloop = RuntimeLoop::new();
let cancel_flag = rloop.get_cancel_flag();

// When polled, increment the value.
// This should normally be your poll function. We increment x as a test.
let target = Arc::new(Mutex::new(Pollable::new(0, Box::new(|x| x+1) ) ) );

// When the polled value changes, print a message.
if let Ok(mut target) = target.lock() {
    target.add_trigger(Signal::new(
        Box::new(|x| println!("Increment: {:?}", x))
    ));
}

// Add the pollable to the runtime loop.
rloop.add_pollable(target.clone());

// This is an async runtime loop.  Though it is acceptable to call rloop.run() without spawning a new thread, spawning a new thread will enable external cancellation of the runtime loop.
let runtime_thread = thread::spawn(move || {
   if let Result::Err(s) = rloop.run() {
       panic!(s); }
});

// Run for N milliseconds before canceling and joining threads.
thread::sleep(time::Duration::from_millis(100)); //let the thread run for a bit
cancel_flag.store(true, Ordering::Relaxed); //Cancel runtime loop!
if let Result::Err(s) = runtime_thread.join() {
   panic!(s);
}

许可证

根据您的选择,受以下任一许可证的许可:

贡献

除非您明确表示,否则根据Apache-2.0许可证定义,您有意提交的任何贡献,均应按上述方式双重许可,不附加任何其他条款或条件。

依赖关系

~0.6–0.9MB
~13K SLoC