diff --git a/code/examples/misc_8.rs b/code/examples/misc_8.rs new file mode 100644 index 0000000..5a01e98 --- /dev/null +++ b/code/examples/misc_8.rs @@ -0,0 +1,39 @@ +use core::pin::Pin; +use core::marker::PhantomPinned; + +#[derive(Default)] +struct MyPinnedType { + addr: usize, + _pin: PhantomPinned, +} + +impl MyPinnedType { + fn my_pin_fn(self: Pin<&mut Self>) { + let me = unsafe { Pin::into_inner_unchecked(self) }; + let me_addr = me as *mut Self as usize; + if me.addr == 0 { + me.addr = me_addr; + } else { + assert_eq!(me.addr, me_addr, "Pinned value was moved.") + } + } +} + +trait MyUnpinTrait { + fn into_pinned_type(self: Pin<&mut Self>) -> Pin<&mut MyPinnedType>; +} +impl MyUnpinTrait for MyPinnedType { + fn into_pinned_type(self: Pin<&mut Self>) -> Pin<&mut MyPinnedType> { + self + } +} +impl Unpin for dyn MyUnpinTrait {} + +fn main() { + let mut pinned_type = MyPinnedType::default(); + + Pin::new((&mut pinned_type) as &mut dyn MyUnpinTrait).into_pinned_type().my_pin_fn(); + + let pinned_type = Box::new(pinned_type); + Pin::from(pinned_type).as_mut().my_pin_fn(); +} \ No newline at end of file diff --git a/code/examples/stderr/misc_8.stderr b/code/examples/stderr/misc_8.stderr new file mode 100644 index 0000000..3a30f45 --- /dev/null +++ b/code/examples/stderr/misc_8.stderr @@ -0,0 +1,8 @@ +error[E0321]: cross-crate traits with a default impl, like `Unpin`, can only be implemented for a struct/enum type, not `(dyn MyUnpinTrait + 'static)` + --> examples/misc_8.rs:30:1 + | +30 | impl Unpin for dyn MyUnpinTrait {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type + +For more information about this error, try `rustc --explain E0321`. +error: could not compile `code` (example "misc_8") due to 1 previous error diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 9c820a7..a54623b 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -11,6 +11,7 @@ # Miscellaneous - [Miscellaneous 1](./misc/1.md) +- [Miscellaneous 8](./misc/8.md) # Trait Solver diff --git a/src/misc/8.md b/src/misc/8.md new file mode 100644 index 0000000..a2f5b86 --- /dev/null +++ b/src/misc/8.md @@ -0,0 +1,14 @@ +# Misc 8 @Darksonn + +```rust +{{#include ../../code/examples/misc_8.rs}} +``` + +
+Solution + +``` +{{#include ../../code/examples/stderr/misc_8.stderr}} +``` + +