diff --git a/node-graph/libraries/core-types/src/context.rs b/node-graph/libraries/core-types/src/context.rs index 472d8717fe..4e2e75ddd4 100644 --- a/node-graph/libraries/core-types/src/context.rs +++ b/node-graph/libraries/core-types/src/context.rs @@ -448,186 +448,12 @@ impl ExtractFootprint for () { } } -// ========================================== -// EXTRACT TRAIT IMPLS FOR `OwnedContextImpl` -// ========================================== +// ============== +// TYPE `Context` +// ============== -impl ArcCtx for OwnedContextImpl {} - -impl ExtractFootprint for OwnedContextImpl { - fn try_footprint(&self) -> Option<&Footprint> { - self.footprint.as_ref() - } -} -impl ExtractRealTime for OwnedContextImpl { - fn try_real_time(&self) -> Option { - self.real_time - } -} -impl ExtractAnimationTime for OwnedContextImpl { - fn try_animation_time(&self) -> Option { - self.animation_time - } -} -impl ExtractPointerPosition for OwnedContextImpl { - fn try_pointer_position(&self) -> Option { - self.pointer_position - } -} -impl ExtractPosition for OwnedContextImpl { - fn try_position(&self) -> Option> { - self.position.clone().map(|x| x.into_iter()) - } -} -impl ExtractIndex for OwnedContextImpl { - fn try_index(&self) -> Option> { - self.index.clone().map(|x| x.into_iter()) - } -} -impl ExtractVarArgs for OwnedContextImpl { - fn vararg(&self, index: usize) -> Result, VarArgsResult> { - let Some(ref inner) = self.varargs else { - let Some(ref parent) = self.parent else { - return Err(VarArgsResult::NoVarArgs); - }; - return parent.vararg(index); - }; - inner.get(index).map(|x| x.as_ref() as DynRef<'_>).ok_or(VarArgsResult::IndexOutOfBounds) - } - - fn varargs_len(&self) -> Result { - let Some(ref inner) = self.varargs else { - let Some(ref parent) = self.parent else { - return Err(VarArgsResult::NoVarArgs); - }; - return parent.varargs_len(); - }; - Ok(inner.len()) - } - - fn hash_varargs(&self, mut hasher: &mut dyn Hasher) { - match (&self.varargs, &self.parent) { - (Some(inner), _) => { - for arg in inner.iter() { - arg.hash(&mut hasher); - } - } - (None, Some(parent)) => { - parent.hash_varargs(hasher); - } - _ => (), - }; - } -} - -impl CloneVarArgs for Arc { - fn arc_clone(&self) -> Option> { - Some(self.clone()) - } -} - -// ====================================== -// TYPES `Context` AND `OwnedContextImpl` -// ====================================== - -pub type OwnedContext = Option>; pub type Context<'a> = ContextImpl<'a>; type DynRef<'a> = &'a (dyn Any + Send + Sync); -type DynBox = Box; - -#[derive(dyn_any::DynAny)] -pub struct OwnedContextImpl { - parent: Option>, - footprint: Option, - real_time: Option, - animation_time: Option, - pointer_position: Option, - position: Option>, - // This could be converted into a single enum to save extra bytes - index: Option>, - varargs: Option>, -} - -impl std::fmt::Debug for OwnedContextImpl { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("OwnedContextImpl") - .field("parent", &self.parent.as_ref().map(|_| "")) - .field("footprint", &self.footprint) - .field("real_time", &self.real_time) - .field("animation_time", &self.animation_time) - .field("pointer_position", &self.pointer_position) - .field("index", &self.index) - .field("varargs_len", &self.varargs.as_ref().map(|x| x.len())) - .finish() - } -} - -impl Default for OwnedContextImpl { - #[track_caller] - fn default() -> Self { - Self::empty() - } -} - -impl graphene_hash::CacheHash for OwnedContextImpl { - fn cache_hash(&self, state: &mut H) { - self.footprint.cache_hash(state); - self.real_time.cache_hash(state); - self.animation_time.cache_hash(state); - self.pointer_position.cache_hash(state); - self.position.cache_hash(state); - self.index.cache_hash(state); - self.hash_varargs(state); - } -} - -impl OwnedContextImpl { - #[track_caller] - pub fn from(value: T) -> Self { - OwnedContextImpl::from_flags(value, ContextFeatures::all()) - } - - #[track_caller] - pub fn from_flags(value: T, bitflags: ContextFeatures) -> Self { - let parent = bitflags - .contains(ContextFeatures::VARARGS) - .then(|| match value.varargs_len() { - Ok(x) if x > 0 => value.arc_clone(), - _ => None, - }) - .flatten(); - let footprint = bitflags.contains(ContextFeatures::FOOTPRINT).then(|| value.try_footprint().copied()).flatten(); - let real_time = bitflags.contains(ContextFeatures::REAL_TIME).then(|| value.try_real_time()).flatten(); - let animation_time = bitflags.contains(ContextFeatures::ANIMATION_TIME).then(|| value.try_animation_time()).flatten(); - let pointer_position = bitflags.contains(ContextFeatures::POINTER_POSITION).then(|| value.try_pointer_position()).flatten(); - let position = bitflags.contains(ContextFeatures::POSITION).then(|| value.try_position()).flatten().map(|x| x.collect()); - let index = bitflags.contains(ContextFeatures::INDEX).then(|| value.try_index()).flatten().map(|x| x.collect()); - - OwnedContextImpl { - parent, - footprint, - real_time, - animation_time, - pointer_position, - position, - index, - varargs: None, - } - } - - pub const fn empty() -> Self { - OwnedContextImpl { - parent: None, - footprint: None, - real_time: None, - animation_time: None, - pointer_position: None, - position: None, - index: None, - varargs: None, - } - } -} pub trait DynHash { fn dyn_hash(&self, state: &mut dyn Hasher); @@ -685,57 +511,6 @@ impl std::fmt::Debug for OwnedSlot { } } -impl OwnedContextImpl { - pub fn set_footprint(&mut self, footprint: Footprint) { - self.footprint = Some(footprint); - } - - pub fn with_footprint(mut self, footprint: Footprint) -> Self { - self.footprint = Some(footprint); - self - } - pub fn with_real_time(mut self, real_time: f64) -> Self { - self.real_time = Some(real_time); - self - } - pub fn with_animation_time(mut self, animation_time: f64) -> Self { - self.animation_time = Some(animation_time); - self - } - pub fn with_pointer_position(mut self, pointer_position: DVec2) -> Self { - self.pointer_position = Some(pointer_position); - self - } - pub fn with_position(mut self, position: DVec2) -> Self { - if let Some(current_position) = &mut self.position { - current_position.insert(0, position); - } else { - self.position = Some(vec![position]); - } - self - } - pub fn with_index(mut self, index: usize) -> Self { - if let Some(current_index) = &mut self.index { - current_index.insert(0, index); - } else { - self.index = Some(vec![index]); - } - self - } - pub fn with_vararg(mut self, value: Box) -> Self { - assert!(self.varargs.is_none_or(|value| value.is_empty())); - self.varargs = Some(Arc::new([value])); - self - } - pub fn into_context(self) -> Option> { - Some(Arc::new(self)) - } - pub fn erase_parent(mut self) -> Self { - self.parent = None; - self - } -} - pub type SourceId = u64; #[derive(Clone, Copy, Debug)]