|
| 1 | +use std::any::{Any, TypeId}; |
| 2 | +use std::cell::UnsafeCell; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::fmt; |
| 5 | +use std::mem; |
| 6 | +use std::ops::Deref; |
| 7 | + |
| 8 | +pub struct OptCell<T>(UnsafeCell<Option<T>>); |
| 9 | + |
| 10 | +impl<T> OptCell<T> { |
| 11 | + #[inline] |
| 12 | + pub fn new(val: Option<T>) -> OptCell<T> { |
| 13 | + OptCell(UnsafeCell::new(val)) |
| 14 | + } |
| 15 | + |
| 16 | + #[inline] |
| 17 | + pub fn set(&self, val: T) { |
| 18 | + unsafe { |
| 19 | + let opt = self.0.get(); |
| 20 | + debug_assert!((*opt).is_none()); |
| 21 | + *opt = Some(val) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + #[inline] |
| 26 | + pub unsafe fn get_mut(&mut self) -> &mut T { |
| 27 | + let opt = &mut *self.0.get(); |
| 28 | + opt.as_mut().unwrap() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl<T> Deref for OptCell<T> { |
| 33 | + type Target = Option<T>; |
| 34 | + #[inline] |
| 35 | + fn deref<'a>(&'a self) -> &'a Option<T> { |
| 36 | + unsafe { &*self.0.get() } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl<T: Clone> Clone for OptCell<T> { |
| 41 | + #[inline] |
| 42 | + fn clone(&self) -> OptCell<T> { |
| 43 | + OptCell::new((**self).clone()) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +pub struct PtrMapCell<V: ?Sized>(UnsafeCell<PtrMap<Box<V>>>); |
| 48 | + |
| 49 | +#[derive(Clone, Debug)] |
| 50 | +enum PtrMap<T> { |
| 51 | + Empty, |
| 52 | + One(TypeId, T), |
| 53 | + Many(HashMap<TypeId, T>) |
| 54 | +} |
| 55 | + |
| 56 | +impl<V: ?Sized + fmt::Debug + Any + 'static> PtrMapCell<V> { |
| 57 | + #[inline] |
| 58 | + pub fn new() -> PtrMapCell<V> { |
| 59 | + PtrMapCell(UnsafeCell::new(PtrMap::Empty)) |
| 60 | + } |
| 61 | + |
| 62 | + #[inline] |
| 63 | + pub fn get(&self, key: TypeId) -> Option<&V> { |
| 64 | + let map = unsafe { &*self.0.get() }; |
| 65 | + match *map { |
| 66 | + PtrMap::Empty => None, |
| 67 | + PtrMap::One(id, ref v) => if id == key { |
| 68 | + Some(v) |
| 69 | + } else { |
| 70 | + None |
| 71 | + }, |
| 72 | + PtrMap::Many(ref hm) => hm.get(&key) |
| 73 | + }.map(|val| &**val) |
| 74 | + } |
| 75 | + |
| 76 | + #[inline] |
| 77 | + pub fn get_mut(&mut self, key: TypeId) -> Option<&mut V> { |
| 78 | + let mut map = unsafe { &mut *self.0.get() }; |
| 79 | + match *map { |
| 80 | + PtrMap::Empty => None, |
| 81 | + PtrMap::One(id, ref mut v) => if id == key { |
| 82 | + Some(v) |
| 83 | + } else { |
| 84 | + None |
| 85 | + }, |
| 86 | + PtrMap::Many(ref mut hm) => hm.get_mut(&key) |
| 87 | + }.map(|val| &mut **val) |
| 88 | + } |
| 89 | + |
| 90 | + #[inline] |
| 91 | + pub unsafe fn insert(&self, key: TypeId, val: Box<V>) { |
| 92 | + let mut map = &mut *self.0.get(); |
| 93 | + match *map { |
| 94 | + PtrMap::Empty => *map = PtrMap::One(key, val), |
| 95 | + PtrMap::One(..) => { |
| 96 | + let one = mem::replace(map, PtrMap::Empty); |
| 97 | + match one { |
| 98 | + PtrMap::One(id, one) => { |
| 99 | + debug_assert!(id != key); |
| 100 | + let mut hm = HashMap::with_capacity(2); |
| 101 | + hm.insert(id, one); |
| 102 | + hm.insert(key, val); |
| 103 | + mem::replace(map, PtrMap::Many(hm)); |
| 104 | + }, |
| 105 | + _ => unreachable!() |
| 106 | + } |
| 107 | + }, |
| 108 | + PtrMap::Many(ref mut hm) => { hm.insert(key, val); } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + #[inline] |
| 113 | + pub unsafe fn one(&self) -> &V { |
| 114 | + let map = &*self.0.get(); |
| 115 | + match *map { |
| 116 | + PtrMap::One(_, ref one) => one, |
| 117 | + _ => panic!("not PtrMap::One value, {:?}", *map) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl<V: ?Sized + fmt::Debug + Any + 'static> Clone for PtrMapCell<V> where Box<V>: Clone { |
| 123 | + #[inline] |
| 124 | + fn clone(&self) -> PtrMapCell<V> { |
| 125 | + let cell = PtrMapCell::new(); |
| 126 | + unsafe { |
| 127 | + *cell.0.get() = (&*self.0.get()).clone() |
| 128 | + } |
| 129 | + cell |
| 130 | + } |
| 131 | +} |
0 commit comments