Call for testing: Restricting trait implementability and field mutability
Две фичи на nightly.
impl_restriction позволяет указать на определении трейта, в каких модулях можно писать его реализацию.pub mod foo {
pub mod bar {
pub(crate) impl(super) trait Foo {}
}
// `Foo` may be implemented here.
impl bar::Foo for i8 {}
}
// Error: `Foo` cannot be implemented here.
impl foo::bar::Foo for u8 {}mut_restriction позволяет указывать на полях, в каких модулях его можно изменять:pub mod foo {
pub struct Bar {
pub mut(self) alpha: &'static str,
}
impl Bar {
pub fn mutate_inner(&mut self) {
// `alpha` may be mutated here.
self.alpha = "inner";
}
}
}
impl foo::Bar {
pub fn mutate_outer(&mut self) {
// Error: `alpha` cannot be mutated here.
self.alpha = "outer";
}
}Первая фича с
impl(crate) позволяет выразить паттерн sealed trait напрямую.