Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit d7d4cac

Browse files
author
Hendrik van Antwerpen
committed
Promote Appendable::Ctx to proper type parameter
1 parent 730fd89 commit d7d4cac

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

stack-graphs/src/cycles.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use enumset::EnumSet;
3333
use smallvec::SmallVec;
3434
use std::collections::HashMap;
35+
use std::marker::PhantomData;
3536

3637
use crate::arena::Handle;
3738
use crate::arena::List;
@@ -129,31 +130,42 @@ where
129130
// ----------------------------------------------------------------------------
130131
// Cycle detector
131132

132-
pub trait Appendable {
133-
type Ctx;
134-
133+
pub trait Appendable<Ctx> {
135134
fn append_to(
136135
&self,
137136
graph: &StackGraph,
138137
partials: &mut PartialPaths,
139-
ctx: &mut Self::Ctx,
138+
ctx: &mut Ctx,
140139
path: &mut PartialPath,
141140
) -> Result<(), PathResolutionError>;
142-
fn start_node(&self, ctx: &mut Self::Ctx) -> Handle<Node>;
143-
fn end_node(&self, ctx: &mut Self::Ctx) -> Handle<Node>;
141+
fn start_node(&self, ctx: &mut Ctx) -> Handle<Node>;
142+
fn end_node(&self, ctx: &mut Ctx) -> Handle<Node>;
144143
}
145144

146-
#[derive(Clone)]
147-
pub struct AppendingCycleDetector<A> {
145+
pub struct AppendingCycleDetector<Ctx, A>
146+
where
147+
A: Appendable<Ctx>,
148+
{
148149
appendages: List<A>,
150+
ctx: PhantomData<Ctx>,
151+
}
152+
153+
impl<Ctx, A: Appendable<Ctx>> Clone for AppendingCycleDetector<Ctx, A> {
154+
fn clone(&self) -> Self {
155+
Self {
156+
appendages: self.appendages.clone(),
157+
ctx: PhantomData::default(),
158+
}
159+
}
149160
}
150161

151162
pub type Appendables<A> = ListArena<A>;
152163

153-
impl<A: Appendable + Clone> AppendingCycleDetector<A> {
164+
impl<Ctx, A: Appendable<Ctx> + Clone> AppendingCycleDetector<Ctx, A> {
154165
pub fn new() -> Self {
155166
Self {
156167
appendages: List::empty(),
168+
ctx: PhantomData::default(),
157169
}
158170
}
159171

@@ -173,7 +185,7 @@ impl<A: Appendable + Clone> AppendingCycleDetector<A> {
173185
&self,
174186
graph: &StackGraph,
175187
partials: &mut PartialPaths,
176-
ctx: &mut A::Ctx,
188+
ctx: &mut Ctx,
177189
appendables: &mut Appendables<A>,
178190
) -> Result<EnumSet<Cyclicity>, PathResolutionError> {
179191
let mut cycles = EnumSet::new();
@@ -223,9 +235,7 @@ impl<A: Appendable + Clone> AppendingCycleDetector<A> {
223235
}
224236
}
225237

226-
impl Appendable for Edge {
227-
type Ctx = ();
228-
238+
impl Appendable<()> for Edge {
229239
fn append_to(
230240
&self,
231241
graph: &StackGraph,
@@ -245,9 +255,7 @@ impl Appendable for Edge {
245255
}
246256
}
247257

248-
impl Appendable for OwnedOrDatabasePath {
249-
type Ctx = Database;
250-
258+
impl Appendable<Database> for OwnedOrDatabasePath {
251259
fn append_to(
252260
&self,
253261
graph: &StackGraph,

stack-graphs/src/partial.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,13 +2225,13 @@ impl PartialPath {
22252225
/// as a parameter, instead of building it up ourselves, so that you have control over which
22262226
/// particular collection type to use, and so that you can reuse result collections across
22272227
/// multiple calls.
2228-
fn extend<R: Extend<(PartialPath, AppendingCycleDetector<Edge>)>>(
2228+
fn extend<R: Extend<(PartialPath, AppendingCycleDetector<(), Edge>)>>(
22292229
&self,
22302230
graph: &StackGraph,
22312231
partials: &mut PartialPaths,
22322232
file: Option<Handle<File>>,
22332233
edges: &mut Appendables<Edge>,
2234-
path_cycle_detector: AppendingCycleDetector<Edge>,
2234+
path_cycle_detector: AppendingCycleDetector<(), Edge>,
22352235
result: &mut R,
22362236
) {
22372237
let extensions = graph.outgoing_edges(self.end_node);

stack-graphs/src/stitching.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,15 @@ impl<'a> Display for DisplaySymbolStackKey<'a> {
463463
/// [`find_all_complete_partial_paths`]: #method.find_all_complete_partial_paths
464464
pub struct ForwardPartialPathStitcher {
465465
candidate_partial_paths: Vec<Handle<PartialPath>>,
466-
queue: VecDeque<(PartialPath, AppendingCycleDetector<OwnedOrDatabasePath>)>,
466+
queue: VecDeque<(
467+
PartialPath,
468+
AppendingCycleDetector<Database, OwnedOrDatabasePath>,
469+
)>,
467470
// next_iteration is a tuple of queues instead of an queue of tuples so that the path queue
468471
// can be cheaply exposed through the C API as a continuous memory block
469472
next_iteration: (
470473
VecDeque<PartialPath>,
471-
VecDeque<AppendingCycleDetector<OwnedOrDatabasePath>>,
474+
VecDeque<AppendingCycleDetector<Database, OwnedOrDatabasePath>>,
472475
),
473476
appended_paths: Appendables<OwnedOrDatabasePath>,
474477
similar_path_detector: Option<SimilarPathDetector<PartialPath>>,
@@ -590,7 +593,7 @@ impl ForwardPartialPathStitcher {
590593
partials: &mut PartialPaths,
591594
db: &mut Database,
592595
partial_path: &PartialPath,
593-
cycle_detector: AppendingCycleDetector<OwnedOrDatabasePath>,
596+
cycle_detector: AppendingCycleDetector<Database, OwnedOrDatabasePath>,
594597
) -> usize {
595598
self.candidate_partial_paths.clear();
596599
if graph[partial_path.end_node].is_root() {

stack-graphs/tests/it/cycles.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn stitching_simple_identity_cycle_is_detected() {
224224
// test partial path cycle detector
225225
{
226226
let mut paths = Appendables::new();
227-
let mut cd: AppendingCycleDetector<OwnedOrDatabasePath> =
227+
let mut cd: AppendingCycleDetector<Database, OwnedOrDatabasePath> =
228228
AppendingCycleDetector::from(&mut paths, p0.into());
229229
cd.append(&mut paths, p1.into());
230230
assert_eq!(
@@ -320,7 +320,7 @@ fn stitching_composite_identity_cycle_is_detected() {
320320
// test joining cycle detector
321321
{
322322
let mut paths = Appendables::new();
323-
let mut cd: AppendingCycleDetector<OwnedOrDatabasePath> =
323+
let mut cd: AppendingCycleDetector<Database, OwnedOrDatabasePath> =
324324
AppendingCycleDetector::from(&mut paths, p0.into());
325325
cd.append(&mut paths, p1.into());
326326
assert_eq!(

0 commit comments

Comments
 (0)