mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +00:00

This PR productionizes @MichaReiser's suggestion in https://github.com/charliermarsh/ruff/issues/1820#issuecomment-1440204423, by creating a separate crate for the `ast` module (`rust_python_ast`). This will enable us to further split up the `ruff` crate, as we'll be able to create (e.g.) separate sub-linter crates that have access to these common AST utilities. This was mostly a straightforward copy (with adjustments to module imports), as the few dependencies that _did_ require modifications were handled in #3366, #3367, and #3368.
40 lines
913 B
Rust
40 lines
913 B
Rust
use std::hash::Hash;
|
|
|
|
use rustpython_parser::ast::Expr;
|
|
|
|
use crate::comparable::ComparableExpr;
|
|
|
|
/// Wrapper around `Expr` that implements `Hash` and `PartialEq`.
|
|
pub struct HashableExpr<'a>(&'a Expr);
|
|
|
|
impl Hash for HashableExpr<'_> {
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
let comparable = ComparableExpr::from(self.0);
|
|
comparable.hash(state);
|
|
}
|
|
}
|
|
|
|
impl PartialEq<Self> for HashableExpr<'_> {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
let comparable = ComparableExpr::from(self.0);
|
|
comparable == ComparableExpr::from(other.0)
|
|
}
|
|
}
|
|
|
|
impl Eq for HashableExpr<'_> {}
|
|
|
|
impl<'a> From<&'a Expr> for HashableExpr<'a> {
|
|
fn from(expr: &'a Expr) -> Self {
|
|
Self(expr)
|
|
}
|
|
}
|
|
|
|
impl<'a> HashableExpr<'a> {
|
|
pub const fn from_expr(expr: &'a Expr) -> Self {
|
|
Self(expr)
|
|
}
|
|
|
|
pub const fn as_expr(&self) -> &'a Expr {
|
|
self.0
|
|
}
|
|
}
|