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

Why not impl<T> Borrow<T> for NonNan<T> ? #98

Closed
changhe3 opened this issue Sep 7, 2021 · 3 comments
Closed

Why not impl<T> Borrow<T> for NonNan<T> ? #98

changhe3 opened this issue Sep 7, 2021 · 3 comments

Comments

@changhe3
Copy link

changhe3 commented Sep 7, 2021

It seems we can satisfy the following behavior as long as we exclude NAN:

In particular Eq, Ord and Hash must be equivalent for borrowed and owned values: x.borrow() == y.borrow() should give the same result as x == y.

@mbrubeck
Copy link
Collaborator

mbrubeck commented Sep 7, 2021

Fixed in bfabcc9. However, this has limited use because:

  1. It is implemented only for NotNan<f32> and NotNan<f64>. Implementing it for a generic T would be incorrect because T could implement Hash in a way that is inconsistent with NotNan::hash.

  2. This isn't useful for HashMap::get or BTreeMap::get, because f32 and f64 do not implement Hash or Ord themselves.

@changhe3
Copy link
Author

changhe3 commented Sep 7, 2021

I think this is useful for the extremely rare cases where you use float as keys for range queries in data structures like btreemultimap.

@mbrubeck
Copy link
Collaborator

mbrubeck commented Sep 7, 2021

Unfortunately, I think that fails for the same reason. BTreeMultiMap::<K, V>::range has the following signature:

    pub fn range<T: ?Sized, R>(&self, range: R) -> MultiRange<'_, K, V>
    where
        T: Ord,
        K: Borrow<T>,
        R: RangeBounds<T>,

When K = NotNan<f32>, R = Range<f32>, T = f32, this fails the T: Ord bound.

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