Skip to content

Generic parameter for unit's underlying float #51

Open
@tower120

Description

@tower120

Is it possible to make something like Mass<T=f64> instead of just Mass?

For example, I would like to work with f32 precision mass, volume, etc...

Activity

sunsided

sunsided commented on Jun 16, 2023

@sunsided

I tried to give that a spin here using num-traits constrained generics but it ended up in one difficult spot: When T can be anything, the automatically implemented commutative implementations for Mul and Add fail, particularly with Duration:

impl<T: num_trait::Float> Mul<T> for Duration and impl<T: num_trait::Float> Mul<Duration> for T cannot coexist because a user of the create may implement num_trait::Float for Duration, leading to a conflicting trait implementation. That's blocked by RFC 1210.

Resolving this by making operations non-commutative could work, but then the specializations for f64 would have to be implemented manually.

I also thought of feature-gating the implementation, defaulting to <f64> specializations if generic types are not wished for - that's a bridge to be crossed once I'm there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sunsided@tower120

        Issue actions

          Generic parameter for unit's underlying float · Issue #51 · rust-embedded-community/rust-measurements