tamer: asg::graph::AsgRelMut: API cleanup

This does two things:

  1. Removes callback; it didn't add anything of practical value.
     The operation will simply be performed as long as no error is provided
     by the callee.
  2. Consolodates three arguments into `ProposedRel`.  This makes blocks in
     `object_rel!` less verbose and boilerplate-y.

I'll probably implement `TplShape::Unknown` via the dynamic `Ident` `Tpl`
edge before continuing with any cleanup.  This is getting pretty close to
reasonable for future implementations.

DEV-13163
main
Mike Gerwitz 2023-07-27 03:33:06 -04:00
parent 579575a358
commit d889aca13a
3 changed files with 39 additions and 48 deletions

View File

@ -211,8 +211,8 @@ impl Asg {
to_oi: ObjectIndex<OB>, to_oi: ObjectIndex<OB>,
ctx_span: Option<Span>, ctx_span: Option<Span>,
) -> Result<(), AsgError> { ) -> Result<(), AsgError> {
from_oi.pre_add_edge(self, to_oi, ctx_span, |asg| { from_oi.pre_add_edge(self, to_oi, ctx_span).map(|()| {
asg.graph.add_edge( self.graph.add_edge(
from_oi.widen().into(), from_oi.widen().into(),
to_oi.into(), to_oi.into(),
(from_oi.src_rel_ty(), OB::rel_ty(), ctx_span), (from_oi.src_rel_ty(), OB::rel_ty(), ctx_span),
@ -511,14 +511,11 @@ pub trait AsgRelMut<OB: ObjectRelatable>: ObjectRelatable {
/// type for you. /// type for you.
fn pre_add_edge( fn pre_add_edge(
asg: &mut Asg, asg: &mut Asg,
_from_oi: ObjectIndex<Self>, rel: ProposedRel<Self, OB>,
_to_oi: ObjectIndex<OB>,
_ctx_span: Option<Span>,
commit: impl FnOnce(&mut Asg),
) -> Result<(), AsgError>; ) -> Result<(), AsgError>;
} }
impl<O: ObjectRelatable, OB: ObjectRelatable> AsgRelMut<OB> for O { impl<OA: ObjectRelatable, OB: ObjectRelatable> AsgRelMut<OB> for OA {
/// Default edge creation method for all [`ObjectKind`]s. /// Default edge creation method for all [`ObjectKind`]s.
/// ///
/// This takes the place of a default implementation on the trait itself /// This takes the place of a default implementation on the trait itself
@ -531,15 +528,21 @@ impl<O: ObjectRelatable, OB: ObjectRelatable> AsgRelMut<OB> for O {
/// overridden for a particular edge for a particular object /// overridden for a particular edge for a particular object
/// (see [`object::tpl`] as an example). /// (see [`object::tpl`] as an example).
default fn pre_add_edge( default fn pre_add_edge(
asg: &mut Asg, _asg: &mut Asg,
_from_oi: ObjectIndex<O>, _rel: ProposedRel<Self, OB>,
_to_oi: ObjectIndex<OB>,
_ctx_span: Option<Span>,
commit: impl FnOnce(&mut Asg),
) -> Result<(), AsgError> { ) -> Result<(), AsgError> {
Ok(commit(asg)) let _ = _rel.ctx_span; // TODO: remove when used (dead_code)
Ok(())
} }
} }
/// The relationship proposed by [`Asg::add_edge`],
/// requiring approval from [`AsgRelMut::pre_add_edge`].
pub struct ProposedRel<OA: ObjectKind, OB: ObjectKind> {
from_oi: ObjectIndex<OA>,
to_oi: ObjectIndex<OB>,
ctx_span: Option<Span>,
}
#[cfg(test)] #[cfg(test)]
mod test; mod test;

View File

@ -27,7 +27,7 @@ use super::{
}; };
use crate::{ use crate::{
asg::{ asg::{
graph::{object::Tpl, AsgRelMut}, graph::{object::Tpl, AsgRelMut, ProposedRel},
Asg, AsgError, Asg, AsgError,
}, },
f::Map, f::Map,
@ -864,7 +864,6 @@ pub trait ObjectIndexRelTo<OB: ObjectRelatable>: Sized + Clone + Copy {
asg: &mut Asg, asg: &mut Asg,
to_oi: ObjectIndex<OB>, to_oi: ObjectIndex<OB>,
ctx_span: Option<Span>, ctx_span: Option<Span>,
commit: impl FnOnce(&mut Asg),
) -> Result<(), AsgError>; ) -> Result<(), AsgError>;
/// Check whether an edge exists from `self` to `to_oi`. /// Check whether an edge exists from `self` to `to_oi`.
@ -948,14 +947,14 @@ where
asg: &mut Asg, asg: &mut Asg,
to_oi: ObjectIndex<OB>, to_oi: ObjectIndex<OB>,
ctx_span: Option<Span>, ctx_span: Option<Span>,
commit: impl FnOnce(&mut Asg),
) -> Result<(), AsgError> { ) -> Result<(), AsgError> {
O::pre_add_edge( O::pre_add_edge(
asg, asg,
self.widen().must_narrow_into::<O>(), ProposedRel {
to_oi, from_oi: self.widen().must_narrow_into::<O>(),
ctx_span, to_oi,
commit, ctx_span,
},
) )
} }
} }
@ -976,16 +975,16 @@ impl<OB: ObjectRelatable> ObjectIndexRelTo<OB> for ObjectIndexTo<OB> {
asg: &mut Asg, asg: &mut Asg,
to_oi: ObjectIndex<OB>, to_oi: ObjectIndex<OB>,
ctx_span: Option<Span>, ctx_span: Option<Span>,
commit: impl FnOnce(&mut Asg),
) -> Result<(), AsgError> { ) -> Result<(), AsgError> {
macro_rules! pre_add_edge { macro_rules! pre_add_edge {
($ty:ident) => { ($ty:ident) => {
$ty::pre_add_edge( $ty::pre_add_edge(
asg, asg,
self.widen().must_narrow_into::<$ty>(), ProposedRel {
to_oi, from_oi: self.widen().must_narrow_into::<$ty>(),
ctx_span, to_oi,
commit, ctx_span,
},
) )
}; };
} }
@ -1020,10 +1019,9 @@ impl<OB: ObjectRelatable> ObjectIndexRelTo<OB> for ObjectIndexToTree<OB> {
asg: &mut Asg, asg: &mut Asg,
to_oi: ObjectIndex<OB>, to_oi: ObjectIndex<OB>,
ctx_span: Option<Span>, ctx_span: Option<Span>,
commit: impl FnOnce(&mut Asg),
) -> Result<(), AsgError> { ) -> Result<(), AsgError> {
match self { match self {
Self(oito) => oito.pre_add_edge(asg, to_oi, ctx_span, commit), Self(oito) => oito.pre_add_edge(asg, to_oi, ctx_span),
} }
} }
} }

View File

@ -22,7 +22,7 @@
use std::fmt::Display; use std::fmt::Display;
use super::{prelude::*, Doc, Expr, Ident}; use super::{prelude::*, Doc, Expr, Ident};
use crate::{f::Map, parse::util::SPair, span::Span}; use crate::{asg::graph::ProposedRel, f::Map, parse::util::SPair, span::Span};
/// Template with associated name. /// Template with associated name.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
@ -220,20 +220,15 @@ object_rel! {
tree Expr { tree Expr {
fn pre_add_edge( fn pre_add_edge(
asg: &mut Asg, asg: &mut Asg,
from_oi: ObjectIndex<Self>, rel: ProposedRel<Self, Expr>,
to_oi: ObjectIndex<Expr>,
_ctx_span: Option<Span>,
commit: impl FnOnce(&mut Asg),
) -> Result<(), AsgError> { ) -> Result<(), AsgError> {
let tpl_name = from_oi.name(asg); let tpl_name = rel.from_oi.name(asg);
let span = to_oi.resolve(asg).span(); let span = rel.to_oi.resolve(asg).span();
from_oi.try_map_obj_inner( rel.from_oi.try_map_obj_inner(
asg, asg,
try_adapt_to(TplShape::Expr(span), tpl_name), try_adapt_to(TplShape::Expr(span), tpl_name),
)?; ).map(|_| ())
Ok(commit(asg))
} }
}, },
@ -245,23 +240,18 @@ object_rel! {
tree Tpl { tree Tpl {
fn pre_add_edge( fn pre_add_edge(
asg: &mut Asg, asg: &mut Asg,
from_oi: ObjectIndex<Self>, rel: ProposedRel<Self, Tpl>,
to_oi: ObjectIndex<Tpl>,
_ctx_span: Option<Span>,
commit: impl FnOnce(&mut Asg),
) -> Result<(), AsgError> { ) -> Result<(), AsgError> {
let tpl_name = from_oi.name(asg); let tpl_name = rel.from_oi.name(asg);
let apply = to_oi.resolve(asg); let apply = rel.to_oi.resolve(asg);
let apply_shape = apply let apply_shape = apply
.shape() .shape()
.overwrite_span_if_any(apply.span()); .overwrite_span_if_any(apply.span());
from_oi.try_map_obj_inner( rel.from_oi.try_map_obj_inner(
asg, asg,
try_adapt_to(apply_shape, tpl_name), try_adapt_to(apply_shape, tpl_name),
)?; ).map(|_| ())
Ok(commit(asg))
} }
}, },