# Bounds for default method implementations

**URL:** <https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895>\
**Category:** language design\
**Created:** [June 18, 2021, 1:56pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895 "2021-06-18T13:56:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://internals.rust-lang.org/u/2e71828)\
**Post date:** [June 18, 2021, 1:56pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/1 "2021-06-18T13:56:22Z")

</div>

When writing trait definitions, I sometimes need to specify methods that have an obvious implementation for most, but not all, types that implement the trait. The most recent example comes from my answer to [this thread on URLO](https://users.rust-lang.org/t/implement-into-for-trait/61253/2), where I needed to specify parallel methods: One that has a receiver type of `Self` (where `Self: Sized`) and another that has a receiver of type `Box<Self>` (to allow usage by trait objects).

Ideally, I'd like to provide a default implementation of the boxed version that's only valid for `Sized` types, but this isn't possible today. Would something along these lines be reasonable to add to Rust?:

```rust
trait Consume {
    fn consume(self) where Self: Sized;
    fn consume_boxed(self: Box<Self>) default where Self:Sized { // example syntax
        Self::consume(*self)
    }
}

```

This syntax¹ (`default where`) is meant to indicate the bounds required for the default implementation to function. This is distinct from a normal `where` clause, which describes the bounds that must be fulfilled for the method to be called; both clauses may be present on the same method.

In this case, all types that implement the `Consume` trait must have a valid `consume_boxed` implementation (because there's no ordinary `where` clause). `Sized` types can use the default implementation, but `!Sized` types must override `consume_boxed` within the corresponding `impl Consume` block.

Like the unbounded default methods Rust currently has, any `impl Consume` block may provide its own implementation of `consume_boxed`. This is necessary to maintain forward compatibility: The `default where` bound could become satisfied if a mentioned type adds a trait implementation, which isn't supposed to be a breaking change.

¹ I haven't thought through the particular syntax much; that can be bikeshedded if the community decides this is a worthwhile capability to add.

---

<div class="post-metadata">

**Author:** ![Mokuz](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mokuz/32/7225_2.png) [@Mokuz](https://internals.rust-lang.org/u/Mokuz)\
**Post date:** [June 18, 2021, 2:07pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/2 "2021-06-18T14:07:54Z")

</div>

I think this can be done with specialization and by splitting it into multiple traits:

```rust
#![feature(specialization)]

trait Consume {
    // this needs to have a default implementation or you can't impl Consume for T where T: ?Sized,
    fn consume(self)
    where
        Self: Sized
    {
        // a bit better, @2e71828 below
        unimplemented!()
    }
}

trait ConsumeBoxed: Consume {
    fn consume_boxed(self: Box<Self>);
}

impl<T: Consume> ConsumeBoxed for T {
    // default implementation if T: Sized
    default fn consume_boxed(self: Box<Self>) {
        Self::consume(*self);
    }
}

impl Consume for str {}

impl ConsumeBoxed for str {
    fn consume_boxed(self: Box<Self>) {
        // do something
    }
}

```

which is notably more verbose.

---

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://internals.rust-lang.org/u/2e71828)\
**Post date:** [June 18, 2021, 2:23pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/3 "2021-06-18T14:23:35Z")

</div>

> [@Mokuz](#):
>
> which is notably more verbose.

Additionally, the no-op default for `consume()` is unfortunate, as the compiler will no longer complain if you forget to implement it.

Using trait objects is also cumbersome, as you need to remember that `Box<T:Consume>` should be coerced into `Box<dyn ConsumeBoxed>` instead of `Box<dyn Consume>`.

---

<div class="post-metadata">

**Author:** ![Mokuz](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mokuz/32/7225_2.png) [@Mokuz](https://internals.rust-lang.org/u/Mokuz)\
**Post date:** [June 18, 2021, 2:42pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/4 "2021-06-18T14:42:35Z")

</div>

well since `Consume::consume` already needs a default impl might as well do this.

```rust
#![feature(min_specialization)]

trait Consume {
    fn consume(self)
    where
        Self: Sized
    {
        unimplemented!()    
    }
    
    fn consume_boxed(self: Box<Self>);
}

impl<T> Consume for T {
    default fn consume_boxed(self: Box<Self>) {
        Self::consume(*self);
    }
}

impl Consume for str {
    fn consume_boxed(self: Box<Self>) {
        // do something
    }
}

```

---

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://internals.rust-lang.org/u/2e71828)\
**Post date:** [June 18, 2021, 2:56pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/5 "2021-06-18T14:56:59Z")

</div>

That causes problems when trying to implement `Consume` for a `Sized` type: [Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=05d3caaada4fc0f6a5260f598efc97d7)

```rust
impl Consume for u32 {
    fn consume(self) { dbg!(self); }
}

```

```rust
error[E0520]: `consume` specializes an item from a parent `impl`, but that item is not marked `default`
  --> src/lib.rs:27:5
   |
14 | / impl<T> Consume for T {
15 | | default fn consume_boxed(self: Box<Self>) {
16 | | Self::consume(*self);
17 | | }
18 | | }
   | |_- parent `impl` is here
...
27 | fn consume(self) { dbg!(self); }
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot specialize default item `consume`
   |
   = note: to specialize, `consume` in the parent `impl` must be marked `default`

```

* * *

To fix this, you need to add _another_ default implementation of `fn consume()` inside the `impl<T>` block. [Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=1c0ac0d796db70aa5cf7ba7e2105462a)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [June 18, 2021, 3:34pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/6 "2021-06-18T15:34:42Z")

</div>

> [@2e71828](#):
>
> Would something along these lines be reasonable to add to Rust?:
> 
> ```rust
> trait Consume {
> fn consume(self) where Self: Sized;
> fn consume_boxed(self: Box<Self>) default where Self:Sized { // example syntax
> Self::consume(*self)
> }
> }
> 
> ```

IIRC, the way to write this with `feature(specialization)` is

```rust
#![feature(specialization)]

trait Consume {
    fn consume(self)
    where
        Self: Sized;
    fn consume_boxed(self: Box<Self>);
}

// vvv T: Sized implicit anyways
default impl<T> Consume for T {
    fn consume_boxed(self: Box<Self>) {
        Self::consume(*self)
    }
}

```

* * *

The fact that Rust currently complains about missing methods when the method has `Self: Sized` and the implementor is unconditionally unsized is a separate concern. But it has the effect that implementing this trait as-is for `str` is kind-of impossible, unless I’m missing something xD

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [June 18, 2021, 3:50pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/7 "2021-06-18T15:50:54Z")

</div>

> [@steffahn](#):
>
> the way to write this with `feature(specialization)` is […]

Apparently, this approach doesn’t like any overlap either, e.g.

```rust
struct S<T: ?Sized>(T);

impl<T: ?Sized> Consume for S<T> {
    fn consume(self)
    where
        Self: Sized,
    {
    }
    fn consume_boxed(self: Box<Self>) {}
}

```

```plaintext
error[E0119]: conflicting implementations of trait `Consume` for type `S<_>`
  --> src/lib.rs:20:1
   |
12 | default impl<T> Consume for T {
   | ----------------------------- first implementation here
...
20 | impl<T: ?Sized> Consume for S<T> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `S<_>`

error: aborting due to previous error; 1 warning emitted

```

I guess that this needs improvement. Makes me wonder what the effect of a `default impl` is if it _specializes_ a non-`default` impl? ………

* * *

_A few tests later…_ Okay, it doesn’t specialize anything effectively, e.g. see [this playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=17a65d50619a70829522e76c9de0792e) (where actual specialization only happens with an ordinary `impl`, as can be seen by the runtime behavior if you click `Run`). But it still errors in the same cases as ordinary `impl` does, e.g. in [this playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a363424e3a82912bbdec189f031675ba) where the `default` was removed from the `fn consume_boxed` in `impl<T: ?Sized> Consume for S<T>`.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [June 18, 2021, 6:46pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/8 "2021-06-18T18:46:59Z")

</div>

I believe `default impl` allows you to implement only part of all required methods of a trait and then allow later `impl` blocks to implement the rest of the methods to end up with a complete implementation of the trait.

Edit: I was correct:[1210-impl-specialization - The Rust RFC Book](https://rust-lang.github.io/rfcs/1210-impl-specialization.html)

> The specialization design in this RFC also allows for _default impls_ , which can provide specialized defaults without actually providing a full trait implementation:  
> [...]  
> This default impl does _not_ mean that `Add` is implemented for all `Clone` data, but just that when you do impl `Add` and `Self: Clone` , you can leave off `add_assign` :  
> [...]

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [June 18, 2021, 7:44pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/9 "2021-06-18T19:44:10Z")

</div>

This is accomplishable with just `min_specialization`, a split trait, and no dummy impls: [[playground]](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=c523f4648724ec2bfbbbd0490b18e824)

```rust
#![feature(min_specialization)]

trait Consume: ConsumeBoxed + Sized {
    fn consume(self);
}

trait ConsumeBoxed {
    fn consume_boxed(self: Box<Self>);
}

impl<T> ConsumeBoxed for T
where
    T: Consume,
{
    default fn consume_boxed(self: Box<Self>) {
        dbg!("default consume_boxed");
        Self::consume(*self)
    }
}

impl Consume for i32 {
    fn consume(self) {
        dbg!("i32::consume");
        dbg!(self);
    }
}

impl Consume for String {
    fn consume(self) {
        dbg!("String::consume");
        dbg!(self);
    }
}

impl ConsumeBoxed for String {
    fn consume_boxed(self: Box<Self>) {
        dbg!("String::consume_boxed");
        dbg!(self);
    }
}

impl ConsumeBoxed for str {
    fn consume_boxed(self: Box<Self>) {
        dbg!("str::consume_boxed");
        dbg!(self);
    }
}

fn main() {
    let s: String = "owned".into();
    s.consume();
    let s: Box<str> = "boxed".to_string().into_boxed_str();
    s.consume_boxed();
    let s: Box<String> = Box::new("double indirection".into());
    s.consume_boxed();

    let i: i32 = 5;
    i.consume();
    let i: Box<i32> = Box::new(5000);
    i.consume_boxed();
}

```

With full specialization, being able to use only one trait with a partial specialized default is definitely nice, however. [[playground]](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=ca1506f18f4be47fd0d7373f2ae6c7f1)

> ****
>
> ```rust
> #![feature(trivial_bounds)]
> #![feature(specialization)]
> 
> trait Consume {
> fn consume(self)
> where
> Self: Sized;
> fn consume_boxed(self: Box<Self>);
> }
> 
> default impl<T> Consume for T {
> default fn consume_boxed(self: Box<Self>) {
> dbg!("default consume_boxed");
> Self::consume(*self)
> }
> }
> 
> impl Consume for i32 {
> fn consume(self) {
> dbg!("i32::consume");
> dbg!(self);
> }
> }
> 
> impl Consume for String {
> fn consume(self) {
> dbg!("String::consume");
> dbg!(self);
> }
> 
> fn consume_boxed(self: Box<Self>) {
> dbg!("String::consume_boxed");
> dbg!(self);
> }
> }
> 
> impl Consume for str {
> fn consume(self)
> where
> str: Sized,
> {
> unreachable!("type system contradiction")
> }
> fn consume_boxed(self: Box<Self>) {
> dbg!("str::consume_boxed");
> dbg!(self);
> }
> }
> 
> fn main() {
> let s: String = "owned".into();
> s.consume();
> let s: Box<str> = "boxed".to_string().into_boxed_str();
> s.consume_boxed();
> let s: Box<String> = Box::new("double indirection".into());
> s.consume_boxed();
> 
> let i: i32 = 5;
> i.consume();
> let i: Box<i32> = Box::new(5000);
> i.consume_boxed();
> }
> 
> ```

The point of this is mostly to say that `default where` isn't really necessary, because it's pretty much isomorphic to the primary use case (and contains the same fallout issues as) `min_specialization`, so sound specialization would both be a prerequisite to sound `default where` and make `default where` mostly unnecessary.

Default function method implementations are after all almost just sugar for a blanket (partial) specialization. (Exception: all methods provided.)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [June 18, 2021, 8:10pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/10 "2021-06-18T20:10:52Z")

</div>

> [@steffahn](#):
>
> implementing this trait as-is for `str` is kind-of impossible

Oh, wow, I didn’t know of `#![feature(trivial_bounds)]`.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [June 18, 2021, 8:14pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/11 "2021-06-18T20:14:39Z")

</div>

Neither did I, I just did what the compiler told me to do 🙃

Tracking issue: [Tracking issue for RFC #2056: Allow trivial constraints to appear in where clauses · Issue #48214 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/48214)

RFC: [Allow trivial constraints to appear in where clauses by sgrif · Pull Request #2056 · rust-lang/rfcs · GitHub](https://github.com/rust-lang/rfcs/pull/2056)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [June 18, 2021, 8:23pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/12 "2021-06-18T20:23:54Z")

</div>

> [@CAD97](#):
>
> I just did what the compiler told me to do 🙃

Indeed, I noticed that too, the error message if you remove it mentions it. I’ve had to think a bit about why I’ve never seen that message in the past. I got the answer now: Last time I hit this problem I’ve tried something like implementing this trait

```rust
trait Foo {
    fn method(self) where Self: Sized;
}

```

but of course, without specialization, I **_was on stable_** and implementing this trait for `str` on stable _only_ gives the way more subtle

```plaintext
  = help: see issue #48214

```

* * *

The approaches I tried at the time were probably these

```rust
impl Foo for str {
    fn method(self) where Self: Sized {}
}

```

```plaintext
error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:7:5
  |
7 | fn method(self) where Self: Sized {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
  = help: see issue #48214

error: aborting due to previous error

```

and

```rust
impl Foo for str {
    fn method(self) {}
}

```

```plaintext
error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/lib.rs:7:15
  |
7 | fn method(self) {}
  | ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
help: function arguments must have a statically known size, borrowed types always have a known size
  |
7 | fn method(&self) {}
  | ^

error: aborting due to previous error

```

and

```rust
impl Foo for str {}

```

```plaintext
error[E0046]: not all trait items implemented, missing: `method`
 --> src/lib.rs:6:1
  |
2 | fn method(self) where Self: Sized;
  | ---------------------------------- `method` from trait
...
6 | impl Foo for str {}
  | ^^^^^^^^^^^^^^^^ missing `method` in implementation

error: aborting due to previous error

```

Spotting the `#48214` amongst all the errors and figuring out that it’s supposed to be about a _feature that removes the error_ instead of just providing explanation on the error itself is the hard part.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [June 18, 2021, 9:32pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/13 "2021-06-18T21:32:37Z")

</div>

> [@steffahn](#):
>
> Spotting the `#48214` amongst all the errors and figuring out that it’s supposed to be about a _feature that removes the error_ instead of just providing explanation on the error itself is the hard part.

I, too, have had to sort of learn from experience that the compiler will only tell me about features on nightly. It'd be nice if there was some clue to at least check this (though I'd also understand concerns about seeming to imply a feature is coming to stable at all or any time soon).

---

<div class="post-metadata">

**Author:** ![felix.s](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/felix.s/32/8073_2.png) [@felix.s](https://internals.rust-lang.org/u/felix.s)\
**Post date:** [June 19, 2021, 3:14pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/14 "2021-06-19T15:14:32Z")

</div>

I realised [`#[requires]`](https://internals.rust-lang.org/t/brainstorming-allowing-implementing-just-try-fold-and-getting-next-free/9417/6) might be just enough to address this:

```rust
trait Consume {
    fn consume(self) where Self: Sized;
    fn consume_boxed(self: Box<Self>);

    #[requires(consume)]
    default {
        fn consume_boxed(self: Box<Self>) {
            Self::consume(*self)
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 17, 2021, 3:15pm UTC](https://internals.rust-lang.org/t/bounds-for-default-method-implementations/14895/15 "2021-09-17T15:15:08Z")

</div>

Closed on September 17, 2021, 3:15pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
