# Trait-protected elements

**URL:** <https://internals.rust-lang.org/t/trait-protected-elements/16870>\
**Category:** language design\
**Created:** [June 22, 2022, 6:12pm UTC](https://internals.rust-lang.org/t/trait-protected-elements/16870 "2022-06-22T18:12:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImajinDevon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/imajindevon/32/9521_2.png) [@ImajinDevon](https://internals.rust-lang.org/u/ImajinDevon)\
**Post date:** [June 22, 2022, 6:12pm UTC](https://internals.rust-lang.org/t/trait-protected-elements/16870/1 "2022-06-22T18:12:32Z")

</div>

Currently, all trait functions and types are public. Perhaps, more visibility modifiers could be put into place. One modifier that immediately comes to mind is a `protected` modifier, similar to Java's. See below:

```rust
trait A {
    pub(Self) fn protected_fn(); // or maybe another syntax
}

struct B;

impl A for B {
    pub(super) fn protected_fn() { // or for(super) or (for dyn A)
        
    }
}

```

In this case, `A::protected_fun`, would only allow its caller to originate within the struct itself, and from the `A` trait. Therefor, this can be used for replicating the idea of the `abstract class` from within traits.

Although, while speaking to other people on this idea, a few have suggested that encapsulation stay primitive to other languages, since Rust does not rely solely on OOP.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [June 22, 2022, 6:44pm UTC](https://internals.rust-lang.org/t/trait-protected-elements/16870/2 "2022-06-22T18:44:36Z")

</div>

If this is something we want (personally I don't see the use case), please avoid using `pub(foo)` syntax — that should be reserved for actual visibility involving modules, not items.

---

<div class="post-metadata">

**Author:** ![CraftSpider](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/craftspider/32/7551_2.png) [@CraftSpider](https://internals.rust-lang.org/u/CraftSpider)\
**Post date:** [June 22, 2022, 8:34pm UTC](https://internals.rust-lang.org/t/trait-protected-elements/16870/3 "2022-06-22T20:34:55Z")

</div>

Personally, I'm not sold on the idea of item-private items like Java. I would support a proposal to allow traits to have crate-private methods - implemented by users, but only callable in the original crate, which would better match with Rust's existing module-privacy style.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [June 22, 2022, 9:08pm UTC](https://internals.rust-lang.org/t/trait-protected-elements/16870/4 "2022-06-22T21:08:51Z")

</div>

> [@ImajinDevon](#):
>
> a few have suggested that encapsulation stay primitive to other languages, since Rust does not rely solely on OOP.

Note that Rust _absolutely_ supports encapsulation! Struct fields and all non-trait impl members are private-by-default, and only `pub`lic within the modules below the given `pub(in path)`.

What Rust doesn't have is _type based encapsulation_, where you say "only I and impls on `Other` can see this." Instead, you just put the `impl Other` within the modules that can see the item.

Additionally, Rust doesn't have mixins the way that superclass `protected` functions are. Instead of using (and abusing) OOP inheritance for code reuse via superclasses, Rust encourages you to encapsulate the partial functionality via either just using free functions or containing an instance (encapsulating a member field) of the `struct` data and functionality instead.

Java and similar rely on type-driven encapsulation because everything provided on a type _must_ be in a single file. Rust allowing you to use multiple `impl` blocks for the same type allow it to solely use the simpler and yet more powerful module based privacy rules exclusively in order to encapsulate implementation details.

---

<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:** [September 20, 2022, 9:09pm UTC](https://internals.rust-lang.org/t/trait-protected-elements/16870/5 "2022-09-20T21:09:15Z")

</div>

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