Skip to content

Commit

Permalink
"checked" conversions from u16 <-> Op
Browse files Browse the repository at this point in the history
  • Loading branch information
enricozb committed Apr 4, 2024
1 parent edbd751 commit fcf74e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
28 changes: 16 additions & 12 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,6 @@ pub struct Op {
pub op: IntOp,
}

impl From<u16> for Op {
fn from(value: u16) -> Self {
unsafe { std::mem::transmute(value) }
}
}

impl From<Op> for u16 {
fn from(op: Op) -> Self {
unsafe { std::mem::transmute(op) }
}
}

impl Display for Op {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.ty {
Expand All @@ -184,6 +172,22 @@ impl Display for Op {
}
}

impl TryFrom<u16> for Op {
type Error = ();

fn try_from(value: u16) -> Result<Self, Self::Error> {
let [ty, op] = value.to_be_bytes();

Ok(Self { ty: Ty::try_from(ty)?, op: IntOp::try_from(op)? })
}
}

impl From<Op> for u16 {
fn from(op: Op) -> Self {
(op.ty as u16) << 8 | op.op as u16
}
}

impl FromStr for Op {
type Err = ();

Expand Down
13 changes: 4 additions & 9 deletions src/run/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<'a, M: Mode> Net<'a, M> {
/// Annihilates two binary agents.
///
/// ```text
///
///
/// a2 | | a1
/// _|___|_
/// \ /
Expand All @@ -68,7 +68,6 @@ impl<'a, M: Mode> Net<'a, M> {
/// / \
/// | |
/// b1 | | b2
///
/// ```
#[inline(never)]
pub fn anni2(&mut self, a: Port, b: Port) {
Expand All @@ -83,7 +82,7 @@ impl<'a, M: Mode> Net<'a, M> {
/// Commutes two binary agents.
///
/// ```text
///
///
/// a2 | | a1
/// _|___|_
/// \ /
Expand Down Expand Up @@ -115,7 +114,6 @@ impl<'a, M: Mode> Net<'a, M> {
/// \ / \ /
/// | |
/// b1 | | b2
///
/// ```
#[inline(never)]
pub fn comm22(&mut self, a: Port, b: Port) {
Expand Down Expand Up @@ -146,7 +144,7 @@ impl<'a, M: Mode> Net<'a, M> {
/// Commutes a nilary agent and a binary agent.
///
/// ```text
///
///
/// a (---)
/// |
/// |
Expand All @@ -161,7 +159,6 @@ impl<'a, M: Mode> Net<'a, M> {
/// a (---) (---) a
/// | |
/// b1 | | b2
///
/// ```
#[inline(never)]
pub fn comm02(&mut self, a: Port, b: Port) {
Expand Down Expand Up @@ -201,7 +198,6 @@ impl<'a, M: Mode> Net<'a, M> {
/// | | | | |
/// a1 | | a2 | a1 | | a2
/// |
///
/// ```
#[inline(never)]
pub fn mat_num(&mut self, a: Port, b: Port) {
Expand Down Expand Up @@ -251,13 +247,12 @@ impl<'a, M: Mode> Net<'a, M> {
/// | | | |
/// | a2 | a1 | | a2
/// |
///
/// ```
#[inline(never)]
pub fn op_num(&mut self, a: Port, b: Port) {
trace!(self.tracer, a, b);
let a = a.consume_node();
let op = Op::from(a.lab);
let op = Op::try_from(a.lab).unwrap();
let a1 = a.p1.load_target();
if a1.tag() == Num {
self.rwts.oper += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/run/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Port {
/// ports.
#[inline(always)]
pub fn op(&self) -> Op {
self.lab().into()
self.lab().try_into().unwrap()
}

/// Accesses the numeric value of this port; this is valid for [`Num`] ports.
Expand Down

0 comments on commit fcf74e6

Please sign in to comment.