# Idea: ?Sized for impl &T, impl &mut T

**URL:** <https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432>\
**Category:** language design\
**Created:** [November 22, 2020, 1:11am UTC](https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432 "2020-11-22T01:11:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AqlaSolutions](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aqlasolutions/32/7345_2.png) [@AqlaSolutions](https://internals.rust-lang.org/u/AqlaSolutions)\
**Post date:** [November 22, 2020, 1:11am UTC](https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432/1 "2020-11-22T01:11:17Z")

</div>

Example:

This: fn f(a: &impl Trait)

Now desugared as: fn f\<T:Trait\>(a: &T)

But it should be: fn f\<T:Trait + ?Sized\>(a: &T)

Same for &mut T.

Compatibility: Since T declaration is implicit and can't be used inside fn this change doesn't break anything.

Motivation: This change allows passing &dyn and &mut dyn directly to such methods. T becomes dyn Trait, &T becomes &dyn Trait - though T has dynamic size the parameter &T is a fixed size type so everything is fine.

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [November 22, 2020, 1:37am UTC](https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432/2 "2020-11-22T01:37:04Z")

</div>

> [@AqlaSolutions](#):
>
> Compatibility: Since T declaration is implicit and can't be used inside fn this change doesn't break anything.

Sadly this is not true 🙁. Consider:

```rust
fn foo (a: &impl Trait)
{
    bar(a);
}

fn bar<T : Trait> (a: &T)
{}

```

- If the definition of `foo` were to change and accept `?Sized` types / implementors, then it would no longer be able to forward a call to `bar`.

---

<div class="post-metadata">

**Author:** ![AqlaSolutions](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aqlasolutions/32/7345_2.png) [@AqlaSolutions](https://internals.rust-lang.org/u/AqlaSolutions)\
**Post date:** [November 22, 2020, 1:47am UTC](https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432/3 "2020-11-22T01:47:45Z")

</div>

Hm, you are right. And if such method is declared in trait the compiler can't even check the code for such things and remove ?Sized. May be this improvement can be put behind the next "language version".

---

<div class="post-metadata">

**Author:** ![rpjohnst](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rpjohnst/32/9524_2.png) [@rpjohnst](https://internals.rust-lang.org/u/rpjohnst)\
**Post date:** [November 22, 2020, 4:10am UTC](https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432/4 "2020-11-22T04:10:54Z")

</div>

The language doesn't need to change- you can already opt into unsized `impl Trait` types like this:

```rust
fn foo(x: &(impl ?Sized + Trait)) {
    bar(x)
}

fn bar<T: ?Sized + Trait>(x: &T) {}

```

---

<div class="post-metadata">

**Author:** ![AqlaSolutions](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aqlasolutions/32/7345_2.png) [@AqlaSolutions](https://internals.rust-lang.org/u/AqlaSolutions)\
**Post date:** [November 22, 2020, 6:59pm UTC](https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432/5 "2020-11-22T18:59:36Z")

</div>

But that's just... not readable.

---

<div class="post-metadata">

**Author:** ![rpjohnst](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rpjohnst/32/9524_2.png) [@rpjohnst](https://internals.rust-lang.org/u/rpjohnst)\
**Post date:** [November 22, 2020, 8:28pm UTC](https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432/6 "2020-11-22T20:28:59Z")

</div>

Changing the default would just make the other case "not readable:"

```rust
fn foo(x: &(impl Sized + Trait)) {
    bar(x)
}

fn bar<T: Trait>(x: &T) {}
// Or perhaps even this, for consistency:
fn bar<T: Sized + Trait>(x: &T) {}

```

The choice was made based on which case is more common. Having a weird "unreadable" syntax for an uncommon case is better than that case not being possible at all, and it's _certainly_ better than having the common case be "unreadable."

---

<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:** [February 20, 2021, 8:29pm UTC](https://internals.rust-lang.org/t/idea-sized-for-impl-t-impl-mut-t/13432/7 "2021-02-20T20:29:12Z")

</div>

Closed on February 20, 2021, 8:29pm UTC

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