Skip to content

Instantly share code, notes, and snippets.

@arielb1
Last active August 29, 2015 14:08
Show Gist options
  • Save arielb1/970e979670109f995aa9 to your computer and use it in GitHub Desktop.
Save arielb1/970e979670109f995aa9 to your computer and use it in GitHub Desktop.
Strange Types
#![feature(tuple_indexing)]
use std::rc::Rc;
use std::mem;
use std::kinds::marker;
// Invariant: &mut Xyz always points to a valid C xyz.
// Xyz rvalues don't exist.
// These leak. I *could* wrap a box or arena, but that would
// complicate things.
extern "C" {
// struct Xyz;
fn xyz_create() -> *mut Xyz;
fn xyz_play(s: *mut Xyz);
}
pub struct Xyz(marker::NoCopy);
impl Xyz {
pub fn new() -> &'static mut Xyz {
unsafe {
let x = xyz_create();
mem::transmute(x)
}
}
pub fn play(&mut self) {
unsafe { xyz_play(mem::transmute(self)) }
}
}
// Invariant: only the main task has RcMainTask values
pub struct RcMainTask<T>(Rc<T>);
impl<T> RcMainTask<T> {
pub fn new(t: T) -> Option<RcMainTask<T>> {
if on_main_task() {
Some(RcMainTask(Rc::new(t)))
} else { None }
}
pub fn main_clone(self) -> (RcMainTask<T>, RcMainTask<T>) {
let new = RcMainTask(self.0.clone());
(self, new)
}
}
impl<T> Deref<T> for RcMainTask<T> {
fn deref(&self) -> &T { &*self.0 }
}
// - by Sharp
pub struct RcMut<T>(Rc<T>);
impl<T> RcMut<T> {
pub fn new(t: T) -> RcMut<T> {
RcMut(Rc::new(t))
}
pub fn mut_clone(&mut self) -> RcMut<T> {
RcMut(self.0.clone())
}
}
impl<T> Deref<T> for RcMut<T> {
fn deref(&self) -> &T { &*self.0 }
}
// fn on_main_task() -> bool { false /* XXX: implement */ }
// fn main() {}
@arielb1
Copy link
Author

arielb1 commented Oct 28, 2014

Observe that RcMainTask::main_clone would be unsafe outside the main task.

@arielb1
Copy link
Author

arielb1 commented Oct 28, 2014

&mut Xyz and &mut RcMainTask are perfectly fine Send types. However, Xyz is a bottom (can be used to violate memory safety), and RcMainTask is not Send.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment