Should trait bounds be duplicated in struct and impl?
Asked Answered
M

3

66

The following code uses a struct with generic type. While its implementation is only valid for the given trait bound, the struct can be defined with or without the same bound. The struct's fields are private so no other code could create an instance anyway.

trait Trait {
    fn foo(&self);
}

struct Object<T: Trait> {
    value: T,
}

impl<T: Trait> Object<T> {
    fn bar(object: Object<T>) {
        object.value.foo();
    }
}

Should the trait bound for the structure should be omitted to conform to the DRY principle, or should it be given to clarify the dependency? Or are there circumstances one solution should be preferred over the other?

Meitner answered 12/3, 2018 at 6:34 Comment(0)
S
16

It really depends on what the type is for. If it is only intended to hold values which implement the trait, then yes, it should have the trait bound e.g.

trait Child {
    fn name(&self);
}

struct School<T: Child> {
    pupil: T,
}

impl<T: Child> School<T> {
    fn role_call(&self) -> bool {
        // check everyone is here
    }
}

In this example, only children are allowed in the school so we have the bound on the struct.

If the struct is intended to hold any value but you want to offer extra behaviour when the trait is implemented, then no, the bound shouldn't be on the struct e.g.

trait GoldCustomer {
    fn get_store_points(&self) -> i32;
}

struct Store<T> {
    customer: T,
}

impl<T: GoldCustomer> Store {
    fn choose_reward(customer: T) {
        // Do something with the store points
    }
}

In this example, not all customers are gold customers and it doesn't make sense to have the bound on the struct.

Sidesaddle answered 12/3, 2018 at 8:48 Comment(0)
L
106

I believe that the existing answers are misleading. In most cases, you should not put a bound on a struct unless the struct literally will not compile without it.

tl;dr

Bounds on structs express the wrong thing for most people. They are infectious, redundant, sometimes nearsighted, and often confusing. Even when a bound feels right, you should usually leave it off until it's proven necessary.

(In this answer, anything I say about structs applies equally to enums.)


0. Bounds on structs have to be repeated everywhere the struct is

This is the most obvious, but (to me) least compelling reason to avoid writing bounds on structs. As of this writing (Rust 1.65), you have to repeat every struct's bounds on every impl that touches it, which is a good enough reason not to put bounds on structs for now. However, there is an accepted RFC (implied_bounds) which, when implemented and stabilized, will change this by inferring the redundant bounds. But even then, bounds on structs are still usually wrong:

1. Bounds on structs leak out of abstractions.

Your data structure is special. "Object<T> only makes sense if T is Trait," you say. And perhaps you are right. But the decision affects not just Object, but any other data structure that contains an Object<T>, even if it does not always contain an Object<T>. Consider a programmer who wants to wrap your Object in an enum:

enum MyThing<T> {  // error[E0277]: the trait bound `T: Trait` is not satisfied
    Wrapped(your::Object<T>),
    Plain(T),
}

Within the downstream code this makes sense because MyThing::Wrapped is only used with Ts that do implement Thing, while Plain can be used with any type. But if your::Object<T> has a bound on T, this enum can't be compiled without that same bound, even if there are lots of uses for a Plain(T) that don't require such a bound. Not only does this not work, but even if adding the bound doesn't make it entirely useless, it also exposes the bound in the public API of any struct that happens to use MyThing.

Bounds on structs limit what other people can do with them. Bounds on code (impls and functions) do too, of course, but those constraints are (presumably) required by your own code, while bounds on structs are a preemptive strike against anyone downstream who might use your struct in an innovative way. This may be useful, but unnecessary bounds are particularly annoying for innovators because they constrain what can compile without usefully constraining what can actually run (more on that in a moment).

2. Bounds on structs are redundant with bounds on code.

