# Let trait aliases be used in impls, or how to add io::{Read, Write} to libcore

**URL:** <https://internals.rust-lang.org/t/let-trait-aliases-be-used-in-impls-or-how-to-add-io-read-write-to-libcore/14975>\
**Category:** libs\
**Created:** [July 2, 2021, 5:57pm UTC](https://internals.rust-lang.org/t/let-trait-aliases-be-used-in-impls-or-how-to-add-io-read-write-to-libcore/14975 "2021-07-02T17:57:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lachlansneff](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/lachlansneff/32/5924_2.png) [@lachlansneff](https://internals.rust-lang.org/u/lachlansneff)\
**Post date:** [July 2, 2021, 5:57pm UTC](https://internals.rust-lang.org/t/let-trait-aliases-be-used-in-impls-or-how-to-add-io-read-write-to-libcore/14975/1 "2021-07-02T17:57:52Z")

</div>

## Part 1: Trait Aliases

The [`trait_alias`](https://doc.rust-lang.org/beta/unstable-book/language-features/trait-alias.html) unstable feature enables syntax like this:

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

trait Foo = std::fmt::Debug + Send;
trait Bar = Foo + Sync;

// Use trait alias as bound on type parameter.
fn foo<T: Foo>(v: &T) {
    println!("{:?}", v);
}

```

However, the resulting traits (`Foo` and `Bar`) in the above example cannot be implemented on types.

```console
error[E0404]: expected trait, found trait alias `Foo`
 --> src/main.rs:8:6
  |
8 | impl Foo for A {
  | ^^^ not a trait

```

My suggestion comes in two parts:

1. Enable trait aliases to contain multiple regular traits, not just a single regular trait and multiple auto traits.
2. Implementing a trait alias for a type would be the same as if that trait alias were a regular trait with all of the member functions, types, and constants of the constituent traits combined (name conflicts would be a compile error), _except_ that implementing the trait alias implements the constituent traits instead.

## Part 2: Adding `io::Read` and `io::Write` to `core`

If the above suggestion was implemented, `Read` and `Write` traits could be added to `core` as subsets of the full trait in `std`. For example (using the full paths, even though it's invalid syntax):

```rust
trait core::io::Read {
    type Error;
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
    // ... the other methods that don't require allocation
}

```

And then, `std::io::Read` would be reimplemented as a trait alias:

```rust
trait std::io::Read = core::io::Read<Error = std::io::Error> + alloc::io::ReadExt;

```

where

```rust
trait alloc::io::ReadExt: core::io::Read {
    // ... all the read methods that require allocation
}

```

The same applies to the `std::io::Write` trait. It would require careful planning to make sure there was no breakage, but I think this would be possible.

* * *

I'm very curious what people think of this proposal—whether it's desirable to even add io traits to core, whether this proposal would actually work, and any comments or criticism.

---

<div class="post-metadata">

**Author:** ![programmerjake](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/programmerjake/32/5893_2.png) [@programmerjake](https://internals.rust-lang.org/u/programmerjake)\
**Post date:** [July 2, 2021, 11:15pm UTC](https://internals.rust-lang.org/t/let-trait-aliases-be-used-in-impls-or-how-to-add-io-read-write-to-libcore/14975/2 "2021-07-02T23:15:01Z")

</div>

Assuming it can be implemented and works how described, it sounds like a good idea to me!

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [July 7, 2021, 7:00pm UTC](https://internals.rust-lang.org/t/let-trait-aliases-be-used-in-impls-or-how-to-add-io-read-write-to-libcore/14975/3 "2021-07-07T19:00:02Z")

</div>

> [@lachlansneff](#):
>
> Enable trait aliases to contain multiple regular traits, not just a single regular trait and multiple auto traits.

This is already the case. "Trait alias" is a misnomer: it's actually an alias for the RHS of a where clause. For example, `trait Trivial = ;` and `trait Static = 'static;` are both valid trait aliases.

> [@lachlansneff](#):
>
> Implementing a trait alias for a type would be the same as if that trait alias were a regular trait with all of the member functions, types, and constants of the constituent traits combined (name conflicts would be a compile error), _except_ that implementing the trait alias implements the constituent traits instead.

I think that what you're actually suggesting here is permitting folks to write

```rust
impl A + B for C {
  // ...
}

```

where this would expand into `impl A for C` and `impl B for C` in the "obvious" manner. However, there are three problems:

- Should `impl for T {}` work? Presumably it should do nothing.
- Should `impl 'a for T {}` work? Like, should it just check that `T: 'a`?
- What if `A` and `B` share a function with the same name but different signatures? What if they share an associated type with the same name? What does `Self::Assoc` mean now?

I'm not saying this is a bad idea, but it's significantly more subtle than what you propose.

(I guess this isn't the _worst_ idea. One could imagine a hypothetical "generic impl" that makes use of this, e.g. `impl<trait Tr> Tr for Thing<Tr> { ... }`, but trait generics are a distant dream.)

---

<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:** [October 5, 2021, 7:01pm UTC](https://internals.rust-lang.org/t/let-trait-aliases-be-used-in-impls-or-how-to-add-io-read-write-to-libcore/14975/4 "2021-10-05T19:01:00Z")

</div>

Closed on October 5, 2021, 7:01pm UTC

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