|
| 1 | +# `type_alias_impl_trait` |
| 2 | + |
| 3 | +The tracking issue for this feature is: [#63063] |
| 4 | + |
| 5 | +------------------------ |
| 6 | + |
| 7 | +> This feature is not to be confused with [`trait_alias`] or [`impl_trait_in_assoc_type`]. |
| 8 | +
|
| 9 | +### What is `impl Trait`? |
| 10 | + |
| 11 | +`impl Trait` in return position is useful for declaring types that are constrained by traits, but whose concrete type should be hidden: |
| 12 | + |
| 13 | +```rust |
| 14 | +use std::fmt::Debug; |
| 15 | + |
| 16 | +fn new() -> impl Debug { |
| 17 | + 42 |
| 18 | +} |
| 19 | + |
| 20 | +fn main() { |
| 21 | + let thing = new(); |
| 22 | + // What actually is a `thing`? |
| 23 | + // No idea but we know it implements `Debug`, so we can debug print it |
| 24 | + println!("{thing:?}"); |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +See the [reference] for more information about `impl Trait` in return position. |
| 29 | + |
| 30 | +### `type_alias_impl_trait` |
| 31 | + |
| 32 | +However, we might want to use an `impl Trait` in multiple locations but actually use the same concrete type everywhere while keeping it hidden. |
| 33 | +This can be useful in libraries where you want to hide implementation details. |
| 34 | + |
| 35 | +The `#[define_opaque]` attribute must be used to explicitly list opaque items constrained by the item it's on. |
| 36 | + |
| 37 | +```rust |
| 38 | +#![feature(type_alias_impl_trait)] |
| 39 | +# #![allow(unused_variables, dead_code)] |
| 40 | +trait Trait {} |
| 41 | + |
| 42 | +struct MyType; |
| 43 | + |
| 44 | +impl Trait for MyType {} |
| 45 | + |
| 46 | +type Alias = impl Trait; |
| 47 | + |
| 48 | +#[define_opaque(Alias)] // To constrain the type alias to `MyType` |
| 49 | +fn new() -> Alias { |
| 50 | + MyType |
| 51 | +} |
| 52 | + |
| 53 | +#[define_opaque(Alias)] // So we can name the concrete type inside this item |
| 54 | +fn main() { |
| 55 | + let thing: MyType = new(); |
| 56 | +} |
| 57 | + |
| 58 | +// It can be a part of a struct too |
| 59 | +struct HaveAlias { |
| 60 | + stuff: String, |
| 61 | + thing: Alias, |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +In this example, the concrete type referred to by `Alias` is guaranteed to be the same wherever `Alias` occurs. |
| 66 | + |
| 67 | +> Orginally this feature included type aliases as an associated type of a trait. In [#110237] this was split off to [`impl_trait_in_assoc_type`]. |
| 68 | +
|
| 69 | +### `type_alias_impl_trait` in argument position. |
| 70 | + |
| 71 | +Note that using `Alias` as an argument type is *not* the same as argument-position `impl Trait`, as `Alias` refers to a unique type, whereas the concrete type for argument-position `impl Trait` is chosen by the caller. |
| 72 | + |
| 73 | +```rust |
| 74 | +# #![feature(type_alias_impl_trait)] |
| 75 | +# #![allow(unused_variables)] |
| 76 | +# pub mod x { |
| 77 | +# pub trait Trait {} |
| 78 | +# |
| 79 | +# struct MyType; |
| 80 | +# |
| 81 | +# impl Trait for MyType {} |
| 82 | +# |
| 83 | +# pub type Alias = impl Trait; |
| 84 | +# |
| 85 | +# #[define_opaque(Alias)] |
| 86 | +# pub fn new() -> Alias { |
| 87 | +# MyType |
| 88 | +# } |
| 89 | +# } |
| 90 | +# use x::*; |
| 91 | +// this... |
| 92 | +pub fn take_alias(x: Alias) { |
| 93 | + // ... |
| 94 | +} |
| 95 | + |
| 96 | +// ...is *not* the same as |
| 97 | +pub fn take_impl(x: impl Trait) { |
| 98 | + // ... |
| 99 | +} |
| 100 | +# fn main(){} |
| 101 | +``` |
| 102 | + |
| 103 | +```rust,compile_fail,E0308 |
| 104 | +# #![feature(type_alias_impl_trait)] |
| 105 | +# #![allow(unused_variables)] |
| 106 | +# pub mod x { |
| 107 | +# pub trait Trait {} |
| 108 | +# |
| 109 | +# struct MyType; |
| 110 | +# |
| 111 | +# impl Trait for MyType {} |
| 112 | +# |
| 113 | +# pub type Alias = impl Trait; |
| 114 | +# |
| 115 | +# #[define_opaque(Alias)] |
| 116 | +# pub fn new() -> Alias { |
| 117 | +# MyType |
| 118 | +# } |
| 119 | +# } |
| 120 | +# use x::*; |
| 121 | +# pub fn take_alias(x: Alias) { |
| 122 | +# // ... |
| 123 | +# } |
| 124 | +# |
| 125 | +# pub fn take_impl(x: impl Trait) { |
| 126 | +# // ... |
| 127 | +# } |
| 128 | +# |
| 129 | +// a user's crate using the trait and type alias |
| 130 | +struct UserType; |
| 131 | +impl Trait for UserType {} |
| 132 | +
|
| 133 | +# fn main(){ |
| 134 | +let x = UserType; |
| 135 | +take_alias(x); |
| 136 | +// ERROR expected opaque type, found `UserType` |
| 137 | +// this function *actually* takes a `MyType` as is constrained in `new` |
| 138 | +
|
| 139 | +let x = UserType; |
| 140 | +take_impl(x); |
| 141 | +// OK |
| 142 | +
|
| 143 | +let x = new(); |
| 144 | +take_alias(x); |
| 145 | +// OK |
| 146 | +
|
| 147 | +let x = new(); |
| 148 | +take_impl(x); |
| 149 | +// OK |
| 150 | +# } |
| 151 | +``` |
| 152 | + |
| 153 | +Note that the user cannot use `#[define_opaque(Alias)]` to reify the opaque type because only the crate where the type alias is declared may do so. But if this happened in the same crate and the opaque type was reified, they'd get a familiar error: "expected `MyType`, got `UserType`". |
| 154 | + |
| 155 | +[#63063]: https://github.com/rust-lang/rust/issues/63063 |
| 156 | +[#110237]: https://github.com/rust-lang/rust/pull/110237 |
| 157 | +[reference]: https://doc.rust-lang.org/stable/reference/types/impl-trait.html#abstract-return-types |
| 158 | +[`trait_alias`]: ./trait-alias.md |
| 159 | +[`impl_trait_in_assoc_type`]: ./impl-trait-in-assoc-type.md |
0 commit comments