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
Generic smart pointers like Box<T>, Rc<T> and so on are recommended to use associated functions instead of inherent methods to avoid compatibility hazards with Deref. As an example, see Box::leak. This is also documented in the Rust API Guidelines.
This lint warns on every inherent method on types that implement Deref in a fully generic fashion.
(In particular, it should activate when type Target = T, but not when type Target = u8 or type Target = [T]).
Advantage
Avoid confusing overlapping methods.
Prevent SemVer issues with adding new inherent methods to smart pointers (which would potentially be shadowing user-defined methods).
Drawbacks
Using the new code is slightly more cumbersome for the user.
Changing the code as the lint suggests would be a breaking change for an already released crate.
Example
use std::ops::Deref;pubstructMySmartPointer<T>(pubT);impl<T>DerefforMySmartPointer<T>{typeTarget = T;fnderef(&self) -> &Self::Target{&self.0}}impl<T>MySmartPointer<T>{pubfnfoo(&self){unimplemented!()}}
Some (sometimes unjustifiably) use Deref for newtypes, even large projects like bevy. This would afaik lint if it's generic, as an example, struct Meters<T: num::Float>(pub T)
In the case fully generic means no trait bounds, yeah this would work. Even if it's a newtype/marker type that would still be prone to issues
What it does
Generic smart pointers like
Box<T>
,Rc<T>
and so on are recommended to use associated functions instead of inherent methods to avoid compatibility hazards withDeref
. As an example, seeBox::leak
. This is also documented in the Rust API Guidelines.This lint warns on every inherent method on types that implement
Deref
in a fully generic fashion.(In particular, it should activate when
type Target = T
, but not whentype Target = u8
ortype Target = [T]
).Advantage
Drawbacks
Example
Could be written as:
Or, in the case where the user has read and understood the drawbacks, be explicitly allowed:
The text was updated successfully, but these errors were encountered: