Skip to content

Commit

Permalink
feat: Add impl TryFrom<&OpType> for ops::* (#856)
Browse files Browse the repository at this point in the history
Co-authored-by: Agustín Borgna <[email protected]>
  • Loading branch information
doug-q and aborgna-q authored Mar 6, 2024
1 parent eef8972 commit dee2d1f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,25 @@ macro_rules! impl_op_ref_try_into {
impl OpType {
#[doc = "If is an instance of `" $Op "` return a reference to it."]
pub fn [<as_ $sname:snake>](&self) -> Option<&$Op> {
if let OpType::$Op(l) = self {
Some(l)
} else {
None
}
TryInto::<&$Op>::try_into(self).ok()
}

#[doc = "If is an instance of `" $Op "`."]
#[doc = "Returns `true` if the operation is an instance of `" $Op "`."]
pub fn [<is_ $sname:snake>](&self) -> bool {
self.[<as_ $sname:snake>]().is_some()
}
}

impl<'a> TryFrom<&'a OpType> for &'a $Op {
type Error = ();
fn try_from(optype: &'a OpType) -> Result<Self, Self::Error> {
if let OpType::$Op(l) = optype {
Ok(l)
} else {
Err(())
}
}
}
}
};
($Op:tt) => {
Expand Down

0 comments on commit dee2d1f

Please sign in to comment.