Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(pin, arbitrary_self_types, optin_builtin_traits)]
- use std::marker::Unpin;
- use std::mem::PinMut;
- struct SelfReferential {
- foo_ptr: *const u8,
- x: u8,
- }
- impl !Unpin for SelfReferential {}
- impl SelfReferential {
- pub fn new(x: u8) -> Self {
- SelfReferential {
- foo_ptr: ::std::ptr::null(),
- x,
- }
- }
- pub fn reference_self(self: PinMut<Self>) {
- let foo_ptr = &self.x as *const _;
- unsafe { PinMut::get_mut(self) }.foo_ptr = foo_ptr;
- }
- pub fn deref_ptr(&self) {
- if self.foo_ptr != ::std::ptr::null() {
- println!("{:?}", unsafe { *self.foo_ptr });
- }
- }
- }
- fn main() {
- let x = SelfReferential::new(x);
- x.deref_ptr();
- pin!(x);
- x.reference_self();
- x.deref_ptr();
- }
Add Comment
Please, Sign In to add comment