Skip to content

Commit d5aa3d6

Browse files
committed
got something working in ch10
1 parent 895b682 commit d5aa3d6

File tree

6 files changed

+212
-52
lines changed

6 files changed

+212
-52
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "lldb",
9+
"request": "launch",
10+
"name": "Debug executable 'x-pin-experiments'",
11+
"cargo": {
12+
"args": [
13+
"build",
14+
"--bin=x-pin-experiments",
15+
"--package=x-pin-experiments"
16+
],
17+
"filter": {
18+
"name": "x-pin-experiments",
19+
"kind": "bin"
20+
}
21+
},
22+
"args": [],
23+
"cwd": "${workspaceFolder}"
24+
},
25+
{
26+
"type": "lldb",
27+
"request": "launch",
28+
"name": "Debug unit tests in executable 'x-pin-experiments'",
29+
"cargo": {
30+
"args": [
31+
"test",
32+
"--no-run",
33+
"--bin=x-pin-experiments",
34+
"--package=x-pin-experiments"
35+
],
36+
"filter": {
37+
"name": "x-pin-experiments",
38+
"kind": "bin"
39+
}
40+
},
41+
"args": [],
42+
"cwd": "${workspaceFolder}"
43+
}
44+
]
45+
}

ch10/x-pin-experiments/src/main.exe

155 KB
Binary file not shown.

ch10/x-pin-experiments/src/main.pdb

1.43 MB
Binary file not shown.

ch10/x-pin-experiments/src/main.rs

Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,108 @@
1-
use rand::prelude::*;
1+
// fn main() {
2+
// let list1: Vec<SelfRef> = (0..10).map(|_| {
3+
// let mut picker = SelfRef::new();
4+
// picker.pick(2);
5+
// picker
6+
// }).collect();
27

8+
// list1.iter().for_each(|p| println!("{}", p.show()));
9+
10+
// let list2 = Vec::from_iter(list1.iter());
11+
// list2.iter().for_each(|p| println!("{}", p.show()));
12+
// }
313
fn main() {
4-
selfref1();
14+
let mut picker1 = Picker::new();
15+
picker1.pick(2);
16+
println!("{}", picker1.show());
17+
let replaced = std::mem::replace(&mut picker1, Picker::new());
18+
println!("{}", replaced.show());
519
}
620

