georust / geo

Geospatial primitives and algorithms for Rust
https://crates.io/crates/geo
Other
1.49k stars 194 forks source link

Borrowed rstar CachedEnvelope container results in un-ergonomic code #1086

Open urschrei opened 9 months ago

urschrei commented 9 months ago

I was moving some geometries into a CachedEnvelope, and while fixing up the types found the following:

let p1: Point = (0., 1.).into();
let ce = CachedEnvelope::new(p1);
let bce = &ce; // pretend we're getting this out of a lending iterator
let p2: Point = (1., 1.).into();

p2.intersects(bce); // doesn't work
p2.intersects(*bce); // doesn't work
p2.intersects(**bce); // doesn't work
p2.intersects(&**bce); // FINALLY but look at that

Rust-Analyzer wasn't helpful here (which is fine) – you just "have to know" that you have to double-deref and then borrow again. Lots of people don't even know that you can double-deref.

Error messages in same order as above ``` error[E0277]: the trait bound `geo_types::Line: algorithm::intersects::Intersects>>` is not satisfied --> geo/src/algorithm/simplify_vw.rs:429:39 | 429 | && new_segment.intersects(candidate) | ---------- ^^^^^^^^^ the trait `algorithm::intersects::Intersects>>` is not implemented for `geo_types::Line` | | | required by a bound introduced by this call error[E0308]: mismatched types --> geo/src/algorithm/simplify_vw.rs:429:39 | 429 | && new_segment.intersects(*candidate) | ---------- ^^^^^^^^^^ expected `&_`, found `CachedEnvelope>` | | | arguments to this method are incorrect | = note: expected reference `&_` found struct `CachedEnvelope>` error[E0308]: mismatched types --> geo/src/algorithm/simplify_vw.rs:429:39 | 429 | && new_segment.intersects(**candidate) | ---------- ^^^^^^^^^^^ expected `&_`, found `Line` | | | arguments to this method are incorrect ```
Anyway, I'm not sure what we can do about this – `CachedEnvelope` already impls `Deref`.