@@ -1,4 +1,4 @@
1−use std::{cell::Cell, cmp::Ordering, rc::Rc, str::FromStr};
1+use std::{cell::Cell, cmp::Ordering, fmt::Write, rc::Rc};
22
33use num_bigint::BigUint;
44use std::fmt;
@@ -31,40 +31,12 @@ impl PartialOrd for Noun {
3131}
3232
3333fn compare_nouns(p: &Noun, q: &Noun) -> Ordering {
34− match (p, q) {
35− (Noun::Atom(_), Noun::Cell { .. }) => Ordering::Less,
36− (Noun::Cell { .. }, Noun::Atom(_)) => Ordering::Greater,
37− (Noun::Atom(x), Noun::Atom(y)) => x.cmp(y),
38− (
39− Noun::Cell {
40− p: lhs_p, q: lhs_q, ..
41− },
42− Noun::Cell {
43− p: rhs_p, q: rhs_q, ..
44− },
45− ) => (lhs_p, lhs_q).cmp(&(rhs_p, rhs_q)),
46− }
34+ p.hash().cmp(&q.hash())
4735}
4836
4937impl PartialEq for Noun {
5038 fn eq(&self, other: &Self) -> bool {
51− match (self, other) {
52− (Noun::Atom(lhs), Noun::Atom(rhs)) => lhs == rhs,
53−
54− (
55− Noun::Cell {
56− p: lhs_p,
57− q: lhs_q,
58− hash: _lhs_hash,
59− },
60− Noun::Cell {
61− p: rhs_p,
62− q: rhs_q,
63− hash: _rhs_hash,
64− },
65− ) => self.hash() == other.hash() && lhs_p == rhs_p && lhs_q == rhs_q,
66− _ => false,
67− }
39+ self.hash().eq(&other.hash())
6840 }
6941}
7042
@@ -331,24 +303,32 @@ impl<'a> Iterator for NounListIterator<'a> {
331303
332304impl fmt::Display for Noun {
333305 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
334− f.write_str(print_noun(Rc::new(self.clone()), false).as_str())
306+ write_noun(f, self, false)
335307 }
336308}
337309
338−fn print_noun(noun: Rc<Noun>, is_rhs: bool) -> String {
339− match (*noun).clone() {
310+fn write_noun(f: &mut fmt::Formatter, noun: &Noun, is_rhs: bool) -> fmt::Result {
311+ match noun {
340312 Noun::Cell { p, q, .. } if is_rhs => {
341− format!("{} {}", print_noun(p, false), print_noun(q, true))
313+ write_noun(f, p, false)?;
314+ f.write_char(' ')?;
315+ write_noun(f, q, true)
316+ }
317+ Noun::Cell { p, q, .. } => {
318+ f.write_char('[')?;
319+ write_noun(f, p, false)?;
320+ f.write_char(' ')?;
321+ write_noun(f, q, true)?;
322+ f.write_char(']')
342323 }
343− Noun::Cell { p, q, .. } => format!("[{} {}]", print_noun(p, false), print_noun(q, true)),
344324 Noun::Atom(a) => {
345325 let atom_bytes = a.to_bytes_le();
346326 if atom_bytes.len() > 1 && atom_bytes.into_iter().all(|c| (c > 33) && c < 126) {
347− format!("%{}", unsafe {
348− String::from_utf8_unchecked(a.to_bytes_le())
349− })
350− } else if a == BigUint::ZERO {
351− String::from_str("~").unwrap()
327+ f.write_char('%')?;
328+ let cord = unsafe { String::from_utf8_unchecked(a.to_bytes_le()) };
329+ f.write_str(&*cord)
330+ } else if a == &BigUint::ZERO {
331+ f.write_char('~')
352332 } else {
353333 let mut result = String::new();
354334 let mut counter = 0;
@@ -360,8 +340,7 @@ fn print_noun(noun: Rc<Noun>, is_rhs: bool) -> String {
360340 counter += 1;
361341 result.push(char);
362342 }
363− let foo: String = result.chars().rev().collect();
364− foo
343+ f.write_str(&*result.chars().rev().collect::<String>())
365344 }
366345 }
367346 }