Skip to content

Instantly share code, notes, and snippets.

@alexcrichton alexcrichton/foo.rs
Created Oct 6, 2015

Embed
What would you like to do?
#![feature(optin_builtin_traits)]
#![crate_type = "lib"]
use std::cell::{UnsafeCell, RefCell};
use std::ops::{Deref, DerefMut};
use std::sync::Mutex;
pub trait ExceptionSafe {}
impl ExceptionSafe for .. {}
impl<'a, T> !ExceptionSafe for &'a mut T {}
impl<'a, T: NoUnsafeCell> ExceptionSafe for &'a T {}
impl<T: NoUnsafeCell> ExceptionSafe for *mut T {}
impl<T: NoUnsafeCell> ExceptionSafe for *const T {}
impl<T> ExceptionSafe for Mutex<T> {}
pub trait NoUnsafeCell {}
impl NoUnsafeCell for .. {}
impl<T> !NoUnsafeCell for UnsafeCell<T> {}
pub struct PoisoningRefCell<T>(RefCell<T>);
impl<'a, T> ExceptionSafe for &'a PoisoningRefCell<T> {}
pub struct AssertExnSafe<T>(pub T);
impl<T> ExceptionSafe for AssertExnSafe<T> {}
impl<T> Deref for AssertExnSafe<T> {
type Target = T;
fn deref(&self) -> &T { &self.0 }
}
impl<T> DerefMut for AssertExnSafe<T> {
fn deref_mut(&mut self) -> &mut T { &mut self.0 }
}
#[allow(unused)]
fn foo() {
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
fn assert<T: ExceptionSafe>() {}
struct Foo { a: i32 }
assert::<i32>();
assert::<Box<i32>>();
assert::<*mut u32>();
assert::<&u32>();
assert::<Foo>();
assert::<&Foo>();
assert::<RefCell<i32>>();
assert::<&PoisoningRefCell<i32>>();
fn bar<T>() {
assert::<Mutex<T>>();
}
// errors
// assert::<Rc<i32>>();
// assert::<Arc<i32>>();
// assert::<&mut i32>();
// assert::<&RefCell<i32>>();
// assert::<*const UnsafeCell<i32>>();
// assert::<*mut RefCell<i32>>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.