So you don't think downstream innovation is possible? That doesn't mean the struct itself needs a bound. To make it impossible to construct an Object<T> without T: Trait, it is enough to put that bound on the impl that contains Object's constructor(s); if it's impossible to call a_method on an Object<T> without T: Trait you can say that on the impl that contains a_method, or perhaps on a_method itself. (Until implied_bounds is implemented, you have to, anyway, so you don't even have the weak justification of "saving keystrokes.")

Even and especially when you can't think of any way for downstream to use an un-bounded Object<T>, you should not forbid it a priori, because...

3. Bounds on structs mean something different to the type system than bounds on code.

A T: Trait bound on Object<T> means more than "all Object<T>s have to have T: Trait"; it actually means something like "the concept of Object<T> itself does not make sense unless T: Trait", which is a more abstract idea. Think about natural language: I've never seen a purple elephant, but I can easily name the concept of "purple elephant" despite the fact that it corresponds to no real-world animal. Types are a kind of language and it can make sense to refer to the idea of Elephant<Purple>, even when you don't know how to create one and you certainly have no use for one. Similarly, it can make sense to express the type Object<NotTrait> in the abstract even if you don't and can't have one in hand right now. Especially when NotTrait is a type parameter, which may not be known in this context to implement Trait but in some other context does.

Case study: Cell<T>

For one example of a struct that originally had a trait bound which was eventually removed, look no farther than Cell<T>, which originally had a T: Copy bound. In the RFC to remove the bound many people initially made the same kinds of arguments you may be thinking of right now, but the eventual consensus was that "Cell requires Copy" was always the wrong way to think about Cell. The RFC was merged, paving the way for innovations like Cell::as_slice_of_cells, which lets you do things you couldn't before in safe code, including temporarily opt-in to shared mutation. The point is that T: Copy was never a useful bound on Cell<T>, and it would have done no harm (and possibly some good) to leave it off from the beginning.

This kind of abstract constraint can be hard to wrap one's head around, which is probably one reason why it's so often misused. Which relates to my last point:

4. Unnecessary bounds invite unnecessary parameters (which are worse).

This does not apply to all cases of bounds on structs, but it is a common point of confusion. You may, for instance, have a struct with a type parameter that should implement a generic trait, but not know what parameter(s) the trait should take. In such cases it is tempting to use PhantomData to add a type parameter to the main struct, but this is usually a mistake, not least because PhantomData is hard to use correctly. Here are some examples of unnecessary parameters added because of unnecessary bounds: 1 2 3 4 5 In the majority of such cases, the correct solution is simply to remove the bound.

Exceptions to the rule

Okay, when do you need a bound on a struct? I can think of two possible reasons.

  • In Shepmaster's answer, the struct will simply not compile without a bound, because the Iterator implementation for I actually defines what the struct contains. One other way that a struct won't compile without a bound is when its implementation of Drop has to use the trait somehow. Drop can't have bounds that aren't on the struct, for soundness reasons, so you have to write them on the struct as well.

  • When you're writing unsafe code and you want it to rely on a bound (T: Send, for example), you might need to put that bound on the struct. unsafe code is special because it can rely on invariants that are guaranteed by non-unsafe code, so just putting the bound on the impl that contains the unsafe is not necessarily enough.

But in all other cases, unless you really know what you're doing, you should avoid bounds on structs entirely.

Limit answered 25/2, 2021 at 13:54 Comment(9)
A sound and compelling answer which has convinced me to never use bounds in either structs or enums.Fatigue
If we need to implement Drop for the struct, it seems we have to bound generics on the struct? Also an exception?Creuse
@Creuse not sure why you would. Fields are implicitly dropped even if you're writing your own Drop impl. std::mem::drop() also works on any value, regardless of whether it is Drop or not.Unfeigned
@Unfeigned I mean in this case playgroundCreuse
@Creuse hmm I see, looks like doc.rust-lang.org/std/io/struct.BufWriter.html does this. Luckily this case (a type's drop impl needs to call a trait method) is pretty rare.Unfeigned
BTW I personally lean toward making drop lean and having an explicit cleanup method that takes self by value. If it's absolutely required that this cleanup is performed, you can require this is called and make drop unconditionally panicUnfeigned
Re 3: IMHO ignores the case when Object<T> indeed does not make sense as a concept. Elephant<COLOR> only makes sense if COLOR: VisibleColor because even talking about an Elephant<Ultraviolet> isn't meaningful. What is an UV elephant? Deriving from other colors, it means either that the elephant is perceived as the color UV -- but UV cannot be perceived -- or that it only reflects the UV part of the spectrum, but then it would be black.Benyamin
The Cell<T> example also applies only to at-least-somewhat general, re-usable types. For an ApplicationSpecificType<T>, bounds on T reflect you current understanding of what that type means, and if that understanding ever changes, you change the code. If you try to preemptively consider all possible uses of the type, YAGNI applies.Benyamin
These are some good reasons that one doesn't like the idea of struct trait bound; but to call it infectious, nearsighted or confusing are probably more subjective than it actually is. struct trait bound is to be correctly used and there are cases that we probably should try not to use it. Now with RFC-2089 merged it'd make it less repetitive. There are also good explanation for 3, which is simply something one should read before write generic code in rust, that inheritance and supertrait are different.Strict
P
20

Trait bounds that apply to every instance of the struct should be applied to the struct:

struct IteratorThing<I>
where
    I: Iterator,
{
    a: I,
    b: Option<I::Item>,
}

Trait bounds that only apply to certain instances should only be applied to the impl block they pertain to:

struct Pair<T> {
    a: T,
    b: T,
}

impl<T> Pair<T>
where
    T: std::ops::Add<T, Output = T>,
{
    fn sum(self) -> T {
        self.a + self.b
    }
}

impl<T> Pair<T>
where
    T: std::ops::Mul<T, Output = T>,
{
    fn product(self) -> T {
        self.a * self.b
    }
}

to conform to the DRY principle

The redundancy will be removed by RFC 2089:

Eliminate the need for “redundant” bounds on functions and impls where those bounds can be inferred from the input types and other trait bounds. For example, in this simple program, the impl would no longer require a bound, because it can be inferred from the Foo<T> type:

struct Foo<T: Debug> { .. }
impl<T: Debug> Foo<T> {
  //    ^^^^^ this bound is redundant
  ...
}
Pesach answered 12/3, 2018 at 17:20 Comment(2)
The RFC is the answer to the redundancy that struck me in the first place.Meitner
@Meitner yep, I'm in the same boat. Previously, I tended to not put the bounds on the struct because of that (even to my own detriment). It will be interesting to see if removing the requirement to type the same bounds in both places will make people more or less confused. After the RFC, now the code impl<T> Foo<T> { ... } might have a whole bunch of restrictions on it that you won't know unless you look at the struct...Pesach
S
16

It really depends on what the type is for. If it is only intended to hold values which implement the trait, then yes, it should have the trait bound e.g.

trait Child {
    fn name(&self);
}

struct School<T: Child> {
    pupil: T,
}

impl<T: Child> School<T> {
    fn role_call(&self) -> bool {
        // check everyone is here
    }
}

In this example, only children are allowed in the school so we have the bound on the struct.

If the struct is intended to hold any value but you want to offer extra behaviour when the trait is implemented, then no, the bound shouldn't be on the struct e.g.

trait GoldCustomer {
    fn get_store_points(&self) -> i32;
}

struct Store<T> {
    customer: T,
}

impl<T: GoldCustomer> Store {
    fn choose_reward(customer: T) {
        // Do something with the store points
    }
}

In this example, not all customers are gold customers and it doesn't make sense to have the bound on the struct.

Sidesaddle answered 12/3, 2018 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.