mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-16 23:08:05 +08:00
Convert u64 IDs to newtypes (#1532)
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use alloc::vec;
|
||||
use alloc::vec::Vec;
|
||||
use core::iter::Zip;
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use core::slice::Iter;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Brief description: A vec that allows indexing elements by both index and an assigned unique ID
|
||||
/// Goals of this Data Structure:
|
||||
/// Brief description: A vec that allows indexing elements by both index and an assigned unique ID.
|
||||
/// Goals of this data structure:
|
||||
/// - Drop-in replacement for a Vec.
|
||||
/// - Provide an auto-assigned Unique ID per element upon insertion.
|
||||
/// - Add elements to the start or end.
|
||||
@@ -16,8 +17,10 @@ use alloc::vec::Vec;
|
||||
/// This data structure is somewhat similar to a linked list in terms of invariants.
|
||||
/// The downside is that currently it requires a lot of iteration.
|
||||
|
||||
// TODO: Convert from a type alias to a newtype
|
||||
pub type ElementId = u64;
|
||||
#[repr(transparent)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize, specta::Type)]
|
||||
pub struct ElementId(pub u64);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, specta::Type, Hash)]
|
||||
pub struct IdBackedVec<T> {
|
||||
/// Contained elements
|
||||
@@ -34,13 +37,13 @@ impl<T> IdBackedVec<T> {
|
||||
IdBackedVec {
|
||||
elements: vec![],
|
||||
element_ids: vec![],
|
||||
next_id: 0,
|
||||
next_id: ElementId(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Push a new element to the start of the vector
|
||||
pub fn push_front(&mut self, element: T) -> Option<ElementId> {
|
||||
self.next_id += 1;
|
||||
self.next_id = ElementId(self.next_id.0 + 1);
|
||||
self.elements.insert(0, element);
|
||||
self.element_ids.insert(0, self.next_id);
|
||||
Some(self.next_id)
|
||||
@@ -48,7 +51,7 @@ impl<T> IdBackedVec<T> {
|
||||
|
||||
/// Push an element to the end of the vector
|
||||
pub fn push_end(&mut self, element: T) -> Option<ElementId> {
|
||||
self.next_id += 1;
|
||||
self.next_id = ElementId(self.next_id.0 + 1);
|
||||
self.elements.push(element);
|
||||
self.element_ids.push(self.next_id);
|
||||
Some(self.next_id)
|
||||
@@ -57,7 +60,7 @@ impl<T> IdBackedVec<T> {
|
||||
/// Insert an element adjacent to the given ID
|
||||
pub fn insert(&mut self, element: T, id: ElementId) -> Option<ElementId> {
|
||||
if let Some(index) = self.index_from_id(id) {
|
||||
self.next_id += 1;
|
||||
self.next_id = ElementId(self.next_id.0 + 1);
|
||||
self.elements.insert(index, element);
|
||||
self.element_ids.insert(index, self.next_id);
|
||||
return Some(self.next_id);
|
||||
@@ -125,7 +128,7 @@ impl<T> IdBackedVec<T> {
|
||||
}
|
||||
|
||||
/// Enumerate the ids and elements in this container `(&ElementId, &T)`
|
||||
pub fn enumerate(&self) -> core::iter::Zip<core::slice::Iter<ElementId>, core::slice::Iter<T>> {
|
||||
pub fn enumerate(&self) -> Zip<Iter<ElementId>, Iter<T>> {
|
||||
self.element_ids.iter().zip(self.elements.iter())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
use super::id_vec::IdBackedVec;
|
||||
use super::consts::ManipulatorType;
|
||||
use super::id_vec::{ElementId, IdBackedVec};
|
||||
use super::manipulator_group::ManipulatorGroup;
|
||||
use super::manipulator_point::ManipulatorPoint;
|
||||
use super::{consts::ManipulatorType, id_vec::ElementId};
|
||||
use crate::uuid::ManipulatorGroupId;
|
||||
|
||||
use alloc::string::String;
|
||||
use alloc::vec;
|
||||
use alloc::vec::Vec;
|
||||
use core::iter::Zip;
|
||||
use core::slice::Iter;
|
||||
use dyn_any::{DynAny, StaticType};
|
||||
use glam::{DAffine2, DVec2};
|
||||
use kurbo::{BezPath, PathEl, Shape};
|
||||
@@ -460,7 +462,7 @@ impl BezierId {
|
||||
|
||||
/// An iterator over [`bezier_rs::Bezier`] segments constructable via [`Subpath::bezier_iter`].
|
||||
pub struct PathIter<'a> {
|
||||
path: core::iter::Zip<core::slice::Iter<'a, u64>, core::slice::Iter<'a, ManipulatorGroup>>,
|
||||
path: Zip<Iter<'a, ElementId>, Iter<'a, ManipulatorGroup>>,
|
||||
|
||||
last_anchor: Option<DVec2>,
|
||||
last_out_handle: Option<DVec2>,
|
||||
@@ -482,7 +484,8 @@ impl<'a> Iterator for PathIter<'a> {
|
||||
let mut result = None;
|
||||
|
||||
while result.is_none() {
|
||||
let (&id, manipulator_group) = self.path.next()?;
|
||||
let (id, manipulator_group) = self.path.next()?;
|
||||
let id = id.0;
|
||||
|
||||
let in_handle = manipulator_group.points[ManipulatorType::InHandle].as_ref().map(|point| point.position);
|
||||
let anchor = manipulator_group.points[ManipulatorType::Anchor].as_ref().map(|point| point.position);
|
||||
|
||||
Reference in New Issue
Block a user