In rust-clippy, abbiamo la funzione following:Come scrivere una funzione generica su un tipo di puntatore?
fn over<X, F>(left: &[X], right: &[X], mut eq_fn: F) -> bool
where F: FnMut(&X, &X) -> bool {
left.len() == right.len() && left.iter().zip(right).all(|(x, y)|
eq_fn(x, y))
}
Si dà il caso, la rappresentazione AST rustc
s' utilizza un sacco di syntax::ptr::P<T>
puntatori. Quei dereferenziamento a T, e così autodeposto, li costringe implicitamente a &T
se usiamo una chiusura. Se cerchiamo di usare una pianura fn
tuttavia, otteniamo un tipo non corrispondente:
error: type mismatch: the type `fn(&syntax::ast::Expr, &syntax::ast::Expr) -> bool {eq_op::is_exp_equal}` implements the trait `for<'r, 'r> core::ops::FnMut<(&'r syntax::ast::Expr, &'r syntax::ast::Expr)>`, but the trait `for<'r, 'r> core::ops::FnMut<(&'r syntax::ptr::P<syntax::ast::Expr>, &'r syntax::ptr::P<syntax::ast::Expr>)>` is required (expected struct `syntax::ptr::P`, found struct `syntax::ast::Expr`) [E0281]
Posso cambiare la funzione di cui sopra ad accettare sia &[&T]
e &[P<T>]
e automaticamente costringere P<Expr>
in &Expr
? Se é cosi, come?