Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 21, 2019 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/6bf0054788fed8d5bed5b88dfc01b9c3 to your computer and use it in GitHub Desktop.
Save rust-play/6bf0054788fed8d5bed5b88dfc01b9c3 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![feature(box_into_pin)]
use std::pin::Pin;
trait T {
fn test(&self) -> usize;
}
struct X {
_n: i32
}
impl T for X {
fn test(&self) -> usize {
self as *const X as usize
}
}
fn add_inst(vec: &mut Vec<Pin<Box<T>>>) -> &X {
let x = X { _n: 0};
let b = Box::into_pin(Box::new(x));
let ptr = &*b as *const X;
vec.push(b);
unsafe { &*ptr }
}
fn main() {
let mut v = Vec::new();
let x = add_inst(&mut v);
let v0 = x.test();
let y = add_inst(&mut v);
let v1 = y.test();
assert!(v[0].test() == v0);
assert!(v[1].test() == v1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment