Back to The Rust Foundation
GSoC 2026

Implementing impl and mut restrictions

In Rust, trait visibility currently implies implementability. If a trait is visible within a given scope, it can also be implemented in that same scope. However, we do not always want everyone to be able to implement it. To address this, the so-called “sealed trait” pattern is used, which allows a trait to be used while restricting its implementation. This is typically achieved by defining a public trait in a private module. Similarly, field visibility implies mutability. However, we do not always want readable fields to be writable as well. As a result, developers often use private fields together with public getter methods. The Restrictions RFC extends the Rust syntax to be able to explicitly restrict the implementability of traits and mutability of fields. For example, `pub(super) impl(self) trait Trait {}` means that `Trait` can be used within the parent module, while its implementation is restricted to the current module. `pub(super) struct Struct { pub mut(self) field: u8 }` means that `Struct.field` is readable within the parent module, while its mutation is restricted to the current module. These impl and mut restrictions are optional. Although the RFC was accepted in 2022, implementation efforts have stalled. Our goal for this project is to implement both impl and mut restrictions in the Rust compiler.

Project details

Contributor

Ryosuke Yamano

Mentors

Not available

Technologies

Not listed in the archive