diff --git a/.DS_Store b/.DS_Store index f833f46..0e22956 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/14-探讨Rust异步编程框架Mio/探讨Rust异步编程框架Mio.pdf b/14-探讨Rust异步编程框架Mio/探讨Rust异步编程框架Mio.pdf new file mode 100644 index 0000000..405b90e Binary files /dev/null and b/14-探讨Rust异步编程框架Mio/探讨Rust异步编程框架Mio.pdf differ diff --git a/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/Cargo.toml b/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/Cargo.toml new file mode 100644 index 0000000..7173122 --- /dev/null +++ b/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "learn-pin" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.4" diff --git a/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/example.rs b/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/example.rs new file mode 100644 index 0000000..ddbb4f9 --- /dev/null +++ b/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/example.rs @@ -0,0 +1,80 @@ +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + +struct StartState { + min_len: usize, +} + +struct WaitingOnFooTxtState { + min_len: usize, + foo_txt_future: impl Future, +} + +struct WaitingOnBarTxtState { + content: String, + bar_txt_future: impl Future, +} + +struct EndState {} + +enum ExampleStateMachine { + Start(StartState), + WaitingOnFooTxt(WaitingOnFooTxtState), + WaitingOnBarTxt(WaitingOnBarTxtState), + End(EndState), +} + +impl Future for ExampleStateMachine { + type Output = String; // return type of `example` + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { + loop { + match self { // TODO: handle pinning + ExampleStateMachine::Start(state) => { + // from body of `example` + let foo_txt_future = async_read_file("foo.txt"); + // `.await` operation + let state = WaitingOnFooTxtState { + min_len: state.min_len, + foo_txt_future, + }; + *self = ExampleStateMachine::WaitingOnFooTxt(state); + } + ExampleStateMachine::WaitingOnFooTxt(state) => { + match state.foo_txt_future.poll(cx) { + Poll::Pending => return Poll::Pending, + Poll::Ready(content) => { + // from body of `example` + if content.len() < state.min_len { + let bar_txt_future = async_read_file("bar.txt"); + // `.await` operation + let state = WaitingOnBarTxtState { + content, + bar_txt_future, + }; + *self = ExampleStateMachine::WaitingOnBarTxt(state); + } else { + *self = ExampleStateMachine::End(EndState)); + return Poll::Ready(content); + } + } + } + } + ExampleStateMachine::WaitingOnBarTxt(state) => { + match state.bar_txt_future.poll(cx) { + Poll::Pending => return Poll::Pending, + Poll::Ready(bar_txt) => { + *self = ExampleStateMachine::End(EndState)); + // from body of `example` + return Poll::Ready(state.content + &bar_txt); + } + } + } + ExampleStateMachine::End(state) => { + panic!("poll called after Poll::Ready was returned"); + } + } + } + } +} diff --git a/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/main.rs b/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/main.rs new file mode 100644 index 0000000..79b33c4 --- /dev/null +++ b/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/main.rs @@ -0,0 +1,39 @@ +struct Test { + a: String, + b: *const String, // 改成指针 +} + +impl Test { + fn new(txt: &str) -> Self { + Test { + a: String::from(txt), + b: std::ptr::null(), + } + } + + fn init(&mut self) { + let self_ref: *const String = &self.a; + self.b = self_ref; + } + + fn b(&self) -> &String { + unsafe {&*(self.b)} + } +} + +fn main() { + let mut test1 = Test::new("test1"); + test1.init(); + + let mut test2 = Test::new("test2"); + test2.init(); + + println!("a: {}, b: {}", test1.a, test1.b()); + + // 使用swap()函数交换两者, 这里发生了move + std::mem::swap(&mut test1, &mut test2); + + test1.a = "xxxxxxx".to_string(); + + println!("a: {}, b: {}", test2.a, test2.b()); +} \ No newline at end of file diff --git a/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/main1.rs b/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/main1.rs new file mode 100644 index 0000000..e62347c --- /dev/null +++ b/15-探讨为什么Pin在Rust异步编程中如此重要/learn-pin/src/main1.rs @@ -0,0 +1,5 @@ +fn main() { + let mut s1 = String::from("Hello"); + let s2 = s1; // s1的所有权转移给了s2,这里发生了move + // let s3 = s1; // s1的所有权以及转移走了,不能再move,否则会报错:error[E0382]: use of moved value: `s1` +} \ No newline at end of file diff --git a/15-探讨为什么Pin在Rust异步编程中如此重要/探讨为什么Pin在Rust异步编程中如此重要.pdf b/15-探讨为什么Pin在Rust异步编程中如此重要/探讨为什么Pin在Rust异步编程中如此重要.pdf new file mode 100644 index 0000000..9c48521 Binary files /dev/null and b/15-探讨为什么Pin在Rust异步编程中如此重要/探讨为什么Pin在Rust异步编程中如此重要.pdf differ