Name a type's live spelling beside its static one

This commit is contained in:
Dennis Kobert
2026-09-14 12:09:37 +02:00
parent 0a4288eca6
commit 781aef5f2e
7 changed files with 159 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
use core_types::transform::Footprint;
use dyn_any::{DynAny, StaticType, StaticTypeSized};
use dyn_any::DynAny;
use glam::DVec2;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
@@ -106,6 +106,8 @@ impl GetEditorPreferences for DummyPreferences {
}
}
#[derive(dyn_any::DynAny)]
#[dyn_any_derive(project)]
pub struct EditorApi<Io> {
/// Gives access to APIs like resources.
pub application_io: Option<Arc<Io>>,
@@ -155,7 +157,3 @@ impl<T> Debug for EditorApi<T> {
f.debug_struct("EditorApi").finish()
}
}
unsafe impl<T: StaticTypeSized> StaticType for EditorApi<T> {
type Static = EditorApi<T::Static>;
}

View File

@@ -1,7 +1,7 @@
use crate::attribute::Attribute as _;
use crate::bounds::{BoundingBox, RenderBoundingBox};
use crate::transform::ApplyTransform;
use dyn_any::{StaticType, StaticTypeSized};
use dyn_any::StaticType;
use glam::DAffine2;
use graphene_hash::CacheHash;
use std::fmt::Debug;
@@ -879,7 +879,10 @@ impl Attributes {
/// Elements are stored contiguously in a `Vec<T>`, while attributes live in an internal
/// [`Attributes`] store that keeps one attribute per attribute key. Items are accessed by
/// index through element/attribute accessor methods, or consumed as owned [`Item`]s via iteration.
#[derive(Clone, Debug)]
// The list carries its lifetime only through T, so its parameter projects: substituting
// T's static form substitutes the list's and keeps the layout identical.
#[derive(Clone, Debug, dyn_any::DynAny)]
#[dyn_any_derive(project)]
pub struct List<T> {
element: Vec<T>,
attributes: Attributes,
@@ -1192,12 +1195,6 @@ impl<T> ApplyTransform for List<T> {
}
}
// SAFETY: the list carries its lifetime only through T, so substituting T's
// static form substitutes the list's and keeps the layout identical.
unsafe impl<T: StaticTypeSized> StaticType for List<T> {
type Static = List<T::Static>;
}
impl<T> FromIterator<Item<T>> for List<T> {
/// Collects an iterator of [`Item`]s into a [`List`], pre-allocating based on the iterator's size hint.
fn from_iter<I: IntoIterator<Item = Item<T>>>(iter: I) -> Self {
@@ -1220,7 +1217,9 @@ impl<T> FromIterator<Item<T>> for List<T> {
/// An owned item containing an element of type `T` and a set of type-erased scalar attributes.
///
/// Used to build individual items before pushing them into a [`List`], or when consuming items out of a list via [`IntoIterator`].
#[derive(Clone, Debug)]
// As for `List`, the item's lifetime rides its element alone, so the parameter projects.
#[derive(Clone, Debug, dyn_any::DynAny)]
#[dyn_any_derive(project)]
pub struct Item<T> {
element: T,
attributes: ItemAttributeValues,

View File

@@ -695,3 +695,39 @@ mod test_support {
List::new_from_element(Graphic::Group(core_types::record::Group { row: None, content: builder.finish() }))
}
}
#[cfg(test)]
mod relift_tests {
use super::Graphic;
use core_types::arena::Arena;
use core_types::record::{Group, RunBuilder, element_write_hashed};
use dyn_any::Relift;
use vector_types::Vector;
/// `Relift` inverts the erasure a registry row keys on, so an element's `'static`
/// spelling names the borrowing form that live code needs.
#[test]
fn a_graphic_relifts_to_the_arena_lifetime() {
fn resident<'a>(arena: &'a Arena) -> <Graphic<'static> as Relift>::Live<'a> {
let mut builder = RunBuilder::new(arena, element_write_hashed::<Vector>(), &[], 1).unwrap();
builder.push(Vector::default()).unwrap();
Graphic::Group(Group { row: None, content: builder.finish() })
}
let arena = Arena::new(1 << 16).unwrap();
let live: Graphic<'_> = resident(&arena);
assert!(matches!(live, Graphic::Group(_)), "the relifted spelling is the same type, only its borrows differ");
}
/// A borrow-free element relifts to itself, so code over it never spells a lifetime.
#[test]
fn a_borrow_free_element_relifts_to_itself() {
fn same<'a>(vector: Vector) -> <Vector as Relift>::Live<'a> {
vector
}
assert_eq!(same(Vector::default()), Vector::default());
}
}

View File

@@ -3,7 +3,6 @@ use core_types::Color;
use core_types::color::float_to_srgb_u8;
// use crate::vector::Vector; // TODO: Check if Vector is actually used, if so handle differently
use core_types::color::*;
use dyn_any::StaticType;
use glam::{DAffine2, DVec2};
use std::vec::Vec;
@@ -49,7 +48,8 @@ mod base64_serde {
}
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[derive(Clone, Eq, Default)]
#[derive(Clone, Eq, Default, dyn_any::DynAny)]
#[dyn_any_derive(project)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Image<P: Pixel> {
pub width: u32,
@@ -92,14 +92,6 @@ impl<P: Pixel + std::fmt::Debug> std::fmt::Debug for Image<P> {
}
}
unsafe impl<P> StaticType for Image<P>
where
P: dyn_any::StaticTypeSized + Pixel,
P::Static: Pixel,
{
type Static = Image<P::Static>;
}
impl<P: Copy + Pixel> Bitmap for Image<P> {
type Pixel = P;
#[inline(always)]

View File

@@ -9,13 +9,12 @@ use core::borrow::Borrow;
use core_types::bounds::{BoundingBox, RenderBoundingBox};
use core_types::render_complexity::RenderComplexity;
use core_types::transform::Transform;
use dyn_any::StaticType;
use glam::{DAffine2, DVec2};
use kurbo::{Affine, BezPath, Rect, Shape};
use std::collections::HashMap;
/// Represents vector graphics data, composed of Bézier curves in a path or mesh arrangement.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, dyn_any::DynAny)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vector {
pub stroke: Option<Stroke>,
@@ -28,9 +27,6 @@ pub struct Vector {
pub segment_domain: SegmentDomain,
pub region_domain: RegionDomain,
}
unsafe impl StaticType for Vector {
type Static = Self;
}
impl Default for Vector {
fn default() -> Self {