joshsulin
3 years ago
7 changed files with 133 additions and 0 deletions
Binary file not shown.
Binary file not shown.
@ -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" |
@ -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<Output = String>, |
|||
} |
|||
|
|||
struct WaitingOnBarTxtState { |
|||
content: String, |
|||
bar_txt_future: impl Future<Output = String>, |
|||
} |
|||
|
|||
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<Self::Output> { |
|||
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"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
@ -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()); |
|||
} |
@ -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`
|
|||
} |
Binary file not shown.
Loading…
Reference in new issue