Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 63c1aeb

Browse files
committedMay 13, 2024
add alices quiz (no explanation) as quiz misc 8
1 parent 998d091 commit 63c1aeb

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed
 

‎code/examples/misc_8.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use core::pin::Pin;
2+
use core::marker::PhantomPinned;
3+
4+
#[derive(Default)]
5+
struct MyPinnedType {
6+
addr: usize,
7+
_pin: PhantomPinned,
8+
}
9+
10+
impl MyPinnedType {
11+
fn my_pin_fn(self: Pin<&mut Self>) {
12+
let me = unsafe { Pin::into_inner_unchecked(self) };
13+
let me_addr = me as *mut Self as usize;
14+
if me.addr == 0 {
15+
me.addr = me_addr;
16+
} else {
17+
assert_eq!(me.addr, me_addr, "Pinned value was moved.")
18+
}
19+
}
20+
}
21+
22+
trait MyUnpinTrait {
23+
fn into_pinned_type(self: Pin<&mut Self>) -> Pin<&mut MyPinnedType>;
24+
}
25+
impl MyUnpinTrait for MyPinnedType {
26+
fn into_pinned_type(self: Pin<&mut Self>) -> Pin<&mut MyPinnedType> {
27+
self
28+
}
29+
}
30+
impl Unpin for dyn MyUnpinTrait {}
31+
32+
fn main() {
33+
let mut pinned_type = MyPinnedType::default();
34+
35+
Pin::new((&mut pinned_type) as &mut dyn MyUnpinTrait).into_pinned_type().my_pin_fn();
36+
37+
let pinned_type = Box::new(pinned_type);
38+
Pin::from(pinned_type).as_mut().my_pin_fn();
39+
}

‎code/examples/stderr/misc_8.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0321]: cross-crate traits with a default impl, like `Unpin`, can only be implemented for a struct/enum type, not `(dyn MyUnpinTrait + 'static)`
2+
--> examples/misc_8.rs:30:1
3+
|
4+
30 | impl Unpin for dyn MyUnpinTrait {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type
6+
7+
For more information about this error, try `rustc --explain E0321`.
8+
error: could not compile `code` (example "misc_8") due to 1 previous error

‎src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# Miscellaneous
1212

1313
- [Miscellaneous 1](./misc/1.md)
14+
- [Miscellaneous 8](./misc/8.md)
1415

1516
# Trait Solver
1617

‎src/misc/8.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Misc 8 @Darksonn
2+
3+
```rust
4+
{{#include ../../code/examples/misc_8.rs}}
5+
```
6+
7+
<details>
8+
<summary>Solution</summary>
9+
10+
```
11+
{{#include ../../code/examples/stderr/misc_8.stderr}}
12+
```
13+
14+
</details>

0 commit comments

Comments
 (0)
Please sign in to comment.