7-
fn selfref1() {
8-
let mut first = SelfRef1::new(0);
9-
println!("It's OK: {}", first.b());
10-
let second = std::mem::replace(first.as_mut(), *SelfRef1::new(2));
11-
println!("Ooops: {}", first.b());
12-
println!("Ooops: {}", second.b());
21+
#[derive(Clone, Default)]
22+
struct Picker {
23+
numbers: Option<[usize; 3]>,
24+
picked: Option<*const usize>,
1325
}
1426

15-
#[derive(Debug)]
16-
struct SelfRef1 {
17-
a: [char;3],
18-
b: *const char,
19-
}
27+
impl Picker {
28+
fn new() -> Self {
29+
Picker {
30+
numbers: None,
31+
picked: None,
32+
}
33+
}
2034

21-
impl SelfRef1 {
22-
fn new(n: usize) -> Box<Self> {
23-
let mut selfref = Box::new(Self {
24-
a: ['a', 'b', 'c'],
25-
b: std::ptr::null(),
26-
});
27-
selfref.b = &selfref.a[n] as *const _;
28-
selfref
35+
fn pick(&mut self, n: usize) {
36+
self.numbers = Some([1, 2, 3]);
37+
self.picked = Some(&self.numbers.as_ref().unwrap()[n]);
2938
}
3039

31-
fn b(&self) -> &char {
32-
unsafe { &*self.b }
40+
fn show(&self) -> String {
41+
match self.picked {
42+
Some(p) => format!("Pick: {}", unsafe {*p}),
43+
None => format!("Nothing picked yet"),
44+
}
3345
}
3446
}
3547

36-
// =======================================================
37-
use std::pin::Pin;
48+
// fn main() {
49+
// let code_table = ['d','h','e','x','l','w','p','y','z'];
3850

39-
fn selfref2() {
40-
let mut first = SelfRef2::new(0);
41-
println!("It's OK: {}", first.b());
42-
let mut second = SelfRef2::new(2);
43-
std::mem::swap(&mut first, &mut second);
44-
println!("Ooops: {}", first.b());
45-
}
51+
// let mut selfref = SelfRef {
52+
// code_table,
53+
// encoder: Encoder::new(),
54+
// };
4655

47-
#[derive(Debug)]
48-
struct SelfRef2 {
49-
a: [char;3],
50-
b: *const char,
51-
_pin: std::marker::PhantomPinned,
52-
}
56+
// selfref.encoder.set(&selfref.code_table);
5357

54-
impl SelfRef2 {
55-
fn new(n: usize) -> Pin<Box<Self>> {
56-
let mut selfref = Box::pin(Self {
57-
a: ['a', 'b', 'c'],
58-
b: std::ptr::null(),
59-
_pin: std::marker::PhantomPinned,
60-
});
61-
//selfref.b = &selfref.a[n] as *const _;
62-
selfref
63-
}
58+
// selfref.encoder.encode(&[1,2,3,2,3,4,5,2]);
59+
// println!("{}", selfref.encoder.txt);
60+
// }
6461

65-
fn b(&self) -> &char {
66-
unsafe { &*self.b }
67-
}
68-
}
62+
// struct SelfRef<'a> {
63+
// code_table: [char; 9],
64+
// encoder: Encoder<'a>,
65+
// }
66+
67+
// struct Encoder<'a> {
68+
// code_table: Option<&'a [char; 9]>,
69+
// txt: String,
70+
// }
71+
72+
// impl<'a> Encoder<'a> {
73+
// fn new() -> Self {
74+
// Encoder { code_table: None, txt: String::new() }
75+
// }
76+
// fn set(&mut self, code_table: &'a [char; 9]) {
77+
// self.code_table = Some(code_table);
78+
// }
79+
// fn encode(&mut self, numbers: &[usize]) {
80+
// let table = self.code_table.expect("Not set");
81+
// for num in numbers {
82+
// self.txt.push(table[*num]);
83+
// }
84+
// }
85+
// }
86+
87+
// struct SecretFmt<'a> {
88+
// random: &'a String,
89+
// something_big: Vec<u8>,
90+
// }
91+
92+
// impl<'a> SecretFmt<'a> {
93+
// fn new(random: &'a String) -> Self {
94+
// Self {
95+
// random,
96+
// something_big: vec![0u8; 1024 * 1000 * 1000],
97+
// }
98+
// }
99+
100+
// fn fmt(&self, txt: &str) -> String {
101+
// format!("{}: {}", self.random, txt)
102+
// }
103+
// }
104+
105+
// struct MyFormatter<'a> {
106+
// data: String,
107+
// formatter: SecretFmt<'a>,
108+
// }

ch10/x-pin-experiments/step1.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// async fn foo() {
2+
// let numbers: Vec<usize> = (0..10).collect();
3+
// let mut nums = numbers.iter();
4+
5+
// if let Some(n) = nums.next() {
6+
// printfut("Future {n}").wait;
7+
// }
8+
9+
// for num in nums {
10+
// println!("Hello #{num}");
11+
// }
12+
// }
13+
14+
enum SelfRef<'a> {
15+
Start,
16+
Wait(State1<'a>),
17+
Resolved,
18+
}
19+
20+
impl<'a> SelfRef<'a> {
21+
pub fn poll(&mut self) -> bool {
22+
loop {
23+
match self {
24+
SelfRef::Start => {
25+
let numbers: Vec<usize> = (0..10).collect();
26+
let mut nums = numbers.iter();
27+
if let Some(n) = nums.next() {
28+
println!("Future {n}")
29+
}
30+
31+
// store state
32+
*self = SelfRef::Wait(State1 { numbers, nums });
33+
break false;
34+
}
35+
36+
SelfRef::Wait(State1 {numbers, nums }) => {
37+
for num in nums {
38+
println!("Hello #{num}")
39+
}
40+
*self = SelfRef::Resolved;
41+
break true;
42+
}
43+
44+
SelfRef::Resolved => panic!("Polled a resoved future!"),
45+
}
46+
}
47+
}
48+
}
49+
50+
struct State1<'a> {
51+
numbers: Vec<usize>,
52+
nums: Iter<'a, usize>,
53+
}

ch10/x-pin-experiments/step1a.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fn main() {
2+
3+
}
4+
5+
struct SelfRef<'a> {
6+
numbers: [usize; 3],
7+
picked: Option<&'a usize>,
8+
}
9+
10+
impl<'a> SelfRef<'a> {
11+
fn new() -> Self {
12+
SelfRef {numbers: [1,2,3], picked: None}
13+
}
14+
15+
fn pick(&mut self, n: usize) {
16+
self.picked = Some(&self.numbers[n]);
17+
}
18+
19+
fn show(&self) -> usize {
20+
*self.picked.unwrap()
21+
}
22+
}

0 commit comments

Comments
 (0)