You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I had a similar problem to the one described in "C-SEALED" , but the private methods needed to be impossible to be called.
I ended up with a solution equivalent to that:
/// This trait is sealed and cannot be implemented for types outside this crate.pubtraitTheTrait: private::Sealed{// public methods onlyfntest1();}// Implement TheTrait for all types that implement the Sealed Traitimpl<T: private::Sealed>TheTraitforT{fntest1(){//just forward the call to the private implementation
<Selfas private::Sealed>::test1()}}mod private {// Zero or more private methods, not allowed for user to call.pubtraitSealed{//public methodsfntest1();//private methodsfntest2();}// Implement for those types allowedimplSealedforusize{fntest1(){todo!()}fntest2(){todo!()}}}
If you don't mind separating the trait in two, you can simplify the code:
/// This trait is sealed and cannot be implemented for types outside this crate.pubtraitTheTrait: private::Sealed{// public methods onlyfntest1();}mod private {// Zero or more private methods, not allowed for user to call.pubtraitSealed{fntest2();}// Implement for those same types, but no others.implSealedforusize{fntest2(){todo!()}}implcrate::TheTraitforusize{fntest1(){todo!()}}}
I personally think that enforcing the function to be private, generating an error at compile time if used, seems a more solid implementation, even if it require a little of boiler-plate.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I had a similar problem to the one described in "C-SEALED" , but the private methods needed to be impossible to be called.
I ended up with a solution equivalent to that:
If you don't mind separating the trait in two, you can simplify the code:
I personally think that enforcing the function to be private, generating an error at compile time if used, seems a more solid implementation, even if it require a little of boiler-plate.
Beta Was this translation helpful? Give feedback.
All reactions