|
| 1 | +use super::auto_traits::*; |
| 2 | + |
| 3 | +use std::collections::HashSet; |
| 4 | +use std::mem::*; |
| 5 | +use std::ops::Deref; |
| 6 | + |
| 7 | +pub unsafe trait Trace { |
| 8 | + fn trace(t: usize); |
| 9 | + const TRACE_TYPE_INFO: GcTypeInfo; |
| 10 | + const TRACE_CHILD_TYPE_INFO: [Option<GcTypeInfo>; 8]; |
| 11 | + fn trace_transitive_type_info(tti: &mut Tti); |
| 12 | +} |
| 13 | + |
| 14 | +// Ideally would use negative impls |
| 15 | +// Blanket impl seems to be safe, since Mark's blanket requires NoGc |
| 16 | +// If you only implement Mark and not Trace CHILD_TYPE_INFO will all be const None |
| 17 | +unsafe impl<T: Immutable> Trace for T { |
| 18 | + default fn trace(_: usize) {} |
| 19 | + default const TRACE_TYPE_INFO: GcTypeInfo = GcTypeInfo::new::<Self>(); |
| 20 | + default const TRACE_CHILD_TYPE_INFO: [Option<GcTypeInfo>; 8] = [None; 8]; |
| 21 | + default fn trace_transitive_type_info(_: &mut Tti) {} |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 25 | +pub struct GcTypeInfo { |
| 26 | + trace_ptr: fn(usize), |
| 27 | + needs_drop: bool, |
| 28 | + byte_size: u16, |
| 29 | + alignment: u16, |
| 30 | +} |
| 31 | + |
| 32 | +impl GcTypeInfo { |
| 33 | + pub const fn new<T: Trace>() -> Self { |
| 34 | + Self { |
| 35 | + trace_ptr: T::trace, |
| 36 | + needs_drop: needs_drop::<T>(), |
| 37 | + byte_size: size_of::<T>() as u16, |
| 38 | + alignment: align_of::<T>() as u16, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub(crate) const fn one_child<T: Trace>() -> [Option<GcTypeInfo>; 8] { |
| 43 | + [ |
| 44 | + Some(GcTypeInfo::new::<T>()), |
| 45 | + None, |
| 46 | + None, |
| 47 | + None, |
| 48 | + None, |
| 49 | + None, |
| 50 | + None, |
| 51 | + None, |
| 52 | + ] |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +pub struct Tti { |
| 57 | + /// Holds fn ptr of trace_transitive_type_info calls |
| 58 | + parrents: HashSet<usize>, |
| 59 | + type_info: HashSet<GcTypeInfo>, |
| 60 | +} |
| 61 | + |
| 62 | +impl Tti { |
| 63 | + pub fn new() -> Self { |
| 64 | + Self { |
| 65 | + parrents: HashSet::new(), |
| 66 | + type_info: HashSet::new(), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + pub fn add_direct<T: Trace>(&mut self) { |
| 71 | + self.type_info |
| 72 | + .extend(T::TRACE_CHILD_TYPE_INFO.iter().filter_map(|o| *o)); |
| 73 | + } |
| 74 | + |
| 75 | + pub fn add_trans(&mut self, tti: fn(&mut Tti)) { |
| 76 | + if self.parrents.insert(tti as *const fn(&mut Tti) as usize) { |
| 77 | + tti(self) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// # std impls |
| 83 | +
|
| 84 | +unsafe impl<'r, T: Immutable + Trace> Trace for Option<T> { |
| 85 | + default fn trace(o: usize) { |
| 86 | + if let Some(t) = unsafe { &*(o as *const Self) } { |
| 87 | + T::trace(t as *const T as usize) |
| 88 | + } |
| 89 | + } |
| 90 | + default const TRACE_TYPE_INFO: GcTypeInfo = GcTypeInfo::new::<Self>(); |
| 91 | + default const TRACE_CHILD_TYPE_INFO: [Option<GcTypeInfo>; 8] = GcTypeInfo::one_child::<T>(); |
| 92 | + default fn trace_transitive_type_info(tti: &mut Tti) { |
| 93 | + tti.add_direct::<Self>(); |
| 94 | + tti.add_trans(T::trace_transitive_type_info); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +unsafe impl<'r, T: Immutable + Trace + NoGc> Trace for Option<T> { |
| 99 | + fn trace(_: usize) {} |
| 100 | + const TRACE_TYPE_INFO: GcTypeInfo = GcTypeInfo::new::<Self>(); |
| 101 | + const TRACE_CHILD_TYPE_INFO: [Option<GcTypeInfo>; 8] = [None; 8]; |
| 102 | + fn trace_transitive_type_info(_: &mut Tti) {} |
| 103 | +} |
| 104 | + |
| 105 | +unsafe impl<T: Immutable + Trace> Trace for Box<T> { |
| 106 | + default fn trace(b: usize) { |
| 107 | + let t = unsafe { &*(b as *const Self) }.deref(); |
| 108 | + T::trace(t as *const T as usize) |
| 109 | + } |
| 110 | + default const TRACE_TYPE_INFO: GcTypeInfo = GcTypeInfo::new::<Self>(); |
| 111 | + default const TRACE_CHILD_TYPE_INFO: [Option<GcTypeInfo>; 8] = [None; 8]; |
| 112 | + default fn trace_transitive_type_info(_: &mut Tti) {} |
| 113 | +} |
| 114 | + |
| 115 | +unsafe impl<T: Immutable + Trace + NoGc> Trace for Box<T> { |
| 116 | + fn trace(_: usize) {} |
| 117 | + const TRACE_TYPE_INFO: GcTypeInfo = GcTypeInfo::new::<Self>(); |
| 118 | + const TRACE_CHILD_TYPE_INFO: [Option<GcTypeInfo>; 8] = GcTypeInfo::one_child::<T>(); |
| 119 | + fn trace_transitive_type_info(tti: &mut Tti) { |
| 120 | + tti.add_direct::<Self>(); |
| 121 | + tti.add_trans(T::trace_transitive_type_info); |
| 122 | + } |
| 123 | +} |
0 commit comments