The pattern is called "sealed trait", since having Self be bound by private trait makes it impossible to implement outside of your crate. For the same reason, hiding that trait bound is impossible, otherwise there's nothing to explain why your trait can't be implemented by your dependants.
Thanks for your reply.
But I still don't understand it.
I interpreted the above point to mean that other crate structures or trates cannot implement SomeTrait (I am very sorry if I interpreted it incorrectly due to my poor English skills).
However, the following code could be executed without problems.
Am I misunderstanding something?
use rust_test::SomeTrait;
fn main() {
(TestStruct {}).public_method();
().public_method();
}
struct TestStruct {}
trait TestTrait: SomeTrait {}
impl<T> TestTrait for T {}
cargo.toml
[package]
name = "rust_user"
version = "0.1.0"
edition = "2021"
[dependencies]
# `rust_test` includes `SomeTrait`.
rust_test = { path = "../rust_test" }
The m1::m2::Hidden in the code you provided seems to be valid only in the m1 module. Therefore, it seems impossible to implement m1::Foo, which implements m1::m2::Hidden, outside of the m1 module. As a result, m1::Foo cannot be a pub, can it?
In fact, commenting out the "illegal" part of your code still results in an error.
On the other hand, private::PrivateTrait in my code is only valid within a crate, and I give its implementation to SomeTrait within the crate. So SomeTrait can be used outside the crate.
However, since PrivateTrait cannot even use its name outside the crate, I don't think anyone outside the crate would want to know that we are implementing it.