tamer: asg::graph::object: AsRef in place of higher-rank trait bound
We only need a reference to the inner object, for which `AsRef` is the proper and idiomatic solution. There is a lot of boilerplate here that I hope to reduce in the future. DEV-13597main
parent
b87c078894
commit
8739c2c570
|
@ -204,24 +204,26 @@ impl From<Object> for Expr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Object> for &'a Ident {
|
impl AsRef<Object> for Object {
|
||||||
/// Narrow an object into an [`Ident`],
|
fn as_ref(&self) -> &Object {
|
||||||
/// panicing if the object is not of that type.
|
self
|
||||||
fn from(val: &'a Object) -> Self {
|
}
|
||||||
match val {
|
}
|
||||||
Object::Ident(ident) => ident,
|
|
||||||
_ => val.narrowing_panic("an identifier"),
|
impl AsRef<Ident> for Object {
|
||||||
|
fn as_ref(&self) -> &Ident {
|
||||||
|
match self {
|
||||||
|
Object::Ident(ref ident) => ident,
|
||||||
|
_ => self.narrowing_panic("an identifier"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Object> for &'a Expr {
|
impl AsRef<Expr> for Object {
|
||||||
/// Narrow an object into an [`Expr`],
|
fn as_ref(&self) -> &Expr {
|
||||||
/// panicing if the object is not of that type.
|
match self {
|
||||||
fn from(val: &'a Object) -> Self {
|
Object::Expr(ref expr) => expr,
|
||||||
match val {
|
_ => self.narrowing_panic("an expression"),
|
||||||
Object::Expr(expr) => expr,
|
|
||||||
_ => val.narrowing_panic("an expression"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -235,10 +237,7 @@ impl<'a> From<&'a Object> for &'a Expr {
|
||||||
///
|
///
|
||||||
/// Note that [`Object`] is also an [`ObjectKind`],
|
/// Note that [`Object`] is also an [`ObjectKind`],
|
||||||
/// if you do not desire narrowing.
|
/// if you do not desire narrowing.
|
||||||
pub trait ObjectKind = Into<Object>
|
pub trait ObjectKind = Into<Object> where Object: Into<Self> + AsRef<Self>;
|
||||||
where
|
|
||||||
Object: Into<Self>,
|
|
||||||
for<'a> &'a Object: Into<&'a Self>;
|
|
||||||
|
|
||||||
/// Index representing an [`Object`] stored on the [`Asg`](super::Asg).
|
/// Index representing an [`Object`] stored on the [`Asg`](super::Asg).
|
||||||
///
|
///
|
||||||
|
@ -488,7 +487,10 @@ impl ObjectContainer {
|
||||||
pub fn get<O: ObjectKind>(&self) -> &O {
|
pub fn get<O: ObjectKind>(&self) -> &O {
|
||||||
let Self(container) = self;
|
let Self(container) = self;
|
||||||
|
|
||||||
container.as_ref().diagnostic_unwrap(container_oops).into()
|
container
|
||||||
|
.as_ref()
|
||||||
|
.diagnostic_unwrap(container_oops)
|
||||||
|
.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to modify the inner [`Object`],
|
/// Attempt to modify the inner [`Object`],
|
||||||
|
|
Loading…
Reference in New Issue