Skip to content

Commit 9310841

Browse files
modify
1 parent bd5d1a4 commit 9310841

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "learn-pin"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rand = "0.8.4"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::future::Future;
2+
use std::pin::Pin;
3+
use std::task::{Context, Poll};
4+
5+
struct StartState {
6+
min_len: usize,
7+
}
8+
9+
struct WaitingOnFooTxtState {
10+
min_len: usize,
11+
foo_txt_future: impl Future<Output = String>,
12+
}
13+
14+
struct WaitingOnBarTxtState {
15+
content: String,
16+
bar_txt_future: impl Future<Output = String>,
17+
}
18+
19+
struct EndState {}
20+
21+
enum ExampleStateMachine {
22+
Start(StartState),
23+
WaitingOnFooTxt(WaitingOnFooTxtState),
24+
WaitingOnBarTxt(WaitingOnBarTxtState),
25+
End(EndState),
26+
}
27+
28+
impl Future for ExampleStateMachine {
29+
type Output = String; // return type of `example`
30+
31+
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
32+
loop {
33+
match self { // TODO: handle pinning
34+
ExampleStateMachine::Start(state) => {
35+
// from body of `example`
36+
let foo_txt_future = async_read_file("foo.txt");
37+
// `.await` operation
38+
let state = WaitingOnFooTxtState {
39+
min_len: state.min_len,
40+
foo_txt_future,
41+
};
42+
*self = ExampleStateMachine::WaitingOnFooTxt(state);
43+
}
44+
ExampleStateMachine::WaitingOnFooTxt(state) => {
45+
match state.foo_txt_future.poll(cx) {
46+
Poll::Pending => return Poll::Pending,
47+
Poll::Ready(content) => {
48+
// from body of `example`
49+
if content.len() < state.min_len {
50+
let bar_txt_future = async_read_file("bar.txt");
51+
// `.await` operation
52+
let state = WaitingOnBarTxtState {
53+
content,
54+
bar_txt_future,
55+
};
56+
*self = ExampleStateMachine::WaitingOnBarTxt(state);
57+
} else {
58+
*self = ExampleStateMachine::End(EndState));
59+
return Poll::Ready(content);
60+
}
61+
}
62+
}
63+
}
64+
ExampleStateMachine::WaitingOnBarTxt(state) => {
65+
match state.bar_txt_future.poll(cx) {
66+
Poll::Pending => return Poll::Pending,
67+
Poll::Ready(bar_txt) => {
68+
*self = ExampleStateMachine::End(EndState));
69+
// from body of `example`
70+
return Poll::Ready(state.content + &bar_txt);
71+
}
72+
}
73+
}
74+
ExampleStateMachine::End(state) => {
75+
panic!("poll called after Poll::Ready was returned");
76+
}
77+
}
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
struct Test {
2+
a: String,
3+
b: *const String, // 改成指针
4+
}
5+
6+
impl Test {
7+
fn new(txt: &str) -> Self {
8+
Test {
9+
a: String::from(txt),
10+
b: std::ptr::null(),
11+
}
12+
}
13+
14+
fn init(&mut self) {
15+
let self_ref: *const String = &self.a;
16+
self.b = self_ref;
17+
}
18+
19+
fn b(&self) -> &String {
20+
unsafe {&*(self.b)}
21+
}
22+
}
23+
24+
fn main() {
25+
let mut test1 = Test::new("test1");
26+
test1.init();
27+
28+
let mut test2 = Test::new("test2");
29+
test2.init();
30+
31+
println!("a: {}, b: {}", test1.a, test1.b());
32+
33+
// 使用swap()函数交换两者, 这里发生了move
34+
std::mem::swap(&mut test1, &mut test2);
35+
36+
test1.a = "xxxxxxx".to_string();
37+
38+
println!("a: {}, b: {}", test2.a, test2.b());
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let mut s1 = String::from("Hello");
3+
let s2 = s1; // s1的所有权转移给了s2,这里发生了move
4+
// let s3 = s1; // s1的所有权以及转移走了,不能再move,否则会报错:error[E0382]: use of moved value: `s1`
5+
}

0 commit comments

Comments
 (0)