Sometime when I'm reading code from crate I encounter this pattern :
mod private {
pub trait Sealed { }
}
pub trait SomeTrait: private::Sealed {
// ...
}
What does it used for ?
Sometime when I'm reading code from crate I encounter this pattern :
mod private {
pub trait Sealed { }
}
pub trait SomeTrait: private::Sealed {
// ...
}
What does it used for ?
It makes it impossible for other crates to implement SomeTrait
for their own types, as they can never satisfy the prerequisite private::Sealed
.
It's not recommended unless soundness concerns require the crate that defines SomeTrait
to know all implementing types. Usually, it's better to declare SomeTrait
as unsafe
to implement:
/// Safety: All valid implementations must ensure that ...
pub unsafe trait SomeTrait {
// ...
}
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.