Skip to content

Remove RTreeObject trait bound on struct def. via default generic param. - #247

Open
mikwielgus wants to merge 1 commit into
georust:masterfrom
mikwielgus:remove-bounds-from-struct
Open

mikwielgus wants to merge 1 commit into
georust:masterfrom
mikwielgus:remove-bounds-from-struct

Conversation

@mikwielgus

@mikwielgus mikwielgus commented Sep 8, 2026

Copy link
Copy Markdown

Bounds on struct definitions, unlike bounds on functions, are usually unidiomatic in Rust, and it is usually possible to rewrite the code so that there are only bounds on functions, and nothing on the type definition itself. If you care about this, it becomes a contagious problem to rely on rstar in your own generic code, because the T: RTreeObject bound propagates upward.

For example,

pub struct Layout<T: RTreeObject> {
    objects: Vec<T>,
    rtree: RTree<T>,
}

must have an RTreeObject bound on T, even though this code's authors may not want to expose that to their downstream in their code's public interface.

(the above of course also applies to enums)

See also this Stack Overflow answer: https://stackoverflow.com/a/66369912

This PR is a proposal to solve this problem by adding an additional generic parameter E to rstar's types that defaults to RTreeObject::Envelope.

  • I agree to follow the project's code of conduct.
  • I added an entry to rstar/CHANGELOG.md if knowledge of this change could be valuable to users.

…aram.

Bounds on struct definitions, unlike bounds on functions, are usually
unidiomatic in Rust, and it is usually possible to rewrite the code
so that there are only bounds on functions, and nothing on the type
definition itself. If you care about this, it becomes a contagious
problem to rely on `rstar` in your own generic code, because the `T:
RTreeObject` bound propagates upward.

For example,

```
pub struct Layout<T: RTreeObject> {
    objects: Vec<T>,
    rtree: RTree<T>,
}
```

must have an `RTreeObject` bound on `T`, even though this code's authors
may not want to expose that to their downstream in their code's public
interface.

(the above of course also applies to enums)

See also this Stack Overflow answer:
https://stackoverflow.com/a/66369912

This PR is a proposal to solve this problem by adding an additional
generic parameter `E` to `rstar`'s types that defaults to
`RTreeObject::Envelope`.
@mikwielgus
mikwielgus force-pushed the remove-bounds-from-struct branch from a97085a to 45eedda Compare September 8, 2026 22:26
Comment thread rstar/src/node.rs
serde(bound(
serialize = "T: Serialize, T::Envelope: Serialize",
deserialize = "T: Deserialize<'de>, T::Envelope: Deserialize<'de>"
serialize = "T: Serialize, E: Serialize",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These special bounds can afaik be removed now, as they are equal to the default

Comment thread rstar/src/rtree.rs
serde(bound(
serialize = "T: Serialize, T::Envelope: Serialize",
deserialize = "T: Deserialize<'de>, T::Envelope: Deserialize<'de>"
serialize = "T: Serialize, E: Serialize",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Comment thread rstar/src/rtree.rs
))
)]
pub struct RTree<T, Params = DefaultParams>
pub struct RTree<T, Params = DefaultParams, E = <T as RTreeObject>::Envelope>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a generic type parameter that is not really a free parameter does not make sense IMHO. It suggests that RTree<T, Params, E> means something even if E = <T as RTreeObject>::Envelope does not hold and this just is not the case. Or at least it is definitely not something I would want to support in the public API of this crate.

Meaning this might be a reasonable trick for the implementation details of a crate, but not for its public API.

TechPizzaDev pushed a commit to TechPizzaDev/rstar-rs that referenced this pull request Sep 12, 2026
- [X] I agree to follow the project's [code of
conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [ ] I added an entry to `rstar/CHANGELOG.md` if knowledge of this
change could be valuable to users.
---

This has the same motivation as georust#247 (but is orthogonal/independent to
that), particularly that these trait bounds "bleed" into downstream
usage, e.g. in https://docs.rs/polygon_unionfind and
https://github.com/mikwielgus/anyangle .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants