Skip to content

C-NEWTYPE-HIDE neglects to mention other traits implemented by the wrapped type #142

Open
@chris-morgan

Description

@chris-morgan

C-NEWTYPE-HIDE neglects to mention something that I feel is quite important:

Enumerate<Skip<I>> does not just implement Iterator; it may also implement some or all of Debug, Clone, FusedIterator, ExactSizeIterator and DoubleEndedIterator.

By wrapping the Enumerate<Skip<I>> in a newtype, you lose those implementations. (This is also my biggest concern with practical usage of impl Trait, though I definitely want it.)

Thus, I say that for private code, wrapping things in a newtype like that is a mild anti-pattern as it fights against various optimisations and use cases. (Use a type alias, by all means; approximately all the benefits without any of the maintenance cost.)

For public code, wrapping such implementation details that could conceivably change in a newtype is the responsible and elegant thing to do, but the guideline needs to mention this detail about other traits, and that you should also add to your code most or all of these—

#[derive(Clone, Debug)]

impl<I: FusedIterator> FusedIterator for MyTransformResult<I> {}
impl<I: ExactSizeIterator> ExactSizeIterator for MyTransformResult<I> {}
impl<I: DoubleEndedIterator + ExactSizeIterator> DoubleEndedIterator<I> for MyTransformResult<I> {}

(While we’re here: the Result suffix in MyTransformResult should be killed with prejudice.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedAn amendment that's been accepted and can be appliedamendmentAmendments to existing guidelines

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions