Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use case for using IsEqualPrivate #157

Open
philip-peterson opened this issue Aug 8, 2020 · 1 comment
Open

Use case for using IsEqualPrivate #157

philip-peterson opened this issue Aug 8, 2020 · 1 comment

Comments

@philip-peterson
Copy link

philip-peterson commented Aug 8, 2020

Hi there,

I am implementing typed semver for Rust, and in the course of doing so I wanted to basically add code that can compare an (Unsigned, Unsigned, Unsigned) with another (Unsigned, Unsigned, Unsigned). When I went to implement this though, I noticed that the built in is_greater_or_equal function calls Self::compare::<Internal>. Luckily that wasn't a dealbreaker, but also found that the trait IsEqualPrivate is needed as a constraint on the numbers. That type is already exported, but the private.rs file does say to mention if a use case is found for any of the private code.

Unfortunately I needed to fork IsGreaterOrEqual and change the return value to bool in order to get it to work. Still not totally sure on why all the things I tried did not work, but it at least seems to have something to do with the Internal enum not being published. Below is the code I came up with; it's a minimal test case that implements the code I was mentioning. The full version would have the single (Unsigned) replaced with basically a 3-tuple of Unsigneds.

pub trait IsGreaterOrEqual<Rhs = Self> {
    /// The type representing either `True` or `False`
    type Output: Bit;
    /// Method returning `True` or `False`.
    fn is_greater_or_equal(self, rhs: Rhs) -> bool;
}

impl<X1, Y1> IsGreaterOrEqual<(Y1)> for (X1)
where
    X1: Unsigned + Cmp<Y1> + typenum::private::IsEqualPrivate<Y1, <X1 as typenum::Cmp<Y1>>::Output>,
    Y1: Unsigned,
{
    type Output = <X1 as typenum::IsEqual<Y1>>::Output;

    fn is_greater_or_equal(self, rhs: (Y1)) -> bool {
        <Self::Output as Bit>::to_bool()
    }
}

Note that as the code is written in typenum, the return value of is_greater_or_equal is not bool but instead Self::Output, which I was not able to get to compile in my own program.

@paholg
Copy link
Owner

paholg commented Mar 12, 2021

Sorry for the delay.

It makes sense that you'd need the private trait to implement your own IsGreaterOrEqual, but it surprises me that you'd need to. Do you have an example of where typenum's IsGreaterOrEqual didn't work for you? I may be able to help with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants