mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-01 07:24:56 -10:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99f94503c3 | ||
|
|
925075337b | ||
|
|
df70222d61 | ||
|
|
1f9e0cc37b |
No files matched your search
@@ -323,6 +323,7 @@ pub(crate) fn property_from_type(
|
|||||||
Type::Generic(_) => vec![TextLabel::new("Generic Type (Not Supported)").widget_instance()].into(),
|
Type::Generic(_) => vec![TextLabel::new("Generic Type (Not Supported)").widget_instance()].into(),
|
||||||
Type::Fn(_, out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
|
Type::Fn(_, out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
|
||||||
Type::Future(out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
|
Type::Future(out) => return property_from_type(node_id, index, out, number_options, unit, display_decimal_places, step, context),
|
||||||
|
Type::Ref(inner) => return property_from_type(node_id, index, inner, number_options, unit, display_decimal_places, step, context),
|
||||||
};
|
};
|
||||||
|
|
||||||
extra_widgets.push(widgets);
|
extra_widgets.push(widgets);
|
||||||
|
|||||||
@@ -309,6 +309,7 @@ macro_rules! tagged_value {
|
|||||||
pub fn from_type(input: &Type) -> Option<Self> {
|
pub fn from_type(input: &Type) -> Option<Self> {
|
||||||
match input {
|
match input {
|
||||||
Type::Generic(_) => None,
|
Type::Generic(_) => None,
|
||||||
|
Type::Ref(_) => None,
|
||||||
Type::Concrete(concrete_type) => {
|
Type::Concrete(concrete_type) => {
|
||||||
let name = concrete_type.name.as_ref();
|
let name = concrete_type.name.as_ref();
|
||||||
// TODO: Add default implementations for types such as TaggedValue::Subpaths, and use the defaults here and in document_node_types
|
// TODO: Add default implementations for types such as TaggedValue::Subpaths, and use the defaults here and in document_node_types
|
||||||
@@ -569,6 +570,7 @@ impl TaggedValue {
|
|||||||
|
|
||||||
match ty {
|
match ty {
|
||||||
Type::Generic(_) => None,
|
Type::Generic(_) => None,
|
||||||
|
Type::Ref(_) => None,
|
||||||
Type::Concrete(concrete_type) => {
|
Type::Concrete(concrete_type) => {
|
||||||
let ty = concrete_type.id?;
|
let ty = concrete_type.id?;
|
||||||
use std::any::TypeId;
|
use std::any::TypeId;
|
||||||
|
|||||||
@@ -0,0 +1,230 @@
|
|||||||
|
use std::cell::UnsafeCell;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::mem::MaybeUninit;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
pub struct Arena {
|
||||||
|
generation: AtomicU64,
|
||||||
|
offset: AtomicUsize,
|
||||||
|
buf: Box<[UnsafeCell<MaybeUninit<u8>>]>,
|
||||||
|
drops: Mutex<Vec<DropEntry>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DropEntry {
|
||||||
|
offset: usize,
|
||||||
|
drop_fn: unsafe fn(*mut u8),
|
||||||
|
}
|
||||||
|
|
||||||
|
// SAFETY: disjoint regions are handed out by an atomic bump; a region is written
|
||||||
|
// only by its allocating caller before publication, and cross-thread hand-off is
|
||||||
|
// ordered by the Release/Acquire pair on the published handle word.
|
||||||
|
unsafe impl Sync for Arena {}
|
||||||
|
unsafe impl Send for Arena {}
|
||||||
|
|
||||||
|
impl Arena {
|
||||||
|
pub fn new(capacity: usize) -> Self {
|
||||||
|
let buf = (0..capacity).map(|_| UnsafeCell::new(MaybeUninit::uninit())).collect();
|
||||||
|
Self {
|
||||||
|
// Starts at 1 so the null handle word (0) can never upgrade.
|
||||||
|
generation: AtomicU64::new(1),
|
||||||
|
offset: AtomicUsize::new(0),
|
||||||
|
buf,
|
||||||
|
drops: Mutex::new(Vec::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generation(&self) -> u64 {
|
||||||
|
self.generation.load(Ordering::Acquire)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base(&self) -> *mut u8 {
|
||||||
|
self.buf.as_ptr() as *mut u8
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reserve(&self, size: usize, align: usize) -> Option<usize> {
|
||||||
|
debug_assert!(align.is_power_of_two());
|
||||||
|
let base = self.base() as usize;
|
||||||
|
let mut start = 0;
|
||||||
|
self.offset
|
||||||
|
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
|
||||||
|
// Alignment is computed on the absolute address; the backbone
|
||||||
|
// allocation itself has no alignment guarantee.
|
||||||
|
let addr = (base.checked_add(current)?.checked_add(align - 1)?) & !(align - 1);
|
||||||
|
start = addr - base;
|
||||||
|
let end = start.checked_add(size)?;
|
||||||
|
(end <= self.buf.len()).then_some(end)
|
||||||
|
})
|
||||||
|
.ok()?;
|
||||||
|
Some(start)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn alloc<T>(&self, value: T) -> Option<(&T, ArenaWeak<T>)> {
|
||||||
|
let offset = self.reserve(size_of::<T>(), align_of::<T>())?;
|
||||||
|
let ptr = unsafe { self.base().add(offset) }.cast::<T>();
|
||||||
|
// SAFETY: freshly reserved, aligned, in-bounds, unaliased.
|
||||||
|
unsafe { ptr.write(value) };
|
||||||
|
if std::mem::needs_drop::<T>() {
|
||||||
|
unsafe fn glue<T>(p: *mut u8) {
|
||||||
|
unsafe { p.cast::<T>().drop_in_place() }
|
||||||
|
}
|
||||||
|
self.drops.lock().unwrap().push(DropEntry { offset, drop_fn: glue::<T> });
|
||||||
|
}
|
||||||
|
// SAFETY: initialized above; insert-only, so no `&mut` to it can exist.
|
||||||
|
Some((unsafe { &*ptr }, ArenaWeak::new(self.generation(), offset)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn alloc_slice_copy<T: Copy>(&self, src: &[T]) -> Option<&[T]> {
|
||||||
|
let buf = self.alloc_scratch::<T>(src.len())?;
|
||||||
|
for (slot, &value) in buf.iter_mut().zip(src) {
|
||||||
|
slot.write(value);
|
||||||
|
}
|
||||||
|
// SAFETY: every lane written above from `src`.
|
||||||
|
Some(unsafe { std::slice::from_raw_parts(buf.as_ptr().cast::<T>(), src.len()) })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not drop-tracked: callers must either consume every written lane or
|
||||||
|
// restrict themselves to `Copy` payloads (leak, not UB, otherwise).
|
||||||
|
#[allow(clippy::mut_from_ref)]
|
||||||
|
pub fn alloc_scratch<T>(&self, len: usize) -> Option<&mut [MaybeUninit<T>]> {
|
||||||
|
let size = size_of::<T>().checked_mul(len)?;
|
||||||
|
let offset = self.reserve(size, align_of::<T>())?;
|
||||||
|
let ptr = unsafe { self.base().add(offset) }.cast::<MaybeUninit<T>>();
|
||||||
|
// SAFETY: exclusive region; lifetime tied to `&self`, and `reset` takes
|
||||||
|
// `&mut self`, so the slice cannot outlive the generation.
|
||||||
|
Some(unsafe { std::slice::from_raw_parts_mut(ptr, len) })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
let base = self.base();
|
||||||
|
for entry in self.drops.get_mut().unwrap().drain(..) {
|
||||||
|
// SAFETY: registered at alloc time; insert-only means the region was
|
||||||
|
// never overwritten within this generation.
|
||||||
|
unsafe { (entry.drop_fn)(base.add(entry.offset)) }
|
||||||
|
}
|
||||||
|
*self.offset.get_mut() = 0;
|
||||||
|
self.generation.fetch_add(1, Ordering::Release);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Arena {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ArenaWeak<T> {
|
||||||
|
word: u64,
|
||||||
|
_marker: PhantomData<*const T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for ArenaWeak<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T> Copy for ArenaWeak<T> {}
|
||||||
|
|
||||||
|
impl<T> ArenaWeak<T> {
|
||||||
|
pub const NULL: Self = ArenaWeak { word: 0, _marker: PhantomData };
|
||||||
|
|
||||||
|
fn new(generation: u64, offset: usize) -> Self {
|
||||||
|
debug_assert!(offset < u32::MAX as usize);
|
||||||
|
Self {
|
||||||
|
word: ((generation & 0xFFFF_FFFF) << 32) | offset as u64,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn upgrade(self, arena: &Arena) -> Option<&T> {
|
||||||
|
let generation = self.word >> 32;
|
||||||
|
if generation != arena.generation() & 0xFFFF_FFFF {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let offset = (self.word & 0xFFFF_FFFF) as usize;
|
||||||
|
// SAFETY: same generation means the entry was fully written before its
|
||||||
|
// word was published (Release) and cannot move or be overwritten within
|
||||||
|
// a generation (insert-only); the Acquire load that produced this word
|
||||||
|
// ordered the payload writes.
|
||||||
|
Some(unsafe { &*arena.base().add(offset).cast::<T>() })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ArenaCell<T> {
|
||||||
|
word: AtomicU64,
|
||||||
|
_marker: PhantomData<fn() -> T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for ArenaCell<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
word: AtomicU64::new(0),
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ArenaCell<T> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load<'e>(&self, arena: &'e Arena) -> Option<&'e T> {
|
||||||
|
let weak = ArenaWeak::<T> {
|
||||||
|
word: self.word.load(Ordering::Acquire),
|
||||||
|
_marker: PhantomData,
|
||||||
|
};
|
||||||
|
weak.upgrade(arena)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store(&self, weak: ArenaWeak<T>) {
|
||||||
|
self.word.store(weak.word, Ordering::Release);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::atomic::AtomicU32;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn alloc_upgrade_reset_miss() {
|
||||||
|
let mut arena = Arena::new(1024);
|
||||||
|
let cell = ArenaCell::new();
|
||||||
|
let (value, weak) = arena.alloc(41u32).unwrap();
|
||||||
|
assert_eq!(*value, 41);
|
||||||
|
cell.store(weak);
|
||||||
|
assert_eq!(cell.load(&arena), Some(&41));
|
||||||
|
arena.reset();
|
||||||
|
assert_eq!(cell.load(&arena), None, "stale handle must miss");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn capacity_survives_reset() {
|
||||||
|
let mut arena = Arena::new(64 + align_of::<u32>() - 1);
|
||||||
|
for _ in 0..10 {
|
||||||
|
for _ in 0..16 {
|
||||||
|
assert!(arena.alloc(0u32).is_some());
|
||||||
|
}
|
||||||
|
assert!(arena.alloc(0u32).is_none(), "exhausted within generation");
|
||||||
|
arena.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn drop_glue_runs_on_reset() {
|
||||||
|
static DROPS: AtomicU32 = AtomicU32::new(0);
|
||||||
|
struct Probe(#[allow(dead_code)] String);
|
||||||
|
impl Drop for Probe {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
DROPS.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut arena = Arena::new(1024);
|
||||||
|
arena.alloc(Probe("owns heap".into())).unwrap();
|
||||||
|
arena.alloc(Probe("me too".into())).unwrap();
|
||||||
|
assert_eq!(DROPS.load(Ordering::Relaxed), 0);
|
||||||
|
arena.reset();
|
||||||
|
assert_eq!(DROPS.load(Ordering::Relaxed), 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
|
use crate::arena::Arena;
|
||||||
use crate::transform::Footprint;
|
use crate::transform::Footprint;
|
||||||
use glam::DVec2;
|
use glam::DVec2;
|
||||||
pub use no_std_types::context::{ArcCtx, Ctx};
|
pub use no_std_types::context::{ArcCtx, Ctx};
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::borrow::Borrow;
|
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::panic::Location;
|
use std::panic::Location;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -62,7 +62,9 @@ pub trait InjectRealTime {}
|
|||||||
pub trait InjectAnimationTime {}
|
pub trait InjectAnimationTime {}
|
||||||
pub trait InjectPointerPosition {}
|
pub trait InjectPointerPosition {}
|
||||||
pub trait InjectPosition {}
|
pub trait InjectPosition {}
|
||||||
pub trait InjectIndex {}
|
pub trait InjectIndex {
|
||||||
|
fn set_index(&mut self, index: u64);
|
||||||
|
}
|
||||||
pub trait InjectVarArgs {}
|
pub trait InjectVarArgs {}
|
||||||
|
|
||||||
// ================
|
// ================
|
||||||
@@ -101,7 +103,6 @@ impl<T: Ctx> InjectRealTime for T {}
|
|||||||
impl<T: Ctx> InjectAnimationTime for T {}
|
impl<T: Ctx> InjectAnimationTime for T {}
|
||||||
impl<T: Ctx> InjectPointerPosition for T {}
|
impl<T: Ctx> InjectPointerPosition for T {}
|
||||||
impl<T: Ctx> InjectPosition for T {}
|
impl<T: Ctx> InjectPosition for T {}
|
||||||
impl<T: Ctx> InjectIndex for T {}
|
|
||||||
impl<T: Ctx> InjectVarArgs for T {}
|
impl<T: Ctx> InjectVarArgs for T {}
|
||||||
|
|
||||||
// =============
|
// =============
|
||||||
@@ -383,48 +384,6 @@ impl ExtractFootprint for () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// =====================================
|
|
||||||
// EXTRACT TRAIT IMPLS FOR `ContextImpl`
|
|
||||||
// =====================================
|
|
||||||
|
|
||||||
impl Ctx for ContextImpl<'_> {}
|
|
||||||
|
|
||||||
impl ExtractFootprint for ContextImpl<'_> {
|
|
||||||
fn try_footprint(&self) -> Option<&Footprint> {
|
|
||||||
self.footprint
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl ExtractRealTime for ContextImpl<'_> {
|
|
||||||
fn try_real_time(&self) -> Option<f64> {
|
|
||||||
self.real_time
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl ExtractPosition for ContextImpl<'_> {
|
|
||||||
fn try_position(&self) -> Option<impl Iterator<Item = DVec2>> {
|
|
||||||
self.position.clone().map(|x| x.into_iter())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl ExtractIndex for ContextImpl<'_> {
|
|
||||||
fn try_index(&self) -> Option<impl Iterator<Item = usize>> {
|
|
||||||
self.index.clone().map(|x| x.into_iter())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl ExtractVarArgs for ContextImpl<'_> {
|
|
||||||
fn vararg(&self, index: usize) -> Result<DynRef<'_>, VarArgsResult> {
|
|
||||||
let Some(inner) = self.varargs else { return Err(VarArgsResult::NoVarArgs) };
|
|
||||||
inner.get(index).ok_or(VarArgsResult::IndexOutOfBounds).copied()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn varargs_len(&self) -> Result<usize, VarArgsResult> {
|
|
||||||
let Some(inner) = self.varargs else { return Err(VarArgsResult::NoVarArgs) };
|
|
||||||
Ok(inner.len())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hash_varargs(&self, _hasher: &mut dyn Hasher) {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// EXTRACT TRAIT IMPLS FOR `OwnedContextImpl`
|
// EXTRACT TRAIT IMPLS FOR `OwnedContextImpl`
|
||||||
// ==========================================
|
// ==========================================
|
||||||
@@ -680,32 +639,447 @@ impl OwnedContextImpl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, dyn_any::DynAny)]
|
pub type SourceId = u64;
|
||||||
pub struct ContextImpl<'a> {
|
|
||||||
pub(crate) footprint: Option<&'a Footprint>,
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct IndexLink<'a> {
|
||||||
|
pub index: u64,
|
||||||
|
pub outer: Option<&'a IndexLink<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct PositionLink<'a> {
|
||||||
|
pub position: DVec2,
|
||||||
|
pub outer: Option<&'a PositionLink<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type DynSlot<'a> = &'a (dyn AnyHash + Send + Sync);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct VarArgLink<'a> {
|
||||||
|
pub args: &'a [DynSlot<'a>],
|
||||||
|
pub outer: Option<&'a VarArgLink<'a>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct EvalScope<'a> {
|
||||||
real_time: Option<f64>,
|
real_time: Option<f64>,
|
||||||
position: Option<Vec<DVec2>>, // This could be converted into a single enum to save extra bytes
|
animation_time: Option<f64>,
|
||||||
index: Option<Vec<usize>>, // This could be converted into a single enum to save extra bytes
|
pointer_position: Option<DVec2>,
|
||||||
varargs: Option<&'a [DynRef<'a>]>,
|
generations: &'a [(SourceId, u64)],
|
||||||
|
arena: &'a Arena,
|
||||||
|
hash: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> EvalScope<'a> {
|
||||||
|
pub fn new(real_time: Option<f64>, animation_time: Option<f64>, pointer_position: Option<DVec2>, generations: &'a [(SourceId, u64)], arena: &'a Arena) -> Self {
|
||||||
|
let mut scope = Self {
|
||||||
|
real_time,
|
||||||
|
animation_time,
|
||||||
|
pointer_position,
|
||||||
|
generations,
|
||||||
|
arena,
|
||||||
|
hash: 0,
|
||||||
|
};
|
||||||
|
scope.hash = scope.compute_hash(None);
|
||||||
|
scope
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn retained(&self, retain: &[SourceId]) -> EvalScope<'a> {
|
||||||
|
EvalScope {
|
||||||
|
hash: self.compute_hash(Some(retain)),
|
||||||
|
..*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_real_time(&self, real_time: Option<f64>) -> EvalScope<'a> {
|
||||||
|
let mut scope = EvalScope { real_time, ..*self };
|
||||||
|
scope.hash = scope.compute_hash(None);
|
||||||
|
scope
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_animation_time(&self, animation_time: Option<f64>) -> EvalScope<'a> {
|
||||||
|
let mut scope = EvalScope { animation_time, ..*self };
|
||||||
|
scope.hash = scope.compute_hash(None);
|
||||||
|
scope
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compute_hash(&self, retain: Option<&[SourceId]>) -> u64 {
|
||||||
|
let mut hasher = std::hash::DefaultHasher::new();
|
||||||
|
self.real_time.map(f64::to_bits).hash(&mut hasher);
|
||||||
|
self.animation_time.map(f64::to_bits).hash(&mut hasher);
|
||||||
|
self.pointer_position.map(|position| (position.x.to_bits(), position.y.to_bits())).hash(&mut hasher);
|
||||||
|
for (source, generation) in self.generations {
|
||||||
|
if retain.is_none_or(|retain| retain.contains(source)) {
|
||||||
|
(source, generation).hash(&mut hasher);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hasher.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generation(&self, source: SourceId) -> Option<u64> {
|
||||||
|
self.generations.iter().find(|(candidate, _)| *candidate == source).map(|(_, generation)| *generation)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generations(&self) -> &'a [(SourceId, u64)] {
|
||||||
|
self.generations
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn arena(&self) -> &'a Arena {
|
||||||
|
self.arena
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait ExtractArena {
|
||||||
|
type ArenaRef;
|
||||||
|
fn arena(&self) -> Self::ArenaRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct ContextImpl<'a> {
|
||||||
|
index: IndexLink<'a>,
|
||||||
|
position: Option<&'a PositionLink<'a>>,
|
||||||
|
varargs: Option<&'a VarArgLink<'a>>,
|
||||||
|
footprint: Option<&'a Footprint>,
|
||||||
|
scope: &'a EvalScope<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ContextImpl<'a> {
|
impl<'a> ContextImpl<'a> {
|
||||||
pub fn with_footprint<'f>(&self, new_footprint: &'f Footprint, varargs: Option<&'f impl Borrow<[DynRef<'f>]>>) -> ContextImpl<'f>
|
pub fn root(scope: &'a EvalScope<'a>) -> Self {
|
||||||
|
Self {
|
||||||
|
index: IndexLink { index: 0, outer: None },
|
||||||
|
position: None,
|
||||||
|
varargs: None,
|
||||||
|
footprint: None,
|
||||||
|
scope,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scope(&self) -> &'a EvalScope<'a> {
|
||||||
|
self.scope
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn index_head(&self) -> IndexLink<'a> {
|
||||||
|
self.index
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_footprint<'s>(&self, footprint: &'s Footprint) -> ContextImpl<'s>
|
||||||
where
|
where
|
||||||
'a: 'f,
|
'a: 's,
|
||||||
|
{
|
||||||
|
ContextImpl { footprint: Some(footprint), ..*self }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_scope<'s>(&self, scope: &'s EvalScope<'s>) -> ContextImpl<'s>
|
||||||
|
where
|
||||||
|
'a: 's,
|
||||||
|
{
|
||||||
|
ContextImpl { scope, ..*self }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_varargs<'s>(&self, varargs: &'s VarArgLink<'s>) -> ContextImpl<'s>
|
||||||
|
where
|
||||||
|
'a: 's,
|
||||||
|
{
|
||||||
|
ContextImpl { varargs: Some(varargs), ..*self }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_position<'s>(&self, position: &'s PositionLink<'s>) -> ContextImpl<'s>
|
||||||
|
where
|
||||||
|
'a: 's,
|
||||||
|
{
|
||||||
|
ContextImpl { position: Some(position), ..*self }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn promoted<'s>(&self, spilled_head: &'s IndexLink<'s>, inner_index: u64) -> ContextImpl<'s>
|
||||||
|
where
|
||||||
|
'a: 's,
|
||||||
{
|
{
|
||||||
ContextImpl {
|
ContextImpl {
|
||||||
footprint: Some(new_footprint),
|
index: IndexLink {
|
||||||
position: self.position.clone(),
|
index: inner_index,
|
||||||
index: self.index.clone(),
|
outer: Some(spilled_head),
|
||||||
varargs: varargs.map(|x| x.borrow()),
|
},
|
||||||
..*self
|
..*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Ctx for ContextImpl<'_> {}
|
||||||
|
|
||||||
|
impl InjectIndex for ContextImpl<'_> {
|
||||||
|
fn set_index(&mut self, index: u64) {
|
||||||
|
self.index.index = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExtractFootprint for ContextImpl<'_> {
|
||||||
|
fn try_footprint(&self) -> Option<&Footprint> {
|
||||||
|
self.footprint
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ExtractRealTime for ContextImpl<'_> {
|
||||||
|
fn try_real_time(&self) -> Option<f64> {
|
||||||
|
self.scope.real_time
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ExtractAnimationTime for ContextImpl<'_> {
|
||||||
|
fn try_animation_time(&self) -> Option<f64> {
|
||||||
|
self.scope.animation_time
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ExtractPointerPosition for ContextImpl<'_> {
|
||||||
|
fn try_pointer_position(&self) -> Option<DVec2> {
|
||||||
|
self.scope.pointer_position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ExtractIndex for ContextImpl<'_> {
|
||||||
|
fn try_index(&self) -> Option<impl Iterator<Item = usize>> {
|
||||||
|
Some(std::iter::successors(Some(&self.index), |link| link.outer).map(|link| link.index as usize))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ExtractPosition for ContextImpl<'_> {
|
||||||
|
fn try_position(&self) -> Option<impl Iterator<Item = DVec2>> {
|
||||||
|
self.position.map(|head| std::iter::successors(Some(head), |link| link.outer).map(|link| link.position))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ExtractVarArgs for ContextImpl<'_> {
|
||||||
|
fn vararg(&self, index: usize) -> Result<DynRef<'_>, VarArgsResult> {
|
||||||
|
let mut link = self.varargs.ok_or(VarArgsResult::NoVarArgs)?;
|
||||||
|
let mut remaining = index;
|
||||||
|
loop {
|
||||||
|
match link.args.get(remaining) {
|
||||||
|
Some(arg) => return Ok(*arg as DynRef<'_>),
|
||||||
|
None => {
|
||||||
|
remaining -= link.args.len();
|
||||||
|
link = link.outer.ok_or(VarArgsResult::IndexOutOfBounds)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn varargs_len(&self) -> Result<usize, VarArgsResult> {
|
||||||
|
let head = self.varargs.ok_or(VarArgsResult::NoVarArgs)?;
|
||||||
|
Ok(std::iter::successors(Some(head), |link| link.outer).map(|link| link.args.len()).sum())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_varargs(&self, hasher: &mut dyn Hasher) {
|
||||||
|
let mut count = 0u64;
|
||||||
|
let mut link = self.varargs;
|
||||||
|
while let Some(current) = link {
|
||||||
|
for arg in current.args {
|
||||||
|
arg.dyn_hash(&mut *hasher);
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
link = current.outer;
|
||||||
|
}
|
||||||
|
count.hash(&mut &mut *hasher);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'a> ExtractArena for ContextImpl<'a> {
|
||||||
|
type ArenaRef = &'a Arena;
|
||||||
|
fn arena(&self) -> &'a Arena {
|
||||||
|
self.scope.arena
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl graphene_hash::CacheHash for ContextImpl<'_> {
|
||||||
|
fn cache_hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
match self.footprint {
|
||||||
|
Some(footprint) => {
|
||||||
|
1u8.hash(state);
|
||||||
|
footprint.cache_hash(state);
|
||||||
|
}
|
||||||
|
None => 0u8.hash(state),
|
||||||
|
}
|
||||||
|
let mut count = 0u64;
|
||||||
|
for link in std::iter::successors(Some(&self.index), |link| link.outer) {
|
||||||
|
link.index.hash(state);
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
count.hash(state);
|
||||||
|
count = 0;
|
||||||
|
let mut position = self.position;
|
||||||
|
while let Some(link) = position {
|
||||||
|
link.position.x.to_bits().hash(state);
|
||||||
|
link.position.y.to_bits().hash(state);
|
||||||
|
count += 1;
|
||||||
|
position = link.outer;
|
||||||
|
}
|
||||||
|
count.hash(state);
|
||||||
|
self.hash_varargs(state);
|
||||||
|
self.scope.hash.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum VarArgsResult {
|
pub enum VarArgsResult {
|
||||||
IndexOutOfBounds,
|
IndexOutOfBounds,
|
||||||
NoVarArgs,
|
NoVarArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod context_impl_tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::graphene_hash::CacheHash;
|
||||||
|
|
||||||
|
fn hash_of(ctx: &ContextImpl) -> u64 {
|
||||||
|
let mut hasher = std::hash::DefaultHasher::new();
|
||||||
|
ctx.cache_hash(&mut hasher);
|
||||||
|
hasher.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn scope_fixture<'a>(generations: &'a [(SourceId, u64)], arena: &'a Arena) -> EvalScope<'a> {
|
||||||
|
EvalScope::new(Some(0.5), Some(1.5), None, generations, arena)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn equal_contexts_hash_equal() {
|
||||||
|
let arena = Arena::new(64);
|
||||||
|
let generations = [(0, 1), (1, 3)];
|
||||||
|
let scope = scope_fixture(&generations, &arena);
|
||||||
|
let a = ContextImpl::root(&scope);
|
||||||
|
let b = ContextImpl::root(&scope);
|
||||||
|
assert_eq!(hash_of(&a), hash_of(&b));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn each_axis_changes_the_hash() {
|
||||||
|
let arena = Arena::new(64);
|
||||||
|
let generations = [(0, 1)];
|
||||||
|
let scope = scope_fixture(&generations, &arena);
|
||||||
|
let root = ContextImpl::root(&scope);
|
||||||
|
let footprint = Footprint::DEFAULT;
|
||||||
|
|
||||||
|
let mut indexed = root;
|
||||||
|
indexed.set_index(4);
|
||||||
|
let position_link = PositionLink {
|
||||||
|
position: DVec2::new(1.0, 2.0),
|
||||||
|
outer: None,
|
||||||
|
};
|
||||||
|
let time_scope = scope.with_real_time(Some(9.75));
|
||||||
|
let variants = [root.with_footprint(&footprint), indexed, root.with_position(&position_link), root.with_scope(&time_scope)];
|
||||||
|
let root_hash = hash_of(&root);
|
||||||
|
let mut hashes: Vec<u64> = variants.iter().map(hash_of).collect();
|
||||||
|
hashes.push(root_hash);
|
||||||
|
hashes.sort();
|
||||||
|
hashes.dedup();
|
||||||
|
assert_eq!(hashes.len(), variants.len() + 1, "every axis must contribute to the hash");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn index_level_order_matters() {
|
||||||
|
let arena = Arena::new(64);
|
||||||
|
let generations = [];
|
||||||
|
let scope = scope_fixture(&generations, &arena);
|
||||||
|
let root = ContextImpl::root(&scope);
|
||||||
|
|
||||||
|
let outer_one = IndexLink { index: 1, outer: None };
|
||||||
|
let mut one_two = root.promoted(&outer_one, 2);
|
||||||
|
let outer_two = IndexLink { index: 2, outer: None };
|
||||||
|
let mut two_one = root.promoted(&outer_two, 1);
|
||||||
|
assert_ne!(hash_of(&one_two), hash_of(&two_one));
|
||||||
|
|
||||||
|
one_two.set_index(7);
|
||||||
|
two_one.set_index(7);
|
||||||
|
assert_ne!(hash_of(&one_two), hash_of(&two_one), "outer levels must stay hashed after set_index");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn axis_boundaries_are_unambiguous() {
|
||||||
|
let arena = Arena::new(64);
|
||||||
|
let generations = [];
|
||||||
|
let scope = scope_fixture(&generations, &arena);
|
||||||
|
let root = ContextImpl::root(&scope);
|
||||||
|
|
||||||
|
let position = PositionLink {
|
||||||
|
position: DVec2::new(5.0, 5.0),
|
||||||
|
outer: None,
|
||||||
|
};
|
||||||
|
let spilled = root.index_head();
|
||||||
|
let mut deep_index = root.promoted(&spilled, 0);
|
||||||
|
deep_index.set_index(5);
|
||||||
|
let shallow_with_position = root.with_position(&position);
|
||||||
|
assert_ne!(hash_of(&deep_index), hash_of(&shallow_with_position), "index levels must not blur into position levels");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retain_scopes_generation_invalidation() {
|
||||||
|
let arena = Arena::new(64);
|
||||||
|
let initial = [(0, 1), (1, 3)];
|
||||||
|
let bumped_unretained = [(0, 2), (1, 3)];
|
||||||
|
let bumped_retained = [(0, 1), (1, 4)];
|
||||||
|
|
||||||
|
let hash_with = |generations: &[(SourceId, u64)]| {
|
||||||
|
let scope = scope_fixture(generations, &arena).retained(&[1]);
|
||||||
|
let retained_scope_context = ContextImpl::root(&scope);
|
||||||
|
hash_of(&retained_scope_context)
|
||||||
|
};
|
||||||
|
assert_eq!(hash_with(&initial), hash_with(&bumped_unretained), "unretained source bumps must not invalidate");
|
||||||
|
assert_ne!(hash_with(&initial), hash_with(&bumped_retained), "retained source bumps must invalidate");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unretained_scope_sees_every_bump() {
|
||||||
|
let arena = Arena::new(64);
|
||||||
|
let initial = [(0, 1)];
|
||||||
|
let bumped = [(0, 2)];
|
||||||
|
let hash_with = |generations: &[(SourceId, u64)]| {
|
||||||
|
let scope = scope_fixture(generations, &arena);
|
||||||
|
hash_of(&ContextImpl::root(&scope))
|
||||||
|
};
|
||||||
|
assert_ne!(hash_with(&initial), hash_with(&bumped));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn set_index_is_visible_and_keeps_outer_levels() {
|
||||||
|
let arena = Arena::new(64);
|
||||||
|
let generations = [];
|
||||||
|
let scope = scope_fixture(&generations, &arena);
|
||||||
|
let root = ContextImpl::root(&scope);
|
||||||
|
let spilled = root.index_head();
|
||||||
|
let mut ctx = root.promoted(&spilled, 0);
|
||||||
|
ctx.set_index(11);
|
||||||
|
let levels: Vec<usize> = ctx.try_index().unwrap().collect();
|
||||||
|
assert_eq!(levels, vec![11, 0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vararg_chain_concatenates_innermost_first() {
|
||||||
|
let arena = Arena::new(64);
|
||||||
|
let generations = [];
|
||||||
|
let scope = scope_fixture(&generations, &arena);
|
||||||
|
let root = ContextImpl::root(&scope);
|
||||||
|
|
||||||
|
let outer_value = 7u32;
|
||||||
|
let outer_args: [DynSlot; 1] = [&outer_value];
|
||||||
|
let outer_link = VarArgLink { args: &outer_args, outer: None };
|
||||||
|
let outer_ctx = root.with_varargs(&outer_link);
|
||||||
|
|
||||||
|
let inner_value = String::from("inner");
|
||||||
|
let inner_args: [DynSlot; 1] = [&inner_value];
|
||||||
|
let inner_link = VarArgLink {
|
||||||
|
args: &inner_args,
|
||||||
|
outer: Some(&outer_link),
|
||||||
|
};
|
||||||
|
let inner_ctx = root.with_varargs(&inner_link);
|
||||||
|
|
||||||
|
assert_eq!(root.varargs_len(), Err(VarArgsResult::NoVarArgs));
|
||||||
|
assert_eq!(outer_ctx.varargs_len(), Ok(1));
|
||||||
|
assert_eq!(inner_ctx.varargs_len(), Ok(2));
|
||||||
|
assert!(inner_ctx.vararg(0).unwrap().downcast_ref::<String>().is_some());
|
||||||
|
assert!(inner_ctx.vararg(1).unwrap().downcast_ref::<u32>().is_some());
|
||||||
|
assert!(matches!(inner_ctx.vararg(2), Err(VarArgsResult::IndexOutOfBounds)));
|
||||||
|
assert_ne!(hash_of(&outer_ctx), hash_of(&inner_ctx));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn scope_arena_reaches_kernels_through_extract_arena() {
|
||||||
|
let arena = Arena::new(1024);
|
||||||
|
let generations = [];
|
||||||
|
let scope = scope_fixture(&generations, &arena);
|
||||||
|
let ctx = ContextImpl::root(&scope);
|
||||||
|
let (value, _) = ExtractArena::arena(&ctx).alloc(41u32).unwrap();
|
||||||
|
assert_eq!(*value, 41);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
use crate::gpoll::Finality;
|
||||||
|
use std::cell::UnsafeCell;
|
||||||
|
use std::mem::MaybeUninit;
|
||||||
|
use std::sync::atomic::{AtomicU8, AtomicU64, Ordering};
|
||||||
|
|
||||||
|
const SLOT_EMPTY: u8 = 0;
|
||||||
|
const SLOT_FINAL: u8 = 1;
|
||||||
|
const SLOT_PARTIAL: u8 = 2;
|
||||||
|
|
||||||
|
pub struct FrameTable<T, const CAP: usize> {
|
||||||
|
slots: [FrameSlot<T>; CAP],
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FrameSlot<T> {
|
||||||
|
key: AtomicU64,
|
||||||
|
state: AtomicU8,
|
||||||
|
value: UnsafeCell<MaybeUninit<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Lookup<'t, T> {
|
||||||
|
Hit(Finality, &'t T),
|
||||||
|
Vacant(VacantSlot<'t, T>),
|
||||||
|
Full,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct VacantSlot<'t, T> {
|
||||||
|
slot: &'t FrameSlot<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, const CAP: usize> Default for FrameTable<T, CAP> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, const CAP: usize> FrameTable<T, CAP> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
slots: std::array::from_fn(|_| FrameSlot {
|
||||||
|
key: AtomicU64::new(0),
|
||||||
|
state: AtomicU8::new(SLOT_EMPTY),
|
||||||
|
value: UnsafeCell::new(MaybeUninit::uninit()),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lookup(&self, hash: u64) -> Lookup<'_, T> {
|
||||||
|
// Keys are forced odd so the empty sentinel (0) can never collide.
|
||||||
|
let key = hash | 1;
|
||||||
|
for probe in 0..CAP {
|
||||||
|
let slot = &self.slots[(key as usize).wrapping_add(probe) % CAP];
|
||||||
|
let stored = slot.key.load(Ordering::Acquire);
|
||||||
|
if stored == key {
|
||||||
|
return match slot.state.load(Ordering::Acquire) {
|
||||||
|
// SAFETY: a published state was stored with Release after the
|
||||||
|
// value write; the Acquire load above ordered that write.
|
||||||
|
SLOT_FINAL => Lookup::Hit(Finality::AllFinal, unsafe { (*slot.value.get()).assume_init_ref() }),
|
||||||
|
SLOT_PARTIAL => Lookup::Hit(Finality::Partial, unsafe { (*slot.value.get()).assume_init_ref() }),
|
||||||
|
_ => Lookup::Full,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if stored == 0 && slot.key.compare_exchange(0, key, Ordering::AcqRel, Ordering::Acquire).is_ok() {
|
||||||
|
return Lookup::Vacant(VacantSlot { slot });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Lookup::Full
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, const CAP: usize> Drop for FrameTable<T, CAP> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
for slot in &self.slots {
|
||||||
|
if slot.state.load(Ordering::Acquire) != SLOT_EMPTY {
|
||||||
|
// SAFETY: a non-empty state is only ever stored after the value
|
||||||
|
// write in `publish`.
|
||||||
|
unsafe { (*slot.value.get()).assume_init_drop() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'t, T> VacantSlot<'t, T> {
|
||||||
|
pub fn publish(self, value: T, finality: Finality) -> &'t T {
|
||||||
|
// SAFETY: the CAS in `lookup` reserved this slot exclusively for us and
|
||||||
|
// its state is still SLOT_EMPTY, so nobody reads the value yet.
|
||||||
|
let lent = unsafe { &*(*self.slot.value.get()).write(value) };
|
||||||
|
let state = match finality {
|
||||||
|
Finality::AllFinal => SLOT_FINAL,
|
||||||
|
Finality::Partial => SLOT_PARTIAL,
|
||||||
|
};
|
||||||
|
self.slot.state.store(state, Ordering::Release);
|
||||||
|
lent
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(self) {
|
||||||
|
self.slot.key.store(0, Ordering::Release);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::atomic::AtomicU32;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn publish_then_hit_with_finality() {
|
||||||
|
let table = FrameTable::<u32, 8>::new();
|
||||||
|
let Lookup::Vacant(slot) = table.lookup(7) else {
|
||||||
|
panic!("fresh table must be vacant");
|
||||||
|
};
|
||||||
|
assert_eq!(*slot.publish(41, Finality::Partial), 41);
|
||||||
|
let Lookup::Hit(finality, value) = table.lookup(7) else {
|
||||||
|
panic!("published key must hit");
|
||||||
|
};
|
||||||
|
assert_eq!((finality, *value), (Finality::Partial, 41));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn released_slot_is_vacant_again() {
|
||||||
|
let table = FrameTable::<u32, 8>::new();
|
||||||
|
let Lookup::Vacant(slot) = table.lookup(7) else { unreachable!() };
|
||||||
|
slot.release();
|
||||||
|
assert!(matches!(table.lookup(7), Lookup::Vacant(_)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn distinct_keys_probe_past_collisions_until_full() {
|
||||||
|
let table = FrameTable::<u32, 4>::new();
|
||||||
|
for hash in [2, 4, 6, 8] {
|
||||||
|
let Lookup::Vacant(slot) = table.lookup(hash) else {
|
||||||
|
panic!("hash {hash} should find a vacant slot");
|
||||||
|
};
|
||||||
|
slot.publish(hash as u32, Finality::AllFinal);
|
||||||
|
}
|
||||||
|
assert!(matches!(table.lookup(100), Lookup::Full));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn drop_runs_glue_for_published_values_only() {
|
||||||
|
static DROPS: AtomicU32 = AtomicU32::new(0);
|
||||||
|
struct Probe;
|
||||||
|
impl Drop for Probe {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
DROPS.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let table = FrameTable::<Probe, 8>::new();
|
||||||
|
let Lookup::Vacant(slot) = table.lookup(1) else { unreachable!() };
|
||||||
|
slot.publish(Probe, Finality::AllFinal);
|
||||||
|
let Lookup::Vacant(reserved_but_unpublished) = table.lookup(2) else { unreachable!() };
|
||||||
|
reserved_but_unpublished.release();
|
||||||
|
drop(table);
|
||||||
|
assert_eq!(DROPS.load(Ordering::Relaxed), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,229 @@
|
|||||||
|
use crate::context::InjectIndex;
|
||||||
|
use crate::gpoll::{Extent, Finality, GPoll, GraphError};
|
||||||
|
use std::mem::MaybeUninit;
|
||||||
|
use std::ops::Range;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum BatchStatus<'a, T> {
|
||||||
|
Lent(&'a [T], Finality),
|
||||||
|
Filled(&'a mut [T], Finality),
|
||||||
|
Pending,
|
||||||
|
Error(GraphError),
|
||||||
|
NeedBuffer,
|
||||||
|
InvalidRange,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// The first `len` elements of `scratch` must be initialized, and `len` must not exceed `scratch.len()`.
|
||||||
|
pub unsafe fn assume_init_prefix_mut<T>(scratch: &mut [MaybeUninit<T>], len: usize) -> &mut [T] {
|
||||||
|
debug_assert!(len <= scratch.len());
|
||||||
|
unsafe { std::slice::from_raw_parts_mut(scratch.as_mut_ptr().cast::<T>(), len) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait GNode<Input> {
|
||||||
|
type Output;
|
||||||
|
|
||||||
|
fn eval(&self, input: &Input) -> GPoll<Self::Output>;
|
||||||
|
|
||||||
|
fn extent(&self, _input: &Input) -> GPoll<Extent> {
|
||||||
|
GPoll::Final(Extent::Free)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_batch<'a>(&self, input: &'a Input, range: Range<u64>, scratch: Option<&'a mut [MaybeUninit<Self::Output>]>) -> BatchStatus<'a, Self::Output>
|
||||||
|
where
|
||||||
|
Input: InjectIndex + Copy,
|
||||||
|
{
|
||||||
|
let Some(scratch) = scratch else {
|
||||||
|
return BatchStatus::NeedBuffer;
|
||||||
|
};
|
||||||
|
let Some(len) = range.end.checked_sub(range.start).map(|len| len as usize) else {
|
||||||
|
return BatchStatus::InvalidRange;
|
||||||
|
};
|
||||||
|
if scratch.len() < len {
|
||||||
|
return BatchStatus::InvalidRange;
|
||||||
|
}
|
||||||
|
let mut local = *input;
|
||||||
|
let mut finality = Finality::AllFinal;
|
||||||
|
for offset in 0..len {
|
||||||
|
local.set_index(range.start + offset as u64);
|
||||||
|
let abort = match self.eval(&local) {
|
||||||
|
GPoll::Final(value) => {
|
||||||
|
scratch[offset].write(value);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
GPoll::Partial(value) => {
|
||||||
|
scratch[offset].write(value);
|
||||||
|
finality = Finality::Partial;
|
||||||
|
None
|
||||||
|
}
|
||||||
|
GPoll::Pending => Some(BatchStatus::Pending),
|
||||||
|
GPoll::Fallback(boxed) => Some(BatchStatus::Error(boxed.1)),
|
||||||
|
GPoll::Error(e) => Some(BatchStatus::Error(*e)),
|
||||||
|
};
|
||||||
|
if let Some(status) = abort {
|
||||||
|
for written in scratch[..offset].iter_mut() {
|
||||||
|
// SAFETY: every lane before `offset` was written by this loop.
|
||||||
|
unsafe { written.assume_init_drop() };
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// SAFETY: all `len` lanes were written by the loop above.
|
||||||
|
BatchStatus::Filled(unsafe { assume_init_prefix_mut(scratch, len) }, finality)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Input, N> GNode<Input> for &N
|
||||||
|
where
|
||||||
|
N: GNode<Input> + ?Sized,
|
||||||
|
{
|
||||||
|
type Output = N::Output;
|
||||||
|
|
||||||
|
fn eval(&self, input: &Input) -> GPoll<Self::Output> {
|
||||||
|
(**self).eval(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extent(&self, input: &Input) -> GPoll<Extent> {
|
||||||
|
(**self).extent(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_batch<'a>(&self, input: &'a Input, range: Range<u64>, scratch: Option<&'a mut [MaybeUninit<Self::Output>]>) -> BatchStatus<'a, Self::Output>
|
||||||
|
where
|
||||||
|
Input: InjectIndex + Copy,
|
||||||
|
{
|
||||||
|
(**self).eval_batch(input, range, scratch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Input, N> GNode<Input> for Box<N>
|
||||||
|
where
|
||||||
|
N: GNode<Input> + ?Sized,
|
||||||
|
{
|
||||||
|
type Output = N::Output;
|
||||||
|
|
||||||
|
fn eval(&self, input: &Input) -> GPoll<Self::Output> {
|
||||||
|
(**self).eval(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extent(&self, input: &Input) -> GPoll<Extent> {
|
||||||
|
(**self).extent(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_batch<'a>(&self, input: &'a Input, range: Range<u64>, scratch: Option<&'a mut [MaybeUninit<Self::Output>]>) -> BatchStatus<'a, Self::Output>
|
||||||
|
where
|
||||||
|
Input: InjectIndex + Copy,
|
||||||
|
{
|
||||||
|
(**self).eval_batch(input, range, scratch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::sync::atomic::{AtomicU32, Ordering};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct TestInput {
|
||||||
|
index: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InjectIndex for TestInput {
|
||||||
|
fn set_index(&mut self, index: u64) {
|
||||||
|
self.index = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Double;
|
||||||
|
|
||||||
|
impl GNode<TestInput> for Double {
|
||||||
|
type Output = u64;
|
||||||
|
|
||||||
|
fn eval(&self, input: &TestInput) -> GPoll<u64> {
|
||||||
|
GPoll::Final(input.index * 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn spec_loop_fills_scratch_per_lane() {
|
||||||
|
let input = TestInput { index: 0 };
|
||||||
|
let mut scratch = [const { MaybeUninit::uninit() }; 4];
|
||||||
|
let status = Double.eval_batch(&input, 2..6, Some(&mut scratch));
|
||||||
|
let BatchStatus::Filled(lanes, finality) = status else {
|
||||||
|
panic!("expected filled, got {status:?}");
|
||||||
|
};
|
||||||
|
assert_eq!(lanes, &[4, 6, 8, 10]);
|
||||||
|
assert_eq!(finality, Finality::AllFinal);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn probe_without_scratch_requests_a_buffer() {
|
||||||
|
let input = TestInput { index: 0 };
|
||||||
|
assert!(matches!(Double.eval_batch(&input, 0..4, None), BatchStatus::NeedBuffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn undersized_scratch_is_an_invalid_range() {
|
||||||
|
let input = TestInput { index: 0 };
|
||||||
|
let mut scratch = [const { MaybeUninit::uninit() }; 2];
|
||||||
|
assert!(matches!(Double.eval_batch(&input, 0..4, Some(&mut scratch)), BatchStatus::InvalidRange));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn partial_lane_downgrades_batch_finality() {
|
||||||
|
struct PartialAtThree;
|
||||||
|
impl GNode<TestInput> for PartialAtThree {
|
||||||
|
type Output = u64;
|
||||||
|
fn eval(&self, input: &TestInput) -> GPoll<u64> {
|
||||||
|
match input.index {
|
||||||
|
3 => GPoll::Partial(input.index),
|
||||||
|
index => GPoll::Final(index),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let input = TestInput { index: 0 };
|
||||||
|
let mut scratch = [const { MaybeUninit::uninit() }; 4];
|
||||||
|
let status = PartialAtThree.eval_batch(&input, 0..4, Some(&mut scratch));
|
||||||
|
let BatchStatus::Filled(lanes, finality) = status else {
|
||||||
|
panic!("expected filled, got {status:?}");
|
||||||
|
};
|
||||||
|
assert_eq!(lanes, &[0, 1, 2, 3]);
|
||||||
|
assert_eq!(finality, Finality::Partial);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn abort_drops_already_written_lanes() {
|
||||||
|
static DROPS: AtomicU32 = AtomicU32::new(0);
|
||||||
|
struct Probe;
|
||||||
|
impl Drop for Probe {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
DROPS.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
struct PendingAtTwo;
|
||||||
|
impl GNode<TestInput> for PendingAtTwo {
|
||||||
|
type Output = Probe;
|
||||||
|
fn eval(&self, input: &TestInput) -> GPoll<Probe> {
|
||||||
|
match input.index {
|
||||||
|
2 => GPoll::Pending,
|
||||||
|
_ => GPoll::Final(Probe),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let input = TestInput { index: 0 };
|
||||||
|
let mut scratch = [const { MaybeUninit::uninit() }; 4];
|
||||||
|
let status = PendingAtTwo.eval_batch(&input, 0..4, Some(&mut scratch));
|
||||||
|
assert!(matches!(status, BatchStatus::Pending));
|
||||||
|
assert_eq!(DROPS.load(Ordering::Relaxed), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trait_is_object_safe_across_erased_edges() {
|
||||||
|
let erased: Box<dyn GNode<TestInput, Output = u64>> = Box::new(Double);
|
||||||
|
let input = TestInput { index: 21 };
|
||||||
|
assert_eq!(erased.eval(&input), GPoll::Final(42));
|
||||||
|
let mut scratch = [const { MaybeUninit::uninit() }; 2];
|
||||||
|
let status = erased.eval_batch(&input, 0..2, Some(&mut scratch));
|
||||||
|
assert!(matches!(status, BatchStatus::Filled(_, Finality::AllFinal)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,222 @@
|
|||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum ErrorKind {
|
||||||
|
Node(&'static str),
|
||||||
|
ArenaExhausted,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq<&str> for ErrorKind {
|
||||||
|
fn eq(&self, other: &&str) -> bool {
|
||||||
|
matches!(self, ErrorKind::Node(kind) if kind == other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub struct GraphError {
|
||||||
|
pub kind: ErrorKind,
|
||||||
|
pub trace: Vec<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GraphError {
|
||||||
|
pub fn new(kind: &'static str) -> Self {
|
||||||
|
Self {
|
||||||
|
kind: ErrorKind::Node(kind),
|
||||||
|
trace: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum GPoll<T> {
|
||||||
|
Pending,
|
||||||
|
Final(T),
|
||||||
|
Partial(T),
|
||||||
|
Fallback(Box<(T, GraphError)>),
|
||||||
|
Error(Box<GraphError>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> GPoll<T> {
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> GPoll<U> {
|
||||||
|
match self {
|
||||||
|
GPoll::Pending => GPoll::Pending,
|
||||||
|
GPoll::Final(value) => GPoll::Final(f(value)),
|
||||||
|
GPoll::Partial(value) => GPoll::Partial(f(value)),
|
||||||
|
GPoll::Fallback(boxed) => {
|
||||||
|
let (value, e) = *boxed;
|
||||||
|
GPoll::Fallback(Box::new((f(value), e)))
|
||||||
|
}
|
||||||
|
GPoll::Error(e) => GPoll::Error(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn and_then<U>(self, f: impl FnOnce(T) -> GPoll<U>) -> GPoll<U> {
|
||||||
|
match self {
|
||||||
|
GPoll::Pending => GPoll::Pending,
|
||||||
|
GPoll::Final(value) => f(value),
|
||||||
|
GPoll::Partial(value) => match f(value) {
|
||||||
|
GPoll::Final(result) => GPoll::Partial(result),
|
||||||
|
other => other,
|
||||||
|
},
|
||||||
|
GPoll::Fallback(boxed) => {
|
||||||
|
let (value, e) = *boxed;
|
||||||
|
match f(value) {
|
||||||
|
GPoll::Pending => GPoll::Pending,
|
||||||
|
GPoll::Final(result) | GPoll::Partial(result) => GPoll::Fallback(Box::new((result, e))),
|
||||||
|
GPoll::Fallback(inner) => {
|
||||||
|
let (result, _) = *inner;
|
||||||
|
GPoll::Fallback(Box::new((result, e)))
|
||||||
|
}
|
||||||
|
GPoll::Error(inner) => GPoll::Error(inner),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GPoll::Error(e) => GPoll::Error(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn zip<U>(self, other: GPoll<U>) -> GPoll<(T, U)> {
|
||||||
|
match (self, other) {
|
||||||
|
(GPoll::Error(e), _) | (_, GPoll::Error(e)) => GPoll::Error(e),
|
||||||
|
(GPoll::Pending, _) | (_, GPoll::Pending) => GPoll::Pending,
|
||||||
|
(GPoll::Final(a), GPoll::Final(b)) => GPoll::Final((a, b)),
|
||||||
|
(GPoll::Fallback(boxed), GPoll::Final(b) | GPoll::Partial(b)) => {
|
||||||
|
let (a, e) = *boxed;
|
||||||
|
GPoll::Fallback(Box::new(((a, b), e)))
|
||||||
|
}
|
||||||
|
(GPoll::Final(a) | GPoll::Partial(a), GPoll::Fallback(boxed)) => {
|
||||||
|
let (b, e) = *boxed;
|
||||||
|
GPoll::Fallback(Box::new(((a, b), e)))
|
||||||
|
}
|
||||||
|
(GPoll::Fallback(first), GPoll::Fallback(second)) => {
|
||||||
|
let (a, e) = *first;
|
||||||
|
let (b, _) = *second;
|
||||||
|
GPoll::Fallback(Box::new(((a, b), e)))
|
||||||
|
}
|
||||||
|
(GPoll::Partial(a), GPoll::Final(b) | GPoll::Partial(b)) | (GPoll::Final(a), GPoll::Partial(b)) => GPoll::Partial((a, b)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn trace(self, input: usize) -> Self {
|
||||||
|
match self {
|
||||||
|
GPoll::Fallback(mut boxed) => {
|
||||||
|
boxed.1.trace.push(input);
|
||||||
|
GPoll::Fallback(boxed)
|
||||||
|
}
|
||||||
|
GPoll::Error(mut e) => {
|
||||||
|
e.trace.push(input);
|
||||||
|
GPoll::Error(e)
|
||||||
|
}
|
||||||
|
other => other,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fallback(value: T, kind: &'static str) -> Self {
|
||||||
|
GPoll::Fallback(Box::new((value, GraphError::new(kind))))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn error(kind: &'static str) -> Self {
|
||||||
|
GPoll::Error(Box::new(GraphError::new(kind)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn arena_exhausted() -> Self {
|
||||||
|
GPoll::Error(Box::new(GraphError {
|
||||||
|
kind: ErrorKind::ArenaExhausted,
|
||||||
|
trace: Vec::new(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub enum Interrupt {
|
||||||
|
Pending,
|
||||||
|
Error(Box<GraphError>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<GraphError> for Interrupt {
|
||||||
|
fn from(error: GraphError) -> Self {
|
||||||
|
Interrupt::Error(Box::new(error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<Interrupt> for GPoll<T> {
|
||||||
|
fn from(interrupt: Interrupt) -> Self {
|
||||||
|
match interrupt {
|
||||||
|
Interrupt::Pending => GPoll::Pending,
|
||||||
|
Interrupt::Error(e) => GPoll::Error(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum Extent {
|
||||||
|
Free,
|
||||||
|
Exactly(usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Extent {
|
||||||
|
pub fn meet(a: GPoll<Extent>, b: GPoll<Extent>) -> GPoll<Extent> {
|
||||||
|
a.zip(b).and_then(|(a, b)| match (a, b) {
|
||||||
|
(Extent::Free, other) | (other, Extent::Free) => GPoll::Final(other),
|
||||||
|
(Extent::Exactly(n), Extent::Exactly(m)) if n == m => GPoll::Final(Extent::Exactly(n)),
|
||||||
|
(Extent::Exactly(n), Extent::Exactly(m)) => GPoll::fallback(Extent::Exactly(n.min(m)), "extent mismatch"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum Finality {
|
||||||
|
AllFinal,
|
||||||
|
Partial,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Finality {
|
||||||
|
pub fn meet(self, other: Finality) -> Finality {
|
||||||
|
match (self, other) {
|
||||||
|
(Finality::AllFinal, Finality::AllFinal) => Finality::AllFinal,
|
||||||
|
_ => Finality::Partial,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn free_is_the_meet_identity() {
|
||||||
|
let meet = Extent::meet(GPoll::Final(Extent::Free), GPoll::Final(Extent::Exactly(4)));
|
||||||
|
assert_eq!(meet, GPoll::Final(Extent::Exactly(4)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn extent_mismatch_truncates_and_reports() {
|
||||||
|
let meet = Extent::meet(GPoll::Final(Extent::Exactly(3)), GPoll::Final(Extent::Exactly(5)));
|
||||||
|
let GPoll::Fallback(boxed) = meet else {
|
||||||
|
panic!("expected fallback, got {meet:?}");
|
||||||
|
};
|
||||||
|
assert_eq!(boxed.0, Extent::Exactly(3));
|
||||||
|
assert!(boxed.1.kind == "extent mismatch");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn error_dominates_pending_in_zip() {
|
||||||
|
let zipped = GPoll::<u32>::error("boom").zip(GPoll::<u32>::Pending);
|
||||||
|
assert!(matches!(zipped, GPoll::Error(_)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trace_builds_root_to_source_path() {
|
||||||
|
let poll = GPoll::<u32>::error("boom").trace(2).trace(0);
|
||||||
|
let GPoll::Error(e) = poll else { unreachable!() };
|
||||||
|
assert_eq!(e.trace, vec![2, 0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn interrupt_round_trips_to_gpoll() {
|
||||||
|
assert_eq!(GPoll::<u32>::from(Interrupt::Pending), GPoll::Pending);
|
||||||
|
let interrupt = Interrupt::from(GraphError::new("boom"));
|
||||||
|
assert!(matches!(GPoll::<u32>::from(interrupt), GPoll::Error(e) if e.kind == "boom"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
|
pub mod arena;
|
||||||
pub mod bounds;
|
pub mod bounds;
|
||||||
pub mod consts;
|
pub mod consts;
|
||||||
pub mod context;
|
pub mod context;
|
||||||
|
pub mod frame_table;
|
||||||
pub mod generic;
|
pub mod generic;
|
||||||
|
pub mod gnode;
|
||||||
|
pub mod gpoll;
|
||||||
pub mod list;
|
pub mod list;
|
||||||
pub mod math;
|
pub mod math;
|
||||||
pub mod memo;
|
pub mod memo;
|
||||||
|
|||||||
@@ -235,6 +235,7 @@ pub enum Type {
|
|||||||
Fn(Box<Type>, Box<Type>),
|
Fn(Box<Type>, Box<Type>),
|
||||||
/// Represents a future which promises to return the inner type.
|
/// Represents a future which promises to return the inner type.
|
||||||
Future(Box<Type>),
|
Future(Box<Type>),
|
||||||
|
Ref(Box<Type>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Type {
|
impl Default for Type {
|
||||||
@@ -308,6 +309,7 @@ impl Type {
|
|||||||
Self::Concrete(ty) => Some(ty.size),
|
Self::Concrete(ty) => Some(ty.size),
|
||||||
Self::Fn(_, _) => None,
|
Self::Fn(_, _) => None,
|
||||||
Self::Future(_) => None,
|
Self::Future(_) => None,
|
||||||
|
Self::Ref(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,6 +319,7 @@ impl Type {
|
|||||||
Self::Concrete(ty) => Some(ty.align),
|
Self::Concrete(ty) => Some(ty.align),
|
||||||
Self::Fn(_, _) => None,
|
Self::Fn(_, _) => None,
|
||||||
Self::Future(_) => None,
|
Self::Future(_) => None,
|
||||||
|
Self::Ref(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,6 +329,7 @@ impl Type {
|
|||||||
Self::Concrete(_) => self,
|
Self::Concrete(_) => self,
|
||||||
Self::Fn(_, output) => output.nested_type(),
|
Self::Fn(_, output) => output.nested_type(),
|
||||||
Self::Future(output) => output.nested_type(),
|
Self::Future(output) => output.nested_type(),
|
||||||
|
Self::Ref(inner) => inner.nested_type(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,6 +342,7 @@ impl Type {
|
|||||||
Self::Concrete(_) => None,
|
Self::Concrete(_) => None,
|
||||||
Self::Fn(_, output) => output.replace_nested(f),
|
Self::Fn(_, output) => output.replace_nested(f),
|
||||||
Self::Future(output) => output.replace_nested(f),
|
Self::Future(output) => output.replace_nested(f),
|
||||||
|
Self::Ref(inner) => inner.replace_nested(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -347,6 +352,7 @@ impl Type {
|
|||||||
Type::Concrete(ty) => simplify_identifier_name(&ty.name),
|
Type::Concrete(ty) => simplify_identifier_name(&ty.name),
|
||||||
Type::Fn(call_arg, return_value) => format!("{} called with {}", return_value.identifier_name(), call_arg.identifier_name()),
|
Type::Fn(call_arg, return_value) => format!("{} called with {}", return_value.identifier_name(), call_arg.identifier_name()),
|
||||||
Type::Future(ty) => ty.identifier_name(),
|
Type::Future(ty) => ty.identifier_name(),
|
||||||
|
Type::Ref(ty) => ty.identifier_name(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -441,6 +447,7 @@ impl std::fmt::Display for Type {
|
|||||||
Type::Concrete(ty) => write!(f, "{ty}"),
|
Type::Concrete(ty) => write!(f, "{ty}"),
|
||||||
Type::Fn(_, return_value) => write!(f, "{return_value}"),
|
Type::Fn(_, return_value) => write!(f, "{return_value}"),
|
||||||
Type::Future(ty) => write!(f, "{ty}"),
|
Type::Future(ty) => write!(f, "{ty}"),
|
||||||
|
Type::Ref(ty) => write!(f, "{ty}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user