vrtbl/passerine

More Rust idioms

Plecra opened this issue · 7 comments

It's common (almost universal~) for Rust projects to use cargo fmt and cargo clippy to keep code predictable and easier to collaborate on.

I generally like cargo fmt, however one thing I'm not a fan of is the fact that it can't align match arms (IIRC):

match x {
    Some(y) => y,
    None    => 0,
}

As Passerine matches on enums a lot while compiling ASTs, I'd prefer rustfmt to do this. This is basically the only reason I haven't decided to use rustfmt yet.

As for clippy; I agree. Perhaps putting in a PR that enables clippy and ensures all the existing code is clippy-friendly might be a good first contribution.

I'll pop this here in case there's any movement rust-lang/rustfmt#894

Looks like putting #[rustfmt::skip] on the match works (now?):

impl Display for Data {
    /// Displays some Passerine Data in a pretty manner, as if it were printed to console.
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        #[rustfmt::skip]
        match self {
            Data::Heaped(_)   => unreachable!("Can not display heaped data"),
            Data::NotInit     => unreachable!("found uninitialized data on top of stack"),
            Data::Real(n)     => write!(f, "{}", n),
            Data::Integer(n)  => write!(f, "{}", n),
            Data::Boolean(b)  => write!(f, "{}", if *b { "true" } else { "false" }),
            Data::String(s)   => write!(f, "{}", s),
            Data::Lambda(_)   => unreachable!("Can not display naked functions"),
            Data::Closure(c)  => write!(f, "Function ~ {}", c.id),
            Data::Kind(_)     => unreachable!("Can not display naked labels"),
            Data::Label(n, v) => write!(f, "{} {}", n, v),
            Data::Unit        => write!(f, "()"),
            Data::Tuple(t)    => write!(f, "({})", t.iter()
                .map(|i| format!("{}", i))
                .collect::<Vec<String>>()
                .join(", ")
            ),
        }
    }
}

How do you feel about that as a compromise?

Sweet! I took a crack at it here: #57

Appreciated! I have a rustfmt.toml and GitHub Action ready to go. btw, there was a CI build issue due using #[...] in an experimental position, up to you as to how we should proceed 😄. Thanks again for putting this PR in.

I've just decided to go with rustfmt, imperfect as it may be. With @Kethku's clippy fixes from a while back, I think I can say that the usage of Rust idioms in this project has improved significantly. The bytecode verification function and the eventual move towards a strongly-typed type system should alleviate most of the unsafe concerns.