Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2015 19:45
Show Gist options
  • Save anonymous/0cbea83ee7fc018260ef to your computer and use it in GitHub Desktop.
Save anonymous/0cbea83ee7fc018260ef to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![allow(dead_code, unused_variables, unused_imports)]
#![feature(placement_in_syntax, placement_new_protocol)]
#![allow(unused_parens)]
use std::cell::Cell;
struct LeftHandSide<X, Y> { a: Option<X>, b: Option<Y> }
struct A(i32);
#[derive(Debug)]
struct B(i32);
#[derive(Debug)]
struct Kernel(i32);
impl Kernel {
fn build_a_b(self) -> B {
println!("Building a B from {:?}", self);
B(self.0)
}
}
use std::ops::{Place, Placer, InPlace};
struct LhsPlace {
k: Kernel
}
impl Placer<Kernel> for LeftHandSide<A, B> {
type Place = LhsPlace;
fn make_place(self) -> Self::Place {
println!("allocating a place");
LhsPlace { k: Kernel(0) }
}
}
impl Place<Kernel> for LhsPlace {
fn pointer(&mut self) -> *mut Kernel {
let p: *mut Kernel = &mut self.k;
println!("LhsPlace: Handing pointer {:?} off", p);
p
}
}
impl InPlace<Kernel> for LhsPlace {
type Owner = B;
unsafe fn finalize(self) -> B {
self.k.build_a_b()
}
}
fn main() {
let lhs = LeftHandSide { a: Some(A(0)), b: Some(B(0)) };
println!("right before placement expr");
let b = (lhs <- Kernel(10+10));
println!("got here! b: {:?}", b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment