Skip to content

Discourage trait bounds on data structures unless necessary #6

Closed
@dtolnay

Description

@dtolnay

Example from flate2:

pub struct EncoderReader<R: Read> {
    inner: EncoderReaderBuf<BufReader<R>>,
}

Bounds like this are annoying because they transitively infect any structs containing this type. Usually trait bounds should go just on impl blocks, not on data structures.

There are two exceptions where trait bounds on data structures are required.

  1. The data structure refers to an associated type of the trait. Cow is an example of this.
  2. The data structure has a Drop impl that requires trait bounds. Rust currently requires all bounds on the Drop impl to also be present on the data structure.

Relevant flate2 issue: rust-lang/flate2-rs#88

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions