From c4e6dfd310964f53c648f98d8fd873c793799c71 Mon Sep 17 00:00:00 2001 From: jdonszelmann Date: Wed, 12 Jun 2024 21:21:41 +0200 Subject: [PATCH 1/2] remove the main sg hashmap for labels --- scopegraphs-macros/src/label.rs | 24 +++++- scopegraphs/src/completeness/critical_edge.rs | 8 +- scopegraphs/src/completeness/explicit.rs | 16 ++-- scopegraphs/src/completeness/future.rs | 8 +- scopegraphs/src/completeness/mod.rs | 3 +- scopegraphs/src/completeness/unchecked.rs | 4 +- scopegraphs/src/label.rs | 51 ++++++++++++- scopegraphs/src/lib.rs | 2 +- scopegraphs/src/render/mod.rs | 6 +- scopegraphs/src/render/traverse.rs | 4 +- scopegraphs/src/resolve/lookup.rs | 4 +- scopegraphs/src/resolve/mod.rs | 12 +-- scopegraphs/src/scopegraph.rs | 75 +++++++++++++------ scopegraphs/src/storage.rs | 7 -- 14 files changed, 157 insertions(+), 67 deletions(-) diff --git a/scopegraphs-macros/src/label.rs b/scopegraphs-macros/src/label.rs index 4d06fdf..21c6294 100644 --- a/scopegraphs-macros/src/label.rs +++ b/scopegraphs-macros/src/label.rs @@ -28,8 +28,9 @@ pub fn impl_label(input: DeriveInput) -> TokenStream { } let mut variant_names = Vec::new(); + let mut variant_numbers = Vec::new(); - for variant in variants { + for (num, variant) in variants.into_iter().enumerate() { if !variant.fields.is_empty() { return quote_spanned!( variant.span() => compile_error!("cannot derive Label for an enum with fields on variants"); @@ -37,12 +38,31 @@ pub fn impl_label(input: DeriveInput) -> TokenStream { .into(); } + if variant.discriminant.is_some() { + return quote_spanned!( + variant.span() => compile_error!("cannot derive Label for an enum with custom discriminated variants. Use `Label::to_usize()`"); + ) + .into(); + } + variant_names.push(variant.ident); + variant_numbers.push(num); } + let num_variants = variant_numbers.len(); let name = input.ident; quote! { - impl scopegraphs::Label for #name { + unsafe impl scopegraphs::Label for #name { + type Array = [T; #num_variants]; + + fn to_usize(&self) -> usize { + match self { + #( + Self::#variant_names => #variant_numbers + ),* + } + } + fn iter() -> impl Iterator { [ #( diff --git a/scopegraphs/src/completeness/critical_edge.rs b/scopegraphs/src/completeness/critical_edge.rs index 39e42e0..1572b0b 100644 --- a/scopegraphs/src/completeness/critical_edge.rs +++ b/scopegraphs/src/completeness/critical_edge.rs @@ -1,5 +1,5 @@ use crate::completeness::Completeness; -use crate::{Scope, ScopeGraph}; +use crate::{Label, Scope, ScopeGraph}; use std::cell::RefCell; use std::{collections::HashSet, hash::Hash}; @@ -38,7 +38,7 @@ impl CriticalEdgeSet