Unverified Commit dae2f036 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(turbo-tasks): Use Return Position Impl Traits (RPIT) to reimplement…

refactor(turbo-tasks): Use Return Position Impl Traits (RPIT) to reimplement GraphTraversal (#74896)

Reimplements `GraphTraversal` without the manual `Future` implementation. This was [part of an experiment to try to use RPIT in the `Visit` trait](https://vercel.slack.com/archives/C02MK5YRKNZ/p1736864833846679), but I didn't quite get that far.

This code likely didn't use RPIT when it was written because RPIT on traits wasn't stable (or even fully implemented in rustc?) at that time.

This removes some unsafe code and a use of the unstable nightly-only `impl_trait_in_assoc_type` feature.
parent b88a4596
...@@ -68,7 +68,7 @@ where ...@@ -68,7 +68,7 @@ where
impl<T> GraphStore for AdjacencyMap<T> impl<T> GraphStore for AdjacencyMap<T>
where where
T: Eq + Hash + Clone, T: Eq + Hash + Clone + Send,
{ {
type Node = T; type Node = T;
type Handle = T; type Handle = T;
......
...@@ -4,9 +4,9 @@ use super::VisitedNodes; ...@@ -4,9 +4,9 @@ use super::VisitedNodes;
/// A graph store is a data structure that will be built up during a graph /// A graph store is a data structure that will be built up during a graph
/// traversal. It is used to store the results of the traversal. /// traversal. It is used to store the results of the traversal.
pub trait GraphStore { pub trait GraphStore: Send {
type Node; type Node: Send;
type Handle: Clone; type Handle: Clone + Send;
// TODO(alexkirsz) An `entry(from_handle) -> Entry` API would be more // TODO(alexkirsz) An `entry(from_handle) -> Entry` API would be more
// efficient, as right now we're getting the same key multiple times. // efficient, as right now we're getting the same key multiple times.
......
...@@ -18,7 +18,10 @@ impl<T> NonDeterministic<T> { ...@@ -18,7 +18,10 @@ impl<T> NonDeterministic<T> {
} }
} }
impl<T> GraphStore for NonDeterministic<T> { impl<T> GraphStore for NonDeterministic<T>
where
T: Send,
{
type Node = T; type Node = T;
type Handle = (); type Handle = ();
......
...@@ -10,7 +10,7 @@ use super::VisitControlFlow; ...@@ -10,7 +10,7 @@ use super::VisitControlFlow;
pub trait Visit<Node, Abort = !, Impl = ()> { pub trait Visit<Node, Abort = !, Impl = ()> {
type Edge; type Edge;
type EdgesIntoIter: IntoIterator<Item = Self::Edge>; type EdgesIntoIter: IntoIterator<Item = Self::Edge>;
type EdgesFuture: Future<Output = Result<Self::EdgesIntoIter>>; type EdgesFuture: Future<Output = Result<Self::EdgesIntoIter>> + Send;
/// Visits an edge to get to the neighbor node. Should return a /// Visits an edge to get to the neighbor node. Should return a
/// [`VisitControlFlow`] that indicates whether to: /// [`VisitControlFlow`] that indicates whether to:
...@@ -19,8 +19,13 @@ pub trait Visit<Node, Abort = !, Impl = ()> { ...@@ -19,8 +19,13 @@ pub trait Visit<Node, Abort = !, Impl = ()> {
/// * abort the traversal entirely. /// * abort the traversal entirely.
fn visit(&mut self, edge: Self::Edge) -> VisitControlFlow<Node, Abort>; fn visit(&mut self, edge: Self::Edge) -> VisitControlFlow<Node, Abort>;
/// Returns a future that resolves to the outgoing edges of the given /// Returns a future that resolves to the outgoing edges of the given `node`.
/// `node`. ///
/// Lifetimes:
/// - The returned future's lifetime cannot depend on the reference to self because there are
/// multiple `edges` futures created and awaited concurrently.
/// - The returned future's lifetime cannot depend on `node` because `GraphStore::insert`
/// returns a node reference that's only valid for the lifetime of its `&mut self` reference.
fn edges(&mut self, node: &Node) -> Self::EdgesFuture; fn edges(&mut self, node: &Node) -> Self::EdgesFuture;
/// Returns a [Span] for the given `node`, under which all edges are /// Returns a [Span] for the given `node`, under which all edges are
...@@ -41,7 +46,7 @@ pub struct ImplRef; ...@@ -41,7 +46,7 @@ pub struct ImplRef;
impl<Node, VisitFn, NeighFut, NeighIt> Visit<Node, !, ImplRef> for VisitFn impl<Node, VisitFn, NeighFut, NeighIt> Visit<Node, !, ImplRef> for VisitFn
where where
VisitFn: FnMut(&Node) -> NeighFut, VisitFn: FnMut(&Node) -> NeighFut,
NeighFut: Future<Output = Result<NeighIt>>, NeighFut: Future<Output = Result<NeighIt>> + Send,
NeighIt: IntoIterator<Item = Node>, NeighIt: IntoIterator<Item = Node>,
{ {
type Edge = Node; type Edge = Node;
...@@ -63,7 +68,7 @@ impl<Node, VisitFn, NeighFut, NeighIt> Visit<Node, !, ImplValue> for VisitFn ...@@ -63,7 +68,7 @@ impl<Node, VisitFn, NeighFut, NeighIt> Visit<Node, !, ImplValue> for VisitFn
where where
Node: Clone, Node: Clone,
VisitFn: FnMut(Node) -> NeighFut, VisitFn: FnMut(Node) -> NeighFut,
NeighFut: Future<Output = Result<NeighIt>>, NeighFut: Future<Output = Result<NeighIt>> + Send,
NeighIt: IntoIterator<Item = Node>, NeighIt: IntoIterator<Item = Node>,
{ {
type Edge = Node; type Edge = Node;
......
...@@ -36,9 +36,7 @@ ...@@ -36,9 +36,7 @@
#![feature(arbitrary_self_types)] #![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)] #![feature(arbitrary_self_types_pointers)]
#![feature(new_zeroed_alloc)] #![feature(new_zeroed_alloc)]
#![feature(type_alias_impl_trait)]
#![feature(never_type)] #![feature(never_type)]
#![feature(impl_trait_in_assoc_type)]
pub mod backend; pub mod backend;
mod capture_future; mod capture_future;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment