Skip to content

Commit c3077fb

Browse files
committed
2 parents ef0d5d2 + abc7bcc commit c3077fb

File tree

3 files changed

+32
-95
lines changed

3 files changed

+32
-95
lines changed

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: 32 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,45 @@
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();
1+
use std::{marker::PhantomPinned, pin::Pin};
72

8-
// list1.iter().for_each(|p| println!("{}", p.show()));
3+
// use std::pin::pin;
4+
// fn main() {
5+
// let mut x = pin!(MaybeSelfRef::default());
6+
// MaybeSelfRef::init(x.as_mut());
7+
// println!("{:?}", x.as_ref().b());
8+
// }
99

10-
// let list2 = Vec::from_iter(list1.iter());
11-
// list2.iter().for_each(|p| println!("{}", p.show()));
10+
// // Pinning stack
11+
// fn main() {
12+
// let mut x = MaybeSelfRef::default();
13+
// let mut x = unsafe { Pin::new_unchecked(&mut x) };
14+
// MaybeSelfRef::init(x.as_mut());
15+
// println!("{:?}", x.as_ref().b());
1216
// }
17+
1318
fn main() {
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());
19+
let mut x = Box::pin(MaybeSelfRef::default());
20+
x.as_mut().init();
21+
let b = x.as_mut().b().unwrap();
22+
println!("{}", b);
23+
*b = 2;
24+
println!("{x:?}");
1925
}
2026

21-
#[derive(Clone, Default)]
22-
struct Picker {
23-
numbers: Option<[usize; 3]>,
24-
picked: Option<*const usize>,
27+
#[derive(Default, Debug)]
28+
struct MaybeSelfRef {
29+
a: usize,
30+
b: Option<*mut usize>,
31+
_pin: PhantomPinned,
2532
}
2633

27-
impl Picker {
28-
fn new() -> Self {
29-
Picker {
30-
numbers: None,
31-
picked: None,
34+
impl MaybeSelfRef {
35+
fn init(self: Pin<&mut Self>) {
36+
unsafe {
37+
let Self { a, b, .. } = self.get_unchecked_mut();
38+
*b = Some(a);
3239
}
3340
}
3441

35-
fn pick(&mut self, n: usize) {
36-
self.numbers = Some([1, 2, 3]);
37-
self.picked = Some(&self.numbers.as_ref().unwrap()[n]);
38-
}
39-
40-
fn show(&self) -> String {
41-
match self.picked {
42-
Some(p) => format!("Pick: {}", unsafe {*p}),
43-
None => format!("Nothing picked yet"),
44-
}
42+
fn b(self: Pin<&mut Self>) -> Option<&mut usize> {
43+
unsafe { self.get_unchecked_mut().b.map(|b| &mut *b) }
4544
}
4645
}
47-
48-
// fn main() {
49-
// let code_table = ['d','h','e','x','l','w','p','y','z'];
50-
51-
// let mut selfref = SelfRef {
52-
// code_table,
53-
// encoder: Encoder::new(),
54-
// };
55-
56-
// selfref.encoder.set(&selfref.code_table);
57-
58-
// selfref.encoder.encode(&[1,2,3,2,3,4,5,2]);
59-
// println!("{}", selfref.encoder.txt);
60-
// }
61-
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-
// }

0 commit comments

Comments
 (0)