feat: 第一节

This commit is contained in:
Asahi 2024-08-13 18:48:04 +08:00
parent 73a490d26b
commit 3b512ae66e
6 changed files with 182 additions and 0 deletions

17
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/target/debug/${fileBasenameNoExtension}",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# rs-study-future
用来学习和模拟rust中的future并且一步步实现一个简单的运行时库。
## How to run
```shell
cargo run --bin section1-1 # 运行第一节第一小节的代码 其他同理
```

21
src/bin/section1-1.rs Normal file
View File

@ -0,0 +1,21 @@
use std::time::{Duration, Instant};
#[allow(dead_code)]
fn delay_task(delay_time: usize) -> impl Fn() -> () {
// 记录当前时间
let start_time = Instant::now();
return move || {
if Instant::now() - start_time > Duration::from_secs(delay_time as u64) {
println!("已经经过了至少: {:?}", delay_time);
}
};
}
fn main() {
let delay_seconds_time: usize = 5;
let task = delay_task(delay_seconds_time);
loop {
task();
std::thread::sleep(Duration::from_millis(500)); // 休眠500ms
}
}

35
src/bin/section1-2.rs Normal file
View File

@ -0,0 +1,35 @@
use std::time::{Duration, Instant};
#[allow(dead_code)]
fn delay_task(delay_time: usize) -> impl Fn() -> bool {
// 记录当前时间
let start_time = Instant::now();
return move || {
if Instant::now() - start_time > Duration::from_secs(delay_time as u64) {
println!("已经经过了至少: {:?}", delay_time);
return true;
}
false
};
}
const TIME_PIECE: usize = 500;
fn main() {
let delay_seconds_time: usize = 5;
let task01 = delay_task(delay_seconds_time);
let task02 = delay_task(delay_seconds_time * 2);
let mut task01_is_done = false;
let mut task02_is_done = false;
loop {
if !task01_is_done {
task01_is_done = task01();
}
if !task02_is_done {
task02_is_done = task02();
}
if task01_is_done && task02_is_done {
break;
}
std::thread::sleep(Duration::from_millis(TIME_PIECE as u64)); // 休眠500ms
}
println!("退出主线程!");
}

87
src/bin/section1-3.rs Normal file
View File

@ -0,0 +1,87 @@
use std::{
borrow::BorrowMut,
cell::RefCell,
rc::Rc,
time::{Duration, Instant},
};
type TaskStatus = Rc<RefCell<bool>>;
#[allow(dead_code)]
fn delay_task(delay_time: usize) -> (impl FnMut() -> (), TaskStatus) {
// 记录当前时间
let start_time = Instant::now();
let status = Rc::new(RefCell::new(false));
let mut _status = status.clone();
return (
move || {
if !(*_status.borrow())
&& (Instant::now() - start_time > Duration::from_secs(delay_time as u64))
{
println!("已经经过了至少: {:?}", delay_time);
_status.borrow_mut().replace(true);
}
},
status.clone(),
);
}
const TIME_PIECE: usize = 500;
pub struct Task<F, T>
where
F: FnMut() -> T,
{
task_poll_fn: F,
task_status: TaskStatus,
}
impl<F, T> Task<F, T>
where
F: FnMut() -> T,
{
pub fn new(mut task_init_fn: impl FnMut() -> (F, TaskStatus)) -> Task<F, T> {
let (task_poll_fn, task_status) = task_init_fn();
Task {
task_poll_fn,
task_status,
}
}
pub fn run(&mut self) {
if !(*self.task_status.borrow()) {
(self.task_poll_fn)();
}
}
}
fn event_loop<F, T>(task_list: &mut Vec<Task<F, T>>)
where
F: FnMut() -> T,
{
loop{
let mut is_all_done = true;
for task in task_list.iter_mut() {
if !(*task.task_status.borrow()) {
is_all_done = false;
task.run();
break;
}
}
if is_all_done {
break;
}
std::thread::sleep(Duration::from_millis(TIME_PIECE as u64));
}
println!("事件循环退出!");
}
fn main() {
let mut task_list = vec![];
let delay_seconds_time: usize = 5;
task_list.push(Task::new(move || {
return delay_task(delay_seconds_time);
}));
task_list.push(Task::new(move || {
return delay_task(delay_seconds_time * 2);
}));
event_loop(&mut task_list);
println!("退出主线程!");
}

15
src/bin/test.rs Normal file
View File

@ -0,0 +1,15 @@
use std::rc::Rc;
use std::cell::RefCell;
fn main() {
let status = Rc::new(RefCell::new(false)); // 创建一个 Rc<RefCell<bool>>
{
// 使用 borrow_mut 获取内部值的可变引用
let mut status_mut = status.borrow_mut();
*status_mut = true; // 通过解引用修改值
}
// 读取修改后的值
println!("status: {}", *status.borrow());
}