Skip to content

Commit

Permalink
get rid of Deref antipattern
Browse files Browse the repository at this point in the history
  • Loading branch information
tsionyx committed Dec 1, 2024
1 parent 632e92a commit c6ed468
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
21 changes: 4 additions & 17 deletions src/connective/truth_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! that the table represents (for example, A XOR B).
//! Each row of the truth table contains one possible configuration of the input variables
//! (for instance, A=true, B=false), and the result of the operation for those values.
use std::{fmt, ops::Deref};
use std::fmt;

use crate::{arity::two_powers::D, utils::dependent_array::CheckedStorage, CheckedArray};

Expand Down Expand Up @@ -67,7 +67,7 @@ where
where
<D as CheckedArray<ARITY>>::Array<Row<ARITY>>: Clone,
{
let v = self.table.deref().clone().into();
let v = self.table.clone().into_inner();
v.into_iter().map(|(_k, v)| v).collect()
}

Expand Down Expand Up @@ -100,17 +100,6 @@ where
}
}

impl<const ARITY: usize> Deref for TruthTable<ARITY>
where
D: CheckedArray<ARITY>,
{
type Target = <D as CheckedArray<ARITY>>::Array<Row<ARITY>>;

fn deref(&self) -> &Self::Target {
&self.table
}
}

impl<const ARITY: usize> fmt::Display for TruthTable<ARITY>
where
D: CheckedArray<ARITY>,
Expand All @@ -132,7 +121,7 @@ where
}
write!(f, "|")?;

let rows = self.table.deref().clone().into_iter();
let rows = self.table.clone().into_inner();
for (args, res) in rows {
writeln!(f)?;
for arg in args {
Expand All @@ -153,7 +142,6 @@ mod tests {
where
Op: TruthFn<ARITY>,
D: CheckedArray<ARITY>,
<D as CheckedArray<ARITY>>::Array<Row<ARITY>>: Clone,
{
get_mapping::<Op, ARITY>()
.into_iter()
Expand All @@ -165,10 +153,9 @@ mod tests {
where
Op: TruthFn<ARITY>,
D: CheckedArray<ARITY>,
<D as CheckedArray<ARITY>>::Array<Row<ARITY>>: Clone,
{
let table = Op::init().get_truth_table();
table.clone().into()
table.table.into_inner().into()
}

#[test]
Expand Down
12 changes: 6 additions & 6 deletions src/formula/connective.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{any::Any, fmt::Debug, ops::Deref};
use std::{any::Any, fmt::Debug};

use derive_where::derive_where;
use dyn_clone::{clone_trait_object, DynClone};
Expand Down Expand Up @@ -163,11 +163,11 @@ impl<const ARITY: usize, Atom> DynConnective<ARITY, Atom> {
}
}

impl<const ARITY: usize, Atom> Deref for DynConnective<ARITY, Atom> {
type Target = dyn Connective<ARITY>;

fn deref(&self) -> &Self::Target {
self.0.as_ref().up()
impl<'a, const ARITY: usize, Atom: 'a> AsRef<dyn Connective<ARITY> + 'a>
for DynConnective<ARITY, Atom>
{
fn as_ref(&self) -> &(dyn Connective<ARITY> + 'a) {
self.0.up()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/formula/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
E::Partial(f) => operator.compose([f]).into(),
E::Terminal(val) => {
// should be unreachable
E::Terminal(operator.eval([val]))
E::Terminal(operator.as_ref().eval([val]))
}
}
})
Expand All @@ -115,7 +115,7 @@ where
}
(E::Terminal(val1), E::Terminal(val2)) => {
// should be unreachable
E::Terminal(operator.eval([val1, val2]))
E::Terminal(operator.as_ref().eval([val1, val2]))
}
}
})
Expand Down
12 changes: 7 additions & 5 deletions src/formula/formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,27 @@ where
let conn = self.get_connective();

match &conn {
AnyConnective::Nullary(operator) => write!(f, "{}", operator.notation()),
AnyConnective::Nullary(operator) => write!(f, "{}", operator.as_ref().notation()),
AnyConnective::Unary { operator, operand } => {
let notation = operator.as_ref().notation();
if operand.has_obvious_priority_over(self) || operand.has_same_operation(self) {
write!(f, "{}{}", operator.notation(), operand)
write!(f, "{notation}{operand}")
} else {
write!(f, "{}({})", operator.notation(), operand)
write!(f, "{notation}({operand})")
}
}
AnyConnective::Binary {
operator,
operands: (op1, op2),
} => {
let is_associative = operator.is_associative();
let notation = operator.as_ref().notation();
if op1.has_obvious_priority_over(self)
|| (is_associative && op1.has_same_operation(self))
{
write!(f, "{op1}{}", operator.notation())
write!(f, "{op1}{notation}")
} else {
write!(f, "({op1}){}", operator.notation())
write!(f, "({op1}){notation}")
}?;
if op2.has_obvious_priority_over(self)
|| (is_associative && op2.has_same_operation(self))
Expand Down

0 comments on commit c6ed468

Please sign in to comment.